Exemplo n.º 1
0
        public void AddChat(User user, string chatMsg)
        {
            ChatInfo chatInfo = new ChatInfo();
            chatInfo.Chat = chatMsg;
            chatInfo.Name = user.Name;

            NetworkMessage message = new NetworkMessage();
            message.MessageCategory = SystemMessages.SystemPrefix;
            message.MessageType = SystemMessages.SendChat;
            message.MessageContent = NetworkSerializer.Serialize(chatInfo);

            lock (this.serverLock)
            {
                Chat chat = new Chat(user, chatMsg, DateTime.Now);
                user.Lobby.Chats.Add(chat);
                foreach (User u in user.Lobby.Users)
                {
                    u.Connection.SendMessage(message);
                }
            }
        }
Exemplo n.º 2
0
        public void HandleMessage(NetworkMessage message)
        {
            lock (this.statusLock)
            {
                if (Server.EnableLogging)
                {
                    string log = "Received";
                    if (this.user != null && this.user.Name != null)
                    {
                        log += " from " + this.user.Name;
                    }
                    log += ": ";
                    log += message.ToString();
                    Console.WriteLine(log);
                }

                if (message.MessageCategory == SystemMessages.SystemPrefix)
                {
                    switch (message.MessageType)
                    {
                        case SystemMessages.Connect:
                            {
                                ConnectInfo connectInfo = NetworkSerializer.Deserialize<ConnectInfo>(message.MessageContent);
                                if (Server.EnableLogging)
                                {
                                    Console.WriteLine(connectInfo.Username);
                                }
                                this.user = this.server.AddUser(connectInfo.Username, this);
                                AuthenticationInfo auth = new AuthenticationInfo();
                                auth.Username = user.Name;
                                auth.Authentication = user.Authentication;
                                NetworkMessage authMessage = new NetworkMessage();
                                authMessage.MessageCategory = SystemMessages.SystemPrefix;
                                authMessage.MessageType = SystemMessages.Authenticate;
                                authMessage.MessageContent = NetworkSerializer.Serialize(auth);
                                this.serverSocket.SendMessage(authMessage);

                                this.Status = ConnectionStatus.Ready;
                            }
                            break;
                        case SystemMessages.SendChat:
                            {
                                SendChatInfo chatInfo = NetworkSerializer.Deserialize<SendChatInfo>(message.MessageContent);
                                this.server.AddChat(this.user, chatInfo.Chat);
                                if (Server.EnableLogging)
                                {
                                    Console.WriteLine("Receiving chat");
                                }
                            }
                            break;
                        case SystemMessages.EnterLobby:
                            {
                                EnterLobbyInfo enterLobbyInfo = NetworkSerializer.Deserialize<EnterLobbyInfo>(message.MessageContent);
                                Lobby lobby = null;
                                if (this.server.Lobbies.TryGetValue(enterLobbyInfo.Lobby, out lobby))
                                {
                                    this.server.AddUserToLobby(this.user, lobby);
                                }

                            }
                            break;
                        case SystemMessages.Disconnect:
                            {
                                this.server.RemoveUserFromLobby(this.user, this.user.Lobby);
                                this.server.RemoveUser(this.user);
                                this.Status = ConnectionStatus.Exiting;
                                if (Server.EnableLogging)
                                {
                                    Console.WriteLine("Exiting");
                                }
                            }
                            break;
                        case SystemMessages.RequestGame:
                            {
                                GameSpecificationInfo gameSpecificationInfo = NetworkSerializer.Deserialize<GameSpecificationInfo>(message.MessageContent);
                                this.server.ProposeGameWithUsers(this, gameSpecificationInfo.Players, gameSpecificationInfo, this.user.Lobby);
                                if (Server.EnableLogging)
                                {
                                    Console.WriteLine("Proposing game with " + message.MessageContent);
                                }
                            }
                            break;
                        case SystemMessages.AcceptGame:
                            {
                                if (this.RespondedToGame != null)
                                {
                                    this.RespondedToGame(new RespondToGameEventArgs(this, true));
                                }
                            }
                            break;
                        case SystemMessages.DeclineGame:
                            {
                                if (this.RespondedToGame != null)
                                {
                                    this.RespondedToGame(new RespondToGameEventArgs(this, false));
                                }
                            }
                            break;
                    }
                }
                else if (message.MessageCategory == GameMessages.GamePrefix)
                {
                    if (this.GameMessage != null)
                    {
                        this.GameMessage(new GameMessageEventArgs() { Message = message });
                    }
                }
            }
        }
