def prepare(self): """Sets self.user based off hash in existing cookie, or a new cookie.""" uid = self.get_cookie(self.USER_COOKIE_NAME) if not self.application.users.has_user(uid): if uid is None: uid = hash_obj(id(self), add_random=True) self.set_cookie(self.USER_COOKIE_NAME, uid) puid = hash_obj(uid, add_random=True) self.user = self.application.users.add_user(uid, puid) else: self.user = self.application.users.get_user(uid)
def prepare(self): """Sets self.user based off hash in existing cookie, or a new cookie.""" uid = self.get_cookie(self.USER_COOKIE_NAME) if not self.application.users.has_user(uid): if uid is None: uid = hash_obj(id(self), add_random=True) self.set_cookie(self.USER_COOKIE_NAME, uid) puid = hash_obj(uid, add_random=True) self.user = self.application.users.add_user(uid, puid) else: self.user = self.application.users.get_user(uid) self.user.ping()
def prepare(self): """Sets self.user based off hash in existing cookie, or a new cookie.""" uid = self.get_cookie(self.USER_COOKIE_NAME) if not self.application.users.has_user(uid): if uid is None: uid = hash_obj(id(self), add_random=True) expires = datetime.datetime.utcnow() + datetime.timedelta(days=365) self.set_cookie(self.USER_COOKIE_NAME, uid, expires=expires) puid = hash_obj(uid, add_random=True) self.user = self.application.users.add_user(uid, puid) else: self.user = self.application.users.get_user(uid) self.user.ping()
def __init__(self, name, card_paths, is_default=False): """Initalizes a set of distinct Card objects.""" self.name = name prefix = hash_obj(name, add_random=True)[:5] # must be unique self.cards = [Card('card%s%d' % (prefix, i), p) for i, p in enumerate(card_paths)] self.is_default = is_default
def __init__(self, name, card_paths, is_default=False): """Initalizes a set of distinct Card objects.""" self.name = name prefix = hash_obj(name, add_random=True)[:5] # must be unique self.cards = [ Card('card%s%d' % (prefix, i), p) for i, p in enumerate(card_paths) ] self.is_default = is_default
def add(self, name, msg): """Appends a new msg for a user with the given name.""" cur_time = time.time() self.log[self.tail] = { 'user' : name, 'mid' : hash_obj(cur_time, add_random=True), 'msg' : msg, 't' : cur_time, } self.tail = (self.tail + 1) % self.size
def post(self): admin_password = hash_obj(self.get_argument('password')) if admin_password != self.application.admin_password: stdout = '' stderr = 'Invalid password' else: try: with capture_stdout() as stdout_context: exec self.get_argument('code').replace('\r', '') stdout = stdout_context.getvalue() stderr = '' except Exception as exc: stdout = '' stderr = unicode(exc).encode('utf-8') self.write({ 'stdout' : stdout, 'stderr' : stderr, })
def post(self): try: card_set_indices = map(int, self.request.arguments['card_sets']) except ValueError as exc: raise APIError(Codes.NOT_AN_INTEGER, exc) if min(card_set_indices) < 0 or \ max(card_set_indices) >= len(self.application.card_sets): raise APIError(Codes.ILLEGAL_RANGE, card_set_indices) card_sets = [self.application.card_sets[i] for i in card_set_indices] password = self.get_argument('password', '') # not yet implemented if password: password = hash_obj(password) name = self.get_argument('name', '') if not name: name = 'Jogo %d' % (len(self.application.games) + 1) max_score = self.get_argument('max_score') if not max_score: max_score = INFINITY try: max_score = int(max_score) max_players = int(self.get_argument('max_players')) max_clue_length = int(self.get_argument('max_clue_length')) except ValueError as exc: raise APIError(Codes.NOT_AN_INTEGER, exc) if (not Limits.MIN_NAME <= len(name) <= Limits.MAX_NAME) or \ (not Limits.MIN_PLAYERS <= max_players <= Limits.MAX_PLAYERS) or \ (not Limits.MIN_SCORE <= max_score <= Limits.MAX_SCORE) or \ (not Limits.MIN_CLUE_LENGTH <= max_clue_length <= Limits.MAX_CLUE_LENGTH): raise APIError(Codes.ILLEGAL_RANGE) auto = self.get_argument('auto') if not auto: auto = 0 game = Game(self.user, card_sets, password, name, max_players, max_score, max_clue_length, int(self.get_argument('auto')), int(self.get_argument('time_clue')), int(self.get_argument('time_choose')), int(self.get_argument('time_vote'))) self.application.games.append(game) self.write(str(len(self.application.games) - 1))
def post(self): try: card_set_indices = map(int, self.request.arguments['card_sets']) except ValueError as exc: raise APIError(Codes.NOT_AN_INTEGER, exc) if min(card_set_indices) < 0 or \ max(card_set_indices) >= len(self.application.card_sets): raise APIError(Codes.ILLEGAL_RANGE, card_set_indices) card_sets = [self.application.card_sets[i] for i in card_set_indices] password = self.get_argument('password', '') # not yet implemented if password: password = hash_obj(password) name = self.get_argument('name', '') if not name: name = 'Gra %d' % (len(self.application.games) + 1) max_score = self.get_argument('max_score') if not max_score: max_score = INFINITY try: max_score = int(max_score) max_players = int(self.get_argument('max_players')) max_clue_length = int(self.get_argument('max_clue_length')) except ValueError as exc: raise APIError(Codes.NOT_AN_INTEGER, exc) if (not Limits.MIN_NAME <= len(name) <= Limits.MAX_NAME) or \ (not Limits.MIN_PLAYERS <= max_players <= Limits.MAX_PLAYERS) or \ (not Limits.MIN_SCORE <= max_score <= Limits.MAX_SCORE) or \ (not Limits.MIN_CLUE_LENGTH <= max_clue_length <= Limits.MAX_CLUE_LENGTH): raise APIError(Codes.ILLEGAL_RANGE) auto = self.get_argument('auto') if not auto: auto = 0 game = Game(self.user, card_sets, password, name, max_players, max_score, max_clue_length, int(self.get_argument('auto')), int(self.get_argument('time_clue')), int(self.get_argument('time_choose')), int(self.get_argument('time_vote'))) self.application.games.append(game) self.write(str(len(self.application.games) - 1))
def post(self): if not self.application.admin_enable: raise tornado.web.HTTPError(404) admin_password = hash_obj(self.get_argument('password')) if admin_password != self.application.admin_password: stdout = '' stderr = 'Invalid password' else: try: with capture_stdout() as stdout_context: exec(self.get_argument('code').replace('\r', '')) stdout = stdout_context.getvalue() stderr = '' except Exception as exc: stdout = '' stderr = unicode(exc).encode('utf-8') self.write({ 'stdout': stdout, 'stderr': stderr, })
def _get_board(cls, user, game): """Returns a JSON dictionary summarizing the entire game board.""" players = dict((u.puid, u.name) for u in game.players) scores = dict((u.puid, p.score) for u, p in game.players.items()) is_player = user in game.players requires_action = {} for u in game.players: requires_action[u.puid] = { States.BEGIN: game.host == u and \ len(players) >= Limits.MIN_PLAYERS, States.CLUE: game.clue_maker() == u, States.PLAY: not game.round.has_played(u), States.VOTE: not game.round.has_voted(u), States.END: False, # game.host == u, }[game.state] puids = players.keys() ranked = get_sorted_positions(puids, key=lambda puid: scores[puid]) rnd = {} if game.round.has_everyone_played(): rnd['cards'] = [card.to_json() for card in game.round.get_cards()] rnd['cardsHash'] = hash_obj(rnd['cards']) if game.round.has_everyone_voted(): rnd['votes'] = dict((u.puid, card.cid) for u, card in game.round.user_to_vote.items()) rnd['owners'] = dict((u.puid, card.cid) for u, card in game.round.user_to_card.items()) rnd['votesHash'] = hash_obj(rnd['votes']) if game.round.clue: rnd['clue'] = str(game.round.clue) if game.round.clue_maker: rnd['clueMaker'] = game.round.clue_maker.puid rnd['scores'] = dict((u.puid, score) for u, score in game.round.scores.items() if score > 0) plr = {} if is_player and game.players[user].hand: plr['hand'] = [card.to_json() for card in game.players[user].hand] plr['handHash'] = hash_obj(plr['hand']) blob = { 'name' : game.name, 'auto' : game.auto, 'user' : user.puid, 'host' : game.host.puid, 'players' : players, 'colours' : dict((u.puid, col) for u, col in game.colours.items()), 'isHost' : user == game.host, 'isPlayer' : user in game.players, 'maxPlayers' : game.max_players, 'maxScore' : game.max_score if game.max_score != INFINITY else None, 'maxClueLength' : game.max_clue_length, 'scores' : scores, 'order' : [u.puid for u in game.order], 'turn' : game.turn, 'ranked' : dict((uid, rank) for uid, rank in zip(puids, ranked)), 'left' : game.deck.left(), 'size' : game.deck.size(), 'state' : game.state, 'requiresAction' : requires_action, 'round' : rnd, 'player' : plr, 'time_clue' : game.time_clue, 'time_choose' : game.time_choose, 'time_vote' : game.time_vote } return blob
def _get_board(cls, user, game): players = { u.puid: { 'name': u.name, 'score': game.players[u].score, 'color': game.colours[u], 'color_val': display.BunnyPalette.allColors[game.colours[u]] } for u in game.players } requires_action = {} for u in game.players: requires_action[u.puid] = { States.BEGIN: game.host == u and \ len(players) >= Limits.MIN_PLAYERS, States.CLUE: game.clue_maker() == u, States.PLAY: not game.round.has_played(u), States.VOTE: not game.round.has_voted(u), States.END: False, # game.host == u, }[game.state] puids = list(players.keys()) ranked = get_sorted_positions(puids, key=lambda puid: players[puid]['score']) rnd = {} if game.round.has_everyone_played(): rnd['cards'] = [card.to_json() for card in game.round.get_cards()] rnd['cardsHash'] = hash_obj(rnd['cards']) if game.round.has_everyone_voted(): rnd['votes'] = dict( (u.puid, card.cid) for u, card in list(game.round.user_to_vote.items())) rnd['owners'] = dict( (u.puid, card.cid) for u, card in list(game.round.user_to_card.items())) rnd['votesHash'] = hash_obj(rnd['votes']) if game.round.clue: rnd['clue'] = str(game.round.clue) if game.round.clue_maker: rnd['clueMaker'] = game.round.clue_maker.puid rnd['scores'] = dict((u.puid, score) for u, score in list(game.round.scores.items()) if score > 0) plr = {} if game.players[user].hand: plr['hand'] = [card.to_json() for card in game.players[user].hand] plr['handHash'] = hash_obj(plr['hand']) blob = { 'name': game.name, 'user': user.puid, 'host': game.host.puid, 'players': players, 'isHost': user == game.host, 'maxScore': game.max_score if game.max_score != INFINITY else None, 'maxClueLength': game.max_clue_length, 'order': [u.puid for u in game.order], 'turn': game.turn, 'ranked': dict((uid, rank) for uid, rank in zip(puids, ranked)), 'state': game.state, 'requiresAction': requires_action, 'round': rnd, 'player': plr } gamehash = hash_obj(blob) blob.update({'gamehash': gamehash}) return json.dumps(blob)
def _get_board(cls, user, game): """Returns a JSON dictionary summarizing the entire game board.""" players = dict((u.puid, u.name) for u in game.players) scores = dict((u.puid, p.score) for u, p in game.players.items()) is_player = user in game.players requires_action = {} for u in game.players: requires_action[u.puid] = { States.BEGIN: game.host == u and \ len(players) >= Limits.MIN_PLAYERS, States.CLUE: game.clue_maker() == u, States.PLAY: not game.round.has_played(u), States.VOTE: not game.round.has_voted(u), States.END: False, # game.host == u, }[game.state] puids = players.keys() ranked = get_sorted_positions(puids, key=lambda puid: scores[puid]) rnd = {} if game.round.has_everyone_played(): rnd['cards'] = [card.to_json() for card in game.round.get_cards()] rnd['cardsHash'] = hash_obj(rnd['cards']) if game.round.has_everyone_voted(): rnd['votes'] = dict((u.puid, card.cid) for u, card in game.round.user_to_vote.items()) rnd['owners'] = dict((u.puid, card.cid) for u, card in game.round.user_to_card.items()) rnd['votesHash'] = hash_obj(rnd['votes']) if game.round.clue: rnd['clue'] = str(game.round.clue) if game.round.clue_maker: rnd['clueMaker'] = game.round.clue_maker.puid rnd['scores'] = dict((u.puid, score) for u, score in game.round.scores.items() if score > 0) plr = {} if is_player and game.players[user].hand: plr['hand'] = [card.to_json() for card in game.players[user].hand] plr['handHash'] = hash_obj(plr['hand']) blob = { 'name' : game.name, 'user' : user.puid, 'host' : game.host.puid, 'players' : players, 'colours' : dict((u.puid, col) for u, col in game.colours.items()), 'isHost' : user == game.host, 'isPlayer' : user in game.players, 'maxPlayers' : game.max_players, 'maxScore' : game.max_score if game.max_score != INFINITY else None, 'maxClueLength' : game.max_clue_length, 'scores' : scores, 'order' : [u.puid for u in game.order], 'turn' : game.turn, 'ranked' : dict((uid, rank) for uid, rank in zip(puids, ranked)), 'left' : game.deck.left(), 'size' : game.deck.size(), 'state' : game.state, 'requiresAction' : requires_action, 'round' : rnd, 'player' : plr, } return blob