def Post(self, user): """ Handles posting a chat to the lobby. URL format is: /lobby_ajax/<email>/chat """ path_list = self._get_path_list(user) if path_list: command = path_list[2] if command == "chat": data = self.request.get("chat") chat.add_chat("lobby", user, data, 50) else: self.error(http.HTTP_ERROR) self.response.out.write('Invalid request ' + command)
def Post(self, user): """ Our handler for HTTP POST requests - this posts a user's move to the given game. User must be a participant in that game. This attempts to be idempotent, so if the same move is posted twice in a row, the second post is ignored. URL Path should be one of: /game_ajax/<id of game to join>/join (no post params) /game_ajax/<id of game to post a move to>/move POST params are: move: <string data in ICCF format> Special values: 'resign', 'draw' causes a resignation/draw Moves that start with '#' denote a checkmate time: updated time /game_ajax/<id of game to post a chat to>/chat POST params are: chat: <string data> /game_ajax/<id of game to update time>/time POST params are: time: milliseconds left for user Response: HTTP errors: http.HTTP_FORBIDDEN if you try to join a game that is already full or try to move out of turn http.HTTP_UNAUTHORIZED if you aren't a participant and try to move or chat JSON-encoded response: none """ game_to_modify = self._get_game_to_modify(user) if game_to_modify: path_list = self.request.path.strip("/").split("/") command = path_list[2] if command == "join": result = game_to_modify.join(user) if not result: # Error - game is already full, or closed self.error(http.HTTP_FORBIDDEN) elif command == "move": # User is posting a move move = self.request.get("move") timer = self.request.get("time") victor = None is_resignation = False if timer: timer = int(timer) if move: # Handle special endgame move commands if move == "resign": # User resigned victor = get_player_number(game_to_modify, user, True) is_resignation = True elif move == "draw": victor = 0 elif move[0] == "#": # User won by checkmate victor = get_player_number(game_to_modify, user, False) if not game_to_modify.update(user, move, timer, victor): # User tried to move when it was not his turn self.error(http.HTTP_FORBIDDEN) else: # User moved - let's notify the other player if this is an untimed # game. if game_to_modify.game_type == gamemodel.GAME_TYPE_CHESS: if get_player_number(game_to_modify, user, False) == 1: notify_email = game_to_modify.player2.email() else: notify_email = game_to_modify.player1.email() self.email_status(game_to_modify, notify_email, user.nickname(), victor, is_resignation) elif command == "time": # User is posting a time update timer = int(self.request.get("time")) game_to_modify.update(user, timer=timer) elif command == "chat": # User is posting a chat data = self.request.get("chat") if data: chat.add_chat(str(game_to_modify.key()), user, data, GAME_CHAT_LIMIT)
def Post(self, user): """ Our handler for HTTP POST requests - this posts a user's move to the given game. User must be a participant in that game. This attempts to be idempotent, so if the same move is posted twice in a row, the second post is ignored. URL Path should be one of: /game_ajax/<id of game to join>/join (no post params) /game_ajax/<id of game to post a move to>/move POST params are: move: <string data in ICCF format> Special values: 'resign', 'draw' causes a resignation/draw Moves that start with '#' denote a checkmate time: updated time /game_ajax/<id of game to post a chat to>/chat POST params are: chat: <string data> /game_ajax/<id of game to update time>/time POST params are: time: milliseconds left for user Response: HTTP errors: http.HTTP_FORBIDDEN if you try to join a game that is already full or try to move out of turn http.HTTP_UNAUTHORIZED if you aren't a participant and try to move or chat JSON-encoded response: none """ game_to_modify = self._get_game_to_modify(user) if game_to_modify: path_list = self.request.path.strip('/').split('/') command = path_list[2] if command == 'join': result = game_to_modify.join(user) if not result: # Error - game is already full, or closed self.error(http.HTTP_FORBIDDEN) elif command == 'move': # User is posting a move move = self.request.get('move') timer = self.request.get('time') victor = None is_resignation = False if timer: timer = int(timer) if move: # Handle special endgame move commands if move == 'resign': # User resigned victor = get_player_number(game_to_modify, user, True) is_resignation = True elif move == 'draw': victor = 0 elif move[0] == '#': # User won by checkmate victor = get_player_number(game_to_modify, user, False) if not game_to_modify.update(user, move, timer, victor): # User tried to move when it was not his turn self.error(http.HTTP_FORBIDDEN) else: # User moved - let's notify the other player if this is an untimed # game. if game_to_modify.game_type == gamemodel.GAME_TYPE_CHESS: if get_player_number(game_to_modify, user, False) == 1: notify_email = game_to_modify.player2.email() else: notify_email = game_to_modify.player1.email() self.email_status(game_to_modify, notify_email, user.nickname(), victor, is_resignation) elif command == 'time': # User is posting a time update timer = int(self.request.get('time')) game_to_modify.update(user, timer=timer) elif command == 'chat': # User is posting a chat data = self.request.get('chat') if data: chat.add_chat(str(game_to_modify.key()), user, data, GAME_CHAT_LIMIT)