コード例 #1
0
        public async Task StartAsync()
        {
            // who needs precision or quickness >_>
            var nextGameTick = DateTime.UtcNow;

            while (true)
            {
                var currentTime = DateTime.UtcNow;
                if (currentTime > nextGameTick)
                {
                    var diff = currentTime - nextGameTick;
                    nextGameTick = DateTime.UtcNow.AddMilliseconds(_gameConfiguration.GameTickMiliseconds) - diff;

                    ResolveActionsForAutonomousEntities();

                    await _actionResolver.ResolveAsync().ConfigureAwait(false);

                    var activeUsers = await _userRepository.GetActiveAsync().ConfigureAwait(false);

                    foreach (var activeUser in activeUsers)
                    {
                        var player   = _creatureRegistry.GetById(activeUser.Id) as Player;
                        var response = TempCreateStatusResponse(player);
                        await _socketServer.SendAsync(activeUser.Id, response).ConfigureAwait(false);

                        // temp until refactor
                        if (!player.IsAlive)
                        {
                            activeUser.GameState = GameState.PlayerDeath;
                            await _userRepository.AddOrUpdateAsync(activeUser).ConfigureAwait(false);

                            _creatureRegistry.Remove(player.Id);
                            var gameOverResponse = new ServerResponse(ResponseType.GameState, activeUser.GameState.ToString());
                            await _socketServer.SendAsync(activeUser.Id, gameOverResponse).ConfigureAwait(false);
                        }
                    }
                }
            }
        }