Exemplo n.º 1
0
        /// <summary>
        /// Lets the user host a new game.
        /// </summary>
        private void btnNewGame_Click(object sender, EventArgs e)
        {
            if (CnCNetData.IsGameLobbyOpen)
            {
                // Prevent the user from hosting a game if they already are in a game
                // and flash the game lobby window
                WindowFlasher.FlashWindowEx(gameLobbyWindow);
                MessageInfos[currentChannelId].Add(new MessageInfo(Color.White, "You already are in a game!"));
                AddChannelMessageToListBox(currentChannelId);
                return;
            }

            CreateGameForm cgf = new CreateGameForm();
            DialogResult dr = cgf.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string channelName = randomizeChannelName();
                AddChannelMessageToListBox(currentChannelId);

                bool customPassword = true;
                string password = cgf.rtnPassword;
                if (password == String.Empty)
                {
                    // If the user didn't enter a custom password, generate one
                    // following CnCNet protocol specification
                    customPassword = false;
                    password = Utilities.calculateMD5ForBytes(Encoding.Unicode.GetBytes(cgf.rtnGameRoomName)).Substring(0, 10);
                }

                for (int gId = 0; gId < CnCNetData.Games.Count; gId++)
                {
                    if (CnCNetData.Games[gId].RoomName == cgf.rtnGameRoomName)
                    {
                        MessageBox.Show("A game room with the specified name already exists.");
                        cgf.Dispose();
                        return;
                    }
                }

                Logger.Log("Creating a game named " + cgf.rtnGameRoomName + "; channel name " + channelName);

                gameLobbyWindow = new NGameLobby(channelName, true, cgf.rtnMaxPlayers, ProgramConstants.CNCNET_PLAYERNAME, cgf.rtnGameRoomName, password,
                    customPassword, ChatColors, cDefaultChatColor, cmbMessageColor.SelectedIndex + 2);
                CnCNetData.IsGameLobbyOpen = true;
                gameLobbyWindow.Show();
                CnCNetData.ConnectionBridge.SendMessage("JOIN " + channelName);
                gameLobbyWindow.Initialize(cgf.rtnTunnelAddress, cgf.rtnTunnelPort);
                CnCNetData.ConnectionBridge.SendMessage("AWAY " + weirdChar1 + "In game [" + myGame.ToUpper() + "] " + cgf.rtnGameRoomName);
                BroadcastGameCreation();
                oldWindowState = this.WindowState;
                this.WindowState = FormWindowState.Minimized;
                cgf.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executed when an user joins a channel.
        /// </summary>
        /// <param name="channelName">The name of the channel that the user joins to.</param>
        /// <param name="userName">The name of the user who joins the channel.</param>
        /// <param name="ipAddress">UNUSED! Reserved for containing the joining user's IP address
        /// but this client doesn't actually parse it since it's not necessary for tunneled CnCNet games.</param>
        private void Instance_OnUserJoinedChannel(string channelName, string userName, string ipAddress)
        {
            if (this.InvokeRequired)
            {
                // Necessary for thread-safety
                UserJoinChannelCallback d = new UserJoinChannelCallback(Instance_OnUserJoinedChannel);
                this.BeginInvoke(d, channelName, userName, ipAddress);
                return;
            }

            string name = userName;
            if (userName.StartsWith("@"))
            {
                name = name + " (Admin)";
            }

            bool updatePlayerList = false;
            bool channelFound = false;

            int gameCount = GameCollection.Instance.GetGameCount();

            int channelIndex = GameCollection.Instance.GetGameIndexFromChatChannelName(channelName);

            if (channelIndex > -1)
            {
                if (userName.StartsWith("@"))
                {
                    IrcUser iu = new IrcUser(name, true);
                    UserLists[channelIndex].Add(iu);
                }
                else
                {
                    if (userName != ProgramConstants.CNCNET_PLAYERNAME)
                    {
                        IrcUser iu = new IrcUser(userName, false);
                        UserLists[channelIndex].Add(iu);
                    }
                }

                if (currentChannelId == channelIndex)
                {
                    // Update player list if necessary
                    updatePlayerList = true;
                }

                MessageInfos[channelIndex].Add(new MessageInfo(Color.White,
                    string.Format("{0} has joined the {1} lobby.", userName,
                    GameCollection.Instance.GetFullGameNameFromIndex(channelIndex))));
                AddChannelMessageToListBox(channelIndex);
                channelFound = true;

                CnCNetData.ConnectionBridge.SendMessage("WHO " + userName);
            }

            // If the channel the user joined to wasn't found, check if we just joined a game channel
            if (!channelFound && channelName == joinGameChannelName && userName == ProgramConstants.CNCNET_PLAYERNAME)
            {
                Game game = CnCNetData.Games.Find(c => c.ChannelName == channelName);
                if (game != null)
                {
                    Logger.Log("Joining game " + game.RoomName + "; channel name " + channelName);
                    CnCNetData.IsGameLobbyOpen = true;
                    gameLobbyWindow = new NGameLobby(channelName, false, game.MaxPlayers, game.Admin, game.RoomName, "",
                        false, ChatColors, cDefaultChatColor, cmbMessageColor.SelectedIndex + 2);
                    gameLobbyWindow.Show();
                    CnCNetData.ConnectionBridge.SendMessage("AWAY " + weirdChar1 + "In game [" + myGame.ToUpper() + "] " + game.RoomName);
                    oldWindowState = this.WindowState;
                    this.WindowState = FormWindowState.Minimized;
                    joinGameChannelName = String.Empty;
                }
                else
                    Logger.Log("WARNING: No game data found for channel " + channelName);
            }

            if (updatePlayerList)
            {
                UpdatePlayerList();
            }
        }