def register(params): """ Registers player on server and associates him with specified name. :return: ID assigned to player. If ID is equal to -1 then registration failed. """ name = base64.b64decode(params.GET.get("name")).replace('%u', r'\u').decode('unicode-escape') state = get_client_manager().register(name) if not state[0]: return {"id": -1, "err": state[1]} return {"id": state[1]}
def send_msg(params): """ Sends message in chat. """ client = get_client_manager().get_client(int(params.GET.get("id"))) if client is None: return {"status": False, "err": "Not a client"} msg = base64.b64decode(params.GET.get("msg")).replace('%u', r'\u').decode('unicode-escape') get_chat().send(msg, client) return {"status": True}
def show(_): """ :return: Data about game. """ get_game_state().check_for_disconnected_players(get_client_manager()) board = get_game_state().get_board() result = [] for row in board: result_row = {"cells": []} for cell in row: result_row["cells"].append(_serialize_field(cell)) result.append(result_row) next_player_id = get_game_state().get_next_player() next_player = get_client_manager().get_client(next_player_id) return { "board": result, "is_game_running": get_game_state().is_game_running(), "next_player": next_player.get_name() if next_player else None, }
def move(params): """ Informs game about player's move. """ client_id = int(params.GET.get("id")) if get_client_manager().get_client(client_id) is None: return None move_def = params.GET.get("move") grouped = [move_def[n:n+2] for n in range(0, len(move_def), 2)] jumps = [{"x": int(m[0]), "y": int(m[1])} for m in grouped] return get_game_state().make_move(jumps)
def clients(params): """ :return: List of registered players on server. """ return {"players": get_client_manager().get_clients_list()}
def keepalive(params): """ Refreshes client. """ client_id = int(params.GET.get("id")) return {"status": get_client_manager().refresh_client(client_id)}