Mercurial > libervia-backend
annotate src/tools/plugins/games.py @ 713:8bd63daecdbf
plugin XEP-0045: fixed incorrect exceptions name
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 17 Nov 2013 17:02:22 +0100 |
parents | f610864eb7a5 |
children | ecc5a5b34ee1 |
rev | line source |
---|---|
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
3 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
6 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
11 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
16 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
19 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
20 from logging import debug, warning, error |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
21 from twisted.words.protocols.jabber.jid import JID |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
22 from twisted.words.xish import domish |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
23 from time import time |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
24 """This library help manage general games (e.g. card games) and it can be used by plugins""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
25 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
26 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
27 class RoomGame(object): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 """This class is used to help launching a MUC game.""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
29 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
30 def __init__(self, host, plugin_info, ns_tag, player_init_data={}, options={}): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
31 """ |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
32 @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
|
33 @param plugin_info: PLUGIN_INFO map of the game plugin |
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
34 @ns_tag: couple (nameservice, tag) to construct the messages |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
35 @param player_init_data: dictionary for initialization data, applicable to each player |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 @param options: dictionary for game options |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
37 """ |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
38 self.host = host |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
39 self.name = plugin_info["import_name"] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 self.ns_tag = ns_tag |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 self.player_init_data = player_init_data |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
42 self.collectiveGame = self.player_init_data == {} |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 self.options = options |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 self.games = {} |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 self.waiting_inv = {} # Invitation waiting for people to join to launch a game |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 |
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
|
47 def getUniqueName(self, muc_service="", profile_key='@DEFAULT@'): |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
48 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key) |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
49 return "sat_%s_%s" % (self.name.lower(), room) if room != "" else "" |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
50 |
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
|
51 def prepareRoom(self, other_players, room_jid=None, profile_key='@NONE@'): |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
52 """Prepare the room for a game: create it and invite players. |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 @param other_players: list for other players JID userhosts |
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
|
54 @param room_jid: JID of the room to reuse or None to create a new room |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
55 """ |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
56 debug(_('Preparing room for %s game') % self.name) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
57 profile = self.host.memory.getProfileName(profile_key) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
58 if not profile: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
59 error(_("Unknown profile")) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
60 return |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
61 _jid, xmlstream = self.host.getJidNStream(profile) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
62 if other_players is None: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
63 other_players = [] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
64 players = other_players[:] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
65 players.append(_jid.userhost()) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
66 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
67 def roomJoined(room): |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
68 """@param room: instance of wokkel.muc.Room""" |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
69 _room = room.occupantJID.userhostJID() |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
70 if self.collectiveGame is True or other_players == [] and _jid in [user.entity for user in room.roster.values()]: |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
71 self.createGame(_room.userhost(), [] if self.collectiveGame is True else players, profile_key=profile) |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
72 else: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
73 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
74 for player in other_players: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
75 self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
76 |
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
|
77 def after_init(room_jid): |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
78 if room_jid is not None and room_jid != "": |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
79 # a room name has been specified... |
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
|
80 if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms: |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
81 # and we're already in |
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
|
82 roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid]) |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
83 return |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
84 else: |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
85 room_jid = self.getUniqueName(profile_key=profile_key) |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
86 if room_jid == "": |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
87 return |
f610864eb7a5
plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified:
souliane <souliane@mailoo.org>
parents:
711
diff
changeset
|
88 d = self.host.plugins["XEP-0045"].join(JID(room_jid), _jid.user, {}, profile) |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
89 d.addCallback(roomJoined) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
90 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
91 client = self.host.getClient(profile) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
92 if not client: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
93 error(_('No client for this profile key: %s') % profile_key) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
94 return |
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
|
95 client.client_initialized.addCallback(lambda ignore: after_init(room_jid)) |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
96 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
97 def userJoinedTrigger(self, room, user, profile): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
98 """This trigger is used to check if we are waiting for people in this room, |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
99 and to create a game if everybody is here. |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
100 @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
|
101 @user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
102 """ |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
103 _room_jid = room.occupantJID.userhostJID() |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
104 if self.collectiveGame is True: |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
105 room_s = _room_jid.userhost() |
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
106 if room_s in self.games and self.games[room_s]["referee"] == room.occupantJID.full(): |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
107 #we are in a radiocol room, let's start the party ! |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
108 mess = self.createGameElt(JID(room_s + '/' + user.nick)) |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
109 mess.firstChildElement().addChild(self.__create_started_elt()) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
110 self.host.profiles[profile].xmlstream.send(mess) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
111 return True |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
112 if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
113 expected_players = self.waiting_inv[_room_jid][1] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
114 players = [] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
115 for player in expected_players: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
116 for user in room.roster.values(): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
117 if user.entity is not None: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
118 # check people identity |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
119 if user.entity.userhost() == player: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
120 players.append(user.nick) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
121 continue |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
122 else: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
123 # TODO: how to check the identity with only a nickname?? |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
124 if user.nick == JID(player).user: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
125 players.append(user.nick) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
126 if len(players) < len(expected_players): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
127 # Someone here was not invited! He can stay but he doesn't play :p |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
128 return True |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
129 # When we have all people in the room, we create the game |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
130 del self.waiting_inv[_room_jid] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
131 self.createGame(_room_jid.userhost(), players, profile_key=profile) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
132 return True |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
133 |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
134 def createGame(self, room_jid, players=[], profile_key='@NONE@'): |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
135 """Create a new game |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
136 @param room_jid: jid of the room |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
137 @param players: list of players nick (nick must exist in the room) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
138 @param profile_key: %(doc_profile_key)s""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
139 debug(_("Creating %s game") % self.name) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
140 room = JID(room_jid).userhost() |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
141 profile = self.host.memory.getProfileName(profile_key) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
142 if not profile: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
143 error(_("profile %s is unknown") % profile_key) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
144 return |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
145 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room, profile) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
146 if not room_nick: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
147 error('Internal error') |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
148 return |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
149 referee = room + '/' + room_nick |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
150 if room in self.games: |
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
151 warning(_("%s game already started in room %s") % (self.name, room)) |
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
152 return |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
153 self.games[room] = {'referee': referee} |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
154 self.games[room].update(self.options) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
155 if self.collectiveGame is True: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
156 mess = self.createGameElt(JID(room)) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
157 mess.firstChildElement().addChild(self.__create_started_elt()) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
158 self.host.profiles[profile].xmlstream.send(mess) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
159 return |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
160 # non collaborative game = individual data and messages |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
161 status = {} |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
162 players_data = {} |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
163 for player in players: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
164 # The dict must be COPIED otherwise it is shared between all users |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
165 players_data[player] = self.player_init_data.copy() |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
166 status[player] = "init" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
167 # each player send a message to all the others |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
168 mess = self.createGameElt(JID(room + '/' + player)) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
169 mess.firstChildElement().addChild(self.__create_started_elt(players)) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
170 self.host.profiles[profile].xmlstream.send(mess) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
171 # specific data to each player |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
172 self.games[room].update({'players': players, 'status': status, 'players_data': players_data}) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
173 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
174 def createCollectiveGame(self, room_jid, profile_key='@NONE@'): |
711
c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents:
683
diff
changeset
|
175 return self.createGame(self, room_jid, players=[], profile_key=profile_key) |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
176 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
177 def playerReady(self, player, referee, profile_key='@NONE@'): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
178 """Must be called when player is ready to start a new game""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
179 profile = self.host.memory.getProfileName(profile_key) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
180 if not profile: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
181 error(_("profile %s is unknown") % profile_key) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
182 return |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
183 debug('new player ready: %s' % profile) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
184 mess = self.createGameElt(JID(referee)) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
185 ready_elt = mess.firstChildElement().addElement('player_ready') |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
186 ready_elt['player'] = player |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
187 self.host.profiles[profile].xmlstream.send(mess) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
188 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
189 def newRound(self, room_jid, data, profile): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
190 """Launch a new round (reinit the user data)""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
191 debug(_('new round for %s game') % self.name) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
192 game_data = self.games[room_jid.userhost()] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
193 players = game_data['players'] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
194 players_data = game_data['players_data'] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
195 game_data['stage'] = "init" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
196 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
197 common_data, msg_elts = data if data is not None else (None, None) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
198 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
199 if isinstance(msg_elts, dict): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
200 for player in players: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
201 to_jid = JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
202 mess = self.createGameElt(to_jid) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
203 if isinstance(msg_elts[player], domish.Element): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
204 mess.firstChildElement().addChild(msg_elts[player]) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
205 self.host.profiles[profile].xmlstream.send(mess) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
206 elif isinstance(msg_elts, domish.Element): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
207 mess = self.createGameElt(room_jid) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
208 mess.firstChildElement().addChild(msg_elts) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
209 self.host.profiles[profile].xmlstream.send(mess) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
210 if common_data is not None: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
211 for player in players: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
212 players_data[player].update(common_data) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
213 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
214 def createGameElt(self, to_jid, type_="normal"): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
215 """Create a generic domish Element for the game""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
216 type_ = "normal" if to_jid.resource else "groupchat" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
217 elt = domish.Element((None, 'message')) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
218 elt["to"] = to_jid.full() |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
219 elt["type"] = type_ |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
220 elt.addElement(self.ns_tag) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
221 return elt |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
222 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
223 def __create_started_elt(self, players=None): |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
224 """Create a game "started" domish Element""" |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
225 started_elt = domish.Element((None, 'started')) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
226 if players is None: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
227 return started_elt |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
228 idx = 0 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
229 for player in players: |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
230 player_elt = domish.Element((None, 'player')) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
231 player_elt.addContent(player) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
232 player_elt['index'] = str(idx) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
233 idx += 1 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
234 started_elt.addChild(player_elt) |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
235 return started_elt |