annotate src/tools/plugins/games.py @ 690:d8e7a58eaa00

misc: added a file for frontend's string operations: - the method getURLParams defined here is needed for Libervia autologin feature
author souliane <souliane@mailoo.org>
date Fri, 08 Nov 2013 19:14:13 +0100
parents 75e4f5e2cc65
children c9792d0be499
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
3
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT: a jabber client
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
6
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
11
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
16
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
19
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
20 from logging import debug, warning, error
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
21 from twisted.words.protocols.jabber.jid import JID
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from twisted.words.xish import domish
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from time import time
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
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
33 @param name: the name of this name
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
34 @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
35 @param options: dictionary for game options
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
36 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
37 self.host = host
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
38 self.name = plugin_info["import_name"]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
39 self.ns_tag = ns_tag
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
40 self.player_init_data = player_init_data
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
41 self.collectiveGame = self.player_init_data is {}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
42 self.options = options
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
43 self.games = {}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
44 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
45
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
46 # args: room_jid, referee, players, profile
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
47 host.bridge.addSignal("%sGo" % self.name, ".plugin", signature='ssass')
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
48
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
49 def prepareRoom(self, other_players, profile_key='@NONE@'):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
50 """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
51 @param other_players: list for other players JID userhosts
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
52 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
53 debug(_('Preparing room for %s game') % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
54 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
55 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
56 error(_("Unknown profile"))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
57 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
58 _jid, xmlstream = self.host.getJidNStream(profile)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
59 if other_players is None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
60 other_players = []
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
61 players = other_players[:]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
62 players.append(_jid.userhost())
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
63
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
64 def roomJoined(room):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
65 _room = room.occupantJID.userhostJID()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
66 if self.collectiveGame is True or other_players == [] and _jid in [user.entity for user in room.roster.values()]:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
67 self.createGame(_room.userhost(), None if self.collectiveGame is True else players, profile_key=profile)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
68 else:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
69 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
70 for player in other_players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
71 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
72
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
73 def after_init(ignore):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
74 room_name = "sat_%s_%s" % (self.name.lower(), self.host.plugins["XEP-0045"].getUniqueName(profile_key))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
75 print "\n\n===> room_name:", room_name
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
76 muc_service = None
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
77 for service in self.host.memory.getServerServiceEntities("conference", "text", profile):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
78 if not ".irc." in service.userhost():
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
79 #FIXME:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
80 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
81 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
82 #to manage this, but this hack fill do it for test purpose
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
83 muc_service = service
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
84 break
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
85 if not muc_service:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
86 error(_("Can't find a MUC service"))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
87 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
88
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
89 d = self.host.plugins["XEP-0045"].join(JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
90 d.addCallback(roomJoined)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
91
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
92 client = self.host.getClient(profile)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
93 if not client:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
94 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
95 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
96 client.client_initialized.addCallback(after_init)
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 def userJoinedTrigger(self, room, user, profile):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
99 """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
100 and to create a game if everybody is here.
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
101 @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
102 @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
103 """
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
104 _room_jid = room.occupantJID.userhostJID()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
105 if self.collectiveGame is True:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
106 if _room_jid in self.games and self.games[_room_jid]["referee"] == room.occupantJID.full():
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 !
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
108 mess = self.createGameElt(JID(_room_jid + '/' + user.nick))
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
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
134 def createGame(self, room_jid, players=None, profile_key='@NONE@'):
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 if room in self.games:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
146 warning(_("%s game already started in room %s") % (self.name, room))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
147 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
148 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
149 if not room_nick:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
150 error('Internal error')
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
151 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
152 referee = room + '/' + room_nick
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@'):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
175 return self.createGame(self, room_jid, players=None, profile_key=profile_key)
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