annotate src/plugins/plugin_misc_room_game.py @ 718:074970227bc0

plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
author souliane <souliane@mailoo.org>
date Thu, 21 Nov 2013 18:23:08 +0100
parents src/tools/plugins/games.py@358018c5c398
children 539f278bc265
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
3
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT: a jabber client
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
6
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
11
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
16
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
19
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
20 from logging import debug, warning, error
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
21 from twisted.words.protocols.jabber.jid import JID
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from twisted.words.xish import domish
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from time import time
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
24 from wokkel import disco, iwokkel
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
25 from zope.interface import implements
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
26 try:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
27 from twisted.words.protocols.xmlstream import XMPPHandler
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
28 except ImportError:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
29 from wokkel.subprotocols import XMPPHandler
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
30
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
31 # Don't forget to set it to False before you commit
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
32 _DEBUG = False
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
33 _DEBUG_FILE = False
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
34
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
35 PLUGIN_INFO = {
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
36 "name": "Room game",
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
37 "import_name": "ROOM-GAME",
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
38 "type": "MISC",
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
39 "protocols": [],
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
40 "dependencies": ["XEP-0045", "XEP-0249"],
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
41 "main": "RoomGame",
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
42 "handler": "no", # handler MUST be "no" (dynamic inheritance)
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
43 "description": _("""Base class for MUC games""")
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
44 }
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
45
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
46
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
47 class RoomGame(object):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
48 """This class is used to help launching a MUC game."""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
49
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
50 # Values for self.invite_mode (who can invite after the game creation)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
51 FROM_ALL, FROM_NONE, FROM_REFEREE, FROM_PLAYERS = xrange(0, 4)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
52 # Values for self.wait_mode (for who we should wait before creating the game)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
53 FOR_ALL, FOR_NONE = xrange(0, 2)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
54 # Values for self.join_mode (who can join the game - NONE means solo game)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
55 ALL, INVITED, NONE = xrange(0, 3)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
56 # Values for ready_mode (how to turn a MUC user into a player)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
57 ASK, FORCE = xrange(0, 2)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
58
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
59 MESSAGE = '/message'
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
60 REQUEST = '%s/%s[@xmlns="%s"]'
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
61
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
62 def __init__(self, host):
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
63 self.host = host
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
64
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
65 def _init_(self, host, plugin_info, ns_tag, game_init={}, player_init={}):
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
66 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
67 @param host
711
c9792d0be499 plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents: 683
diff changeset
68 @param plugin_info: PLUGIN_INFO map of the game plugin
c9792d0be499 plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents: 683
diff changeset
69 @ns_tag: couple (nameservice, tag) to construct the messages
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
70 @param game_init: dictionary for general game initialization
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
71 @param player_init: dictionary for player initialization, applicable to each player
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
72 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
73 self.host = host
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
74 self.name = plugin_info["import_name"]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
75 self.ns_tag = ns_tag
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
76 self.request = self.REQUEST % (self.MESSAGE, ns_tag[1], ns_tag[0])
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
77 self.game_init = game_init
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
78 self.player_init = player_init
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
79 self.games = {}
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
80 self.invitations = {} # list of couple (x, y) with x the time and y a list of users
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
81
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
82 # These are the default settings, which can be overwritten by child class after initialization
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
83 self.invite_mode = self.FROM_PLAYERS if self.player_init == {} else self.FROM_NONE
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
84 self.wait_mode = self.FOR_NONE if self.player_init == {} else self.FOR_ALL
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
85 self.join_mode = self.INVITED
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
86 self.ready_mode = self.FORCE # TODO: asking for confirmation is not implemented
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
87
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
88 host.trigger.add("MUC user joined", self.userJoinedTrigger)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
89 host.trigger.add("MUC user left", self.userLeftTrigger)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
90
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
91 def createOrInvite(self, room, other_players, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
92 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
93 This is called only when someone explicitly wants to play.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
94 The game must not be created if one already exists in the room,
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
95 or its creation could be postponed until all the expected players
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
96 join the room (in that case it will be created from userJoinedTrigger).
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
97 @param room: instance of wokkel.muc.Room
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
98 @param other_players: list for other players JID userhosts
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
99 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
100 user_jid = self.host.getJidNStream(profile)[0]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
101 room_jid_s = room.occupantJID.userhost()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
102 nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
103 nicks = [nick]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
104 if self.gameExists(room_jid_s):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
105 if not self.checkJoinAuth(room_jid_s, user_jid.userhost(), nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
106 return
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
107 nicks.extend(self.invitePlayers(room, other_players, nick, profile))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
108 self.updatePlayers(room_jid_s, nicks, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
109 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
110 self.initGame(room_jid_s, nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
111 (auth, waiting, missing) = self.checkWaitAuth(room, other_players)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
112 nicks.extend(waiting)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
113 nicks.extend(self.invitePlayers(room, missing, nick, profile))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
114 if auth:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
115 self.createGame(room_jid_s, nicks, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
116 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
117 self.updatePlayers(room_jid_s, nicks, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
118
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
119 def initGame(self, room_jid_s, referee_nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
120 """Important: do not add the referee to 'players' yet. For a
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
121 <players /> message to be emitted whenever a new player is joining,
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
122 it is necessary to not modify 'players' outside of updatePlayers.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
123 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
124 referee = room_jid_s + '/' + referee_nick
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
125 self.games[room_jid_s] = {'referee': referee, 'players': [], 'started': False}
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
126 self.games[room_jid_s].update(self.game_init)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
127
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
128 def gameExists(self, room_jid_s, started=False):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
129 """Return True if a game has been initialized/started.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
130 @param started: if False, the game must be initialized only,
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
131 otherwise it must be initialized and started with createGame.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
132 @return: True if a game is initialized/started in that room"""
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
133 return room_jid_s in self.games and (not started or self.games[room_jid_s]['started'])
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
134
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
135 def checkJoinAuth(self, room_jid_s, user_jid_s=None, nick="", verbose=False):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
136 """Checks if this profile is allowed to join the game.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
137 The parameter nick is used to check if the user is already
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
138 a player in that game. When this method is called from
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
139 userJoinedTrigger, nick is also used to check the user
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
140 identity instead of user_jid_s (see TODO remark below).
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
141 @param room_jid_s: the room hosting the game
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
142 @param user_jid_s: JID userhost of the user
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
143 @param nick: nick of the user
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
144 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
145 auth = False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
146 if not self.gameExists(room_jid_s):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
147 auth = False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
148 elif self.join_mode == self.ALL or self.isPlayer(room_jid_s, nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
149 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
150 elif self.join_mode == self.INVITED:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
151 # considering all the batches of invitations
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
152 for invitations in self.invitations[room_jid_s]:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
153 if user_jid_s is not None:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
154 if user_jid_s in invitations[1]:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
155 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
156 break
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
157 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
158 # TODO: that's not secure enough but what to do if
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
159 # wokkel.muc.User's 'entity' attribute is not set?!
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
160 if nick in [JID(invited).user for invited in invitations[1]]:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
161 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
162 break
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
163
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
164 if not auth and (verbose or _DEBUG):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
165 debug(_("%s not allowed to join the game %s in %s") % (user_jid_s or nick, self.name, room_jid_s))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
166 return auth
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
167
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
168 def updatePlayers(self, room_jid_s, nicks, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
169 """Signal to the room or to each player that some players joined the game"""
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
170 if nicks == []:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
171 return
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
172 new_nicks = set(nicks).difference(self.games[room_jid_s]['players'])
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
173 if len(new_nicks) == 0:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
174 return
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
175 self.games[room_jid_s]['players'].extend(new_nicks)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
176 self.signalPlayers(room_jid_s, [JID(room_jid_s)], profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
177
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
178 def signalPlayers(self, room_jid_s, recipients, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
179 """Let these guys know that we are playing (they may not play themselves)."""
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
180 if self.gameExists(room_jid_s, started=True):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
181 element = self.createStartElement(self.games[room_jid_s]['players'])
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
182 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
183 element = self.createStartElement(self.games[room_jid_s]['players'], name="players")
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
184 for recipient in recipients:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
185 self.send(recipient, element, profile=profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
186
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
187 def invitePlayers(self, room, other_players, nick, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
188 """Invite players to a room, associated game may exist or not.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
189 @param room: wokkel.muc.Room instance
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
190 @param other_players: list of JID userhosts to invite
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
191 @param nick: nick of the user who send the invitation
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
192 @return: list of the invited players who were already in the room
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
193 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
194 room_jid = room.occupantJID.userhostJID()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
195 room_jid_s = room.occupantJID.userhost()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
196 if not self.checkInviteAuth(room_jid_s, nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
197 return []
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
198 self.invitations.setdefault(room_jid_s, [])
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
199 # TODO: remove invitation waiting for too long, using the time data
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
200 self.invitations[room_jid_s].append((time(), other_players))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
201 nicks = [nick]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
202 for player_jid in [JID(player) for player in other_players]:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
203 # TODO: find a way to make it secure
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
204 other_nick = self.host.plugins["XEP-0045"].getRoomNickOfUser(room, player_jid, secure=False)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
205 if other_nick is None:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
206 self.host.plugins["XEP-0249"].invite(player_jid, room_jid, {"game": self.name}, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
207 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
208 nicks.append(other_nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
209 return nicks
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
210
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
211 def checkInviteAuth(self, room_jid_s, nick, verbose=False):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
212 """Checks if this profile is allowed to invite players"""
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
213 auth = False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
214 if self.invite_mode == self.FROM_ALL or not self.gameExists(room_jid_s):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
215 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
216 elif self.invite_mode == self.FROM_NONE:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
217 auth = not self.gameExists(room_jid_s, started=True)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
218 elif self.invite_mode == self.FROM_REFEREE:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
219 auth = self.isReferee(room_jid_s, nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
220 elif self.invite_mode == self.FROM_PLAYERS:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
221 auth = self.isPlayer(room_jid_s, nick)
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
222 if not auth and (verbose or _DEBUG):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
223 debug(_("%s not allowed to invite for the game %s in %s") % (nick, self.name, room_jid_s))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
224 return auth
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
225
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
226 def isReferee(self, room_jid_s, nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
227 """Checks if the player with this nick is the referee for the game in this room"""
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
228 if not self.gameExists(room_jid_s):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
229 return False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
230 return room_jid_s + '/' + nick == self.games[room_jid_s]['referee']
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
231
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
232 def isPlayer(self, room_jid_s, nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
233 """Checks if the player with this nick is a player for the game in this room.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
234 Important: the referee is not in the 'players' list right after the game
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
235 initialization - check with isReferee to be sure nick is not a player.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
236 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
237 if not self.gameExists(room_jid_s):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
238 return False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
239 return nick in self.games[room_jid_s]['players'] or self.isReferee(room_jid_s, nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
240
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
241 def checkWaitAuth(self, room, other_players, verbose=False):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
242 """Check if we must wait before starting the game or not.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
243 @return: (x, y, z) with:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
244 x: False if we must wait, True otherwise
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
245 y: the nicks of the players that have been checked and confirmed
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
246 z: the players that have not been checked or that are missing
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
247 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
248 if self.wait_mode == self.FOR_NONE or other_players == []:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
249 result = (True, [], other_players)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
250 elif len(room.roster) < len(other_players) + 1:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
251 result = (False, [], other_players)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
252 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
253 # TODO: find a way to make it secure
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
254 (nicks, missing) = self.host.plugins["XEP-0045"].getRoomNicksOfUsers(room, other_players, secure=False)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
255 result = (len(nicks) == len(other_players), nicks, missing)
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
256 if not result[0] and (verbose or _DEBUG):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
257 debug(_("Still waiting for %s before starting the game %s in %s") % (result[2], self.name, room.occupantJID.userhost()))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
258 return result
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
259
712
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
260 def getUniqueName(self, muc_service="", profile_key='@DEFAULT@'):
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
261 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
262 return "sat_%s_%s" % (self.name.lower(), room) if room != "" else ""
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
263
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
264 def prepareRoom(self, other_players=[], room_jid=None, profile_key='@NONE@'):
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
265 """Prepare the room for a game: create it and invite players.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
266 @param other_players: list for other players JID userhosts
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
267 @param room_jid: JID userhost of the room to reuse or None to create a new room
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
268 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
269 debug(_('Preparing room for %s game') % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
270 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
271 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
272 error(_("Unknown profile"))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
273 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
274
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
275 def roomJoined(room):
711
c9792d0be499 plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents: 683
diff changeset
276 """@param room: instance of wokkel.muc.Room"""
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
277 self.createOrInvite(room, other_players, profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
278
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
279 def afterClientInit(room_jid):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
280 """Create/join the given room, or a unique generated one if no room is specified.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
281 @param room_jid: room to join
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
282 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
283 if room_jid is not None and room_jid != "": # a room name has been specified
712
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
284 if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
285 roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid])
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
286 return
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
287 else:
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
288 room_jid = self.getUniqueName(profile_key=profile_key)
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
289 if room_jid == "":
f610864eb7a5 plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents: 711
diff changeset
290 return
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
291 user_jid = self.host.getJidNStream(profile)[0]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
292 d = self.host.plugins["XEP-0045"].join(JID(room_jid), user_jid.user, {}, profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
293 d.addCallback(roomJoined)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
294
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
295 client = self.host.getClient(profile)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
296 if not client:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
297 error(_('No client for this profile key: %s') % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
298 return
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
299 client.client_initialized.addCallback(lambda ignore: afterClientInit(room_jid))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
300
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
301 def userJoinedTrigger(self, room, user, profile):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
302 """This trigger is used to check if the new user can take part of a game,
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
303 create the game if we were waiting for him or just update the players list.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
304 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
305 @user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
306 @return: True to not interrupt the main process.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
307 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
308 room_jid_s = room.occupantJID.userhost()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
309 profile_nick = room.occupantJID.resource
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
310 if not self.isReferee(room_jid_s, profile_nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
311 return True # profile is not the referee
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
312 if not self.checkJoinAuth(room_jid_s, nick=user.nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
313 # user not allowed but let him know that we are playing :p
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
314 self.signalPlayers(room_jid_s, [JID(room_jid_s + '/' + user.nick)], profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
315 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
316 if self.wait_mode == self.FOR_ALL:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
317 # considering the last batch of invitations
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
318 batch = len(self.invitations[room_jid_s]) - 1
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
319 if batch < 0:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
320 error("Invitations from %s to play %s in %s have been lost!" % (profile_nick, self.name, room_jid_s))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
321 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
322 other_players = self.invitations[room_jid_s][batch][1]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
323 (auth, nicks, dummy) = self.checkWaitAuth(room, other_players)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
324 if auth:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
325 del self.invitations[room_jid_s][batch]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
326 nicks.insert(0, profile_nick) # add the referee
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
327 self.createGame(room_jid_s, nicks, profile_key=profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
328 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
329 # let the room know that a new player joined
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
330 self.updatePlayers(room_jid_s, [user.nick], profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
331 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
332
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
333 def userLeftTrigger(self, room, user, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
334 """This trigger is used to update or stop the game when a user leaves.
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
335 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
336 @user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
337 @return: True to not interrupt the main process.
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
338 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
339 room_jid_s = room.occupantJID.userhost()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
340 profile_nick = room.occupantJID.resource
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
341 if not self.isReferee(room_jid_s, profile_nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
342 return True # profile is not the referee
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
343 if self.isPlayer(room_jid_s, user.nick):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
344 try:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
345 self.games[room_jid_s]['players'].remove(user.nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
346 except ValueError:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
347 pass
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
348 if self.wait_mode == self.FOR_ALL:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
349 # allow this user to join the game again
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
350 user_jid = user.entity.userhost()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
351 if len(self.invitations[room_jid_s]) == 0:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
352 self.invitations[room_jid_s].append((time(), [user_jid]))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
353 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
354 batch = 0 # add to the first batch of invitations
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
355 if user_jid not in self.invitations[room_jid_s][batch][1]:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
356 self.invitations[room_jid_s][batch][1].append(user_jid)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
357 return True
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
358
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
359 def checkCreateGameAndInit(self, room_jid_s, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
360 """Check if that profile can create the game. If the game can be created
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
361 but is not initialized yet, this method will also do the initialization
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
362 @return: a couple (create, sync) with:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
363 - create: set to True to allow the game creation
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
364 - sync: set to True to advice a game synchronization
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
365 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
366 user_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
367 if not user_nick:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
368 error('Internal error')
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
369 return False, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
370 if self.gameExists(room_jid_s):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
371 referee = self.isReferee(room_jid_s, user_nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
372 if self.gameExists(room_jid_s, started=True):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
373 warning(_("%s game already created in room %s") % (self.name, room_jid_s))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
374 return False, referee
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
375 elif not referee:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
376 warning(_("%s game in room %s can only be created by %s") % (self.name, room_jid_s, user_nick))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
377 return False, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
378 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
379 self.initGame(room_jid_s, user_nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
380 return True, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
381
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
382 def createGame(self, room_jid_s, nicks=[], profile_key='@NONE@'):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
383 """Create a new game - this can be called directly from a frontend
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
384 and skips all the checks and invitation system, but the game must
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
385 not exist and all the players must be in the room already.
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
386 @param room_jid: JID userhost of the room
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
387 @param nicks: list of players nicks in the room
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
388 @param profile_key: %(doc_profile_key)s"""
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
389 debug(_("Creating %s game in room %s") % (self.name, room_jid_s))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
390 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
391 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
392 error(_("profile %s is unknown") % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
393 return
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
394 (create, sync) = self.checkCreateGameAndInit(room_jid_s, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
395 if not create:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
396 if sync:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
397 debug(_('Synchronize game %s in %s for %s') % (self.name, room_jid_s, ', '.join(nicks)))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
398 # TODO: we should call a method to re-send the information to a player who left
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
399 # and joined the room again, currently: we may restart a whole new round...
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
400 self.updatePlayers(room_jid_s, nicks, profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
401 return
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
402 self.games[room_jid_s]['started'] = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
403 self.updatePlayers(room_jid_s, nicks, profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
404 if self.player_init == {}:
711
c9792d0be499 plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents: 683
diff changeset
405 return
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
406 # specific data to each player
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
407 status = {}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
408 players_data = {}
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
409 for nick in nicks:
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
410 # The dict must be COPIED otherwise it is shared between all users
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
411 players_data[nick] = self.player_init.copy()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
412 status[nick] = "init"
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
413 self.games[room_jid_s].update({'status': status, 'players_data': players_data})
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
414
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
415 def playerReady(self, player, referee, profile_key='@NONE@'):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
416 """Must be called when player is ready to start a new game"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
417 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
418 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
419 error(_("profile %s is unknown") % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
420 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
421 debug('new player ready: %s' % profile)
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
422 self.send(JID(referee), 'player_ready', {'player': player}, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
423
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
424 def newRound(self, room_jid, data, profile):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
425 """Launch a new round (reinit the user data)"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
426 debug(_('new round for %s game') % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
427 game_data = self.games[room_jid.userhost()]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
428 players = game_data['players']
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
429 players_data = game_data['players_data']
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
430 game_data['stage'] = "init"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
431
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
432 common_data, msg_elts = data if data is not None else (None, None)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
433
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
434 if isinstance(msg_elts, dict):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
435 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
436 to_jid = JID(room_jid.userhost() + "/" + player) # FIXME: gof:
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
437 elem = msg_elts[player] if isinstance(msg_elts[player], domish.Element) else None
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
438 self.send(to_jid, elem, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
439 elif isinstance(msg_elts, domish.Element):
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
440 self.send(room_jid, msg_elts, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
441 if common_data is not None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
442 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
443 players_data[player].update(common_data)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
444
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
445 def createGameElt(self, to_jid, type_="normal"):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
446 """Create a generic domish Element for the game"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
447 type_ = "normal" if to_jid.resource else "groupchat"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
448 elt = domish.Element((None, 'message'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
449 elt["to"] = to_jid.full()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
450 elt["type"] = type_
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
451 elt.addElement(self.ns_tag)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
452 return elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
453
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
454 def createStartElement(self, players=None, name="started"):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
455 """Create a game "started" domish Element
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
456 @param name: element name (default: "started").
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
457 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
458 started_elt = domish.Element((None, name))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
459 if players is None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
460 return started_elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
461 idx = 0
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
462 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
463 player_elt = domish.Element((None, 'player'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
464 player_elt.addContent(player)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
465 player_elt['index'] = str(idx)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
466 idx += 1
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
467 started_elt.addChild(player_elt)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
468 return started_elt
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
469
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
470 def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
471 """
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
472 @param to_jid: recipient JID
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
473 @param elem: domish.Element, unicode or a couple:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
474 - domish.Element to be directly added as a child to the message
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
475 - unicode name or couple (uri, name) to create a new domish.Element
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
476 and add it as a child to the message (see domish.Element.addElement)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
477 @param attrs: dictionary of attributes for the new child
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
478 @param content: unicode that is appended to the child content
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
479 @param profile: the profile from which the message is sent
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
480 """
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
481 if profile is None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
482 error(_("Message can not be sent without a sender profile"))
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
483 return
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
484 msg = self.createGameElt(to_jid)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
485 if elem is not None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
486 if isinstance(elem, domish.Element):
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
487 msg.firstChildElement().addChild(elem)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
488 else:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
489 elem = msg.firstChildElement().addElement(elem)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
490 if attrs is not None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
491 elem.attributes.update(attrs)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
492 if content is not None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
493 elem.addContent(content)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
494 self.host.profiles[profile].xmlstream.send(msg)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
495
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
496 if _DEBUG_FILE:
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
497 # From here you will see all the game messages
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
498 file_ = open("/tmp/game_messages", "a")
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
499 file_.write("%s from %s to %s: %s\n" % (self.name, profile, "room" if to_jid.resource is None else to_jid.resource, elem.toXml()))
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
500 file_.close()
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
501
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
502 def getHandler(self, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
503 return RoomGameHandler(self)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
504
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
505
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
506 class RoomGameHandler (XMPPHandler):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
507 implements(iwokkel.IDisco)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
508
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
509 def __init__(self, plugin_parent):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
510 self.plugin_parent = plugin_parent
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
511 self.host = plugin_parent.host
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
512
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
513 def connectionInitialized(self):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
514 self.xmlstream.addObserver(self.plugin_parent.request, self.plugin_parent.room_game_cmd, profile=self.parent.profile)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
515
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
516 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
517 return [disco.DiscoFeature(self.plugin_parent.ns_tag[0])]
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
518
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
519 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
520 return []