Beispiel #1
0
        public ServerGame(Server server, ServerClient owner, bool isPrivate, string privateKey, GameConfiguration gameConfiguration, Map map)
            : base(gameConfiguration, map)
        {
            Server = server;

            Owner = owner;

            IsPrivate = isPrivate;
            PrivateKey = privateKey;

            UnitIdCounter = 0;
        }
Beispiel #2
0
 public void Run()
 {
     while (!ShuttingDown)
     {
         Socket socket = Listener.AcceptSocket();
         lock (this)
         {
             Stream        clientStream;
             NetworkStream networkStream = new NetworkStream(socket);
             if (UseTLS)
             {
                 SslStream secureStream = new SslStream(networkStream, false, AcceptAnyCertificate, null);
                 secureStream.AuthenticateAsServer(Certificate, false, SslProtocols.Tls12, false);
                 clientStream = secureStream;
             }
             else
             {
                 clientStream = networkStream;
             }
             ServerClient client = new ServerClient(clientStream, this);
             Clients.Add(client);
         }
     }
 }
Beispiel #3
0
 public void OnGameStart(ServerGame game, ServerClient opponent)
 {
     _Opponent = opponent;
     GameStart start = new GameStart(game.GameConfiguration, PlayerState.GetBaseArmy(), Opponent.PlayerState.GetBaseArmy(), Opponent.Name, PlayerState.ReinforcementPoints);
     QueueMessage(new ServerToClientMessage(start));
 }
Beispiel #4
0
 public void OnOpponentFound(ServerClient opponent)
 {
     _Opponent = opponent;
     // Set the player states in the base game object so they can be used for turn state flipping
     Players[0] = Owner.PlayerState;
     Players[1] = Opponent.PlayerState;
     Owner.OnGameStart(this, _Opponent);
     _Opponent.OnGameStart(this, Owner);
     // This is used to connect the player states of both players
     // This isn't done in OnGameStart because the _Opponent fields aren't set at that point yet
     Owner.OnPostGameStart();
     _Opponent.OnPostGameStart();
     // There is a time limit on how long the deployment may take
     // If it expires without a player deploying any units they will likely quickly lose control of the map and can only deploy units later
     StartDeploymentTimer();
 }
Beispiel #5
0
        public void OnClientTermination(ServerClient client)
        {
            Clients.Remove(client);
            switch (client.State)
            {
                case ClientState.WaitingForOpponent:
                    CancelGame(client);
                    break;

                case ClientState.InGame:
                    OnGameEnd(client.Game, GameOutcomeType.Desertion, client.Opponent);
                    break;

                default:
                    // Not an issue
                    break;
            }
        }
Beispiel #6
0
 public void OnCancelGameRequest(ServerClient client)
 {
     CancelGame(client);
 }
Beispiel #7
0
 void CancelGame(ServerClient client)
 {
     ServerGame game = client.Game;
     if (game.IsPrivate)
         PrivateGames.Remove(game.Owner.Name);
     else
         PublicGames.Remove(game.PrivateKey);
 }
Beispiel #8
0
 public void Run()
 {
     while (!ShuttingDown)
     {
         Socket socket = Listener.AcceptSocket();
         lock (this)
         {
             Stream clientStream;
             NetworkStream networkStream = new NetworkStream(socket);
             if (UseTLS)
             {
                 SslStream secureStream = new SslStream(networkStream, false, AcceptAnyCertificate, null);
                 secureStream.AuthenticateAsServer(Certificate, false, SslProtocols.Tls12, false);
                 clientStream = secureStream;
             }
             else
                 clientStream = networkStream;
             ServerClient client = new ServerClient(clientStream, this);
             Clients.Add(client);
         }
     }
 }
Beispiel #9
0
        public bool OnJoinGameRequest(ServerClient client, JoinGameRequest request, out ServerGame game)
        {
            if (request.IsPrivate)
            {
                string key = request.PrivateKey;
                if (!PrivateGames.TryGetValue(key, out game))
                    return false;
                PrivateGames.Remove(key);
            }
            else
            {
                string key = request.Owner;
                if (!PublicGames.TryGetValue(key, out game))
                    return false;
                PublicGames.Remove(key);
            }

            ActiveGames.Add(game);
            game.StartDeploymentTimer();
            return true;
        }
Beispiel #10
0
 public void OnGameEnd(ServerGame game, GameOutcomeType outcome, ServerClient winner = null)
 {
     ActiveGames.Remove(game);
     game.EndGame(new GameEndBroadcast(outcome, winner));
 }
Beispiel #11
0
 public CreateGameReply OnCreateGameRequest(ServerClient client, CreateGameRequest request, out ServerGame game)
 {
     Map map = GetMap(request.GameConfiguration.Map);
     if (map == null)
         throw new ServerClientException("No such map");
     ValidateGameConfiguration(request.GameConfiguration);
     if (request.IsPrivate)
     {
         string privateKey = GeneratePrivateKey();
         game = new ServerGame(this, client, true, privateKey, request.GameConfiguration, map);
         PrivateGames[privateKey] = game;
         return new CreateGameReply(privateKey);
     }
     else
     {
         game = new ServerGame(this, client, false, null, request.GameConfiguration, map);
         PublicGames[client.Name] = game;
         return new CreateGameReply();
     }
 }
Beispiel #12
0
        bool NameIsInUse(string name)
        {
            ServerClient client = Clients.Find((ServerClient x) => x.Name == name);

            return(client != null);
        }
Beispiel #13
0
 public void OnGameEnd(ServerGame game, GameOutcomeType outcome, ServerClient winner = null)
 {
     ActiveGames.Remove(game);
     game.EndGame(new GameEndBroadcast(outcome, winner));
 }
Beispiel #14
0
 public void OnCancelGameRequest(ServerClient client)
 {
     CancelGame(client);
 }