annotate src/tools/plugins/games.py @ 716:30eb49e4e05d

frontends tools: added symbols for MUC user activity identification
author souliane <souliane@mailoo.org>
date Thu, 21 Nov 2013 15:38:53 +0100
parents ecc5a5b34ee1
children 358018c5c398
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
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 !
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
108 self.send(JID(room_s + '/' + user.nick), self.createStartElement(), profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
109 return True
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
110 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
111 expected_players = self.waiting_inv[_room_jid][1]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
112 players = []
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
113 for player in expected_players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
114 for user in room.roster.values():
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
115 if user.entity is not None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
116 # check people identity
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
117 if user.entity.userhost() == player:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
118 players.append(user.nick)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
119 continue
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
120 else:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
121 # 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
122 if user.nick == JID(player).user:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
123 players.append(user.nick)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
124 if len(players) < len(expected_players):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
125 # 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
126 return True
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
127 # 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
128 del self.waiting_inv[_room_jid]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
129 self.createGame(_room_jid.userhost(), players, profile_key=profile)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
130 return True
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
131
711
c9792d0be499 plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
souliane <souliane@mailoo.org>
parents: 683
diff changeset
132 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
133 """Create a new game
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
134 @param room_jid: jid of the room
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
135 @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
136 @param profile_key: %(doc_profile_key)s"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
137 debug(_("Creating %s game") % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
138 room = JID(room_jid).userhost()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
139 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
140 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
141 error(_("profile %s is unknown") % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
142 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
143 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
144 if not room_nick:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
145 error('Internal error')
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
146 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
147 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
148 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
149 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
150 return
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
151 self.games[room] = {'referee': referee}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
152 self.games[room].update(self.options)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
153 if self.collectiveGame is True:
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
154 self.send(JID(room), self.createStartElement(), profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
155 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
156 # non collaborative game = individual data and messages
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
157 status = {}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
158 players_data = {}
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
159 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
160 # 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
161 players_data[player] = self.player_init_data.copy()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
162 status[player] = "init"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
163 # each player send a message to all the others
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
164 self.send(JID(room + '/' + player), self.createStartElement(players), profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
165 # specific data to each player
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
166 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
167
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
168 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
169 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
170
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
171 def playerReady(self, player, referee, profile_key='@NONE@'):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
172 """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
173 profile = self.host.memory.getProfileName(profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
174 if not profile:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
175 error(_("profile %s is unknown") % profile_key)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
176 return
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
177 debug('new player ready: %s' % profile)
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
178 self.send(JID(referee), 'player_ready', {'player': player}, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
179
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
180 def newRound(self, room_jid, data, profile):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
181 """Launch a new round (reinit the user data)"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
182 debug(_('new round for %s game') % self.name)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
183 game_data = self.games[room_jid.userhost()]
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
184 players = game_data['players']
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
185 players_data = game_data['players_data']
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
186 game_data['stage'] = "init"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
187
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
188 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
189
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
190 if isinstance(msg_elts, dict):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
191 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
192 to_jid = JID(room_jid.userhost() + "/" + player) # FIXME: gof:
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
193 elem = msg_elts[player] if isinstance(msg_elts[player], domish.Element) else None
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
194 self.send(to_jid, elem, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
195 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
196 self.send(room_jid, msg_elts, profile=profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
197 if common_data is not None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
198 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
199 players_data[player].update(common_data)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
200
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
201 def createGameElt(self, to_jid, type_="normal"):
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
202 """Create a generic domish Element for the game"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
203 type_ = "normal" if to_jid.resource else "groupchat"
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
204 elt = domish.Element((None, 'message'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
205 elt["to"] = to_jid.full()
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
206 elt["type"] = type_
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
207 elt.addElement(self.ns_tag)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
208 return elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
209
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
210 def createStartElement(self, players=None):
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
211 """Create a game "started" domish Element"""
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
212 started_elt = domish.Element((None, 'started'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
213 if players is None:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
214 return started_elt
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
215 idx = 0
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
216 for player in players:
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
217 player_elt = domish.Element((None, 'player'))
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
218 player_elt.addContent(player)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
219 player_elt['index'] = str(idx)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
220 idx += 1
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
221 started_elt.addChild(player_elt)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
diff changeset
222 return started_elt
714
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
223
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
224 def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
225 """
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
226 @param to_jid: recipient JID
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
227 @param elem: domish.Element, unicode or a couple:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
228 - domish.Element to be directly added as a child to the message
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
229 - unicode name or couple (uri, name) to create a new domish.Element
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
230 and add it as a child to the message (see domish.Element.addElement)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
231 @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
232 @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
233 @param profile: the profile from which the message is sent
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
234 """
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
235 if profile is None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
236 error(_("Message can not be sent without a sender profile"))
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
237 return
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
238 msg = self.createGameElt(to_jid)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
239 if elem is not None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
240 if isinstance(elem, domish.Element):
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
241 msg.firstChildElement().addChild(elem)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
242 else:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
243 elem = msg.firstChildElement().addElement(elem)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
244 if attrs is not None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
245 elem.attributes.update(attrs)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
246 if content is not None:
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
247 elem.addContent(content)
ecc5a5b34ee1 plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents: 712
diff changeset
248 self.host.profiles[profile].xmlstream.send(msg)