annotate libervia/backend/plugins/plugin_misc_room_game.py @ 4230:314d3c02bb67

core (xmpp): Add a timeout for messages processing to avoid blocking the queue.
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2024 12:21:04 +0200
parents 4b842c1fb686
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
683
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
24 log = getLogger(__name__)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
25 from twisted.words.protocols.jabber import jid
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
26 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
27 from twisted.internet import defer
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
28 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
29 from wokkel import disco, iwokkel
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
30 from zope.interface import implementer
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
31 import copy
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
32
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
33 try:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
34 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
35 except ImportError:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
36 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
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 # 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
39 _DEBUG = False
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
40
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
41 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
42 C.PI_NAME: "Room game",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
43 C.PI_IMPORT_NAME: "ROOM-GAME",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
44 C.PI_TYPE: "MISC",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
45 C.PI_PROTOCOLS: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
46 C.PI_DEPENDENCIES: ["XEP-0045", "XEP-0249"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 C.PI_MAIN: "RoomGame",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
48 C.PI_HANDLER: "no", # handler MUST be "no" (dynamic inheritance)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 C.PI_DESCRIPTION: _("""Base class for MUC games"""),
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
50 }
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
51
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
52
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
53 # FIXME: this plugin is broken, need to be fixed
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
54
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
56 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
57 """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
58
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
59 bridge methods callbacks: _prepare_room, _player_ready, _create_game
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
60 Triggered methods: user_joined_trigger, user_left_trigger
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
61 Also called from subclasses: new_round
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
62
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
63 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
64 """
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
65
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
66 # Values for self.invite_mode (who can invite after the game creation)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
67 FROM_ALL, FROM_NONE, FROM_REFEREE, FROM_PLAYERS = range(0, 4)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
68 # Values for self.wait_mode (for who we should wait before creating the game)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 FOR_ALL, FOR_NONE = range(0, 2)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
70 # Values for self.join_mode (who can join the game - NONE means solo game)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
71 ALL, INVITED, NONE = range(0, 3)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
72 # Values for ready_mode (how to turn a MUC user into a player)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 ASK, FORCE = range(0, 2)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
74
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 MESSAGE = "/message"
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
76 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
77
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
78 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
79 """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
80 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
81
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
82 class MyGame(object):
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
83
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
84 def inherit_from_room_game(self, host):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
85 global RoomGame
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
86 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
87 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
88
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
89 def __init__(self, host):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
90 self.inherit_from_room_game(host)
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
91 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
92
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
93 """
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
94 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
95
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
96 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
97 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
98 @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
99 @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
100 @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
101 @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
102 @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
103 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
104 self.host = host
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
105 self.name = plugin_info["import_name"]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
106 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
107 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
108 if game_init is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
109 game_init = {}
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
110 if player_init is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
111 player_init = {}
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
112 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
113 self.player_init = player_init
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
114 self.games = {}
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
115 self.invitations = {} # values are a couple (x, y) with x the time and y a list of users
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
116
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
117 # 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
118 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
119 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
120 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
121 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
122
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
123 # 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
124 # 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
125 # 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
126 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
127
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
128 host.trigger.add("MUC user joined", self.user_joined_trigger)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
129 host.trigger.add("MUC user left", self.user_left_trigger)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
130
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
131 def _create_or_invite(self, room_jid, other_players, profile):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
132 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
133 This is called only when someone explicitly wants to play.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
134
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
135 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
136 also its creation could be postponed until all the expected players
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
137 join the room (in that case it will be created from user_joined_trigger).
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
138 @param room (wokkel.muc.Room): the room
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
139 @param other_players (list[jid.JID]): list of the other players JID (bare)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
140 """
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
141 # FIXME: broken !
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
142 raise NotImplementedError("To be fixed")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
143 client = self.host.get_client(profile)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
144 user_jid = self.host.get_jid_n_stream(profile)[0]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
145 nick = self.host.plugins["XEP-0045"].get_room_nick(client, room_jid)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
146 nicks = [nick]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
147 if self._game_exists(room_jid):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
148 if not self._check_join_auth(room_jid, user_jid, nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
149 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
150 nicks.extend(self._invite_players(room_jid, other_players, nick, profile))
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
151 self._update_players(room_jid, nicks, True, profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
152 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
153 self._init_game(room_jid, nick)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
154 (auth, waiting, missing) = self._check_wait_auth(room_jid, other_players)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
155 nicks.extend(waiting)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
156 nicks.extend(self._invite_players(room_jid, missing, nick, profile))
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
157 if auth:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
158 self.create_game(room_jid, nicks, profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
159 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
160 self._update_players(room_jid, nicks, False, profile)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
161
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
162 def _init_game(self, room_jid, referee_nick):
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
163 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
164
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
165 @param room_jid (jid.JID): JID of the room
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
166 @param referee_nick (unicode): nickname of the referee
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
167 """
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
168 # Important: do not add the referee to 'players' yet. For a
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
169 # <players /> message to be emitted whenever a new player is joining,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
170 # it is necessary to not modify 'players' outside of _update_players.
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 referee_jid = jid.JID(room_jid.userhost() + "/" + referee_nick)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 self.games[room_jid] = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 "referee": referee_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 "players": [],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 "started": False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
176 "status": {},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 }
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
178 self.games[room_jid].update(copy.deepcopy(self.game_init))
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
179 self.invitations.setdefault(room_jid, [])
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
180
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
181 def _game_exists(self, room_jid, started=False):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
182 """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
183 @param started: if False, the game must be initialized to return True,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
184 otherwise it must be initialized and started with create_game.
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
185 @return: True if a game is initialized/started in that room"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
186 return room_jid in self.games and (not started or self.games[room_jid]["started"])
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
187
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
188 def _check_join_auth(self, room_jid, user_jid=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
189 """Checks if this profile is allowed to join the game.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
190
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
191 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
192 a player in that game. When this method is called from
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
193 user_joined_trigger, 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
194 identity instead of user_jid_s (see TODO comment below).
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
195 @param room_jid (jid.JID): the JID of the room hosting the game
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
196 @param user_jid (jid.JID): JID of the user
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
197 @param nick (unicode): 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
198 @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
199 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
200 auth = False
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
201 if not self._game_exists(room_jid):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
202 auth = False
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
203 elif self.join_mode == self.ALL or self.is_player(room_jid, nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
204 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
205 elif self.join_mode == self.INVITED:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
206 # considering all the batches of invitations
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
207 for invitations in self.invitations[room_jid]:
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
208 if user_jid is not None:
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
209 if user_jid.userhostJID() in invitations[1]:
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
210 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
211 break
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
212 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
213 # 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
214 # wokkel.muc.User's 'entity' attribute is not set?!
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
215 if nick in [invited.user for invited in invitations[1]]:
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
216 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
217 break
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
218
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
219 if not auth and (verbose or _DEBUG):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
221 _("%(user)s not allowed to join the game %(game)s in %(room)s")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
222 % {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
223 "user": user_jid.userhost() or nick,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
224 "game": self.name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
225 "room": room_jid.userhost(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
226 }
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
227 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
228 return auth
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
229
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
230 def _update_players(self, room_jid, nicks, sync, profile):
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
231 """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
232 If sync is True, the news players are synchronized with the game data they have missed.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
233 Remark: self.games[room_jid]['players'] should not be modified outside this method.
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
234 @param room_jid (jid.JID): JID of the room
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
235 @param nicks (list[unicode]): list of players nicks in the room (referee included, in first position)
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
236 @param sync (bool): set to True to send synchronization data to the new players
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
237 @param profile (unicode): %(doc_profile)s
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
238 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
239 if nicks == []:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
240 return
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
241 # this is better than set(nicks).difference(...) as it keeps the order
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242 new_nicks = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
243 nick for nick in nicks if nick not in self.games[room_jid]["players"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
244 ]
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
245 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
246 return
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
247
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
248 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
249 for nick in new_nicks:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
250 self.games[room_jid]["status"][nick] = status
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
251
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
252 sync = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
253 sync
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
254 and self._game_exists(room_jid, True)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
255 and len(self.games[room_jid]["players"]) > 0
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
256 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
257 setStatus("desync" if sync else "init")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
258 self.games[room_jid]["players"].extend(new_nicks)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
259 self._synchronize_room(room_jid, [room_jid], profile)
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
260 if sync:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
261 setStatus("init")
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
262
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
263 def _synchronize_room(self, room_jid, recipients, profile):
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
264 """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
265 also send the synchronization data to the players who recently joined the game.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
266 @param room_jid (jid.JID): JID of the room
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
267 @recipients (list[jid.JID]): list of JIDs, the recipients of the message could be:
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
268 - room JID
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
269 - room JID + "/" + user nick
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
270 @param profile (unicode): %(doc_profile)s
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
271 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
272 if self._game_exists(room_jid, started=True):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
273 element = self._create_start_element(self.games[room_jid]["players"])
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
274 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
275 element = self._create_start_element(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
276 self.games[room_jid]["players"], name="players"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
277 )
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
278 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
279
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
280 sync_args = []
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
281 sync_data = self._get_sync_data(room_jid)
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
282 for nick in sync_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 user_jid = jid.JID(room_jid.userhost() + "/" + nick)
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
284 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
285 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
286 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
287 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
288 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
289 else:
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
290 user_elements = [(child, None, None) for child in sync_data[nick]]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
291 sync_args.append(([user_jid, user_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
292
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
293 for recipient in recipients:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
294 self._send_elements(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
295 for args, kwargs in sync_args:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
296 self._send_elements(*args, **kwargs)
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
297
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
298 def _get_sync_data(self, room_jid, 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
299 """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
300 has the state 'desync' or if he's been contained by force_nicks.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
301 @param room_jid (jid.JID): JID of the room
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
302 @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
303 @return: a mapping between player nicks and a list of elements to
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
304 be sent by self._synchronize_room 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
305 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
306 if not self._game_exists(room_jid):
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
307 return {}
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
308 data = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
309 status = self.games[room_jid]["status"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
310 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
311 if force_nicks is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
312 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
313 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
314 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
315 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
316 for nick in nicks:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
317 elements = self.get_sync_data_for_player(room_jid, nick)
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
318 if elements:
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
319 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
320 return data
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
321
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
322 def get_sync_data_for_player(self, room_jid, nick):
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
323 """This method may (and should probably) be overwritten by a child class.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
324 @param room_jid (jid.JID): JID of the room
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
325 @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
326 @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
327 """
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
328 return []
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
329
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
330 def _invite_players(self, room_jid, 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
331 """Invite players to a room, associated game may exist or not.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
332
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
333 @param other_players (list[jid.JID]): list of the players to invite
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
334 @param nick (unicode): nick of the user who send the invitation
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
335 @return: list[unicode] 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
336 """
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
337 raise NotImplementedError("Need to be fixed !")
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
338 # FIXME: this is broken and unsecure !
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
339 if not self._check_invite_auth(room_jid, nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
340 return []
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
341 # TODO: remove invitation waiting for too long, using the time data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
342 self.invitations[room_jid].append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 (time(), [player.userhostJID() for player in other_players])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
344 )
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
345 nicks = []
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
346 for player_jid in [player.userhostJID() for player in other_players]:
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
347 # TODO: find a way to make it secure
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
348 other_nick = self.host.plugins["XEP-0045"].getRoomEntityNick(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
349 room_jid, player_jid, secure=self.testing
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
350 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
351 if other_nick is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
352 self.host.plugins["XEP-0249"].invite(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
353 player_jid, room_jid, {"game": self.name}, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
354 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
355 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
356 nicks.append(other_nick)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
357 return nicks
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
358
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
359 def _check_invite_auth(self, room_jid, nick, verbose=False):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
360 """Checks if this user is allowed to invite players
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
361
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
362 @param room_jid (jid.JID): JID of the room
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
363 @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
364 @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
365 @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
366 """
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
367 auth = False
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
368 if self.invite_mode == self.FROM_ALL or not self._game_exists(room_jid):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
369 auth = True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
370 elif self.invite_mode == self.FROM_NONE:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
371 auth = not self._game_exists(room_jid, started=True) and self.is_referee(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
372 room_jid, nick
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
373 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
374 elif self.invite_mode == self.FROM_REFEREE:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
375 auth = self.is_referee(room_jid, nick)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
376 elif self.invite_mode == self.FROM_PLAYERS:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
377 auth = self.is_player(room_jid, 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
378 if not auth and (verbose or _DEBUG):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
379 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
380 _("%(user)s not allowed to invite for the game %(game)s in %(room)s")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
381 % {"user": nick, "game": self.name, "room": room_jid.userhost()}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
382 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
383 return auth
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
384
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
385 def is_referee(self, room_jid, nick):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
386 """Checks if the player with this nick is the referee for the game in this room"
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
387 @param room_jid (jid.JID): room JID
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
388 @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
389 @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
390 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
391 if not self._game_exists(room_jid):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
392 return False
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
393 return (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
394 jid.JID(room_jid.userhost() + "/" + nick) == self.games[room_jid]["referee"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
395 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
396
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
397 def is_player(self, room_jid, nick):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
398 """Checks if the user with this nick is a player for the game in this room.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
399 @param room_jid (jid.JID): JID of the room
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
400 @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
401 @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
402 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
403 if not self._game_exists(room_jid):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
404 return False
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
405 # Important: the referee is not in the 'players' list right after
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
406 # the game initialization, that's why we do also check with is_referee
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
407 return nick in self.games[room_jid]["players"] or self.is_referee(room_jid, nick)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
408
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
409 def _check_wait_auth(self, room, other_players, verbose=False):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
410 """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
411
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
412 @param room (wokkel.muc.Room): the room
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
413 @param other_players (list[jid.JID]): list of the players without the referee
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
414 @param verbose (bool): display debug message
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
415 @return: (x, y, z) with:
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
416 x: False if we must wait, True otherwise
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
417 y: the nicks of the players that have been checked and confirmed
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
418 z: the JID of the players that have not been checked or that are missing
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
419 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
420 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
421 result = (True, [], other_players)
791
23b0c949b86c plugins room games, XEP-0045: small fixes, improved docstrings
souliane <souliane@mailoo.org>
parents: 790
diff changeset
422 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
423 # 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
424 result = (False, [], other_players)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
425 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
426 # TODO: find a way to make it secure
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
427 (nicks, missing) = self.host.plugins["XEP-0045"].getRoomNicksOfUsers(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
428 room, other_players, secure=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
429 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
430 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
431 if not result[0] and (verbose or _DEBUG):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
432 log.debug(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
433 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
434 "Still waiting for %(users)s before starting the game %(game)s in %(room)s"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
435 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
436 % {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
437 "users": result[2],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
438 "game": self.name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
439 "room": room.occupantJID.userhost(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
440 }
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
441 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
442 return result
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
443
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
444 def get_unique_name(self, muc_service=None, profile_key=C.PROF_KEY_NONE):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
445 """Generate unique room name
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
446
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
447 @param muc_service (jid.JID): you can leave empty to autofind the muc service
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
448 @param profile_key (unicode): %(doc_profile_key)s
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
449 @return: jid.JID (unique name for a new room to be created)
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
450 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
451 client = self.host.get_client(profile_key)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
452 # FIXME: jid.JID must be used instead of strings
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
453 room = self.host.plugins["XEP-0045"].get_unique_name(client, muc_service)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
454 return jid.JID("sat_%s_%s" % (self.name.lower(), room.userhost()))
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
455
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
456 def _prepare_room(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
457 self, other_players=None, room_jid_s="", profile_key=C.PROF_KEY_NONE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
458 ):
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
459 room_jid = jid.JID(room_jid_s) if room_jid_s else None
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
460 other_players = [jid.JID(player).userhostJID() for player in other_players]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
461 return self.prepare_room(other_players, room_jid, profile_key)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
462
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
463 def prepare_room(self, other_players=None, room_jid=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
464 """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
465
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
466 @param other_players (list[JID]): list of other players JID (bare)
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
467 @param room_jid (jid.JID): JID of the room, or None to generate a unique name
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
468 @param profile_key (unicode): %(doc_profile_key)s
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
469 """
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
470 # FIXME: need to be refactored
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
471 client = self.host.get_client(profile_key)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
472 log.debug(_("Preparing room for %s game") % self.name)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
473 profile = self.host.memory.get_profile_name(profile_key)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
474 if not profile:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
475 log.error(_("Unknown profile"))
1257
83ed877541e3 plugin misc_room_game, radiocol, tarot: fixes bridge method "prepareRoom" declaration (add "async=True")
souliane <souliane@mailoo.org>
parents: 993
diff changeset
476 return defer.succeed(None)
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
477 if other_players is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
478 other_players = []
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
479
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
480 # Create/join the given room, or a unique generated one if no room is specified.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
481 if room_jid is None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
482 room_jid = self.get_unique_name(profile_key=profile_key)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
483 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
484 self.host.plugins["XEP-0045"].check_room_joined(client, room_jid)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
485 self._create_or_invite(client, room_jid, other_players)
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
486 return defer.succeed(None)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
487
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
488 user_jid = self.host.get_jid_n_stream(profile)[0]
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
489 d = self.host.plugins["XEP-0045"].join(room_jid, user_jid.user, {}, profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
490 return d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
491 lambda __: self._create_or_invite(client, room_jid, other_players)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
492 )
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
493
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
494 def user_joined_trigger(self, room, user, profile):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
495 """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
496
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
497 @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
498 @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
499 @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
500 """
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
501 room_jid = room.occupantJID.userhostJID()
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
502 profile_nick = room.occupantJID.resource
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
503 if not self.is_referee(room_jid, profile_nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
504 return True # profile is not the referee
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
505 if not self._check_join_auth(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
506 room_jid, user.entity if user.entity else None, user.nick
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
507 ):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
508 # user not allowed but let him know that we are playing :p
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
509 self._synchronize_room(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
510 room_jid, [jid.JID(room_jid.userhost() + "/" + user.nick)], profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
511 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
512 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
513 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
514 # considering the last batch of invitations
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
515 batch = len(self.invitations[room_jid]) - 1
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
516 if batch < 0:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
517 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
518 "Invitations from %s to play %s in %s have been lost!"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
519 % (profile_nick, self.name, room_jid.userhost())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
520 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
521 return True
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
522 other_players = self.invitations[room_jid][batch][1]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
523 (auth, nicks, __) = self._check_wait_auth(room, other_players)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
524 if auth:
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
525 del self.invitations[room_jid][batch]
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
526 nicks.insert(0, profile_nick) # add the referee
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
527 self.create_game(room_jid, nicks, profile_key=profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
528 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
529 # let the room know that a new player joined
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
530 self._update_players(room_jid, [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
531 return True
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
532
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
533 def user_left_trigger(self, room, user, profile):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
534 """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
535
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
536 @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
537 @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
538 @return: True to not interrupt the main process.
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
539 """
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
540 room_jid = room.occupantJID.userhostJID()
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
541 profile_nick = room.occupantJID.resource
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
542 if not self.is_referee(room_jid, profile_nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
543 return True # profile is not the referee
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
544 if self.is_player(room_jid, user.nick):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
545 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
546 self.games[room_jid]["players"].remove(user.nick)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
547 except ValueError:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
548 pass
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
549 if len(self.games[room_jid]["players"]) == 0:
828
8f335c03eebb plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents: 825
diff changeset
550 return True
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
551 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
552 # allow this user to join the game again
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
553 user_jid = user.entity.userhostJID()
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
554 if len(self.invitations[room_jid]) == 0:
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
555 self.invitations[room_jid].append((time(), [user_jid]))
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
556 else:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
557 batch = 0 # add to the first batch of invitations
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
558 if user_jid not in self.invitations[room_jid][batch][1]:
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
559 self.invitations[room_jid][batch][1].append(user_jid)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
560 return True
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
561
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
562 def _check_create_game_and_init(self, room_jid, profile):
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
563 """Check if that profile can create the game. If the game can be created
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
564 but is not initialized yet, this method will also do the initialization.
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
565
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
566 @param room_jid (jid.JID): JID of the room
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
567 @param profile
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
568 @return: a couple (create, sync) with:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
569 - create: set to True to allow the game creation
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
570 - 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
571 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
572 user_nick = self.host.plugins["XEP-0045"].get_room_nick(room_jid, profile)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
573 if not user_nick:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
574 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
575 "Internal error: profile %s has not joined the room %s"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
576 % (profile, room_jid.userhost())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
577 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
578 return False, False
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
579 if self._game_exists(room_jid):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
580 is_referee = self.is_referee(room_jid, user_nick)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
581 if self._game_exists(room_jid, started=True):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
582 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
583 _("%(game)s game already created in room %(room)s")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
584 % {"game": self.name, "room": room_jid.userhost()}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
585 )
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
586 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
587 elif not is_referee:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
588 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
589 _("%(game)s game in room %(room)s can only be created by %(user)s")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
590 % {"game": self.name, "room": room_jid.userhost(), "user": user_nick}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
591 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
592 return False, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
593 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
594 self._init_game(room_jid, user_nick)
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
595 return True, False
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
596
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
597 def _create_game(self, room_jid_s, nicks=None, profile_key=C.PROF_KEY_NONE):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
598 self.create_game(jid.JID(room_jid_s), nicks, profile_key)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
599
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
600 def create_game(self, room_jid, nicks=None, profile_key=C.PROF_KEY_NONE):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
601 """Create a new game.
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
602
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
603 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
604 but the game must not exist and all the players must be in the room already.
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
605 @param room_jid (jid.JID): JID of the room
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
606 @param nicks (list[unicode]): list of players nicks in the room (referee included, in first position)
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
607 @param profile_key (unicode): %(doc_profile_key)s
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
608 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
609 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
610 _("Creating %(game)s game in room %(room)s")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
611 % {"game": self.name, "room": room_jid}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
612 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
613 profile = self.host.memory.get_profile_name(profile_key)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
614 if not profile:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
615 log.error(_("profile %s is unknown") % profile_key)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
616 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
617 (create, sync) = self._check_create_game_and_init(room_jid, profile)
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
618 if nicks is None:
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
619 nicks = []
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
620 if not create:
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
621 if sync:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
622 self._update_players(room_jid, nicks, True, profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
623 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
624 self.games[room_jid]["started"] = True
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
625 self._update_players(room_jid, 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
626 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
627 # specific data to each player (score, private data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
628 self.games[room_jid].setdefault("players_data", {})
825
e3f4d80f987d plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents: 811
diff changeset
629 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
630 # The dict must be COPIED otherwise it is shared between all users
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
631 self.games[room_jid]["players_data"][nick] = copy.deepcopy(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
632 self.player_init
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
633 )
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
634
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
635 def _player_ready(self, player_nick, referee_jid_s, profile_key=C.PROF_KEY_NONE):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
636 self.player_ready(player_nick, jid.JID(referee_jid_s), profile_key)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
637
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
638 def player_ready(self, player_nick, referee_jid, 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
639 """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
640
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
641 @param player: the player nick in the room
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
642 @param referee_jid (jid.JID): JID of the referee
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
643 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
644 profile = self.host.memory.get_profile_name(profile_key)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
645 if not profile:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
646 log.error(_("profile %s is unknown") % profile_key)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
647 return
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
648 log.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
649 # TODO: we probably need to add the game and room names in the sent message
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
650 self.send(referee_jid, "player_ready", {"player": player_nick}, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
651
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
652 def new_round(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
653 """Launch a new round (reinit the user data)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
654
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
655 @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
656 @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
657 - common_data: backend initialization data for the new round
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
658 - 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
659 @param profile
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
660 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
661 log.debug(_("new round for %s game") % self.name)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
662 game_data = self.games[room_jid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
663 players = game_data["players"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
664 players_data = game_data["players_data"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
665 game_data["stage"] = "init"
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
666
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
667 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
668
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
669 if isinstance(msg_elts, dict):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
670 for player in players:
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 1257
diff changeset
671 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
672 elem = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
673 msg_elts[player]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
674 if isinstance(msg_elts[player], domish.Element)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
675 else None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
676 )
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
677 self.send(to_jid, elem, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
678 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
679 self.send(room_jid, msg_elts, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
680 if common_data is not None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
681 for player in players:
877
386ab96af575 plugin room_game: fix issues with data structures initialization
souliane <souliane@mailoo.org>
parents: 857
diff changeset
682 players_data[player].update(copy.deepcopy(common_data))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
683
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
684 def _create_game_elt(self, to_jid):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
685 """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
686
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
687 @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
688 @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
689 """
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
690 type_ = "normal" if to_jid.resource else "groupchat"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
691 elt = domish.Element((None, "message"))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
692 elt["to"] = to_jid.full()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
693 elt["type"] = type_
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
694 elt.addElement(self.ns_tag)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
695 return elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
696
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
697 def _create_start_element(self, players=None, name="started"):
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
698 """Create a domish Element listing the game users
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
699
790
19262fb77230 plugin room game: improved docstrings, added '_' as prefix for internal methods names
souliane <souliane@mailoo.org>
parents: 771
diff changeset
700 @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
701 @param name: element name:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
702 - "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
703 - "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
704 @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
705 """
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
706 started_elt = domish.Element((None, name))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
707 if players is None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
708 return started_elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
709 idx = 0
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
710 for player in players:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
711 player_elt = domish.Element((None, "player"))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
712 player_elt.addContent(player)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
713 player_elt["index"] = str(idx)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
714 idx += 1
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
715 started_elt.addChild(player_elt)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
716 return started_elt
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
717
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
718 def _send_elements(self, to_jid, data, profile=None):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
719 """ TODO
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
720
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
721 @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
722 @param data: list of (elem, attr, content) with:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
723 - elem: domish.Element, unicode or a couple:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
724 - 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
725 - 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
726 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
727 - attrs: dictionary of attributes for the new child
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
728 - 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
729 @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
730 @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
731 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
732 client = self.host.get_client(profile)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
733 msg = self._create_game_elt(to_jid)
746
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
734 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
735 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
736 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
737 msg.firstChildElement().addChild(elem)
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
738 else:
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
739 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
740 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
741 elem.attributes.update(attrs)
539f278bc265 plugin room_games, radiocol: send the current queue to new players
souliane <souliane@mailoo.org>
parents: 718
diff changeset
742 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
743 elem.addContent(content)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
744 client.send(msg)
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 844
diff changeset
745 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
746
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
747 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
748 """ TODO
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
749
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
750 @param to_jid: recipient JID
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
751 @param elem: domish.Element, unicode or a couple:
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
752 - 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
753 - 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
754 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
755 @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
756 @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
757 @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
758 @return: a Deferred instance
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
759 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
760 return self._send_elements(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
761
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
762 def get_handler(self, client):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
763 return RoomGameHandler(self)
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
764
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
765
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
766 @implementer(iwokkel.IDisco)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
767 class RoomGameHandler(XMPPHandler):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
768
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
769 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
770 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
771 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
772
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
773 def connectionInitialized(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
774 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
775 self.plugin_parent.request,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
776 self.plugin_parent.room_game_cmd,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
777 profile=self.parent.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
778 )
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
779
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
780 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
781 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
782
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
783 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 714
diff changeset
784 return []