Example #1
0
        public Server(IPlayerRepository playerRepository, BattleRepository battleRepository)
        {
            _playerRepository = playerRepository;
            _battleRepository = battleRepository;

            _listener = new TcpListener(IPAddress.Parse("0.0.0.0"), Port);
            _listener.Start();
            _listener.BeginAcceptTcpClient(HandleConnection, _listener);

            _dispatcher = new MessageDispatcher(new InMemoryMessageHandlerFactory(
                                                    _connectedClients, playerRepository, battleRepository));

            _logger.Log($"Listening on {Port}...");
        }
Example #2
0
        static void Main()
        {
            Console.WriteLine("=== TypeRealm server ===");
            //var playerRepository = new PlayerRepository();
            var playerRepository = new CachedPlayerRepository(
                new DiskPlayerRepository("players.data"));
            var battleRepository = new BattleRepository();

            var persistence = Task.Run(() =>
            {
                // TODO: NOT THREAD SAFE. Maybe make repositories thread-safe.
                while (!_isExiting)
                {
                    Thread.Sleep(_saveInterval);
                    playerRepository.PersistAllPending();
                    Console.WriteLine("Persisted all pending players.");
                }

                Thread.Sleep(_saveInterval);
                playerRepository.PersistAllPending();
            });

            using (var server = new Server(playerRepository, battleRepository))
            {
                while (Console.ReadLine() != "exit")
                {
                    Console.WriteLine("Type 'exit' to gracefully exit the server.");
                }

                // Stops saving players after saving all of them one last time.
                _isExiting = true;

                while (!persistence.IsCompleted && !persistence.IsCanceled && !persistence.IsFaulted)
                {
                    Console.WriteLine("Saving data to disk...");
                    Console.ReadLine();
                }

                // TODO: Gracefully exit the server, giving time for all players to quit.
            }
        }
 public StartBattleCommandHandler(IEnumerable <ConnectedClient> activeClients, IPlayerRepository playerRepository, BattleRepository battleRepository)
 {
     _activeClients    = activeClients;
     _playerRepository = playerRepository;
     _battleRepository = battleRepository;
 }
 public StopBattleCommandHandler(BattleRepository battleRepository, IPlayerRepository playerRepository)
 {
     _battleRepository = battleRepository;
     _playerRepository = playerRepository;
 }
Example #5
0
 public InMemoryMessageHandlerFactory(IEnumerable <ConnectedClient> clients, IPlayerRepository playerRepository, BattleRepository battleRepository)
 {
     _handlers.Add(typeof(AttackCommand), new AttackCommandHandler(battleRepository));
     _handlers.Add(typeof(StartBattleCommand), new StartBattleCommandHandler(clients, playerRepository, battleRepository));
     _handlers.Add(typeof(StartMovingToZoneCommand), new StartMovingToZoneCommandHandler(playerRepository));
     _handlers.Add(typeof(TurnAroundCommand), new TurnAroundCommandHandler(playerRepository));
     _handlers.Add(typeof(MoveForCommand), new MoveForCommandHandler(playerRepository));
     _handlers.Add(typeof(FinishWalkingCommand), new FinishWalkingCommandHandler(playerRepository));
     _handlers.Add(typeof(StopBattleCommand), new StopBattleCommandHandler(battleRepository, playerRepository));
 }
 public AttackCommandHandler(BattleRepository battleRepository)
 {
     _battleRepository = battleRepository;
 }