def handleChat(self, req): # It must support auth player = self.getPlayer(req) Client.get('players').postMessage( what = 'chat', phrase = getArg(req, 'phrase'), author = player.id ) return {}
def __init__(self): Resource.__init__(self) self.putChild('clients', DictResource(Client)) self.putChild('games', DictResource(Game)) if not STATIC_PREFIX.startswith('http'): # got no static server mediaPath = os.path.join(os.path.dirname(world.__file__), 'm') self.child_m = static.File(mediaPath) Client.get('players').postMessage(what = 'chat', author = 'World', phrase = 'Started working...')
def handleStartGame(self, req): """ When player has enough invitations, he can send a 'startGame' """ starter = self.getPlayer(req) # check everything ids = getArg(req, 'with').split(' ') partners = [starter] errMsgs = [] for id in ids: player = Client.get(id) if not player: errMsgs += ["No player %s" % id] continue partners += [player] # remove invitation starter.postMessage(what = 'uninvited', gameType = getArg(req, 'gameType'), who = player.id) # Now let's start # FIXME Account for other kinds of games here thousand.Game(partners) rv = {} if errMsgs != []: # it's partial success so we don't raise exception rv['err'] = {'msg': "\n".join(errMsgs)} return rv
def handleInvite(self, req): """ Any player can send 'invite' with list of target ids Then each of the targets gets and keeps an invitation """ inviter = self.getPlayer(req) ids = getArg(req, 'target').split(' ') errMsgs = [] for id in ids: if id == inviter.id: errMsgs += ["You can't invite yourself"] continue who = Client.get(id) if not who: errMsgs += ["No client %s" % id] # TODO Don't send if he already got equivalent invitation. who.postMessage(what = 'invited', who = inviter.id, gameType = getArg(req, 'gameType')) rv = {} if errMsgs != []: # it's partial success so we don't raise exception rv['err'] = {'msg': "\n".join(errMsgs)} return rv
def _getPlayerById(self, id): rv = Client.get(id) if isinstance(rv, Player): return rv raise ValueError("No player %s" % id)