def launch_game() -> None: game_name = session["game"] player = session.get("player") if player is None: return game = manager.get_game(game_name) if game is None: return game.launch() # GameRun(players) payload = dict( game=game.map_name, runs=game.n_run, diff=game.difficulty, by=player, small_scale=game.small_scale, ) emit( "status-update", dict(status=game.status, payload=payload), json=True, broadcast=True, room=game_name, ) wait_and_run(3, launch_run, game_name, game.curr_run_id)
def end_game(game_name: GameName, run_id: int) -> None: # global game game = manager.get_game(game_name) if game is None or game.curr_run_id != run_id or game.done: return print(f"Ending run {game.curr_run_id+1}") with app.test_request_context("/"): # 1: get current place (city_name, hint), (lon, lat) = game.current.place answer = dict(name=city_name, lon=lon, lat=lat) # 2: end game records = game.current.records results, done = game.end() payload = dict( results=records, answer=answer, leaderboard=game.get_current_leaderboard(), done=done, ) socketio.emit( "status-update", dict(status=game.status, payload=payload), json=True, broadcast=True, room=game_name, ) # 3: continue? if done: timers[game_name] = wait_and_run(game.wait_time, terminate_game, game_name) else: timers[game_name] = wait_and_run( game.wait_time, launch_run, game_name, game.curr_run_id )
def process_guess(data: Coordinates) -> None: # global duration_thread game_name = session["game"] game = manager.get_game(game_name) player = session.get("player") if player is None or game is None: return # player = data["player"] print("Receiving guess from", player) lon, lat = data["lon"], data["lat"] res, done = game.current.process_answer((lon, lat), player) res["total_score"] = ( game.scores[player] + res["score"] ) # We need to add res["score"] between game.scores isn't updated yet # emit("log", f"Player <{player}> has scored {res['score']} points", broadcast=True, # room=game_name) emit( "new-guess", dict(player=player, dist=res["dist"], delta=res["delta"], score=res["score"]), broadcast=True, room=game_name, ) emit("score", res, json=True) if done: try: print(f"Interrupting run {game.curr_run_id+1}\n") safe_cancel(timers[game_name]) except AttributeError: pass end_game(game_name, game.curr_run_id)
def leave_game() -> None: if "player" not in session: return player = session["player"] game_name = session["game"] game = manager.get_game(game_name) if game is None: return game.remove_player(player) leave_room(game_name) emit( "player-left", PlayerLeftNotification(player=player), broadcast=True, room=game_name, ) print(f"<{player}> disconnected!") if not game.players and not game.is_permanent: manager.remove_game(game_name) close_room(game_name) safe_cancel(timers[game_name]) del timers[game_name] print(f"Game <{game_name}> was removed!")
def init_game(data: ConnectionPayload) -> None: print("Trying to connect with payload", data) game_name: GameName = session["game"] player: Player = session.get("player", Player("unknown")) print(f"Receive <event:connection[to={game_name}]> from <player:{player}>") if not manager.exists(game_name): print(f"Game <game:{game_name}> does not exist") del session["game"] emit("redirect", RedirectNotification(url=url_for("serve_main")), json=True) # return redirect(url_for("serve_main")) join_room(game_name) game = manager.get_game(game_name) if game is None: raise KeyError(f"Game <game:{game_name}> is None") pseudo: Optional[str] if "pseudo" in data and data["pseudo"]: pseudo = Pseudo(data["pseudo"]) else: pseudo = None if "player" in session: player = session["player"] print(f"Receive <event:connection> from existing player <player:{player}>") if player not in game.players: game.add_player(player, pseudo) else: player, pseudo = game.add_player(pseudo=pseudo) session["player"] = player print(f"Player <{player}> connected to game <{game_name}> with pseudo <{pseudo}>") leaderboard = game.get_current_leaderboard() emit( "init", InitNotification( player=player, launched=game.launched, pseudo=pseudo, game=game.map_name, current=game.curr_run_id, runs=game.n_run, diff=game.difficulty, # precision=game.precision_mode, game_name=game.map_display_name, leaderboard=leaderboard, pseudos=game.pseudos, ), ) emit( "new-player", NewPlayerNotification( player=player, pseudo=pseudo, score=game.get_player_score(player) ), broadcast=True, room=game_name, )
def update_pseudo(data: NameChangePayload) -> None: game_name: GameName = session["game"] player: Optional[Player] = session.get("player") game = manager.get_game(game_name) if player is None or game is None: return pseudo = data["name"] if is_valid_pseudo(pseudo): pseudo = process_pseudo(pseudo) game.add_pseudo(player, pseudo) else: pseudo = game.request_pseudo(player) emit( "new-name", NewNameNotification(player=player, pseudo=pseudo), room=game_name, broadcast=True, json=True, )
def launch_run(game_name: GameName, run_id: int) -> None: # global duration_thread game = manager.get_game(game_name) if game is None or game.curr_run_id != run_id: return print(f"Launching run {game.curr_run_id+1} for game <{game_name}>") with app.test_request_context("/"): hint = game.launch_run() payload = dict(hint=hint, current=game.curr_run_id, total=game.n_run) print(f"Hint is '{hint}'") socketio.emit( "status-update", dict(status=game.status, payload=payload), json=True, room=game_name, broadcast=True, ) timers[game_name] = wait_and_run( game.current.duration, end_game, game_name, game.curr_run_id )
def serve_game(name): if not manager.exists(name): if "game" in session: del session["game"] return redirect(url_for("serve_main")) else: session["game"] = name game_name = session["game"] game = manager.get_game(game_name) params = dict( map=game.map_name, wait_time=game.wait_time, bbox=game.bbox, ssl_disabled=CONF["disableSSL"], difficulty=game.difficulty, allow_zoom=game.allow_zoom, precision_mode=game.precision_mode, duration=game.duration, ) return render_template( "main.html", game_name=name, params=params, debug=IS_LOCAL )
def terminate_game(game_name: GameName) -> None: game = manager.get_game(game_name) if game is None or not game.done: return game.terminate() payload = GameEndPayload( leaderboard=game.get_current_leaderboard(), full=game.get_final_results(), # TODO: remove useless data ) with app.test_request_context("/"): status = game.status print( f"Ending game <{game_name}> (emitting <event:status-update> " f"with status={status})" ) socketio.emit( "status-update", GameEndNotification(status=status, payload=payload), json=True, broadcast=True, room=game_name, ) manager.relaunch_game(game_name)