annotate src/plugins/plugin_misc_room_game.py @ 956:132de9d487ac

plugin XEP-0065: fixed initialisation sequence
author Goffi <goffi@goffi.org>
date Mon, 31 Mar 2014 16:08:15 +0200
parents e1842ebcb2f3
children 301b342c697a
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
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 791
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
683
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
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 746
diff changeset
20 from sat.core.i18n import _
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 877
diff changeset
21 from sat.core.constants import Const as C
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from logging import debug, warning, error
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber.jid import JID
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
24 from twisted.words.xish import domish
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
25 from twisted.internet import defer
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
26 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
27 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
28 from zope.interface import implements
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
29 import copy
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
30 try:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
31 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
32 except ImportError:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
33 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
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 # 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
36 _DEBUG = False
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
37
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
38 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
39 "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
40 "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
41 "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
42 "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
43 "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
44 "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
45 "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
46 "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
47 }
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
48
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
49
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
50 class RoomGame(object):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
51 """This class is used to help launching a MUC game.
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
52
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
53 Bridge methods callbacks: prepareRoom, playerReady, createGame
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
54 Triggered methods: userJoinedTrigger, userLeftTrigger
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
55 Also called from subclasses: newRound
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
56
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
57 For examples of messages sequences, please look in sub-classes.
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
58 """
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
59
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
60 # 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
61 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
62 # 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
63 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
64 # 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
65 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
66 # 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
67 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
68
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
69 MESSAGE = '/message'
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
70 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
71
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
72 def __init__(self, host):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
73 """For other plugin to dynamically inherit this class, it is necessary to not use __init__ but _init_.
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
74 The subclass itself must be initialized this way:
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
75
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
76 class MyGame(object):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
77
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
78 def inheritFromRoomGame(self, host):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
79 global RoomGame
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
80 RoomGame = host.plugins["ROOM-GAME"].__class__
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
81 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {})
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
82
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
83 def __init__(self, host):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
84 self.inheritFromRoomGame(host)
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
85 RoomGame._init_(self, host, ...)
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
86
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
87 """
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
88 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
89
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
90 def _init_(self, host, plugin_info, ns_tag, game_init=None, player_init=None):
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
91 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
92 @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
93 @param plugin_info: PLUGIN_INFO map of the game plugin
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
94 @param 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
95 @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
96 @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
97 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
98 self.host = host
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
99 self.name = plugin_info["import_name"]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
100 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
101 self.request = self.REQUEST % (self.MESSAGE, ns_tag[1], ns_tag[0])
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
102 if game_init is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
103 game_init = {}
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
104 if player_init is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
105 player_init = {}
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
106 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
107 self.player_init = player_init
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
108 self.games = {}
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
109 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
110
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
111 # 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
112 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
113 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
114 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
115 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
116
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
117 # this has been added for testing purpose. It is sometimes needed to remove a dependence
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
118 # while building the synchronization data, for example to replace a call to time.time()
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
119 # by an arbitrary value. If needed, this attribute would be set to True from the testcase.
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
120 self.testing = False
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
121
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
122 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
123 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
124
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
125 def _createOrInvite(self, room, other_players, profile):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
126 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
127 This is called only when someone explicitly wants to play.
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
128 The game will not be created if one already exists in the room,
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
129 also its creation could be postponed until all the expected players
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
130 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
131 @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
132 @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
133 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
134 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
135 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
136 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
137 nicks = [nick]
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
138 if self._gameExists(room_jid_s):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
139 if not self._checkJoinAuth(room_jid_s, user_jid.userhost(), nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
140 return
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
141 nicks.extend(self._invitePlayers(room, other_players, nick, profile))
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
142 self._updatePlayers(room_jid_s, nicks, True, profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
143 else:
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
144 self._initGame(room_jid_s, nick)
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
145 (auth, waiting, missing) = self._checkWaitAuth(room, other_players)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
146 nicks.extend(waiting)
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
147 nicks.extend(self._invitePlayers(room, missing, nick, profile))
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
148 if auth:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
149 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
150 else:
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
151 self._updatePlayers(room_jid_s, nicks, False, profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
152
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
153 def _initGame(self, room_jid_s, referee_nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
154 """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
155 <players /> message to be emitted whenever a new player is joining,
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
156 it is necessary to not modify 'players' outside of _updatePlayers.
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
157 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
158 referee = room_jid_s + '/' + referee_nick
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
159 self.games[room_jid_s] = {'referee': referee, 'players': [], 'started': False, 'status': {}}
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
160 self.games[room_jid_s].update(copy.deepcopy(self.game_init))
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
161 self.invitations.setdefault(room_jid_s, [])
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
162
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
163 def _gameExists(self, room_jid_s, started=False):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
164 """Return True if a game has been initialized/started.
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
165 @param started: if False, the game must be initialized to return True,
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
166 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
167 @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
168 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
169
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
170 def _checkJoinAuth(self, room_jid_s, user_jid_s=None, nick="", verbose=False):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
171 """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
172 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
173 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
174 userJoinedTrigger, nick is also used to check the user
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
175 identity instead of user_jid_s (see TODO comment below).
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
176 @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
177 @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
178 @param nick: nick of the user
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
179 @return: True if this profile can join the game
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
180 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
181 auth = False
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
182 if not self._gameExists(room_jid_s):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
183 auth = False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
184 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
185 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
186 elif self.join_mode == self.INVITED:
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
187 user_jid_s = JID(user_jid_s).userhost()
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
188 # 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
189 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
190 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
191 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
192 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
193 break
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
194 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
195 # 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
196 # 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
197 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
198 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
199 break
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
200
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
201 if not auth and (verbose or _DEBUG):
844
f3513c8cc2e6 misc: fix unnamed arguments in format strings
souliane <souliane@mailoo.org>
parents: 828
diff changeset
202 debug(_("%(user)s not allowed to join the game %(game)s in %(room)s") % {'user': user_jid_s or nick, 'game': self.name, 'room': room_jid_s})
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
203 return auth
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
204
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
205 def _updatePlayers(self, room_jid_s, nicks, sync, profile):
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
206 """Update the list of players and signal to the room that some players joined the game.
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
207 If sync is True, the news players are synchronized with the game data they have missed.
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
208 Remark: self.games[room_jid_s]['players'] should not be modified outside this method.
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
209 @param room_jid_s: room userhost
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
210 @param nicks: list of players nicks in the room (referee included, in first position)
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
211 @param sync: set to True to send synchronization data to the new players
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
212 @param profile
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
213 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
214 if nicks == []:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
215 return
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
216 # this is better than set(nicks).difference(...) as it keeps the order
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
217 new_nicks = [nick for nick in nicks if nick not in self.games[room_jid_s]['players']]
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
218 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
219 return
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
220
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
221 def setStatus(status):
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
222 for nick in new_nicks:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
223 self.games[room_jid_s]['status'][nick] = status
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
224
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
225 sync = sync and self._gameExists(room_jid_s, True) and len(self.games[room_jid_s]['players']) > 0
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
226 setStatus('desync' if sync else 'init')
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
227 self.games[room_jid_s]['players'].extend(new_nicks)
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
228 self._synchronizeRoom(room_jid_s, [JID(room_jid_s)], profile)
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
229 if sync:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
230 setStatus('init')
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
231
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
232 def _synchronizeRoom(self, room_jid_s, recipients, profile):
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
233 """Communicate the list of players to the whole room or only to some users,
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
234 also send the synchronization data to the players who recently joined the game.
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
235 @param room_jid_s: room userhost
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
236 @recipients: list of JIDs, the recipients of the message could be:
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
237 - room JID
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
238 - room JID + "/" + user nick
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
239 @param profile
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
240 """
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
241 if self._gameExists(room_jid_s, started=True):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
242 element = self._createStartElement(self.games[room_jid_s]['players'])
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
243 else:
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
244 element = self._createStartElement(self.games[room_jid_s]['players'], name="players")
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
245 elements = [(element, None, None)]
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
246
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
247 sync_args = []
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
248 sync_data = self._getSyncData(room_jid_s)
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
249 for nick in sync_data:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
250 user_jid = JID(room_jid_s + '/' + nick)
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
251 if user_jid in recipients:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
252 user_elements = copy.deepcopy(elements)
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
253 for child in sync_data[nick]:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
254 user_elements.append((child, None, None))
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
255 recipients.remove(user_jid)
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
256 else:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
257 user_elements = [(child, None, None) for child in sync_data[nick]]
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
258 sync_args.append(([user_jid, user_elements], {'profile': profile}))
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
259
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
260 for recipient in recipients:
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
261 self._sendElements(recipient, elements, profile=profile)
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
262 for args, kwargs in sync_args:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
263 self._sendElements(*args, **kwargs)
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
264
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
265 def _getSyncData(self, room_jid_s, force_nicks=None):
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
266 """The synchronization data are returned for each player who
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
267 has the state 'desync' or if he's been contained by force_nicks.
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
268 @param room_jid_s: room userhost
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
269 @param force_nicks: force the synchronization for this list of the nicks
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
270 @return: a mapping between player nicks and a list of elements to
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
271 be sent by self._synchronizeRoom for the game to be synchronized.
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
272 """
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
273 if not self._gameExists(room_jid_s):
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
274 return {}
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
275 data = {}
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
276 status = self.games[room_jid_s]['status']
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
277 nicks = [nick for nick in status if status[nick] == 'desync']
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
278 if force_nicks is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
279 force_nicks = []
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
280 for nick in force_nicks:
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
281 if nick not in nicks:
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
282 nicks.append(nick)
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
283 for nick in nicks:
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
284 elements = self.getSyncDataForPlayer(room_jid_s, nick)
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
285 if elements:
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
286 data[nick] = elements
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
287 return data
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
288
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
289 def getSyncDataForPlayer(self, room_jid_s, nick):
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
290 """This method may (and should probably) be overwritten by a child class.
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
291 @param room_jid_s: room userhost
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
292 @param nick: the nick of the player to be synchronized
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
293 @return: a list of elements to synchronize this player with the game.
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
294 """
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
295 return []
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
296
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
297 def _invitePlayers(self, room, other_players, nick, profile):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
298 """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
299 @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
300 @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
301 @param nick: nick of the user who send the invitation
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
302 @return: list of room nicks for invited players who are already in the room
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
303 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
304 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
305 room_jid_s = room.occupantJID.userhost()
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
306 if not self._checkInviteAuth(room_jid_s, nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
307 return []
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
308 # TODO: remove invitation waiting for too long, using the time data
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
309 players_jids = [JID(player) for player in other_players]
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
310 self.invitations[room_jid_s].append((time(), [player.userhost() for player in players_jids]))
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
311 nicks = []
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
312 for player_jid in [player.userhostJID() for player in players_jids]:
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
313 # TODO: find a way to make it secure
857
3c270d691e56 test: fix the tests for plugin room game
souliane <souliane@mailoo.org>
parents: 849
diff changeset
314 other_nick = self.host.plugins["XEP-0045"].getRoomNickOfUser(room, player_jid, secure=self.testing)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
315 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
316 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
317 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
318 nicks.append(other_nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
319 return nicks
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
320
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
321 def _checkInviteAuth(self, room_jid_s, nick, verbose=False):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
322 """Checks if this user is allowed to invite players
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
323 @param room_jid_s: room userhost
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
324 @param nick: user nick in the room
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
325 @param verbose: display debug message
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
326 @return: True if the user is allowed to invite other players
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
327 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
328 auth = False
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
329 if self.invite_mode == self.FROM_ALL or not self._gameExists(room_jid_s):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
330 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
331 elif self.invite_mode == self.FROM_NONE:
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
332 auth = not self._gameExists(room_jid_s, started=True) and self.isReferee(room_jid_s, nick)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
333 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
334 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
335 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
336 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
337 if not auth and (verbose or _DEBUG):
844
f3513c8cc2e6 misc: fix unnamed arguments in format strings
souliane <souliane@mailoo.org>
parents: 828
diff changeset
338 debug(_("%(user)s not allowed to invite for the game %(game)s in %(room)s") % {'user': nick, 'game': self.name, 'room': room_jid_s})
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
339 return auth
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
340
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
341 def isReferee(self, room_jid_s, nick):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
342 """Checks if the player with this nick is the referee for the game in this room"
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
343 @param room_jid_s: room userhost
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
344 @param nick: user nick in the room
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
345 @return: True if the user is the referee of the game in this room
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
346 """
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
347 if not self._gameExists(room_jid_s):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
348 return False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
349 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
350
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
351 def isPlayer(self, room_jid_s, nick):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
352 """Checks if the user with this nick is a player for the game in this room.
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
353 @param room_jid_s: room userhost
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
354 @param nick: user nick in the room
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
355 @return: True if the user is a player of the game in this room
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
356 """
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
357 if not self._gameExists(room_jid_s):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
358 return False
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
359 # Important: the referee is not in the 'players' list right after
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
360 # the game initialization, that's why we do also check with isReferee
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
361 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
362
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
363 def _checkWaitAuth(self, room, other_players, verbose=False):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
364 """Check if we must wait for other players before starting the game.
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
365
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
366 @param room: wokkel.muc.Room instance
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
367 @param other_players: list of players JID userhosts without the referee
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
368 @param verbose: display debug message
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
369 @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
370 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
371 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
372 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
373 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
374 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
375 result = (True, [], other_players)
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
376 elif len(room.roster) < len(other_players):
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
377 # do not check the players until we may actually have them all
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
378 result = (False, [], other_players)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
379 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
380 # 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
381 (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
382 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
383 if not result[0] and (verbose or _DEBUG):
844
f3513c8cc2e6 misc: fix unnamed arguments in format strings
souliane <souliane@mailoo.org>
parents: 828
diff changeset
384 debug(_("Still waiting for %(users)s before starting the game %(game)s in %(room)s") % {'users': result[2], 'game': self.name, 'room': room.occupantJID.userhost()})
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
385 return result
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
386
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
387 def getUniqueName(self, muc_service=None, profile_key=C.PROF_KEY_NONE):
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
388 """Generate unique room name
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
389
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
390 @param muc_service: you can leave empty to autofind the muc service
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
391 @param profile_key: %(doc_profile_key)s
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
392 @return: a unique name for a new room to be created
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
393 """
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
394 # FIXME: jid.JID must be used instead of strings
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
395 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
396 return "sat_%s_%s" % (self.name.lower(), room.full())
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
397
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 877
diff changeset
398 def prepareRoom(self, other_players=None, room_jid_s=None, profile_key=C.PROF_KEY_NONE):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
399 """Prepare the room for a game: create it if it doesn't exist and invite players.
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
400
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
401 @param other_players: list for other players JID userhosts
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
402 @param room_jid_s: JID userhost of the room, or None to generate a unique name
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
403 @param profile_key
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
404 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
405 debug(_('Preparing room for %s game') % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
406 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
407 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
408 error(_("Unknown profile"))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
409 return
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
410 if other_players is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
411 other_players = []
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
412
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
413 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
414 """@param room: instance of wokkel.muc.Room"""
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
415 self._createOrInvite(room, [JID(player).userhost() for player in other_players], profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
416
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
417 # Create/join the given room, or a unique generated one if no room is specified.
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
418 if room_jid_s is not None and room_jid_s != "": # a room name has been specified
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
419 if room_jid_s in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
420 roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid_s])
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
421 return
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
422 else:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
423 room_jid_s = self.getUniqueName(profile_key=profile_key)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
424 if room_jid_s == "":
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
425 return
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
426 user_jid = self.host.getJidNStream(profile)[0]
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
427 d = self.host.plugins["XEP-0045"].join(JID(room_jid_s), user_jid.user, {}, profile)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
428 return d.addCallback(roomJoined)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
429
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
430 def userJoinedTrigger(self, room, user, profile):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
431 """This trigger is used to check if the new user can take part of a game, create the game if we were waiting for him or just update the players list.
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
432
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
433 @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
434 @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
435 @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
436 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
437 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
438 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
439 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
440 return True # profile is not the referee
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
441 if not self._checkJoinAuth(room_jid_s, user.entity.userhost() if user.entity else None, user.nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
442 # user not allowed but let him know that we are playing :p
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
443 self._synchronizeRoom(room_jid_s, [JID(room_jid_s + '/' + user.nick)], profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
444 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
445 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
446 # 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
447 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
448 if batch < 0:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
449 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
450 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
451 other_players = self.invitations[room_jid_s][batch][1]
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
452 (auth, nicks, dummy) = self._checkWaitAuth(room, other_players)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
453 if auth:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
454 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
455 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
456 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
457 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
458 # let the room know that a new player joined
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
459 self._updatePlayers(room_jid_s, [user.nick], True, profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
460 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
461
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
462 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
463 """This trigger is used to update or stop the game when a user leaves.
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
464
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
465 @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
466 @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
467 @return: True to not interrupt the main process.
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
468 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
469 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
470 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
471 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
472 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
473 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
474 try:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
475 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
476 except ValueError:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
477 pass
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
478 if len(self.games[room_jid_s]['players']) == 0:
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
479 del self.games[room_jid_s] # finish the game
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
480 return True
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
481 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
482 # allow this user to join the game again
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
483 user_jid_s = user.entity.userhost()
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
484 if len(self.invitations[room_jid_s]) == 0:
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
485 self.invitations[room_jid_s].append((time(), [user_jid_s]))
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
486 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
487 batch = 0 # add to the first batch of invitations
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
488 if user_jid_s not in self.invitations[room_jid_s][batch][1]:
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
489 self.invitations[room_jid_s][batch][1].append(user_jid_s)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
490 return True
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
491
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
492 def _checkCreateGameAndInit(self, room_jid_s, profile):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
493 """Check if that profile can create the game. If the game can be created but is not initialized yet, this method will also do the initialization.
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
494
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
495 @param room_jid_s: room userhost
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
496 @param profile
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
497 @return: a couple (create, sync) with:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
498 - create: set to True to allow the game creation
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
499 - sync: set to True to advice a game synchronization
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
500 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
501 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
502 if not user_nick:
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
503 error('Internal error: profile %s has not joined the room %s' % (profile, room_jid_s))
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
504 return False, False
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
505 if self._gameExists(room_jid_s):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
506 is_referee = self.isReferee(room_jid_s, user_nick)
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
507 if self._gameExists(room_jid_s, started=True):
844
f3513c8cc2e6 misc: fix unnamed arguments in format strings
souliane <souliane@mailoo.org>
parents: 828
diff changeset
508 warning(_("%(game)s game already created in room %(room)s") % {'game': self.name, 'room': room_jid_s})
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
509 return False, is_referee
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
510 elif not is_referee:
844
f3513c8cc2e6 misc: fix unnamed arguments in format strings
souliane <souliane@mailoo.org>
parents: 828
diff changeset
511 warning(_("%(game)s game in room %(room)s can only be created by %(user)s") % {'game': self.name, 'room': room_jid_s, 'user': user_nick})
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
512 return False, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
513 else:
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
514 self._initGame(room_jid_s, user_nick)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
515 return True, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
516
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 877
diff changeset
517 def createGame(self, room_jid_s, nicks=None, profile_key=C.PROF_KEY_NONE):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
518 """Create a new game.
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
519
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
520 This can be called directly from a frontend and skips all the checks and invitation system,
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
521 but the game must not exist and all the players must be in the room already.
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
522 @param room_jid: JID userhost of the room
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
523 @param nicks: list of players nicks in the room (referee included, in first position)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
524 @param profile_key: %(doc_profile_key)s
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
525 """
844
f3513c8cc2e6 misc: fix unnamed arguments in format strings
souliane <souliane@mailoo.org>
parents: 828
diff changeset
526 debug(_("Creating %(game)s game in room %(room)s") % {'game': self.name, 'room': room_jid_s})
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
527 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
528 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
529 error(_("profile %s is unknown") % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
530 return
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
531 (create, sync) = self._checkCreateGameAndInit(room_jid_s, profile)
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
532 if nicks is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
533 nicks = []
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
534 if not create:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
535 if sync:
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
536 self._updatePlayers(room_jid_s, nicks, True, profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
537 return
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
538 self.games[room_jid_s]['started'] = True
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
539 self._updatePlayers(room_jid_s, nicks, False, profile)
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
540 if self.player_init:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
541 # specific data to each player (score, private data)
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
542 self.games[room_jid_s].setdefault('players_data', {})
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
543 for nick in nicks:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
544 # The dict must be COPIED otherwise it is shared between all users
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
545 self.games[room_jid_s]['players_data'][nick] = copy.deepcopy(self.player_init)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
546
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 877
diff changeset
547 def playerReady(self, player, referee, profile_key=C.PROF_KEY_NONE):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
548 """Must be called when player is ready to start a new game
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
549
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
550 @param player: the player nick in the room
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
551 @param referee: referee userhost
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
552 """
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
553 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
554 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
555 error(_("profile %s is unknown") % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
556 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
557 debug('new player ready: %s' % profile)
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
558 # TODO: we probably need to add the game and room names in the sent message
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
559 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
560
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
561 def newRound(self, room_jid, data, profile):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
562 """Launch a new round (reinit the user data)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
563
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
564 @param room_jid: room userhost
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
565 @param data: a couple (common_data, msg_elts) with:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
566 - common_data: backend initialization data for the new round
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
567 - msg_elts: dict to map each user to his specific initialization message
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
568 @param profile
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
569 """
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
570 debug(_('new round for %s game') % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
571 game_data = self.games[room_jid.userhost()]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
572 players = game_data['players']
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
573 players_data = game_data['players_data']
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
574 game_data['stage'] = "init"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
575
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
576 common_data, msg_elts = copy.deepcopy(data) if data is not None else (None, None)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
577
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
578 if isinstance(msg_elts, dict):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
579 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
580 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
581 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
582 self.send(to_jid, elem, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
583 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
584 self.send(room_jid, msg_elts, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
585 if common_data is not None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
586 for player in players:
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
587 players_data[player].update(copy.deepcopy(common_data))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
588
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
589 def _createGameElt(self, to_jid):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
590 """Create a generic domish Element for the game messages
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
591
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
592 @param to_jid: JID of the recipient
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
593 @return: the created element
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
594 """
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
595 type_ = "normal" if to_jid.resource else "groupchat"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
596 elt = domish.Element((None, 'message'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
597 elt["to"] = to_jid.full()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
598 elt["type"] = type_
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
599 elt.addElement(self.ns_tag)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
600 return elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
601
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
602 def _createStartElement(self, players=None, name="started"):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
603 """Create a domish Element listing the game users
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
604
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
605 @param players: list of the players
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
606 @param name: element name:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
607 - "started" to signal the players that the game has been started
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
608 - "players" to signal the list of players when the game is not started yet
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
609 @return the create element
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
610 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
611 started_elt = domish.Element((None, name))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
612 if players is None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
613 return started_elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
614 idx = 0
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
615 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
616 player_elt = domish.Element((None, 'player'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
617 player_elt.addContent(player)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
618 player_elt['index'] = str(idx)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
619 idx += 1
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
620 started_elt.addChild(player_elt)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
621 return started_elt
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
622
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
623 def _sendElements(self, to_jid, data, profile=None):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
624 """ TODO
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
625
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
626 @param to_jid: recipient JID
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
627 @param data: list of (elem, attr, content) with:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
628 - elem: domish.Element, unicode or a couple:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
629 - domish.Element to be directly added as a child to the message
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
630 - unicode name or couple (uri, name) to create a new domish.Element
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
631 and add it as a child to the message (see domish.Element.addElement)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
632 - attrs: dictionary of attributes for the new child
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
633 - content: unicode that is appended to the child content
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
634 @param profile: the profile from which the message is sent
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
635 @return: a Deferred instance
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
636 """
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
637 if profile is None:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
638 error(_("Message can not be sent without a sender profile"))
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
639 return defer.fail(None)
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
640 msg = self._createGameElt(to_jid)
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
641 for elem, attrs, content in data:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
642 if elem is not None:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
643 if isinstance(elem, domish.Element):
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
644 msg.firstChildElement().addChild(elem)
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
645 else:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
646 elem = msg.firstChildElement().addElement(elem)
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
647 if attrs is not None:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
648 elem.attributes.update(attrs)
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
649 if content is not None:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
650 elem.addContent(content)
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
651 self.host.profiles[profile].xmlstream.send(msg)
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
652 return defer.succeed(None)
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
653
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
654 def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
655 """ TODO
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
656
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
657 @param to_jid: recipient JID
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
658 @param elem: domish.Element, unicode or a couple:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
659 - domish.Element to be directly added as a child to the message
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
660 - unicode name or couple (uri, name) to create a new domish.Element
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
661 and add it as a child to the message (see domish.Element.addElement)
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
662 @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
663 @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
664 @param profile: the profile from which the message is sent
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
665 @return: a Deferred instance
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
666 """
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
667 return self._sendElements(to_jid, [(elem, attrs, content)], profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
668
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
669 def getHandler(self, profile):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
670 return RoomGameHandler(self)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
671
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
672
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
673 class RoomGameHandler (XMPPHandler):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
674 implements(iwokkel.IDisco)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
675
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
676 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
677 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
678 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
679
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
680 def connectionInitialized(self):
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
681 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
682
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
683 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
684 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
685
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
686 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
687 return []