def resetGameState(self, request): # Remove move ownership for all users # Resets move availablities to True game_id = request.game_id # for the moment, resets every move for provided game_id moves = Move.query().fetch() moves_deleted = Move.query(Move.game_id == game_id).fetch() game = Game.query(Game.game_id == game_id).get() if game == None: return StringMessage(message = "No Game found for ID: {0} ".format(game_id)) print("game id is {0} {1}".format(game_id, moves[0].game_id )) # Deleting Game game.key.delete() # Deleting Moves for move in moves_deleted: print("Deleting moves, {0}".format(move)) move.key.delete() return StringMessage(message = "Game Reset Complete, deleted {0} moves for Game: {1} ".format(len(moves_deleted), game_id))
def _check_game_state(game_id): """ Checks whether there's a victory condition, losing condition, or no more available moves """ print("\n\nInside check game state, game_id: " + game_id) moves = Move.query(Move.game_id == game_id).fetch() available_moves = Move.query(Move.available == True, Move.game_id == game_id).fetch() if len(moves) == 0: print("\n\n game_id not found {0} \n\n".format(game_id)) return "game_id_not_found" winner_id = GuessANumberApi._check_winning_condition(game_id) if winner_id != False: print("\n\n############### Game won by:" + winner_id + " ###############\n\n") return winner_id if len(available_moves) == 0: print("\n\n Game Ended, No more moves left {0} \n\n".format(game_id)) return "no_more_moves" print("\n\nNo winners yet for game: {0} \n\n".format(game_id)) return "no_winners_yet"
def resetGameState(self, request): # Remove move ownership for all users # Resets move availablities to True game_id = request.game_id # for the moment, resets every move for provided game_id moves = Move.query().fetch() moves_deleted = Move.query(Move.game_id == game_id).fetch() game = Game.query(Game.game_id == game_id).get() if game == None: return StringMessage( message="No Game found for ID: {0} ".format(game_id)) print("game id is {0} {1}".format(game_id, moves[0].game_id)) # Deleting Game game.key.delete() # Deleting Moves for move in moves_deleted: print("Deleting moves, {0}".format(move)) move.key.delete() return StringMessage( message="Game Reset Complete, deleted {0} moves for Game: {1} ". format(len(moves_deleted), game_id))
def _check_game_state(game_id): """ Checks whether there's a victory condition, losing condition, or no more available moves """ print("\n\nInside check game state, game_id: " + game_id) moves = Move.query(Move.game_id == game_id).fetch() available_moves = Move.query(Move.available == True, Move.game_id == game_id).fetch() if len(moves) == 0: print("\n\n game_id not found {0} \n\n".format(game_id)) return "game_id_not_found" winner_id = GuessANumberApi._check_winning_condition(game_id) if winner_id != False: print("\n\n############### Game won by:" + winner_id + " ###############\n\n") return winner_id if len(available_moves) == 0: print( "\n\n Game Ended, No more moves left {0} \n\n".format(game_id)) return "no_more_moves" print("\n\nNo winners yet for game: {0} \n\n".format(game_id)) return "no_winners_yet"
def makeMove(self, request): """ Asigns specific move to a user for a specific game_id, as long as its available """ 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) 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: print("\n\n Game Won By {0} \n\n".format(winner_id)) return StringMessage(message = "\n\n Game Won By {0} \n\n".format(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)) return "no_more_moves" 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) GuessANumberApi._check_game_state(game_id) return StringMessage(message = "Move {0} assign to {1} for game_id: {2}, x:{3} and y:{4}".format(move.description, user_id, game_id, x, y) )
def _show_game_picture(game_id): """ Print visual representation of game state """ moves = Move.query(Move.game_id == game_id).order(Move.x, Move.y).fetch() if len(moves) == 0: print("\n\nCant print game state, Invalid game_id {0}\n\n".format( game_id)) return StringMessage( message="Invalid move parameters. no game found") player1, player2 = GuessANumberApi._get_players_in_game(game_id) print("Current Players for Game ID {0}: {1}, {2}".format( game_id, player1, player2)) m_00 = Move.query(Move.x == 0, Move.y == 0, Move.game_id == game_id).fetch(1)[0] m_01 = Move.query(Move.x == 0, Move.y == 1, Move.game_id == game_id).fetch(1)[0] m_02 = Move.query(Move.x == 0, Move.y == 2, Move.game_id == game_id).fetch(1)[0] m_10 = Move.query(Move.x == 1, Move.y == 0, Move.game_id == game_id).fetch(1)[0] m_11 = Move.query(Move.x == 1, Move.y == 1, Move.game_id == game_id).fetch(1)[0] m_12 = Move.query(Move.x == 1, Move.y == 2, Move.game_id == game_id).fetch(1)[0] m_20 = Move.query(Move.x == 2, Move.y == 0, Move.game_id == game_id).fetch(1)[0] m_21 = Move.query(Move.x == 2, Move.y == 1, Move.game_id == game_id).fetch(1)[0] m_22 = Move.query(Move.x == 2, Move.y == 2, Move.game_id == game_id).fetch(1)[0] m_00 = m_00.user_id or m_00.description m_01 = m_01.user_id or m_01.description m_02 = m_02.user_id or m_02.description m_10 = m_10.user_id or m_10.description m_11 = m_11.user_id or m_11.description m_12 = m_12.user_id or m_12.description m_20 = m_20.user_id or m_20.description m_21 = m_21.user_id or m_21.description m_22 = m_22.user_id or m_22.description print("\n\n\n") print("TIC TAC TOE GAME") print("\n") print(" {0} | {1} | {2} ".format(m_00, m_01, m_02)) print("-----------------------------") print(" {0} | {1} | {2} ".format(m_10, m_11, m_12)) print("-----------------------------") print(" {0} | {1} | {2} ".format(m_20, m_21, m_22)) print("\n\n\n")
def _show_game_picture(game_id): """ Print visual representation of game state """ moves = Move.query(Move.game_id == game_id).order(Move.x, Move.y).fetch() if len(moves) == 0: print("\n\nCant print game state, Invalid game_id {0}\n\n".format(game_id)) return StringMessage(message = "Invalid move parameters. no game found" ) player1,player2 = GuessANumberApi._get_players_in_game(game_id) print("Current Players for Game ID {0}: {1}, {2}".format(game_id, player1, player2) ) m_00 = Move.query(Move.x == 0, Move.y == 0, Move.game_id == game_id).fetch(1)[0] m_01 = Move.query(Move.x == 0, Move.y == 1, Move.game_id == game_id).fetch(1)[0] m_02 = Move.query(Move.x == 0, Move.y == 2, Move.game_id == game_id).fetch(1)[0] m_10 = Move.query(Move.x == 1, Move.y == 0, Move.game_id == game_id).fetch(1)[0] m_11 = Move.query(Move.x == 1, Move.y == 1, Move.game_id == game_id).fetch(1)[0] m_12 = Move.query(Move.x == 1, Move.y == 2, Move.game_id == game_id).fetch(1)[0] m_20 = Move.query(Move.x == 2, Move.y == 0, Move.game_id == game_id).fetch(1)[0] m_21 = Move.query(Move.x == 2, Move.y == 1, Move.game_id == game_id).fetch(1)[0] m_22 = Move.query(Move.x == 2, Move.y == 2, Move.game_id == game_id).fetch(1)[0] m_00 = m_00.user_id or m_00.description m_01 = m_01.user_id or m_01.description m_02 = m_02.user_id or m_02.description m_10 = m_10.user_id or m_10.description m_11 = m_11.user_id or m_11.description m_12 = m_12.user_id or m_12.description m_20 = m_20.user_id or m_20.description m_21 = m_21.user_id or m_21.description m_22 = m_22.user_id or m_22.description print("\n\n\n") print("TIC TAC TOE GAME") print("\n") print(" {0} | {1} | {2} ".format(m_00, m_01, m_02)) print("-----------------------------") print(" {0} | {1} | {2} ".format(m_10, m_11, m_12)) print("-----------------------------") print(" {0} | {1} | {2} ".format(m_20, m_21, m_22)) print("\n\n\n")
def get_game_history(self, request): ''' returns the usual GameForm, plus all related positions and all moves ''' game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: return endpoints.NotFoundException("Game not found") game_key = game.key game = game.to_form("") ships = Ship.query(ancestor=game_key).order(-Ship.created).fetch() ship_forms = [] for s in ships: position_forms = [] positions = Position.query(ancestor=s.key).fetch() for p in positions: position_forms.append(XYMessage(x=p.x, y=p.y, hit=p.hit)) ship_forms.append( ShipMessage(player=s.player.get().name, ship=s.ship, created_date=s.created, positions=position_forms)) moves = Move.query(ancestor=game_key).order(-Move.created).fetch() move_forms = [] for m in moves: move_forms.append( MoveMessage(player=m.player.get().name, x=m.x, y=m.y, created_date=m.created)) form = FullGameInfo(game=game, ships=ship_forms, moves=move_forms) return form
def historyForGame(self, game): """return list of moves from this game""" list = Move.query(Move.gameKey == game.key).fetch() for move in list: move.game = game move.user = self.getPlayer(game, move) return list
def _get_players_in_game(game_id): moves = Move.query(Move.game_id == game_id).fetch() if len(moves) == 0: return StringMessage( message="Invalid move parameters. no game found") print("Getting players in game...") user_ids = [] for move in moves: user_id = move.user_id # print("checking for ID: {0}".format( user_id) ) if user_id not in user_ids and user_id != None: # print("ID: {0} was inserted".format( user_id) ) user_ids.append(user_id) print(user_ids) if len(user_ids) == 2: player1 = user_ids[0] player2 = user_ids[1] elif len(user_ids) == 1: player1 = user_ids[0] player2 = None else: player1 = None player2 = None print(player2, player1) return [player1, player2]
def _prepReminder(): games = Game.query(Game.gameCurrentMove < 9 and Game.gameCurrentMove > 0).fetch() remindees = '' if games: # If there are games ready for sign up, # format announcement and set it in memcache for each game for game in games: last_move = Move.query( Move.moveNumber == game.gameCurrentMove - 1).get() if last_move: current_time = datetime.utcnow() if last_move.moveTime and ( current_time - last_move.moveTime > timedelta(days=5)): next_player = Player.query( Player.displayName == game.nextPlayer).get() if next_player.mainEmail: print 'next_player.mainEmail', next_player.mainEmail remindees.join(next_player.displayName) mail.send_mail( 'noreply@{}.appspotmail.com'.format( app_identity.get_application_id()), next_player.mainEmail, 'Coming back to tic-tac-toe?', 'It has been 5 days since the last move on:\r\n\r\n{}' .format(game.name)) return remindees
def _get_players_in_game(game_id): moves = Move.query(Move.game_id == game_id).fetch() if len(moves) == 0: return StringMessage(message = "Invalid move parameters. no game found" ) print("Getting players in game...") user_ids = [] for move in moves: user_id = move.user_id # print("checking for ID: {0}".format( user_id) ) if user_id not in user_ids and user_id != None: # print("ID: {0} was inserted".format( user_id) ) user_ids.append(user_id) print(user_ids) if len(user_ids) == 2: player1 = user_ids[0] player2 = user_ids[1] elif len(user_ids) == 1: player1 = user_ids[0] player2 = None else: player1 = None player2 = None print(player2, player1) return [player1, player2]
def _prepReminder(): games = Game.query( Game.gameCurrentMove < 9 and Game.gameCurrentMove > 0).fetch() remindees = '' if games: # If there are games ready for sign up, # format announcement and set it in memcache for each game for game in games: last_move = Move.query( Move.moveNumber==game.gameCurrentMove-1).get() if last_move: current_time = datetime.utcnow() if last_move.moveTime and (current_time - last_move.moveTime > timedelta(days=5)): next_player = Player.query(Player.displayName == game.nextPlayer).get() if next_player.mainEmail: print 'next_player.mainEmail', next_player.mainEmail remindees.join(next_player.displayName) mail.send_mail( 'noreply@{}.appspotmail.com' .format( app_identity.get_application_id()), next_player.mainEmail, 'Coming back to tic-tac-toe?', 'It has been 5 days since the last move on:\r\n\r\n{}' .format( game.name) ) return remindees
def getGameHistory(self, request): """ shows a list of all the moves for each game""" game_key = ndb.Key(urlsafe=request.websafeGameKey) moves = Move.query(ancestor=game_key).fetch() if not moves: raise endpoints.NotFoundException('No moves found') else: return MovesForm(items=[move._copyMoveToForm for move in moves])
def get_moves(self, request): """Gets all moves for a particular game""" try: game_key = ndb.Key(urlsafe=request.urlsafe_game_key) except: raise endpoints.NotFoundException('Bad Key, Game not found!') moves = Move.query(ancestor=game_key).order(Move.number) return MoveForms(items=[move.to_form() for move in moves])
def get_game_history(self, request): """get moves history for each game requested""" url_safe_key = request.urlsafe_game_key game = get_by_urlsafe(url_safe_key, Game) moves = Move.query(Move.game == game.key) moves_list = [] for move in moves: moves_list.append(move.to_form()) return MoveForms(items=moves_list)
def _get_players_in_game(game_id): """ returns player ids from a game """ moves = Move.query(Move.game_id == game_id).fetch() if len(moves) == 0: return StringMessage(message = "Invalid move parameters. no game found" ) print("Getting players in game...") user_ids = [] game = Game.query(Game.game_id == game_id).get() user_ids.append(game.player1) user_ids.append(game.player2) return user_ids
def cancel_game(self, request): """ Delete an unfinished game """ game_id = request.game_id game = Game.query(Game.game_id == game_id).get() if game == None: return StringMessage(message = "No Game found for ID: {0} ".format(game_id)) if game.finished == True: return StringMessage(message = "Can't delete a game thats already finished") # for the moment, resets every move for provided game_id moves = Move.query().fetch() moves_deleted = Move.query(Move.game_id == game_id).fetch() print("game id is {0} {1}".format(game_id, moves[0].game_id )) # Deleting Game game.key.delete() # Deleting Moves for move in moves_deleted: move.key.delete() return StringMessage(message = "Game Reset Complete, deleted {0} moves for Game: {1} ".format(len(moves_deleted), game_id))
def startGame(self, request): """ Initializes all 9 posible moves using provided game_id """ game_id = request.game_id player1 = request.player1 player2 = request.player2 if request.game_id == None: return StringMessage(message = "Failed, Empty game_id. Please enter a valid unique game_id") if request.player1 == None or request.player2 == None: return StringMessage(message = "Failed, Missing Players. Make sure both player ids are present") if request.player1 == request.player2: return StringMessage(message = "Failed, Player Ids must be different") game_exists = len(Move.query(Move.game_id == game_id).fetch()) > 0 if game_exists: return StringMessage(message = "Game Creation Failed, Game ID already exists: {0}".format( game_id ) ) # Creating Game game = Game(game_id = game_id, player1 = request.player1, player2 = request.player2) game.put() print("New Game Created: {0}".format(game)) mv1 = Move(x = 0, y = 0, game_id = game_id, available = True, description = "[0,0]") mv2 = Move(x = 0, y = 1, game_id = game_id, available = True, description = "[0,1]") mv3 = Move(x = 0, y = 2, game_id = game_id, available = True, description = "[0,2]") mv4 = Move(x = 1, y = 0, game_id = game_id, available = True, description = "[1,0]") mv5 = Move(x = 1, y = 1, game_id = game_id, available = True, description = "[1,1]") mv6 = Move(x = 1, y = 2, game_id = game_id, available = True, description = "[1,2]") mv7 = Move(x = 2, y = 0, game_id = game_id, available = True, description = "[2,0]") mv8 = Move(x = 2, y = 1, game_id = game_id, available = True, description = "[2,1]") mv9 = Move(x = 2, y = 2, game_id = game_id, available = True, description = "[2,2]") # ndb.put_multi([ mv1, mv2, mv3, mv4, mv5, mv6, mv7, mv8, mv9]) for m in [ mv1, mv2, mv3, mv4, mv5, mv6, mv7, mv8, mv9]: print(" saving: {0}".format(m) ) m.put() return StringMessage(message = "New Game Created, ID: {0} | Player 1: {1} | Player 2: {2}".format( game_id, player1, player2 ) )
def show_game_ids(self, request): moves = Move.query().fetch() game_ids = [] for move in moves: game_id = move.game_id if game_id not in game_ids: game_ids.append(game_id) #Showing game moves per game GuessANumberApi._show_game_picture(game_id) GuessANumberApi._check_game_state(game_id) print( "\n\n Total Game IDS: {0}, IDS: {1} \n\n".format( len(game_ids), str(game_ids) ) ) return StringMessage(message= "Total Moves: {0}, Total Game IDS: {1}, IDS: {2}".format( len(moves), len(game_ids), str(game_ids) ) )
def get_game_history(self, request): """Return a game's move history. Args: urlsafe_game_key (str): The game URL. Returns: moves (Move[]): An array of Move instances containing the game's move history. Raises: NotFoundException: If game does not exist. """ game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found!') moves = Move.query(Move.game == game.key).order(Move.created_at) return HistoryForms(items=[move.to_form() for move in moves])
def show_game_ids(self, request): moves = Move.query().fetch() game_ids = [] for move in moves: game_id = move.game_id if game_id not in game_ids: game_ids.append(game_id) #Showing game moves per game GuessANumberApi._show_game_picture(game_id) GuessANumberApi._check_game_state(game_id) print("\n\n Total Game IDS: {0}, IDS: {1} \n\n".format( len(game_ids), str(game_ids))) return StringMessage( message="Total Moves: {0}, Total Game IDS: {1}, IDS: {2}".format( len(moves), len(game_ids), str(game_ids)))
def makeMove(self, request): """ Asigns specific move to a user for a specific game_id, as long as its available """ 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) 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: print("\n\n Game Won By {0} \n\n".format(winner_id)) return StringMessage( message="\n\n Game Won By {0} \n\n".format(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)) return "no_more_moves" 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) GuessANumberApi._check_game_state(game_id) return StringMessage( message="Move {0} assign to {1} for game_id: {2}, x:{3} and y:{4}". format(move.description, user_id, game_id, x, y))
def _check_winning_condition(game_id): """ Checks whether there's a victory condition and returns winner user_id if there is, else false""" # Find game with only 2 allowed users, to be done... moves = Move.query(Move.game_id == game_id).fetch() user_ids = GuessANumberApi._get_players_in_game(game_id) if len(moves) == 0: print("\n\n game_id not found {0} \n\n".format(game_id)) return False return "game_id not found" if None in user_ids: print("\n\n not all users have played a move: {0} \n\n".format(user_ids)) return False return "not all users have played a move" print("\n\nChecking winning condition for game id: " + game_id) user_1 = user_ids[0] user_2 = user_ids[1] for i in range(0,3): # Checking for Horizontal Wins horizontal_moves = Move.query(Move.game_id == game_id, Move.x == i) horizontal_moves = [h.user_id for h in horizontal_moves] unique_owner = list( set(horizontal_moves) ) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\nHorizontal Winning condition met, User: {0} Won! Row: {1} \n\n".format(winner_id, i)) return winner_id # Checking for Vertical Wins vertical_moves = Move.query(Move.game_id == game_id, Move.y == i) vertical_moves = [h.user_id for h in vertical_moves] unique_owner = list( set(vertical_moves) ) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\n Vertical Winning condition met, User: {0} Won!, Column: {1} \n\n".format(winner_id, i)) return winner_id # Checking Cross Wins diagonal_moves = [] for i in range(0,3): m = Move.query(Move.x == i, Move.y == i).fetch()[0] unique_owner = list(set(diagonal_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\n Diagonal Winning condition met, User: {0} Won!, Column: {1} \n\n".format(winner_id, i)) return winner_id # Checking Cross Wins diagonal_moves = [] for i in range(0,3): m = Move.query(Move.x == i, Move.y == 2-i).fetch()[0] diagonal_moves.append(m.user_id) diagonal_moves.append(m.user_id) unique_owner = list(set(diagonal_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\n Diagonal Winning condition met, User: {0} Won!, Column: {1} \n\n".format(winner_id, i)) return winner_id print("\n\n No winning conditions met \n\n") return False
def show_game_history(self, request): """Shows the history of a particular game""" game = utils.get_by_urlsafe(request.urlsafe_game_key, Game) moves = Move.query(Move.game == game.key).order(Move.move_index) return MoveForms(items=[move.to_form() for move in moves])
def _check_winning_condition(game_id): """ Checks whether there's a victory condition and returns winner user_id if there is, else false""" moves = Move.query(Move.game_id == game_id).fetch() game = Game.query(Game.game_id == game_id).get() user_ids = GuessANumberApi._get_players_in_game(game_id) if len(moves) == 0 or game == None: print("\n\n game_id not found {0} \n\n".format(game_id)) return False user_1 = game.player1 user_2 = game.player2 if user_1 == None or user_2 == None: print("\n\n not all users have played a move, Player1: {0}, Player2: {1} \n\n".format(user_1,user_2)) return False print("\n\nChecking winning condition for game id: " + game_id) for i in range(0,3): # Checking for Horizontal Wins horizontal_moves = Move.query(Move.game_id == game_id, Move.x == i) horizontal_moves = [h.user_id for h in horizontal_moves] unique_owner = list( set(horizontal_moves) ) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\nHorizontal Winning condition met, User: {0} Won! Row: {1} \n\n".format(winner_id, i)) return winner_id # Checking for Vertical Wins vertical_moves = Move.query(Move.game_id == game_id, Move.y == i) vertical_moves = [h.user_id for h in vertical_moves] unique_owner = list( set(vertical_moves) ) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\n Vertical Winning condition met, User: {0} Won!, Column: {1} \n\n".format(winner_id, i)) return winner_id # Checking Cross Wins diagonal_moves = [] for i in range(0,3): m = Move.query(Move.game_id == game_id, Move.x == i, Move.y == i).fetch()[0] diagonal_moves.append(m.user_id) unique_owner = list(set(diagonal_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print("\n\n Diagonal Winning condition met, User: {0} Won!, Column: {1} \n\n".format(winner_id, i)) return winner_id # Checking Cross Wins diagonal_moves = [] checking_moves = [] for i in range(0,3): m = Move.query(Move.game_id == game_id, Move.x == i, Move.y == 2-i).fetch()[0] for i in range(0,3): m = Move.query(Move.game_id == game_id, Move.x == i, Move.y == 2-i).fetch()[0] diagonal_moves.append(m.user_id) checking_moves.append(m) unique_owner = list(set(diagonal_moves)) print("uniq owner {0}".format( unique_owner)) print("owner length " + str(len(unique_owner))) if len(unique_owner) == 1 : print("uniq owner is" + str(unique_owner)) print("owner length " + str(len(unique_owner))) if len(unique_owner) == 1 and unique_owner[0] != None: winner_id = unique_owner[0] print("\n\n Diagonal Winning condition met, User: {0} Won!, Column: {1} \n\n".format(winner_id, i)) return winner_id print("\n\n No winning conditions met \n\n") return False
def startGame(self, request): """ Initializes all 9 posible moves using provided game_id """ game_id = request.game_id player1 = request.player1 player2 = request.player2 if request.game_id == None: return StringMessage( message= "Failed, Empty game_id. Please enter a valid unique game_id") if request.player1 == None or request.player2 == None: return StringMessage( message= "Failed, Missing Players. Make sure both player ids are present" ) if request.player1 == request.player2: return StringMessage( message="Failed, Player Ids must be different") game_exists = len(Move.query(Move.game_id == game_id).fetch()) > 0 if game_exists: return StringMessage( message="Game Creation Failed, Game ID already exists: {0}". format(game_id)) # Creating Game game = Game(game_id=game_id, player1=request.player1, player2=request.player2) game.put() print("New Game Created: {0}".format(game)) mv1 = Move(x=0, y=0, game_id=game_id, available=True, description="[0,0]") mv2 = Move(x=0, y=1, game_id=game_id, available=True, description="[0,1]") mv3 = Move(x=0, y=2, game_id=game_id, available=True, description="[0,2]") mv4 = Move(x=1, y=0, game_id=game_id, available=True, description="[1,0]") mv5 = Move(x=1, y=1, game_id=game_id, available=True, description="[1,1]") mv6 = Move(x=1, y=2, game_id=game_id, available=True, description="[1,2]") mv7 = Move(x=2, y=0, game_id=game_id, available=True, description="[2,0]") mv8 = Move(x=2, y=1, game_id=game_id, available=True, description="[2,1]") mv9 = Move(x=2, y=2, game_id=game_id, available=True, description="[2,2]") # ndb.put_multi([ mv1, mv2, mv3, mv4, mv5, mv6, mv7, mv8, mv9]) for m in [mv1, mv2, mv3, mv4, mv5, mv6, mv7, mv8, mv9]: print(" saving: {0}".format(m)) m.put() return StringMessage( message="New Game Created, ID: {0} | Player 1: {1} | Player 2: {2}" .format(game_id, player1, player2))
def _check_winning_condition(game_id): """ Checks whether there's a victory condition and returns winner user_id if there is, else false""" # Find game with only 2 allowed users, to be done... moves = Move.query(Move.game_id == game_id).fetch() user_ids = GuessANumberApi._get_players_in_game(game_id) if len(moves) == 0: print("\n\n game_id not found {0} \n\n".format(game_id)) return False return "game_id not found" if None in user_ids: print("\n\n not all users have played a move: {0} \n\n".format( user_ids)) return False return "not all users have played a move" print("\n\nChecking winning condition for game id: " + game_id) user_1 = user_ids[0] user_2 = user_ids[1] for i in range(0, 3): # Checking for Horizontal Wins horizontal_moves = Move.query(Move.game_id == game_id, Move.x == i) horizontal_moves = [h.user_id for h in horizontal_moves] unique_owner = list(set(horizontal_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print( "\n\nHorizontal Winning condition met, User: {0} Won! Row: {1} \n\n" .format(winner_id, i)) return winner_id # Checking for Vertical Wins vertical_moves = Move.query(Move.game_id == game_id, Move.y == i) vertical_moves = [h.user_id for h in vertical_moves] unique_owner = list(set(vertical_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print( "\n\n Vertical Winning condition met, User: {0} Won!, Column: {1} \n\n" .format(winner_id, i)) return winner_id # Checking Cross Wins diagonal_moves = [] for i in range(0, 3): m = Move.query(Move.x == i, Move.y == i).fetch()[0] unique_owner = list(set(diagonal_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print( "\n\n Diagonal Winning condition met, User: {0} Won!, Column: {1} \n\n" .format(winner_id, i)) return winner_id # Checking Cross Wins diagonal_moves = [] for i in range(0, 3): m = Move.query(Move.x == i, Move.y == 2 - i).fetch()[0] diagonal_moves.append(m.user_id) diagonal_moves.append(m.user_id) unique_owner = list(set(diagonal_moves)) if None not in unique_owner and len(unique_owner) == 1: winner_id = unique_owner[0] print( "\n\n Diagonal Winning condition met, User: {0} Won!, Column: {1} \n\n" .format(winner_id, i)) return winner_id print("\n\n No winning conditions met \n\n") return False
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="" )