Beispiel #1
0
        private void ProcessGameTick(object source, ElapsedEventArgs e)
        {
            Console.Out.WriteLine($"Attempting to update from a list containing {_queue.Size()} updates.");

            IEnumerable <IGameUpdate> gameUpdates  = _queue.PullBatch(_BATCHSIZE);
            IList <IGameCommand>      gameCommands = new List <IGameCommand>();

            foreach (var gameUpdate in gameUpdates)
            {
                gameCommands.Add(_decoder.Decode(gameUpdate));
            }

            _serverStateManager.Process();

            string gameState = "";

            foreach (var playerLoggedIn in _serverGameState.PlayersLoggedIn)
            {
                IGameCommand command = new GameCommand() // TODO move to an extension method or extend the service to do this
                {
                    Command = GameCommands.Login,
                    Data    = new Tuple <Guid, object>(playerLoggedIn.Key, playerLoggedIn.Value)
                };

                string encodedResult = _encoder.Encode(command);
                gameState += "_" + encodedResult;
            }

            foreach (var playerCoordinate in _serverGameState.PlayerCoordinates)
            {
                IGameCommand command = new GameCommand()
                {
                    Command = GameCommands.Position,
                    Data    = new Tuple <Guid, object>(playerCoordinate.Key, playerCoordinate.Value)
                };

                string encodedResult = _encoder.Encode(command);
                gameState += "_" + encodedResult;
            }

            _broadcaster.Broadcast(gameState);
        }
Beispiel #2
0
        private Thread CreateProcessorThread()
        {
            Thread processorThread = new Thread(() =>
            {
                while (true)
                {
                    string gameState = _queue.Pull();
                    if (gameState != null)
                    {
                        string[] gameCommands = gameState.Split("_");

                        foreach (var command in gameCommands)
                        {
                            if (command != null)
                            {
                                _commandDecoder.Decode(command); // TODO rename Decode to something more appropiate
                            }
                        }
                    }
                }
            });

            return(processorThread);
        }