def new_game(self, request): """Creates new game""" player_one = User.query(User.name == request.player_one_name).get() player_two = User.query(User.name == request.player_two_name).get() if not player_one: raise endpoints.NotFoundException('Player one "' + request.player_one_name + '" does not exist!') if not player_two: raise endpoints.NotFoundException('Player two "' + request.player_two_name + '" does not exist!') game = Game.new_game(player_one.key, player_two.key, request.freak_factor) # Increment active count of games taskqueue.add(url='/tasks/increment_active_games') message = "Game created successfully" game_history = GameHistory(parent=game.key) game_history.history.append(game) game_history.messages.append(message) game_history.put() return game.to_form(message)
def _capture_game_snapshot(game, char): # game history is per game, so parent should be game and not the user # game history id history_id = GameHistory.allocate_ids(size=1, parent=game.key)[0] # game history key generated using history_id and game as its ancestor history_key = ndb.Key(GameHistory, history_id, parent=game.key) game_history = GameHistory(key=history_key, step_char=char, game_snapshot=game) # save game history game_history.put()
def raise_bid(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # Validity logic if game.game_over or game.cancelled: return game.to_form('Game already over!') bidding_player = Player.query( Player.game == game.key, Player.order == game.bid_player % game.players + 1).get() bidding_user = bidding_player.user.get() if bidding_user.password != request.password: return game.to_form('Invalid password.') if (game.bid_face == game.die_faces and game.bid_total == game.dice_total * game.players): return game.to_form('Already at max possible bid. Bid cannot be ' 'rasied. Call liar to end game.') if request.bid_face < 1 or request.bid_face > game.die_faces: return game.to_form('Invalid face number. Must be between 1 ' 'and ' + str(game.die_faces) + '.') if request.bid_face < game.bid_face: return game.to_form('Invalid dice face. Must be greater than or ' 'equal to the current dice face bid:%d.' % (game.bid_face)) if (request.bid_face == game.bid_face and request.bid_total <= game.bid_total): return game.to_form('Invalid bid. If not raising dice face, ' 'must raise dice total') # Game logic game.bid_face = request.bid_face game.bid_total = request.bid_total game.turn += 1 # Update player info if game.bid_player == game.players: game.bid_player = 1 else: game.bid_player += 1 game.put() # Record History game_history = GameHistory(game=game.key, turn=game.turn, player=bidding_player.key, bid_face=game.bid_face, bid_total=game.bid_total) game_history.put() # Find the next player next_player = Player.query( Player.game == game.key, Player.order == game.bid_player % game.players + 1).get() next_user = next_player.user.get() if next_user.email is not None: task_params = ({ 'email': next_user.email, 'user_name': next_user.user_name, 'game_key': game.key.urlsafe(), 'dice': Dice.query(Dice.player == next_player.key) .order(Dice.face), 'bid_face': game.bid_face, 'bid_total': game.bid_total, 'bid_player': bidding_user.user_name}) taskqueue.add( params=task_params, url='/tasks/send_your_turn' ) return game.to_form('Current bid is now face: %d, number %d. It is ' '%s\'s turn.' % (request.bid_face, request.bid_total, next_user.user_name))
def makeMove(self, request): """ Asigns specific move to a user for a specific game_id, as long as its available It also checks if the move is valid, returning the appropiate message for each situation. It also keeps track of every valid move made by a user """ x = request.x y = request.y game_id = request.game_id user_id = request.user_id game = Game.query(Game.game_id == game_id).get() queried_move = Move.query(Move.x == x, Move.y == y, Move.game_id == game_id).fetch(1) all_moves = Move.query(Game.game_id==game_id) if game == None : print("\n\nInvalid Move, Wrong Game ID\n\n") return StringMessage(message = "Invalid Move, Wrong Game ID" ) winner_id = GuessANumberApi._check_winning_condition(game_id) if winner_id != False and winner_id != "no_winners_yet": print("\n\n Game Won By {0} \n\n".format(winner_id)) game.finish_game(winner_id, "game_won") return StringMessage(message = "\n\n Game Won By {0} \n\n".format(winner_id), game_state="game_won", winner_id=winner_id) available_moves = Move.query(Move.available == True, Move.game_id == game_id).fetch() if len(available_moves) == 0: print("\n\n Game Ended, No more moves left {0} \n\n".format(game_id)) game.finish_game(winner_id, "no_more_moves") return StringMessage(message = "\n\n Game Ended, No more moves left {0} \n\n".format(game_id), game_state="no_more_moves", winner_id=winner_id or "") if user_id == None or user_id not in [game.player1, game.player2]: print("\n\nInvalid move parameters\n\n") return StringMessage(message = "Invalid Move, Wrong User ID" ) if len(queried_move) == 0: print("\n\nInvalid move parameters\n\n") return StringMessage(message = "Invalid move parameters, Wrong Game ID or Move out of range" ) if user_id == game.last_play_user_id: print("\n\n This Player already moved\n\n") return StringMessage(message = "Invalid move, This Player already moved" ) move = queried_move[0] if move.available != True: print("\n\nMove already done by: {0} \n\n".format(move.user_id)) return StringMessage(message = "Move {0} has already been made by User with ID: : {1}" .format(move.description, move.user_id) ) move.user_id = user_id move.available = False move.put() game.last_play_user_id = user_id game.put() GuessANumberApi._show_game_picture(game_id) state = GuessANumberApi._check_game_state(game_id) if state == False: state = "no_winners_yet" string_move = "[{0},{1}]".format(move.x, move.y) saved_move = GameHistory(game_id = game.game_id, user_id = user_id, move = string_move, game_state = state) saved_move.put() print(saved_move) # Final Winning Check winner_id = GuessANumberApi._check_winning_condition(game_id) if winner_id != False and winner_id != "no_winners_yet": print("\n\n Game Won By {0} \n\n".format(winner_id)) game.finish_game(winner_id, "game_won") return StringMessage(message = "\n\n Game Won By {0} \n\n".format(winner_id), game_state="game_won", winner_id=winner_id) return StringMessage(message = "New Move {0} assigned to {1} Game ID: {2}, x:{3} and y:{4}" .format(move.description, user_id, game_id, x, y), game_state="game_active", winner_id="" )