Exemplo n.º 1
0
        private void Login()
        {
            //Check input isn't empty, else error message is shown
            if (usernameBox.TextLength > 0 && passwordBox.TextLength > 0)
            {
                //Sends input to login function, if input is good, sends user to main menu
                //Else error message is shown
                var result = NetworkClasses.Login(usernameBox.Lines[0], passwordBox.Lines[0],
                                                  Helpers.GetLocalIpAddress());
                if (result == "good" || result == "online")
                {
                    NetworkClasses.UpdateUserValue("User_List", "Online", "Online", User.PlayerId);
                    Form form = new MainMenuForm();
                    form.Show();
                    Dispose();
                }
                else if (result == "pass" || result == "user")
                {
                    MessageBox.Show("Invalid Username/Password", "Login error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }

                /*else if (result == "online")
                 * {
                 *  MessageBox.Show("User already logged in", "Login error", MessageBoxButtons.OK,
                 *      MessageBoxIcon.Error);
                 * }*/
            }
            else
            {
                MessageBox.Show("Username and password cannot be blank.", "Login error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
 private void spectateButton_Click(object sender, EventArgs e)
 {
     if (!spectateButton.Enabled)
     {
         return;
     }
     //Checks if user is banned
     if (NetworkClasses.GetPlayer(User.PlayerId).Tables[0].Rows[0]["IsBanned"].ToString() == "0")
     {
         //Connect as a spectator
         if (Client.Connect(false))
         {
             try
             {
                 NetworkClasses.UpdateUserValue("User_List", "Online", "Spectating", User.PlayerId);
                 //TODO Start game?
             }
             catch (Exception exception)
             {
                 Console.WriteLine(exception);
             }
         }
         else
         {
             Console.WriteLine("Couldn't Connect");
         }
     }
     else
     {
         MessageBox.Show("Can't connect to game, you have been banned. Please contact administrator to lift ban.", "Join Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 3
0
 private void passChange_Click(object sender, EventArgs e)
 {
     if (passChangeText.TextLength >= 5 && passChangeText.TextLength <= 20)
     {
         if (ContainsVaildChars(passChangeText.Text))
         {
             if (NetworkClasses.UpdateUserValue("User_List", "Password", StringCipher.Encrypt(passChangeText.Text, "thomas").ToString(), User.PlayerId))
             {
                 MessageBox.Show("Password has been changed", "Password Updated", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 passChangeText.Text = "";
             }
             else
             {
                 MessageBox.Show("Something went wrong", "Password Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }
         }
         else
         {
             MessageBox.Show("Password must contain only letters and numbers.", "Password Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
     else
     {
         MessageBox.Show("Password must be between 5 and 20 characters.", "Password Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Exemplo n.º 4
0
 private void nameChange_Click(object sender, EventArgs e)
 {
     if (nameChangeText.TextLength > 0 && nameChangeText.TextLength <= 20)
     {
         if (ContainsVaildChars(nameChangeText.Text))
         {
             if (NetworkClasses.UpdateUserValue("User_List", "_Character", nameChangeText.Text, User.PlayerId))
             {
                 MessageBox.Show("Username has been changed to " + nameChangeText.Text + ".", "Username Updated",
                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
                 nameChangeText.Text = "";
             }
             else
             {
                 MessageBox.Show("Username is already taken.", "Username Error", MessageBoxButtons.OK,
                                 MessageBoxIcon.Warning);
             }
         }
         else
         {
             MessageBox.Show("Username must contain only letters and numbers.", "Username Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
     else
     {
         MessageBox.Show("Username must be between 1 and 20 characters.", "Username Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
        /// <summary>
        /// Once all players are ready, adds players to the Game controller and starts the game
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void start_game_Click(object sender, EventArgs e)
        {
            var ds  = NetworkClasses.GetServer(User.PlayerId, User.LocalIp);
            var row = ds.Tables[0].Rows[0];

            var grabber = NetworkClasses.GetPlayer(int.Parse(row["Host"].ToString()));

            //Host
            LobbyController.AddPlayer(int.Parse(grabber.Tables[0].Rows[0]["Player_ID"].ToString()), grabber.Tables[0].Rows[0]["_Character"].ToString());

            for (var i = 2; i <= 6; i++)
            {
                if (string.IsNullOrEmpty(row["Player_" + i].ToString()))
                {
                    continue;
                }
                grabber = NetworkClasses.GetPlayer(int.Parse(row["Player_" + i].ToString()));
                LobbyController.AddPlayer(int.Parse(grabber.Tables[0].Rows[0]["Player_ID"].ToString()), grabber.Tables[0].Rows[0]["_Character"].ToString());
            }
            NetworkClasses.UpdateServerValue("Status", "In Progress", "Host", User.PlayerId);
            NetworkClasses.UpdateUserValue("User_List", "Online", "In Game", User.PlayerId);
            LobbyController.StartGame();
            Host.StartGame();
            _timer.Stop();
            //_chat.Dispose();
            _gameTimer.Start();
            if (!_profile.IsDisposed)
            {
                _profile.Dispose();
            }
            Hide();

            //MainMenuForm waiter = new MainMenuForm();
            //waiter.Show();
        }
        private void spectateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ds = NetworkClasses.GetServerByPlayerId(NetworkClasses.GetPlayer(BoxOFriends.SelectedItems[0].Text).Tables[0].Rows[0]["Player_ID"].ToString());

            Client.Conn = ds.Tables[0].Rows[0]["Host_IP"].ToString();
            Client.NetClient.Start();
            Client.Connect(false);
            NetworkClasses.UpdateUserValue("User_List", "Online", "Spectating", User.PlayerId);
            //Dispose();
        }
        private void logoutButton_Click(object sender, EventArgs e)
        {
            _timer.Stop();
            NetworkClasses.UpdateUserValue("User_List", "Online", "Offline", User.PlayerId);
            Form login = new LoginForm();

            _friends.Dispose();
            login.Show();
            Dispose();
        }
 /// <summary>
 /// Checks if user is closing the application, closes accordingly
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void MainMenuForm_Closing(object sender, FormClosingEventArgs e)
 {
     if (e.CloseReason != CloseReason.UserClosing)
     {
         return;
     }
     _friends.Dispose();
     _timer.Stop();
     Dispose();
     NetworkClasses.UpdateUserValue("User_List", "Online", "Offline", User.PlayerId);
     Environment.Exit(0);
 }
        /// <summary>
        /// On click, starts the NetHost and takes user to the host lobby
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HostButton_Click(object sender, EventArgs e)
        {
            Host.ServerStart();
            NetworkClasses.UpdateUserValue("User_Stats", "Games_Hosted", "Games_Hosted + 1", User.PlayerId);
            NetworkClasses.UpdateUserValue("User_List", "Online", "In Lobby", User.PlayerId);
            _timer.Stop();
            Form gameList = new HostGameListForm();

            _friends.Dispose();
            gameList.Show();
            Dispose();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Deletes the server from the database, and stop the NetServer
        /// </summary>
        public static void ServerStop()
        {
            //Disconnects local Client
            Client.ClientStop();
            //Sends message to clients that the server is closing
            var outMsg = _server.CreateMessage();

            outMsg.Write((byte)PacketTypes.Closed);
            _server.SendToAll(outMsg, NetDeliveryMethod.ReliableOrdered);
            //Shuts down server and deletes it from the database
            _shouldStop = true;
            _server.Shutdown("Closed");
            NetworkClasses.DeleteServer(User.PlayerId);
            NetworkClasses.UpdateUserValue("User_List", "_Character", null, User.PlayerId);
        }
Exemplo n.º 11
0
        /// <summary>
        /// On click, resets character to null, stops NetServer, and takes user back to main menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void leaveGame_Click(object sender, EventArgs e)
        {
            _timer.Stop();
            NetworkClasses.UpdateUserValue("User_List", "_Character", null, User.PlayerId);
            NetworkClasses.UpdateUserValue("User_List", "Online", "Online", User.PlayerId);
            Host.ServerStop();
            Form form = new MainMenuForm();

            form.Show();
            _chat.Dispose();
            if (!_profile.IsDisposed)
            {
                _profile.Dispose();
            }
            Dispose();
        }
Exemplo n.º 12
0
 /// <summary>
 /// When the window is closed, the server is stopped, and the application is closed
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void HostGameListForm_Closing(object sender, FormClosingEventArgs e)
 {
     //if (e.CloseReason == CloseReason.UserClosing)
     //{
     _timer.Stop();
     _chat.Dispose();
     if (!_profile.IsDisposed)
     {
         _profile.Dispose();
     }
     Dispose();
     NetworkClasses.UpdateUserValue("User_List", "Online", "Offline", User.PlayerId);
     Host.ServerStop();
     Environment.Exit(0);
     //}
 }
Exemplo n.º 13
0
        private void gameTimer_tick(object sender, EventArgs e)
        {
            if (!Client.gameClose)
            {
                return;
            }
            _gameTimer.Stop();
            if (!_chat.IsDisposed)
            {
                _chat.Dispose();
            }
            NetworkClasses.UpdateUserValue("User_List", "Online", "Online", User.PlayerId);
            MessageBox.Show("Game Closed.", "Update", MessageBoxButtons.OK);
            Form form = new MainMenuForm();

            form.Show();
        }
        private void joinGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ds = NetworkClasses.GetServerByPlayerId(NetworkClasses.GetPlayer(BoxOFriends.SelectedItems[0].Text).Tables[0].Rows[0]["Player_ID"].ToString());

            //Console.WriteLine(ds.Tables[0].Rows[0]["Host"]);
            Client.Conn = ds.Tables[0].Rows[0]["Host_IP"].ToString();
            Client.NetClient.Start();
            Client.Connect();
            NetworkClasses.JoinServer(Client.Conn, User.PlayerId);
            //Increments games joined
            NetworkClasses.UpdateUserValue("User_Stats", "Games_Joined", "Games_Joined+1", User.PlayerId);
            NetworkClasses.UpdateUserValue("User_List", "Online", "In Lobby", User.PlayerId);
            Form form = new PlayerLobby();

            form.Show();
            Dispose();
        }
Exemplo n.º 15
0
 private void char_list_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         if (NetworkClasses.CheckCharacterAvailable(Client.Conn, char_list.SelectedItem.ToString()))
         {
             NetworkClasses.UpdateUserValue("User_List", "_Character", char_list.SelectedItem.ToString(), User.PlayerId);
         }
         else
         {
             MessageBox.Show("Character Unavailable", "Character has already been selected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
     catch (Exception)
     {
         MessageBox.Show("Invalid character", "Please choose a valid character", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 16
0
 /// <summary>
 /// Checks if user is closing the application, clsoes accordingly
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void PlayerLobby_Closing(object sender, FormClosingEventArgs e)
 {
     if (e.CloseReason != CloseReason.UserClosing)
     {
         return;
     }
     _timer.Stop();
     _chat.Dispose();
     if (!_profile.IsDisposed)
     {
         _profile.Dispose();
     }
     Dispose();
     NetworkClasses.UpdateUserValue("User_List", "_Character", null, User.PlayerId);
     NetworkClasses.UpdateUserValue("User_List", "Online", "Offline", User.PlayerId);
     NetworkClasses.FindRemovePlayer(Client.Conn, User.PlayerId);
     Client.ClientStop();
     Environment.Exit(0);
 }
Exemplo n.º 17
0
        /// <summary>
        /// Tells the server to delete it from list, stops loop and shuts down NetClient
        /// </summary>
        public static void ClientStop()
        {
            var outMsg = NetClient.CreateMessage();

            outMsg.Write((byte)PacketTypes.Leave);
            outMsg.Write(User.PlayerId);
            NetClient.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered);
            NetClient.WaitMessage(1000);
            NetClient.Shutdown("Closed");
            //ends the receive loop
            _shouldStop = true;
            Conn        = "";
            isSpectator = false;
            NetworkClasses.UpdateUserValue("User_List", "_Character", null, User.PlayerId);
            GameLoop = new Thread(Program.Run);
            if (GameLoop.IsAlive)
            {
                GameLoop.Abort();
            }
        }
 /// <summary>
 /// Enabled by selecting a server
 /// On click, joins the selected server and takes the user to the player lobby
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void join_Click(object sender, EventArgs e)
 {
     if (!join.Enabled)
     {
         return;
     }
     //Checks if user is banned
     if (NetworkClasses.GetPlayer(User.PlayerId).Tables[0].Rows[0]["IsBanned"].ToString() == "0")
     {
         if (Client.Connect())
         {
             try
             {
                 NetworkClasses.JoinServer(serverList.SelectedItems[0].SubItems[1].Text, User.PlayerId);
                 //Increments games joined
                 NetworkClasses.UpdateUserValue("User_Stats", "Games_Joined", "Games_Joined+1", User.PlayerId);
                 NetworkClasses.UpdateUserValue("User_List", "Online", "In Lobby", User.PlayerId);
                 Form lobby = new PlayerLobby();
                 lobby.Show();
                 Dispose();
             }
             catch (Exception exception)
             {
                 //if invalid, refresh the list (game may no longer exist or be in a joinable state)
                 serverList.Items.Clear();
                 ListServers();
                 Console.WriteLine(exception);
             }
         }
         else
         {
             Console.WriteLine("Couldn't Connect");
         }
     }
     else
     {
         MessageBox.Show("Can't connect to game, you have been banned. Please contact administrator to lift ban.", "Join Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 19
0
        /// <summary>
        /// Updates the list of players with the current information about the server via the database
        /// </summary>
        private void UpdateList()
        {
            var done = true;

            try
            {
                //Gets server info and places it into a dataset
                var ds = NetworkClasses.GetServer(Client.Conn);

                //Checks if game has started
                if (ds.Tables[0].Rows[0]["Status"].ToString() == "In Progress")
                {
                    NetworkClasses.UpdateUserValue("User_List", "Online", "In Game", User.PlayerId);
                    //MainMenuForm form = new MainMenuForm();
                    //form.Show();
                    //_chat.Dispose();
                    if (!_profile.IsDisposed)
                    {
                        _profile.Dispose();
                    }
                    _timer.Stop();
                    _gameTimer.Start();
                    Hide();
                    return;
                }

                var currentNumPlayers = NetworkClasses.GetNumPlayers(int.Parse(ds.Tables[0].Rows[0]["Server_ID"].ToString()));
                if (_players.Count == currentNumPlayers && _players.Count >= 1)
                {
                    //Check if characters have changed
                    var newPlayerChars = new List <DataSet>
                    {
                        NetworkClasses.GetPlayer(int.Parse(ds.Tables[0].Rows[0]["Host"].ToString()))
                    };
                    for (var i = 2; i <= 6; i++)
                    {
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["Player_" + i].ToString()))
                        {
                            newPlayerChars.Add(
                                NetworkClasses.GetPlayer(int.Parse(ds.Tables[0].Rows[0]["Player_" + i].ToString())));
                        }
                    }
                    for (var i = 0; i < newPlayerChars.Count; i++)
                    {
                        if (_players[i].Tables[0].Rows[0]["_Character"].ToString() !=
                            newPlayerChars[i].Tables[0].Rows[0]["_Character"].ToString())
                        {
                            done = false;
                            break;
                        }
                    }
                    if (done)
                    {
                        return;
                    }
                }

                //Updating
                _players.Clear();
                playerList.Items.Clear();
                var row = ds.Tables[0].Rows[0];

                var grabber = NetworkClasses.GetPlayer(int.Parse(row["Host"].ToString()));
                _players.Add(grabber);
                var Character = "";

                //Host
                var listItem = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString());
                Character = grabber.Tables[0].Rows[0]["_Character"].ToString();
                listItem.SubItems.Add(Character);

                //Add the row entry to the listview
                playerList.Items.Add(listItem);

                for (var i = 2; i <= 6; i++)
                {
                    if (!string.IsNullOrEmpty(row["Player_" + i].ToString()))
                    {
                        grabber = NetworkClasses.GetPlayer(int.Parse(row["Player_" + i].ToString()));
                        _players.Add(grabber);
                        listItem  = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString());
                        Character = grabber.Tables[0].Rows[0]["_Character"].ToString();
                        listItem.SubItems.Add(Character);
                        playerList.Items.Add(listItem);
                    }
                }
            }
            catch (Exception e) //Thrown if server no longer exists
            {
                Console.WriteLine(e);
                //If the host leaves, the server no longer exists and the removing the player will throw an exception
                try
                {
                    NetworkClasses.FindRemovePlayer(Client.Conn, User.PlayerId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                _chat.Dispose();
                Form form = new MainMenuForm();
                form.Show();
                _timer.Stop();
                Client.ClientStop();

                NetworkClasses.UpdateUserValue("User_List", "_Character", null, User.PlayerId);
                NetworkClasses.UpdateUserValue("User_List", "Online", "Online", User.PlayerId);
                Dispose();
                MessageBox.Show("Host left the game", "Server Disconnected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }