예제 #1
0
def start_game():
    """Starts game

    Sets `game_running` in DB
    """
    logger.info('Starting game')

    with storage.get_redis_storage().pipeline(transaction=True) as pipeline:
        with locking.acquire_redis_lock(pipeline, 'game_starting_lock'):
            already_started = storage.game.get_game_running()
            if already_started:
                logger.info('Game already started')
                return
            storage.game.set_game_running(1)

    storage.caching.cache_teamtasks(round=0)

    game_state = storage.game.get_game_state(round=0)
    if not game_state:
        logger.warning('Initial game_state missing')
    else:
        with storage.get_redis_storage().pipeline(
                transaction=True) as pipeline:
            logger.info(f"Initializing game_state with {game_state.to_dict()}")
            pipeline.set('game_state', game_state.to_json())
            pipeline.publish('scoreboard', game_state.to_json())
            pipeline.execute()
예제 #2
0
파일: auxiliary.py 프로젝트: wqsemc/ForcAD
def startup(**_kwargs):
    """Task to run on start of celery, schedules game start"""
    game_config = storage.game.get_current_global_config()

    logger.info(f'Received game config: {game_config}')

    with storage.get_redis_storage().pipeline(transaction=True) as pipeline:
        with locking.acquire_redis_lock(pipeline, 'game_starting_lock'):
            already_started = storage.game.get_game_running()

            if not already_started:
                logger.info("Game is not running, initializing...")
                start_game.apply_async(eta=game_config.start_time, )

                game_state = storage.game.construct_game_state_from_db(round=0)
                if not game_state:
                    logger.warning('Initial game_state missing')
                else:
                    logger.info(
                        f"Initializing game_state with {game_state.to_dict()}")
                    pipeline.set('game_state', game_state.to_json())
                    pipeline.execute()

                    storage.get_wro_sio_manager().emit(
                        event='update_scoreboard',
                        data={'data': game_state.to_json()},
                        namespace='/game_events',
                    )
예제 #3
0
파일: auxiliary.py 프로젝트: wqsemc/ForcAD
def start_game():
    """Starts game

    Sets `game_running` in DB
    """
    logger.info('Starting game')

    with storage.get_redis_storage().pipeline(transaction=True) as pipeline:
        with locking.acquire_redis_lock(pipeline, 'game_starting_lock'):
            already_started = storage.game.get_game_running()
            if already_started:
                logger.info('Game already started')
                return

            storage.game.set_round_start(round=0)
            storage.game.set_game_running(True)

        game_state = storage.game.construct_game_state_from_db(round=0)
        if not game_state:
            logger.warning('Initial game_state missing')
        else:
            logger.info(f"Initializing game_state with {game_state.to_dict()}")
            pipeline.set('game_state', game_state.to_json())
            pipeline.execute()

            storage.get_wro_sio_manager().emit(
                event='update_scoreboard',
                data={'data': game_state.to_json()},
                namespace='/game_events',
            )
예제 #4
0
    def run(self, *args, **kwargs):
        """Process new round
            Updates current round variable, then processes all teams.
            This function also caches previous state and notifies frontend of a new round (for classic game mode).
            Only one instance of process_round that updates round is to be be run!
        """

        game_running = storage.game.get_game_running()
        if not game_running:
            logger.info('Game is not running, exiting')
            return

        with storage.get_redis_storage().pipeline(
                transaction=True) as pipeline:
            with locking.acquire_redis_lock(pipeline, 'round_update:lock'):
                current_round = storage.game.get_real_round_from_db()
                round_to_check = current_round

                if self.should_update_round():
                    self.update_round(current_round)
                    round_to_check = current_round + 1
                    self.update_attack_data(round_to_check)

        if not round_to_check:
            logger.info("Not processing, round is 0")
            return

        if self.should_update_game_state():
            self.update_game_state(current_round)

        teams = storage.teams.get_teams()
        random.shuffle(teams)
        tasks = storage.tasks.get_tasks()
        random.shuffle(tasks)

        args = itertools.product(teams, tasks, [round_to_check])

        if self.round_type == 'full':
            logger.info("Running full round")
            celery_tasks.modes.run_full_round.starmap(args).apply_async()
        elif self.round_type == 'check_gets':
            logger.info("Running check_gets round")
            celery_tasks.modes.run_check_gets_round.starmap(args).apply_async()
        elif self.round_type == 'puts':
            logger.info("Running puts round")
            celery_tasks.modes.run_puts_round.starmap(args).apply_async()
        else:
            logger.critical(
                f"Invalid round type supplied: {self.round_type}, falling back to full"
            )
            celery_tasks.modes.run_full_round.starmap(args).apply_async()