Exemplo n.º 1
0
        private void LbGameList_DoubleLeftClick(object sender, EventArgs e)
        {
            if (lbGameList.SelectedIndex < 0 || lbGameList.SelectedIndex >= lbGameList.Items.Count)
            {
                return;
            }

            HostedLANGame hg = (HostedLANGame)lbGameList.Items[lbGameList.SelectedIndex].Tag;

            if (hg.Game.InternalName.ToUpper() != localGame.ToUpper())
            {
                lbChatMessages.AddMessage("The selected game is for " +
                                          gameCollection.GetGameNameFromInternalName(hg.Game.InternalName) + "!");
                return;
            }

            if (hg.Locked)
            {
                lbChatMessages.AddMessage("The selected game is locked!");
                return;
            }

            if (hg.IsLoadedGame)
            {
                if (!hg.Players.Contains(ProgramConstants.PLAYERNAME))
                {
                    lbChatMessages.AddMessage("You do not exist in the saved game!");
                    return;
                }
            }
            else
            {
                if (hg.Players.Contains(ProgramConstants.PLAYERNAME))
                {
                    lbChatMessages.AddMessage("Your name is already taken in the game.");
                    return;
                }
            }

            if (hg.GameVersion != ProgramConstants.GAME_VERSION)
            {
                // TODO Show warning
            }

            lbChatMessages.AddMessage("Attempting to join game " + hg.RoomName + "...");

            try
            {
                var client = new TcpClient(hg.EndPoint.Address.ToString(), ProgramConstants.LAN_GAME_LOBBY_PORT);

                byte[] buffer;

                if (hg.IsLoadedGame)
                {
                    var spawnSGIni = new IniFile(ProgramConstants.GamePath +
                                                 ProgramConstants.SAVED_GAME_SPAWN_INI);

                    int loadedGameId = spawnSGIni.GetIntValue("Settings", "GameID", -1);

                    lanGameLoadingLobby.SetUp(false, hg.EndPoint, client, loadedGameId);
                    lanGameLoadingLobby.Enable();

                    buffer = encoding.GetBytes("JOIN" + ProgramConstants.LAN_DATA_SEPARATOR +
                                               ProgramConstants.PLAYERNAME + ProgramConstants.LAN_DATA_SEPARATOR +
                                               loadedGameId + ProgramConstants.LAN_MESSAGE_SEPARATOR);

                    client.GetStream().Write(buffer, 0, buffer.Length);
                    client.GetStream().Flush();

                    lanGameLoadingLobby.PostJoin();
                }
                else
                {
                    lanGameLobby.SetUp(false, hg.EndPoint, client);
                    lanGameLobby.Enable();

                    buffer = encoding.GetBytes("JOIN" + ProgramConstants.LAN_DATA_SEPARATOR +
                                               ProgramConstants.PLAYERNAME + ProgramConstants.LAN_MESSAGE_SEPARATOR);

                    client.GetStream().Write(buffer, 0, buffer.Length);
                    client.GetStream().Flush();

                    lanGameLobby.PostJoin();
                }
            }
            catch (Exception ex)
            {
                lbChatMessages.AddMessage(null,
                                          "Connecting to the game failed! Message: " + ex.Message, Color.White);
            }
        }
Exemplo n.º 2
0
        private void HandleNetworkMessage(string data, IPEndPoint endPoint)
        {
            string[] commandAndParams = data.Split(' ');

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

            string command = commandAndParams[0];

            string[] parameters = data.Substring(command.Length + 1).Split(
                new char[] { ProgramConstants.LAN_DATA_SEPARATOR },
                StringSplitOptions.RemoveEmptyEntries);

            LANLobbyUser user = players.Find(p => p.EndPoint.Equals(endPoint));

            switch (command)
            {
            case "ALIVE":
                if (parameters.Length < 2)
                {
                    return;
                }

                int    gameIndex = Conversions.IntFromString(parameters[0], -1);
                string name      = parameters[1];

                if (user == null)
                {
                    Texture2D gameTexture = unknownGameIcon;

                    if (gameIndex > -1 && gameIndex < gameCollection.GameList.Count)
                    {
                        gameTexture = gameCollection.GameList[gameIndex].Texture;
                    }

                    user = new LANLobbyUser(name, gameTexture, endPoint);
                    players.Add(user);
                    lbPlayerList.AddItem(user.Name, gameTexture);
                }

                user.TimeWithoutRefresh = TimeSpan.Zero;

                break;

            case "CHAT":
                if (user == null)
                {
                    return;
                }

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

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

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

                lbChatMessages.AddMessage(new ChatMessage(user.Name,
                                                          chatColors[colorIndex].XNAColor, DateTime.Now, parameters[1]));

                break;

            case "QUIT":
                if (user == null)
                {
                    return;
                }

                int index = players.FindIndex(p => p == user);

                players.RemoveAt(index);
                lbPlayerList.Items.RemoveAt(index);
                break;

            case "GAME":
                if (user == null)
                {
                    return;
                }

                HostedLANGame game = new HostedLANGame();
                if (!game.SetDataFromStringArray(gameCollection, parameters))
                {
                    return;
                }
                game.EndPoint = endPoint;

                int existingGameIndex = lbGameList.HostedGames.FindIndex(g => ((HostedLANGame)g).EndPoint.Equals(endPoint));

                if (existingGameIndex > -1)
                {
                    lbGameList.HostedGames[existingGameIndex] = game;
                }
                else
                {
                    lbGameList.HostedGames.Add(game);
                }

                lbGameList.Refresh();

                break;
            }
        }