def test_eat_gold(): game = Game(width=10, height=10) g = Engine.create_game_state(game.id, game.width, game.height) Engine.add_snakes_to_board(g, [SNAKE_1]) g.gold.append([3, 1]) g.sanity_check() with open('lib/game/data/gold_test_1.in') as f: start_state = f.read().strip() assert(g.to_string().strip() == start_state) g.snakes[0].move = Engine.MOVE_EAST g = Engine.resolve_moves(g) g.sanity_check() g.snakes[0].move = Engine.MOVE_EAST g = Engine.resolve_moves(g) g.sanity_check() with open('lib/game/data/gold_test_1.out') as f: end_state = f.read().strip() assert(g.to_string().strip() == end_state.strip()) assert(len(g.gold) == 0)
def test_snake(): is_admin = (g.team.type == Team.TYPE_ADMIN) snake_url = g.team.snake_url if is_admin and request.args.get('url'): snake_url = request.args.get('url') else: if 'admin' in request.url: abort(404) # Fake a game game = Game(id=1, width=10, height=10) snakes = [engine.Snake(g.team.id, snake_url)] game_state = engine.Engine.create_game_state(game.id, game.width, game.height) engine.Engine.add_random_snakes_to_board(game_state, snakes) results = { 'info': ai.whois(snakes)[0], 'start': ai.start(game, game_state)[0], 'move': ai.move(game, game_state)[0], 'end': ai.end(game, game_state)[0] } return render_template('test.html', is_admin=is_admin, snake_url=snake_url, results=results)
def rematch_game(game_id): game = Game.find_one({'_id': game_id}) game_state = GameState.find({'game_id': game.id}, limit=1)[0] teams = [ Team.find_one({'_id': snake.team_id}) for snake in game_state.snakes + game_state.dead_snakes ] return create_game(game.team_id, teams, game.width, game.height, game.turn_time, game.mode)[0]
def create_game(team_id, teams, width, height, turn_time, mode): if not teams or len(teams) == 0: raise Exception('No teams added. You need at least one...') # Create snakes and fetch whois for each snakes = [ Snake(team_id=team.id, url=team.snake_url, name=team.teamname) for team in teams ] _update_snakes(snakes, ai.whois(snakes)) # Create game team_ids = [team.id for team in teams] print team_ids game = Game(width=width, height=height, turn_time=turn_time, mode=mode, team_id=team_id, team_ids=team_ids) game.insert() # Create the first GameState game_state = Engine.create_game_state(game.id, game.width, game.height, game.mode) # Init the first GameState Engine.add_random_snakes_to_board(game_state, snakes) Engine.add_starting_food_to_board(game_state) # Notify snakes that we're about to start _update_snakes(game_state.snakes, ai.start(game, game_state)) # Save the first GameState game_state.insert() if len(snakes) > 1: _update_slack( game.id, '%d brave snakes enter the grid: %s' % (len(snakes), ', '.join([s.name for s in snakes]))) return game, game_state
def start_game(game_id, manual=None): game = Game.find_one({'_id': game_id}) if not game: raise Exception('Could not find game %s' % game_id) if manual: game.state = Game.STATE_MANUAL game.save() else: game.mark_ready() return game
def maybe_run_game(mode): # Get queue by mode. If invalid mode, select random queue queue = Game.queues.get(mode, random.choice(Game.queues.values())) game_id = queue.dequeue(timeout=10) if not game_id: logger.info('No game is ready') return game_to_run = Game.find_one({'_id': game_id}) if not game_to_run: logger.warning('Game not found: %s', game_id) return logger.info('Running game: %s', game_to_run.id) run_game(game_to_run) logger.info('Finished game: %s', game_to_run.id)
def test_move_back_on_self(): game = Game(width=10, height=10) g = Engine.create_game_state(game.id, game.width, game.height) Engine.add_snakes_to_board(g, [SNAKE_1]) g.sanity_check() with open('lib/game/data/move_right.in') as f: start_state = f.read().strip() assert(g.to_string().strip() == start_state) g.snakes[0].move = Engine.MOVE_WEST g = Engine.resolve_moves(g) g.sanity_check() with open('lib/game/data/empty.out') as f: end_state = f.read().strip() assert(g.to_string().strip() == end_state.strip())
def test_head_to_head_uneven(): game = Game(width=10, height=10) g = Engine.create_game_state(game.id, game.width, game.height) Engine.add_snakes_to_board(g, [SNAKE_1, SNAKE_5]) g.sanity_check() with open('lib/game/data/head_to_head_uneven.in') as f: start_state = f.read().strip() assert(g.to_string().strip() == start_state) g.snakes[0].move = Engine.MOVE_EAST g.snakes[1].move = Engine.MOVE_WEST g = Engine.resolve_moves(g) g.sanity_check() with open('lib/game/data/head_to_head_uneven.out') as f: end_state = f.read().strip() print g.to_string().strip() assert(g.to_string().strip() == end_state.strip())
# Run this after updating games_list.py or incrementing game versions to make sure that the appropriate game is in the database. from lib.games.games_list import list_of_game_classes from lib.models.game import Game from database import db code_names = iter(list_of_game_classes) db_games = Game.query.all() for code_name in code_names: game = Game(list_of_game_classes[code_name].NAME, list_of_game_classes[code_name].UUID, list_of_game_classes[code_name].REVISION_NUMBER, code_name) if any(game.equals(db_game) for db_game in db_games): continue db.session.add(game) db.session.commit()