Exemplo n.º 3
0
 public Chat(User sender, string message, DateTime time)
 {
     this.Sender = sender;
     this.Message = message;
     this.Time = time;
 }
Exemplo n.º 4
0
        public User AddUser(string id, Connection connection)
        {
            LobbyInfo lobbyInfo = new LobbyInfo();
            User user;
            lock (this.serverLock)
            {
                while (this.users.Any(u => u.Name == id))
                {
                    id += "0";
                }
                user = new User(id, connection);
                this.users.Add(user);

                lobbyInfo.Lobbies.AddRange(this.Lobbies.Keys);
            }

            NetworkMessage message = new NetworkMessage();
            message.MessageCategory = SystemMessages.SystemPrefix;
            message.MessageType = SystemMessages.SendLobbies;
            message.MessageContent = NetworkSerializer.Serialize(lobbyInfo);
            user.Connection.SendMessage(message);
            return user;
        }
Exemplo n.º 5
0
        public void RemoveUserFromLobby(User user, Lobby lobby)
        {
            AddRemoveUserInfo removeUserInfo = new AddRemoveUserInfo();
            removeUserInfo.User = user.Name;
            lock (this.serverLock)
            {
                lobby.Users.Remove(user);
                user.Lobby = null;

                NetworkMessage message = new NetworkMessage();
                message.MessageCategory = SystemMessages.SystemPrefix;
                message.MessageType = SystemMessages.RemoveUser;
                message.MessageContent = NetworkSerializer.Serialize(removeUserInfo);

                foreach (User u in lobby.Users)
                {
                    u.Connection.SendMessage(message);
                }
                UserInfo userInfo = new UserInfo();
                NetworkMessage message2 = new NetworkMessage();
                message2.MessageCategory = SystemMessages.SystemPrefix;
                message2.MessageType = SystemMessages.SendUsers;
                message2.MessageContent = NetworkSerializer.Serialize(userInfo);
                user.Connection.SendMessage(message2);
            }
        }
Exemplo n.º 6
0
 public void RemoveUser(User user)
 {
     lock (this.serverLock)
     {
         this.users.Remove(user);
     }
 }
Exemplo n.º 7
0
        public void AddUserToLobby(User user, Lobby lobby)
        {
            AddRemoveUserInfo addUserInfo = new AddRemoveUserInfo();
            addUserInfo.User = user.Name;

            lock (this.serverLock)
            {
                NetworkMessage message = new NetworkMessage();
                message.MessageCategory = SystemMessages.SystemPrefix;
                message.MessageType = SystemMessages.AddUser;
                message.MessageContent = NetworkSerializer.Serialize(addUserInfo);

                foreach (User u in lobby.Users)
                {
                    u.Connection.SendMessage(message);
                }

                UserInfo userInfo = new UserInfo();
                userInfo.Users.AddRange(lobby.Users.Select(u => u.Name));
                NetworkMessage message2 = new NetworkMessage();
                message2.MessageCategory = SystemMessages.SystemPrefix;
                message2.MessageType = SystemMessages.SendUsers;
                message2.MessageContent = NetworkSerializer.Serialize(userInfo);

                user.Connection.SendMessage(message2);

                lobby.Users.Add(user);
                user.Lobby = lobby;
            }
        }