Example #1
0
        public void AddClient(Connection connection)
        {
            connection.MessageRecieved += OnMessageRecieved;
            this.connections.Add(connection);

            if (this == Server.instance.rooms[0])
            {
                SendRoomList(connection);
                return;
            }


            ClientInformationMessage m = new ClientInformationMessage(connection.ClientId, connection.Username, connections.Count - 1, 0);

            connection.Send(m);
            connection.PlayerID = connections.Count - 1;
            for (int i = 0; i < connections.Count; i++)
            {
                try
                {
                    m.informationType = 1;
                    if (connections[i] != connection)
                    {
                        connections[i].Send(m);
                    }
                    ClientInformationMessage m1 = new ClientInformationMessage(connections[i].ClientId, connections[i].Username, connections[i].PlayerID, 1);
                    connection.Send(m1);
                }
                catch (Exception e)
                {
                    ConsoleLogs.ConsoleLog(ConsoleColor.Red, e.ToString());
                    disconnectedConnections.Add(connections[i]);
                    connections[i].MessageRecieved -= OnMessageRecieved;
                }
            }

            if (this != Server.instance.rooms[0])
            {
                SendRoomInfo(connection);
                Console.WriteLine("ECH");
            }
        }
Example #2
0
        void OnMessageRecieved(MessageBase message, Connection senderConnection)
        {
            if (message is DisconnectMessage)
            {
                disconnectedConnections.Add(senderConnection);
                senderConnection.MessageRecieved -= OnMessageRecieved;
                HelperFunctions.RemoveDisconnectedClients(disconnectedConnections, connections);
                CheckRoomState();

                for (int i = 0; i < connections.Count; i++)
                {
                    ClientInformationMessage m = new ClientInformationMessage(connections[i].ClientId, connections[i].Username, i, 0);
                    connections[i].PlayerID = i;
                    connections[i].Send(m);
                }
            }

            if (message is ConnectMessage)
            {
                string clientID = (DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds.ToString() + "feral" + Server.instance.numOfConnectedClients;
                int    playerID = connections.Count - 1;

                ClientInformationMessage m = new ClientInformationMessage(clientID, ((ConnectMessage)message).userName, playerID, 0);
                senderConnection.Send(m);
                senderConnection.Username = ((ConnectMessage)message).userName;
                senderConnection.ClientId = clientID;
                senderConnection.PlayerID = playerID;

                if (this == Server.instance.rooms[0])
                {
                }
                else
                {
                    for (int i = 0; i < connections.Count; i++)
                    {
                        try
                        {
                            m.informationType = 1;
                            if (connections[i] != senderConnection)
                            {
                                connections[i].Send(m);
                            }
                            ClientInformationMessage m1 = new ClientInformationMessage(connections[i].ClientId, connections[i].Username, connections[i].PlayerID, 1);
                            senderConnection.Send(m1);
                        }
                        catch (Exception e)
                        {
                            ConsoleLogs.ConsoleLog(ConsoleColor.Red, e.ToString());
                            disconnectedConnections.Add(connections[i]);
                            connections[i].MessageRecieved -= OnMessageRecieved;
                        }
                    }
                }
            }

            if (message is RoomCreationMessage)
            {
                CreateRoom((RoomCreationMessage)message, senderConnection);
            }

            if (message is RoomJoinMessage)
            {
                var  m      = (RoomJoinMessage)message;
                Room target = FindRoom(m.roomID);

                if (target != null && !target.isLobby && target.playerCount < target.maxPlayerNumber)
                {
                    ((RoomJoinMessage)message).result = 0;
                    senderConnection.Send(message);
                    LeaveRoom(senderConnection, target);
                }
                else
                {
                    ((RoomJoinMessage)message).result = 1;
                    senderConnection.Send(message);
                }
            }

            if (message is RoomLobbyMessage)
            {
                var m = (RoomLobbyMessage)message;

                LeaveRoom(senderConnection);
                ((RoomLobbyMessage)message).result = 0;
                senderConnection.Send(message);
            }

            if (message is PlayerRenameMessage)
            {
                senderConnection.Username = ((PlayerRenameMessage)message).newName;
                if (senderConnection == connections[0])
                {
                    roomHost = ((PlayerRenameMessage)message).newName;
                }
            }

            if (message is RoomListUpdateMessage)
            {
                SendRoomList(senderConnection);
            }

            if (message.EMessageType != eMessageTypes.RoomJoinMessage)
            {
                for (int i = 0; i < connections.Count; i++)
                {
                    try
                    {
                        connections[i].Send(message);
                    }
                    catch (Exception e)
                    {
                        ConsoleLogs.ConsoleLog(ConsoleColor.Red, e.ToString());
                        disconnectedConnections.Add(connections[i]);
                        connections[i].MessageRecieved -= OnMessageRecieved;
                    }
                }
            }

            ConsoleLogs.LogMessage(message);
        }