Ejemplo n.º 1
0
        private void AddPlayer(LANPlayerInfo lpInfo)
        {
            if (Players.Find(p => p.Name == lpInfo.Name) != null ||
                Players.Count >= SGPlayers.Count ||
                SGPlayers.Find(p => p.Name == lpInfo.Name) == null)
            {
                lpInfo.TcpClient.Close();
                return;
            }

            if (Players.Count == 0)
            {
                lpInfo.Ready = true;
            }

            Players.Add(lpInfo);

            lpInfo.MessageReceived += LpInfo_MessageReceived;
            lpInfo.ConnectionLost  += LpInfo_ConnectionLost;

            sndJoinSound.Play();

            AddNotice(lpInfo.Name + " connected from " + lpInfo.IPAddress);
            lpInfo.StartReceiveLoop();

            CopyPlayerDataToUI();
            BroadcastOptions();
            UpdateDiscordPresence();
        }
Ejemplo n.º 2
0
        private void AddPlayer(LANPlayerInfo lpInfo)
        {
            if (Players.Find(p => p.Name == lpInfo.Name) != null ||
                Players.Count >= MAX_PLAYER_COUNT || Locked)
            {
                return;
            }

            Players.Add(lpInfo);

            if (IsHost && Players.Count == 1)
            {
                Players[0].Ready = true;
            }

            lpInfo.MessageReceived += LpInfo_MessageReceived;
            lpInfo.ConnectionLost  += LpInfo_ConnectionLost;

            AddNotice(lpInfo.Name + " connected from " + lpInfo.IPAddress);
            lpInfo.StartReceiveLoop();

            CopyPlayerDataToUI();
            BroadcastPlayerOptions();
            OnGameOptionChanged();
        }
Ejemplo n.º 3
0
        private void ListenForClients()
        {
            listener = new TcpListener(IPAddress.Any, ProgramConstants.LAN_GAME_LOBBY_PORT);
            listener.Start();

            while (true)
            {
                TcpClient client;

                try
                {
                    client = listener.AcceptTcpClient();
                }
                catch (Exception ex)
                {
                    Logger.Log("Listener error: " + ex.Message);
                    break;
                }

                Logger.Log("New client connected from " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());

                LANPlayerInfo lpInfo = new LANPlayerInfo(encoding);
                lpInfo.SetClient(client);

                Thread thread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
                thread.Start(lpInfo);
            }
        }
 private void CleanUpPlayer(LANPlayerInfo lpInfo)
 {
     Logger.Log("Cleaning up LAN player " + lpInfo.Name);
     Players.Remove(lpInfo);
     lpInfo.MessageReceived -= LpInfo_MessageReceived;
     lpInfo.TcpClient.Close();
 }
Ejemplo n.º 5
0
 private void Server_HandleFileHashMessage(LANPlayerInfo sender, string hash)
 {
     if (hash != localFileHash)
     {
         AddNotice(sender.Name + " - modified files detected! They could be cheating!", Color.Red);
     }
     sender.Verified = true;
 }
Ejemplo n.º 6
0
 private void Server_HandleReadyRequest(LANPlayerInfo sender)
 {
     if (!sender.Ready)
     {
         sender.Ready = true;
         CopyPlayerDataToUI();
         BroadcastOptions();
     }
 }
Ejemplo n.º 7
0
        private void HandleClientMessage(string data, LANPlayerInfo lpInfo)
        {
            lpInfo.TimeSinceLastReceivedMessage = TimeSpan.Zero;

            foreach (var cmdHandler in hostCommandHandlers)
            {
                if (cmdHandler.Handle(lpInfo, data))
                {
                    return;
                }
            }

            Logger.Log("Unknown LAN command from " + lpInfo.ToString() + " : " + data);
        }
Ejemplo n.º 8
0
        private void Client_HandleOptionsMessage(string data)
        {
            if (IsHost)
            {
                return;
            }

            string[]  parts             = data.Split(ProgramConstants.LAN_DATA_SEPARATOR);
            const int PLAYER_INFO_PARTS = 3;
            int       pCount            = (parts.Length - 1) / PLAYER_INFO_PARTS;

            if (pCount * PLAYER_INFO_PARTS + 1 != parts.Length)
            {
                return;
            }

            int savedGameIndex = Conversions.IntFromString(parts[0], -1);

            if (savedGameIndex < 0 || savedGameIndex >= ddSavedGame.Items.Count)
            {
                return;
            }

            ddSavedGame.SelectedIndex = savedGameIndex;

            Players.Clear();

            for (int i = 0; i < pCount; i++)
            {
                int    baseIndex = 1 + i * PLAYER_INFO_PARTS;
                string pName     = parts[baseIndex];
                bool   ready     = Conversions.IntFromString(parts[baseIndex + 1], -1) > 0;
                string ipAddress = parts[baseIndex + 2];

                LANPlayerInfo pInfo = new LANPlayerInfo(encoding);
                pInfo.Name      = pName;
                pInfo.Ready     = ready;
                pInfo.IPAddress = ipAddress;
                Players.Add(pInfo);
            }

            if (Players.Count > 0) // Set IP of host
            {
                Players[0].IPAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
            }

            CopyPlayerDataToUI();
        }
