comparison src/tools/plugins/games.py @ 711:c9792d0be499

plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
author souliane <souliane@mailoo.org>
date Sun, 17 Nov 2013 16:30:46 +0100
parents 75e4f5e2cc65
children f610864eb7a5
comparison
equal deleted inserted replaced
710:3344f1d8a232 711:c9792d0be499
28 """This class is used to help launching a MUC game.""" 28 """This class is used to help launching a MUC game."""
29 29
30 def __init__(self, host, plugin_info, ns_tag, player_init_data={}, options={}): 30 def __init__(self, host, plugin_info, ns_tag, player_init_data={}, options={}):
31 """ 31 """
32 @param host 32 @param host
33 @param name: the name of this name 33 @param plugin_info: PLUGIN_INFO map of the game plugin
34 @ns_tag: couple (nameservice, tag) to construct the messages
34 @param player_init_data: dictionary for initialization data, applicable to each player 35 @param player_init_data: dictionary for initialization data, applicable to each player
35 @param options: dictionary for game options 36 @param options: dictionary for game options
36 """ 37 """
37 self.host = host 38 self.host = host
38 self.name = plugin_info["import_name"] 39 self.name = plugin_info["import_name"]
39 self.ns_tag = ns_tag 40 self.ns_tag = ns_tag
40 self.player_init_data = player_init_data 41 self.player_init_data = player_init_data
41 self.collectiveGame = self.player_init_data is {} 42 self.collectiveGame = self.player_init_data == {}
42 self.options = options 43 self.options = options
43 self.games = {} 44 self.games = {}
44 self.waiting_inv = {} # Invitation waiting for people to join to launch a game 45 self.waiting_inv = {} # Invitation waiting for people to join to launch a game
45
46 # args: room_jid, referee, players, profile
47 host.bridge.addSignal("%sGo" % self.name, ".plugin", signature='ssass')
48 46
49 def prepareRoom(self, other_players, profile_key='@NONE@'): 47 def prepareRoom(self, other_players, profile_key='@NONE@'):
50 """Prepare the room for a game: create it and invite players. 48 """Prepare the room for a game: create it and invite players.
51 @param other_players: list for other players JID userhosts 49 @param other_players: list for other players JID userhosts
52 """ 50 """
60 other_players = [] 58 other_players = []
61 players = other_players[:] 59 players = other_players[:]
62 players.append(_jid.userhost()) 60 players.append(_jid.userhost())
63 61
64 def roomJoined(room): 62 def roomJoined(room):
63 """@param room: instance of wokkel.muc.Room"""
65 _room = room.occupantJID.userhostJID() 64 _room = room.occupantJID.userhostJID()
66 if self.collectiveGame is True or other_players == [] and _jid in [user.entity for user in room.roster.values()]: 65 if self.collectiveGame is True or other_players == [] and _jid in [user.entity for user in room.roster.values()]:
67 self.createGame(_room.userhost(), None if self.collectiveGame is True else players, profile_key=profile) 66 self.createGame(_room.userhost(), [] if self.collectiveGame is True else players, profile_key=profile)
68 else: 67 else:
69 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data 68 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data
70 for player in other_players: 69 for player in other_players:
71 self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile) 70 self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile)
72 71
101 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User} 100 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}
102 @user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID 101 @user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID
103 """ 102 """
104 _room_jid = room.occupantJID.userhostJID() 103 _room_jid = room.occupantJID.userhostJID()
105 if self.collectiveGame is True: 104 if self.collectiveGame is True:
106 if _room_jid in self.games and self.games[_room_jid]["referee"] == room.occupantJID.full(): 105 room_s = _room_jid.userhost()
106 if room_s in self.games and self.games[room_s]["referee"] == room.occupantJID.full():
107 #we are in a radiocol room, let's start the party ! 107 #we are in a radiocol room, let's start the party !
108 mess = self.createGameElt(JID(_room_jid + '/' + user.nick)) 108 mess = self.createGameElt(JID(room_s + '/' + user.nick))
109 mess.firstChildElement().addChild(self.__create_started_elt()) 109 mess.firstChildElement().addChild(self.__create_started_elt())
110 self.host.profiles[profile].xmlstream.send(mess) 110 self.host.profiles[profile].xmlstream.send(mess)
111 return True 111 return True
112 if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]): 112 if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]):
113 expected_players = self.waiting_inv[_room_jid][1] 113 expected_players = self.waiting_inv[_room_jid][1]
129 # When we have all people in the room, we create the game 129 # When we have all people in the room, we create the game
130 del self.waiting_inv[_room_jid] 130 del self.waiting_inv[_room_jid]
131 self.createGame(_room_jid.userhost(), players, profile_key=profile) 131 self.createGame(_room_jid.userhost(), players, profile_key=profile)
132 return True 132 return True
133 133
134 def createGame(self, room_jid, players=None, profile_key='@NONE@'): 134 def createGame(self, room_jid, players=[], profile_key='@NONE@'):
135 """Create a new game 135 """Create a new game
136 @param room_jid: jid of the room 136 @param room_jid: jid of the room
137 @param players: list of players nick (nick must exist in the room) 137 @param players: list of players nick (nick must exist in the room)
138 @param profile_key: %(doc_profile_key)s""" 138 @param profile_key: %(doc_profile_key)s"""
139 debug(_("Creating %s game") % self.name) 139 debug(_("Creating %s game") % self.name)
140 room = JID(room_jid).userhost() 140 room = JID(room_jid).userhost()
141 profile = self.host.memory.getProfileName(profile_key) 141 profile = self.host.memory.getProfileName(profile_key)
142 if not profile: 142 if not profile:
143 error(_("profile %s is unknown") % profile_key) 143 error(_("profile %s is unknown") % profile_key)
144 return 144 return
145 if room in self.games:
146 warning(_("%s game already started in room %s") % (self.name, room))
147 return
148 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room, profile) 145 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room, profile)
149 if not room_nick: 146 if not room_nick:
150 error('Internal error') 147 error('Internal error')
151 return 148 return
152 referee = room + '/' + room_nick 149 referee = room + '/' + room_nick
150 if room in self.games:
151 warning(_("%s game already started in room %s") % (self.name, room))
152 return
153 self.games[room] = {'referee': referee} 153 self.games[room] = {'referee': referee}
154 self.games[room].update(self.options) 154 self.games[room].update(self.options)
155 if self.collectiveGame is True: 155 if self.collectiveGame is True:
156 mess = self.createGameElt(JID(room)) 156 mess = self.createGameElt(JID(room))
157 mess.firstChildElement().addChild(self.__create_started_elt()) 157 mess.firstChildElement().addChild(self.__create_started_elt())
170 self.host.profiles[profile].xmlstream.send(mess) 170 self.host.profiles[profile].xmlstream.send(mess)
171 # specific data to each player 171 # specific data to each player
172 self.games[room].update({'players': players, 'status': status, 'players_data': players_data}) 172 self.games[room].update({'players': players, 'status': status, 'players_data': players_data})
173 173
174 def createCollectiveGame(self, room_jid, profile_key='@NONE@'): 174 def createCollectiveGame(self, room_jid, profile_key='@NONE@'):
175 return self.createGame(self, room_jid, players=None, profile_key=profile_key) 175 return self.createGame(self, room_jid, players=[], profile_key=profile_key)
176 176
177 def playerReady(self, player, referee, profile_key='@NONE@'): 177 def playerReady(self, player, referee, profile_key='@NONE@'):
178 """Must be called when player is ready to start a new game""" 178 """Must be called when player is ready to start a new game"""
179 profile = self.host.memory.getProfileName(profile_key) 179 profile = self.host.memory.getProfileName(profile_key)
180 if not profile: 180 if not profile: