def do_GET_game_results(self, path_parts, params): ''' GET endpoint for /game/<gid>/results ''' if len(path_parts) < 3: self.send_error( 404, 'Invalid request for "%s"' % '/'.join(path_parts)) return assert (path_parts[2] == 'results') gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() results = [] for puzzle in game.puzzles: for scrambl in puzzle: results.append( [scrambl.pid, scrambl.solved, len(scrambl.indices)]) self.wfile.write('%s' % json.dumps(results)) return
def do_GET_game(self, path_parts, params): ''' Entry point for all GET endpoints for /game/<gid> ''' assert (path_parts[0] == 'game') if len(path_parts) < 2: self.send_error(404, 'Game id is missing') return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return if len(path_parts) == 2: return self.do_GET_game_status(path_parts, params) action = path_parts[2] if action == 'user': return self.do_GET_game_user(path_parts, params) elif action == 'results': return self.do_GET_game_results(path_parts, params) else: self.send_error(404) return
def do_GET_game_results(self, path_parts, params): ''' GET endpoint for /game/<gid>/results ''' if len(path_parts) < 3: self.send_error(404, 'Invalid request for "%s"' % '/'.join(path_parts)) return assert(path_parts[2] == 'results') gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() results = [] for puzzle in game.puzzles: for scrambl in puzzle: results.append([scrambl.pid, scrambl.solved, len(scrambl.indices)]) self.wfile.write('%s' % json.dumps(results)) return
def do_GET_game_status(self, path_parts, params): gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Cannot find information for game id %s' % gid) return # list of each user time_remaining = game.timer() state = None if game.solved: state = '<b><u>Closed Scramble Solved!</u></b><br><br>Beginning next round.' elif time_remaining <= 0: state = '<b><u>Time expired.</u></b><br><br>Beginning next round.' player_stats = self._solved_puzzle_stats(game) values = {'timer': time_remaining if time_remaining > 0 else 0, 'state': state, 'player_stats': player_stats} users = list() for user in game.users: users.append({'name': user.game_name, 'mystery': user.mystery_solver, 'scramble': user.scramble.pretty_name if user.scramble is not None else ''}) values['users'] = users self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write('%s' % json.dumps(values)) return
def do_GET_game(self, path_parts, params): ''' Entry point for all GET endpoints for /game/<gid> ''' assert(path_parts[0] == 'game') if len(path_parts) < 2: self.send_error(404, 'Game id is missing') return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return if len(path_parts) == 2: return self.do_GET_game_status(path_parts, params) action = path_parts[2] if action == 'user': return self.do_GET_game_user(path_parts, params) elif action == 'results': return self.do_GET_game_results(path_parts, params) else: self.send_error(404) return
def do_GET_game_scramble(self, path_parts, params): ''' GET endpoint for /game/<gid>/user/<uid>/scramble/<pid> ''' if len(path_parts) < 6: self.send_error(404, 'Invalid request for "%s"' % '/'.join(path_parts)) return assert(path_parts[4] == 'scramble') gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return uid = path_parts[3] try: user = game.get_user(uid) except KeyError: self.send_error(404, 'Unknown userid "%s"' % uid) return pid = path_parts[5] try: scrambl = game.get_scramble(pid) except KeyError: self.send_error(404, 'Unknown scramble "%s"' % pid) return solvers = list() mystery_solver = '' for u in game.users: if scrambl.mystery: if u.mystery_solver: solvers.append(u.game_name) else: solvers.append(u.game_name) if u.mystery_solver: mystery_solver = u.game_name values = { 'scramble': scrambl.scramble, 'solved': scrambl.solved, 'hidden': (scrambl.mystery and not user.mystery_solver), 'message': scrambl.message, 'scramble_name': user.scramble.pretty_name if user.scramble is not None else '', 'solvers': ', '.join(solvers), 'mystery_solver': mystery_solver, } self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write('%s' % json.dumps(values)) return
def do_POST_game_scramble(self, path_parts, params): if len(path_parts) < 4: self.send_error(404) return assert (path_parts[0] == 'game') assert (path_parts[2] == 'scramble') if 'uid' not in params: self.send_error(404, 'Post request missing "uid"') return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return pid = path_parts[3] try: scrambl = game.get_scramble(pid) except KeyError: self.send_error(404, 'Unknown scramble "%s"' % pid) return keys = sorted( [key for key in params.keys() if key.startswith('l')]) guess = '' for key in keys: for char in params[key]: guess += char guess_message = 'Guess "%s" is ' % guess uid = params['uid'][0] engine.record_stat(time.time(), 'scramble_guess', pid, uid) if scrambl.guess(guess): game.solve(pid, uid) ts = time.time() engine.record_stat(ts, 'scramble_solve', pid, uid) engine.record_stat(ts, 'scramble_keys', len(game.get_scramble(pid).indices), uid) if game.solved: engine.record_stat(ts, 'puzzle_solve', game.gid, game.puzzle) guess_message += 'correct' else: guess_message += 'incorrect' params['message'] = [guess_message] path = ['game', gid, 'user', uid] return self.do_GET_game_user(path, params)
def do_POST_game_scramble(self, path_parts, params): if len(path_parts) < 4: self.send_error(404) return assert(path_parts[0] == 'game') assert(path_parts[2] == 'scramble') if 'uid' not in params: self.send_error(404, 'Post request missing "uid"') return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return pid = path_parts[3] try: scrambl = game.get_scramble(pid) except KeyError: self.send_error(404, 'Unknown scramble "%s"' % pid) return keys = sorted([key for key in params.keys() if key.startswith('l')]) guess = '' for key in keys: for char in params[key]: guess += char guess_message = 'Guess "%s" is ' % guess uid = params['uid'][0] engine.record_stat(time.time(), 'scramble_guess', pid, uid) if scrambl.guess(guess): game.solve(pid, uid) ts = time.time() engine.record_stat(ts, 'scramble_solve', pid, uid) engine.record_stat(ts, 'scramble_keys', len(game.get_scramble(pid).indices), uid) if game.solved: engine.record_stat(ts, 'puzzle_solve', game.gid, game.puzzle) guess_message += 'correct' else: guess_message += 'incorrect' params['message'] = [guess_message] path = ['game', gid, 'user', uid] return self.do_GET_game_user(path, params)
def do_GET_game_user(self, path_parts, params): ''' GET endpoint for /game/<gid>/user/<uid> ''' if len(path_parts) < 4: self.send_error( 404, 'Invalid request for "%s"' % '/'.join(path_parts)) return assert (path_parts[2] == 'user') if len(path_parts) > 4: action = path_parts[4] if action == 'scramble': return self.do_GET_game_scramble(path_parts, params) else: self.send_error( 404, 'Invalid action "%s"' % '/'.join(path_parts)) return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return uid = path_parts[3] try: user = game.get_user(uid) except KeyError: self.send_error(404, 'Unknown userid "%s"' % uid) return if user.scramble == scramble.game.DUMMY_SCRAMBLE: game.user_ready(uid) engine.record_stat(time.time(), 'scramble_start', user.scramble.pid, user.uid) if game.completed(): return self.load_credits(game, user) elif not game.all_users_ready(): return self.load_wait_screen(game, user) else: message = None if 'message' in params: message = params['message'][0] return self.load_scramble(game, user, message)
def do_GET_game_user(self, path_parts, params): ''' GET endpoint for /game/<gid>/user/<uid> ''' if len(path_parts) < 4: self.send_error(404, 'Invalid request for "%s"' % '/'.join(path_parts)) return assert(path_parts[2] == 'user') if len(path_parts) > 4: action = path_parts[4] if action == 'scramble': return self.do_GET_game_scramble(path_parts, params) else: self.send_error(404, 'Invalid action "%s"' % '/'.join(path_parts)) return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return uid = path_parts[3] try: user = game.get_user(uid) except KeyError: self.send_error(404, 'Unknown userid "%s"' % uid) return if user.scramble == scramble.game.DUMMY_SCRAMBLE: game.user_ready(uid) engine.record_stat(time.time(), 'scramble_start', user.scramble.pid, user.uid) if game.completed(): return self.load_credits(game, user) elif not game.all_users_ready(): return self.load_wait_screen(game, user) else: message = None if 'message' in params: message = params['message'][0] return self.load_scramble(game, user, message)
def do_POST_game_user(self, path_parts, params): if len(path_parts) < 4: self.send_error(404) return assert (path_parts[0] == 'game') assert (path_parts[2] == 'user') if 'scramble' not in params: self.send_error(404) return scramble_change = params['scramble'][0] gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return uid = path_parts[3] try: user = game.get_user(uid) except KeyError: self.send_error(404, 'Unknown userid "%s"' % uid) return if scramble_change == 'next': if user.scramble.next_scramble is not None: user.scramble = user.scramble.next_scramble elif scramble_change == 'prev': if user.scramble.prev_scramble is not None: user.scramble = user.scramble.prev_scramble else: self.send_error( 404, 'Unknown scramble action: "%s"' % scramble_change) return path = ['game', gid, 'user', uid] engine.record_stat(time.time(), 'scramble_start', user.scramble.pid, user.uid) return self.do_GET_game_user(path, params)
def do_GET_game_status(self, path_parts, params): gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Cannot find information for game id %s' % gid) return # list of each user time_remaining = game.timer() state = None if game.solved: state = '<b><u>Closed Scramble Solved!</u></b><br><br>Beginning next round.' elif time_remaining <= 0: state = '<b><u>Time expired.</u></b><br><br>Beginning next round.' player_stats = self._solved_puzzle_stats(game) values = { 'timer': time_remaining if time_remaining > 0 else 0, 'state': state, 'player_stats': player_stats } users = list() for user in game.users: users.append({ 'name': user.game_name, 'mystery': user.mystery_solver, 'scramble': user.scramble.pretty_name if user.scramble is not None else '' }) values['users'] = users self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write('%s' % json.dumps(values)) return
def do_POST_game_advance(self, path_parts, params): if len(path_parts) < 3: self.send_error(404) return assert (path_parts[0] == 'game') assert (path_parts[2] == 'advance') if 'uid' not in params: self.send_error(404, 'Post request missing "uid"') return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return restart = False if game.solved: restart = True elif game.timer() <= 0: engine.record_stat(time.time(), 'puzzle_expired', game.gid, game.puzzle) restart = True if restart: # load next level game.start_puzzle(game.puzzle + 1) if not game.completed(): engine.record_stat(game.start, 'puzzle_start', game.gid, game.puzzle) for user in game.users: engine.record_stat(game.start, 'scramble_start', user.scramble.pid, user.uid) uid = params['uid'][0] path = ['game', gid, 'user', uid] return self.do_GET_game_user(path, params)
def do_POST_game_user(self, path_parts, params): if len(path_parts) < 4: self.send_error(404) return assert(path_parts[0] == 'game') assert(path_parts[2] == 'user') if 'scramble' not in params: self.send_error(404) return scramble_change = params['scramble'][0] gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return uid = path_parts[3] try: user = game.get_user(uid) except KeyError: self.send_error(404, 'Unknown userid "%s"' % uid) return if scramble_change == 'next': if user.scramble.next_scramble is not None: user.scramble = user.scramble.next_scramble elif scramble_change == 'prev': if user.scramble.prev_scramble is not None: user.scramble = user.scramble.prev_scramble else: self.send_error(404, 'Unknown scramble action: "%s"' % scramble_change) return path = ['game', gid, 'user', uid] engine.record_stat(time.time(), 'scramble_start', user.scramble.pid, user.uid) return self.do_GET_game_user(path, params)
def do_POST_game_advance(self, path_parts, params): if len(path_parts) < 3: self.send_error(404) return assert(path_parts[0] == 'game') assert(path_parts[2] == 'advance') if 'uid' not in params: self.send_error(404, 'Post request missing "uid"') return gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return restart = False if game.solved: restart = True elif game.timer() <= 0: engine.record_stat(time.time(), 'puzzle_expired', game.gid, game.puzzle) restart = True if restart: # load next level game.start_puzzle(game.puzzle + 1) if not game.completed(): engine.record_stat(game.start, 'puzzle_start', game.gid, game.puzzle) for user in game.users: engine.record_stat(game.start, 'scramble_start', user.scramble.pid, user.uid) uid = params['uid'][0] path = ['game', gid, 'user', uid] return self.do_GET_game_user(path, params)
def do_GET_game_scramble(self, path_parts, params): ''' GET endpoint for /game/<gid>/user/<uid>/scramble/<pid> ''' if len(path_parts) < 6: self.send_error( 404, 'Invalid request for "%s"' % '/'.join(path_parts)) return assert (path_parts[4] == 'scramble') gid = path_parts[1] try: game = engine.game(gid) except KeyError: self.send_error(404, 'Unknown game id %s' % gid) return uid = path_parts[3] try: user = game.get_user(uid) except KeyError: self.send_error(404, 'Unknown userid "%s"' % uid) return pid = path_parts[5] try: scrambl = game.get_scramble(pid) except KeyError: self.send_error(404, 'Unknown scramble "%s"' % pid) return solvers = list() mystery_solver = '' for u in game.users: if scrambl.mystery: if u.mystery_solver: solvers.append(u.game_name) else: solvers.append(u.game_name) if u.mystery_solver: mystery_solver = u.game_name values = { 'scramble': scrambl.scramble, 'solved': scrambl.solved, 'hidden': (scrambl.mystery and not user.mystery_solver), 'message': scrambl.message, 'scramble_name': user.scramble.pretty_name if user.scramble is not None else '', 'solvers': ', '.join(solvers), 'mystery_solver': mystery_solver, } self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write('%s' % json.dumps(values)) return