Ejemplo n.º 9
0
        private void ListenForClients()
        {
            listener = new TcpListener(IPAddress.Any, ProgramConstants.LAN_GAME_LOBBY_PORT);
            listener.Start();

            while (true)
            {
                TcpClient client;

                try
                {
                    client = listener.AcceptTcpClient();
                }
                catch (Exception ex)
                {
                    Logger.Log("Listener error: " + ex.Message);
                    break;
                }

                Logger.Log("New client connected from " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());

                if (Players.Count >= MAX_PLAYER_COUNT)
                {
                    Logger.Log("Dropping client because of player limit.");
                    client.Close();
                    continue;
                }

                if (Locked)
                {
                    Logger.Log("Dropping client because the game room is locked.");
                    client.Close();
                    continue;
                }

                LANPlayerInfo lpInfo = new LANPlayerInfo(encoding);
                lpInfo.SetClient(client);

                Thread thread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
                thread.Start(lpInfo);
            }
        }
Ejemplo n.º 10
0
        public override void Update(GameTime gameTime)
        {
            if (IsHost)
            {
                for (int i = 1; i < Players.Count; i++)
                {
                    LANPlayerInfo lpInfo = (LANPlayerInfo)Players[i];
                    if (!lpInfo.Update(gameTime))
                    {
                        CleanUpPlayer(lpInfo);
                        Players.RemoveAt(i);
                        AddNotice(lpInfo.Name + " - connection timed out");
                        CopyPlayerDataToUI();
                        BroadcastPlayerOptions();
                        BroadcastPlayerExtraOptions();
                        UpdateDiscordPresence();
                        i--;
                    }
                }

                timeSinceGameBroadcast += gameTime.ElapsedGameTime;

                if (timeSinceGameBroadcast > TimeSpan.FromSeconds(GAME_BROADCAST_INTERVAL))
                {
                    BroadcastGame();
                    timeSinceGameBroadcast = TimeSpan.Zero;
                }
            }
            else
            {
                timeSinceLastReceivedCommand += gameTime.ElapsedGameTime;

                if (timeSinceLastReceivedCommand > TimeSpan.FromSeconds(DROPOUT_TIMEOUT))
                {
                    LobbyNotification?.Invoke(this,
                                              new LobbyNotificationEventArgs("Connection to the game host timed out."));
                    BtnLeaveGame_LeftClick(this, EventArgs.Empty);
                }
            }

            base.Update(gameTime);
        }
Ejemplo n.º 11
0
        private void Server_HandleChatMessage(LANPlayerInfo sender, string data)
        {
            string[] parts = data.Split(ProgramConstants.LAN_DATA_SEPARATOR);

            if (parts.Length < 2)
            {
                return;
            }

            int colorIndex = Conversions.IntFromString(parts[0], -1);

            if (colorIndex < 0 || colorIndex >= chatColors.Length)
            {
                return;
            }

            BroadcastMessage(CHAT_COMMAND + " " + sender +
                             ProgramConstants.LAN_DATA_SEPARATOR + colorIndex +
                             ProgramConstants.LAN_DATA_SEPARATOR + data);
        }
Ejemplo n.º 12
0
 private void CleanUpPlayer(LANPlayerInfo lpInfo)
 {
     lpInfo.MessageReceived -= LpInfo_MessageReceived;
     lpInfo.TcpClient.Close();
 }
 public abstract bool Handle(LANPlayerInfo pInfo, string message);
Ejemplo n.º 14
0
        private void HandlePlayerOptionsBroadcast(string data)
        {
            if (IsHost)
            {
                return;
            }

            string[] parts = data.Split(ProgramConstants.LAN_DATA_SEPARATOR);

            int playerCount = parts.Length / 8;

            if (parts.Length != playerCount * 8)
            {
                return;
            }

            Players.Clear();
            AIPlayers.Clear();

            for (int i = 0; i < playerCount; i++)
            {
                int baseIndex = i * 8;

                string name        = parts[baseIndex];
                int    side        = Conversions.IntFromString(parts[baseIndex + 1], -1);
                int    color       = Conversions.IntFromString(parts[baseIndex + 2], -1);
                int    start       = Conversions.IntFromString(parts[baseIndex + 3], -1);
                int    team        = Conversions.IntFromString(parts[baseIndex + 4], -1);
                int    readyStatus = Conversions.IntFromString(parts[baseIndex + 5], -1);
                string ipAddress   = parts[baseIndex + 6];
                int    aiLevel     = Conversions.IntFromString(parts[baseIndex + 7], -1);

                if (side < 0 || side > SideCount + RandomSelectorCount)
                {
                    return;
                }

                if (color < 0 || color > MPColors.Count)
                {
                    return;
                }

                if (start < 0 || start > MAX_PLAYER_COUNT)
                {
                    return;
                }

                if (team < 0 || team > 4)
                {
                    return;
                }

                if (ipAddress == "127.0.0.1")
                {
                    ipAddress = hostEndPoint.Address.ToString();
                }

                bool isAi = aiLevel > -1;
                if (aiLevel > 2)
                {
                    return;
                }

                PlayerInfo pInfo;

                if (!isAi)
                {
                    pInfo      = new LANPlayerInfo(encoding);
                    pInfo.Name = name;
                    Players.Add(pInfo);
                }
                else
                {
                    pInfo         = new PlayerInfo();
                    pInfo.Name    = AILevelToName(aiLevel);
                    pInfo.IsAI    = true;
                    pInfo.AILevel = aiLevel;
                    AIPlayers.Add(pInfo);
                }

                pInfo.SideId           = side;
                pInfo.ColorId          = color;
                pInfo.StartingLocation = start;
                pInfo.TeamId           = team;
                pInfo.Ready            = readyStatus > 0;
                pInfo.IPAddress        = ipAddress;
            }

            CopyPlayerDataToUI();
        }