/// <summary>
        ///   Initialize a set of server interaction args at the given time with the given parameters
        /// </summary>
        /// <param name="success"> </param>
        /// <param name="client"> </param>
        /// <param name="parameters"> </param>
        /// <param name="time"> </param>
        public ServerEventArgs(bool success, Client client, IDictionary<string, string> parameters, DateTime time)
        {
            Success = success;
            Client = client;
            Parameters = parameters ?? new Dictionary<string, string>();

            Time = time;
        }
        private void HandlePlayCardRequest(PlayCardRequestPacket packet, Client client)
        {
            var player = PlayerTable[client];
            var card = packet.Card;
            var cardIndex = packet.CardIndex;

            if (!Game.CanPlayCard(card, cardIndex, player)) return;
            Game.PlayCard(card, cardIndex, player);
            UpdateGameState();
        }
 public override void ReceivePacket(Packet packet, Client client)
 {
     if (packet is AuthenticateUserPacket)
         HandleAuthenticatePacket(packet as AuthenticateUserPacket, client);
     else if (packet is UserDisconnectPacket)
         HandleDisconnectRequest(packet as UserDisconnectPacket, client);
     else if (packet is StartGamePacket)
         HandleStartRequest(packet as StartGamePacket);
     else if (packet is PlayCardRequestPacket)
         HandlePlayCardRequest(packet as PlayCardRequestPacket, client);
 }
 private void HandleAuthenticatePacket(AuthenticateUserPacket packet, Client client)
 {
     if(Game.IsGameStarted || Game.Players.Count >= Game.Settings.MaxPlayers)
     {
         SendPacket(new ServerDisconnectPacket(), client);
         Disconnect(client);
         return;
     }
     var username = packet.Username;
     PlayerTable[client] = username;
     Game.AddPlayer(username);
     UpdateGameState();
 }
Example #5
0
        public override void ReceivePacket(Packet packet, Client client)
        {
            if (!IsAuthenticated(client))
            {
                var authPacket = packet as AuthRequestPacket;
                if (authPacket == null) return;

                HandleAutheticatePacket(authPacket, client);
                return;
            }

            var chatPacket = packet as ChatPacket;
            var nick = nickTable[client];

            if (chatPacket == null) return;
            if (String.IsNullOrEmpty(chatPacket.Message)) return;

            SendPacket(MakePacket("{0}: {1}".format(nick, chatPacket.Message)));
        }
Example #6
0
 protected bool Equals(Client other)
 {
     return Equals(TcpClient, other.TcpClient);
 }
Example #7
0
 private void HandleAutheticatePacket(AuthRequestPacket requestPacket, Client client)
 {
     const string nickInUseKey = "Server:AuthFail:Reason:NickInUse:Data:Nick:<{0}>";
     var username = requestPacket.UserName;
     var args = new ServerEventArgs(false, client);
     if (!IsNickAvailable(username))
     {
         Log.Info(nickInUseKey.format(username));
         SendPacket(new AuthResponsePacket() { Success = false, Message = "NickInUse:{0}".format(username) }, client);
     }
     else
     {
         nickTable[client] = username;
         SendPacket(new AuthResponsePacket() { Success = true, Message = username }, client);
         SendServerMessage("Please welcome {0} to the server!".format(username));
         args.Success = true;
     }
     Authenticate(client, args);
 }
 private void HandleDisconnectRequest(UserDisconnectPacket packet, Client client)
 {
     Disconnect(client);
 }
        public void Connect(string server)
        {
            // See if it's one of the server favorites
            var favoritesFilename = ScreenManager.Content.RootDirectory + @"\Servers.ini";
            var favorites = Parsers.ParseServerFavorites(favoritesFilename);
            if (favorites.ContainsKey(server.ToLower()))
                server = favorites[server.ToLower()];

            var hostAndPort = server.Split(':');
            var host = hostAndPort[0];
            var port = Int32.Parse(hostAndPort[1]);

            TcpClient baseClient;
            try
            {
                baseClient = new TcpClient(host, port);
            }
            catch (SocketException)
            {
                ScreenManager.CloseScreen(this);
                return;
            }

            Client = new Client(baseClient);
            Client.OnReadPacket += OnClientRead;
            Client.OnConnectionLost += (o, args) => ScreenManager.CloseScreen(this);
        }
 /// <summary>
 ///   Initialize a set of server interaction args at the current time with the given parameters
 /// </summary>
 /// <param name="success"> </param>
 /// <param name="client"> </param>
 /// <param name="parameters"> </param>
 public ServerEventArgs(bool success, Client client, IDictionary<string, string> parameters)
     : this(success, client, parameters, DateTime.Now)
 {
 }
 /// <summary>
 ///   Initialize a set of parameterless server interaction args at the current time
 /// </summary>
 /// <param name="success"> </param>
 /// <param name="client"> </param>
 public ServerEventArgs(bool success, Client client)
     : this(success, client, null)
 {
 }