def forfeit(username): # Get user object user_obj = User.objects.filter(username=username) # User does not exist on the DB if len(user_obj) == 0: return Response(data="Username does not exist", status=status.HTTP_400_BAD_REQUEST) # Get user's Game object game_obj = Game.objects.filter(userA=username).order_by("-start_time") user_index = 0 # Try userB if len(game_obj) == 0: game_obj = Game.objects.filter(userB=username).order_by("-start_time") user_index = 1 # No games found for user if len(game_obj) == 0: return Response(data="User does not have an active game", status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Get other username if user_index == 0: other_username = game_obj[0].userB else: other_username = game_obj[0].userA # End the game try: end_game(game_obj[0], other_username, username) return Response(data="game ended", status=status.HTTP_200_OK) except Exception as e: return Response(data=str(e), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def scavage_games(): try: games = Game.objects.all() # Scavage over-time games for game in games: delete_game_bool = False if game.start_time is None: delete_game_bool = True else: max_time = (game.start_time + timedelta(0, game.duration_minutes * 60)).replace(tzinfo=None) now_time = datetime.now().replace(tzinfo=None) if max_time < now_time: delete_game_bool = True if delete_game_bool: if (game.userA is None) or (game.userA == '') or (game.userB == '') or (game.userB is None): game.delete() else: end_game(game, [], [game.userA, game.userB], True) return "Games running in over-time found and dealt with" return "OK" except Exception as e: return "Something went wrong. Exception: " + str(e) + str(e.message)
def __init__(self, data): board.data = data board.rows = data["rows"] board.cols = data["cols"] board.speed = data["speed"] board.multiplier = data["multiplier"] self.total_box_width = data["cols"] * data["multiplier"] self.total_box_height = self.total_height = data["rows"] * data[ "multiplier"] self.total_width = self.total_box_width + 350 board.img_loc = data["img_loc"] board.rotation_speed = data["rotation_speed"] animation.multiplier = board.multiplier #setup the same on animation side board.box_list = [] board.animations = set() # it is a set board.remove_cycle = set() self.running = False self.update_disp = False self.count = 0 self.check_end = False self.state = 1 self.end_setup = False self.animation_owners = [] self.init_window() board.make_image() self.make_boxes() self.players = [] i = 0 # for location fo ingame count for p in data["players"]: self.players.append(player.player(self, p, i)) i += 1 self.alive_players = self.players self.current = self.players[0] self.end_game = end_game(self)
def get(self, request, *args, **kw): # Get the username from GET username = kw["username"] user_index = 0 # Get the game object. Try userA first. gameObj = Game.objects.filter(userA=username) if (len(gameObj) == 0): # Lets try userB gameObj = Game.objects.filter(userB=username) user_index = 1 # If none found -> game is over. return gameresult. if (len(gameObj) == 0): returnJson = { "status" : False, "all_completed" : False } # Redirect to gameresult to the last game of all return Response(returnJson, status=status.HTTP_200_OK) try: goal_hash = request.GET["hash"] except: return Response("Expecting querystring named hash.. /?hash=1234") found = False finished = False # We need to cross the users goals check. user_index = abs(user_index-1) for goal in gameObj[0].shassaros.all()[user_index].goals: if (goal_hash == goal): currShassaro = gameObj[0].shassaros.all()[user_index] if (currShassaro.goals_completed == None): currShassaro.goals_completed = {goal : True} found = True else: goalAdded = None for currIndex in currShassaro.goals_completed: if (goal != currIndex): goalAdded = goal found = True if (goalAdded != None): currShassaro.goals_completed[goal] = True currShassaro.save() if (len(currShassaro.goals_completed) == len(gameObj[0].shassaros.all()[user_index].goals)): other_username = None # And cross it back to original user_index = abs(user_index-1) if(user_index == 0): other_username = gameObj[0].userB else: other_username = gameObj[0].userA end_game(gameObj[0], username, other_username) finished = True if(found): # Notify change via websockets userA_publisher = RedisPublisher(facility="{0}-game".format(gameObj[0].userA), broadcast=True) userB_publisher = RedisPublisher(facility="{0}-game".format(gameObj[0].userB), broadcast=True) userA_publisher.publish_message(RedisMessage(json.dumps(GenerateActiveGame(gameObj[0].userA)))) userB_publisher.publish_message(RedisMessage(json.dumps(GenerateActiveGame(gameObj[0].userB)))) returnJson = { "status" : found, "all_completed" : finished } return Response(returnJson, status=status.HTTP_200_OK)
def play_game(player, board): win_game = False ui.display_dialog_window( "You are a Hero. Leading a wasteful life, you are running out of money. Fortunately, you have\nheard that in the nearby village of Codecools Gate, residents will generously pay for getting\nrid of the rat plague, so you decided to quickly hit the road to anticipate the competitors.\n\nRiding the village on your musk deer, you hear the screams of a villager.\n\nPress 'C' to continue." ) while True: key = key_pressed().lower() if key == "c": clear_screen() hero = list_stats(player['stats'], player) ui.display_board(board, hero) ui.display_dialog_window( "To interact with NPC('!', '?'), doors ('/'), chests('$') and monsters ('*', 'G', 'N', 'S') just go to their place.\n\nTo check your inventory press 'I'.\n\nTo quit game, press 'Q'.\n\nMove your character with keys: 'W', 'A', 'S', 'D'." ) # while True: key = key_pressed().lower() # board = engine.create_board(BOARD_WIDTH, BOARD_HEIGHT) last_position = [player['y'], player['x']] # hero_statistics = player["stats"] hero = list_stats(player['stats'], player) # Create a list of hero stats items = player['items'] if key == 'q': break elif key == 'i': clear_screen() while True: ui.display_items(items) key = key_pressed().lower() if key == 'q': break else: clear_screen() elif key == 'z': clear_screen() elif key.lower() in 'wsad': # TOTALY NEW changing_position = change_position( CONTROL_DICT[key], player, board) player['Map'] = changing_position[2]['Map'] map_elements.insert(0, changing_position[0]) win_game = changing_position[5] if changing_position[1] != False: board = changing_position[1] player['x'] = changing_position[2]['x'] player['y'] = changing_position[2]['y'] # player['stats'] = changing_position[2]['stats'] elif changing_position[3] != False: player = changing_position[2] ui.display_dialog_window( 'You recieve a new item! Open inventory to check what is it' ) # print(player) key_pressed() elif changing_position[4] != False: player["Kills"] += 1 player['stats'] = check_level(player) board = engine.put_player_on_board(board, player, map_elements, last_position) # TOTALY NEW clear_screen() if player['stats']['HP'] <= 0: end = end_game.end_game( 'die' ) # the 'end' depends from the life of Necromancer-rat key_pressed() points = int(player['Kills']) * 1000 save_highscore.save_result(player['name'], points, player['race'], player['class']) # clear_screen() break elif win_game != False: end_game.end_game('win') key_pressed() points = int(player['Kills']) * 1000 save_highscore.save_result(player['name'], points, player['race'], player['class']) break ui.display_board(board, hero) break
break ui.display_board(board, hero) break def main(name, race, hero_class): clear_screen() player = create_player(name, race, hero_class) # board = engine.create_board(BOARD_WIDTH, BOARD_HEIGHT) board = engine.read_map('map.txt') board = engine.put_player_on_board(board, player, map_elements) hero = list_stats(player['stats'], player) ui.display_board(board, hero) play_game(player, board) ui.about_authors() # end_game.end_game(end = None) restart_game() def restart_game(): print('Press to continue') key = key_pressed() if __name__ == '__main__': main() end_game.end_game('win')
def get(self, request, *args, **kw): # Get the username from GET username = kw["username"] user_index = 0 # Get the game object. Try userA first. gameObj = Game.objects.filter(userA=username) if (len(gameObj) == 0): # Lets try userB gameObj = Game.objects.filter(userB=username) user_index = 1 # If none found -> game is over. return gameresult. if (len(gameObj) == 0): returnJson = {"status": False, "all_completed": False} # Redirect to gameresult to the last game of all return Response(returnJson, status=status.HTTP_200_OK) try: goal_hash = request.GET["hash"] except: return Response("Expecting querystring named hash.. /?hash=1234") found = False finished = False # We need to cross the users goals check. user_index = abs(user_index - 1) for goal in gameObj[0].shassaros.all()[user_index].goals: if (goal_hash == goal): currShassaro = gameObj[0].shassaros.all()[user_index] if (currShassaro.goals_completed == None): currShassaro.goals_completed = {goal: True} found = True else: goalAdded = None for currIndex in currShassaro.goals_completed: if (goal != currIndex): goalAdded = goal found = True if (goalAdded != None): currShassaro.goals_completed[goal] = True currShassaro.save() if (len(currShassaro.goals_completed) == len( gameObj[0].shassaros.all()[user_index].goals)): other_username = None # And cross it back to original user_index = abs(user_index - 1) if (user_index == 0): other_username = gameObj[0].userB else: other_username = gameObj[0].userA end_game(gameObj[0], username, other_username) finished = True if (found): # Notify change via websockets userA_publisher = RedisPublisher(facility="{0}-game".format( gameObj[0].userA), broadcast=True) userB_publisher = RedisPublisher(facility="{0}-game".format( gameObj[0].userB), broadcast=True) userA_publisher.publish_message( RedisMessage(json.dumps(GenerateActiveGame(gameObj[0].userA)))) userB_publisher.publish_message( RedisMessage(json.dumps(GenerateActiveGame(gameObj[0].userB)))) returnJson = {"status": found, "all_completed": finished} return Response(returnJson, status=status.HTTP_200_OK)