private void banPlayer_Click(object sender, EventArgs e) { var n = -1; //Checks if user is admin if (NetworkClasses.GetPlayer(User.PlayerId).Tables[0].Rows[0]["IsAdmin"].ToString() == "1") { //Checks for empty textbox and if the value is an int if (banPlayerText.TextLength > 0 && int.TryParse(banPlayerText.Lines[0], out n)) { //Bans the selected player id if (NetworkClasses.BanPlayer(n)) { MessageBox.Show("Successfully Banned Player ID: " + banPlayerText.Lines[0], "Player Banned", MessageBoxButtons.OK, MessageBoxIcon.Information); banPlayerText.Lines[0] = ""; } else { MessageBox.Show("Invalid Player ID", "Ban Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("Player ID must be numeric.", "Ban Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("You are not an admin, contact an admin for privileges.", "Admin 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 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); } }
public void GetFriends() { var playerFriends = NetworkClasses.GetPlayer(User.PlayerId).Tables[0].Rows[0]["Friends"].ToString().Split(','); if (playerFriends[0] == "0") { if (BoxOFriends.Items.Count == 1) { BoxOFriends.Items.Clear(); delFriend.Enabled = false; addToolStripMenuItem.Visible = true; viewProfileToolStripMenuItem.Visible = false; deleteToolStripMenuItem.Visible = false; joinGameToolStripMenuItem.Visible = false; spectateToolStripMenuItem.Visible = false; _friendInfo = new List <DataSet>(); } return; } var datasets = playerFriends.Select(friend => NetworkClasses.GetPlayer(int.Parse(friend))).ToList(); if (datasets.Count != _friendInfo.Count) { BoxOFriends.Items.Clear(); foreach (var player in datasets) { var listItem = new ListViewItem(player.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(player.Tables[0].Rows[0]["Online"].ToString()); BoxOFriends.Items.Add(listItem); } _friendInfo = datasets; return; } for (var i = 0; i < datasets.Count; i++) { if (datasets[i].Tables[0].Rows[0]["Online"].ToString() == _friendInfo[i].Tables[0].Rows[0]["Online"].ToString()) { continue; } BoxOFriends.Items.Clear(); foreach (var player in datasets) { var listItem = new ListViewItem(player.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(player.Tables[0].Rows[0]["Online"].ToString()); BoxOFriends.Items.Add(listItem); } _friendInfo = datasets; if (BoxOFriends.Items.Count == 10) { addToolStripMenuItem.Visible = false; addFriend.Enabled = false; } return; } }
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(); }
/// <summary> /// Updates the form view with the current list of servers in the database /// </summary> private void ListServers() { var ds = NetworkClasses.GetServers(); foreach (DataRow row in ds.Tables[0].Rows) { var grabber = NetworkClasses.GetPlayer(int.Parse(row["Host"].ToString())); var listItem = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(grabber.Tables[0].Rows[0]["Local_IP"].ToString()); listItem.SubItems.Add(NetworkClasses.GetNumPlayers(int.Parse(row["Server_ID"].ToString())) + "/6"); listItem.SubItems.Add(row["Status"].ToString()); //Add the row entry to the listview serverList.Items.Add(listItem); } }
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(); }
/// <summary> /// Updates the list of players with the current information about the server via the database /// </summary> private void UpdateList() { playerList.Items.Clear(); try { var ds = NetworkClasses.GetServer(Client.Conn); var row = ds.Tables[0].Rows[0]; var grabber = NetworkClasses.GetPlayer(int.Parse(row["Host"].ToString())); //Host var listItem = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(grabber.Tables[0].Rows[0]["_Character"].ToString()); //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())) { continue; } grabber = NetworkClasses.GetPlayer(int.Parse(row["Player_" + i].ToString())); listItem = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(grabber.Tables[0].Rows[0]["_Character"].ToString()); playerList.Items.Add(listItem); } } catch (Exception) //Thrown if server no longer exists { Form form = new MainMenuForm(); form.Show(); _timer.Stop(); Client.ClientStop(); NetworkClasses.UpdateCharacter(User.PlayerId, null); MessageBox.Show("Host left the game", "Server Disconnected", MessageBoxButtons.OK, MessageBoxIcon.Error); Dispose(); } }
/// <summary> /// Updates the list of players with the current information about the server via the database /// </summary> private void UpdateList() { //Resets the view playerList.Items.Clear(); //Gets server info and puts it into a dataset var ds = NetworkClasses.GetServer(User.PlayerId, User.LocalIp); var row = ds.Tables[0].Rows[0]; var grabber = NetworkClasses.GetPlayer(int.Parse(row["Host"].ToString())); //Gets ping values for all players var pings = new List <int>(); while (pings.Count < 1) { pings = Host.GetPing(); } //Host var listItem = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(grabber.Tables[0].Rows[0]["_Character"].ToString()); listItem.SubItems.Add(pings[0].ToString() + " ms"); //Add the clients to the listview playerList.Items.Add(listItem); for (var i = 2; i <= 6; i++) { if (string.IsNullOrEmpty(row["Player_" + i].ToString())) { continue; } grabber = NetworkClasses.GetPlayer(int.Parse(row["Player_" + i].ToString())); listItem = new ListViewItem(grabber.Tables[0].Rows[0]["Username"].ToString()); listItem.SubItems.Add(grabber.Tables[0].Rows[0]["_Character"].ToString()); listItem.SubItems.Add(pings[i - 1].ToString() + " ms"); playerList.Items.Add(listItem); } }
/// <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); } }
/// <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.UpdateServerStatus("In Progress", User.PlayerId); LobbyController.StartGame(); Host.StartGame(); }
/// <summary> /// Updates the list of players with the current information about the server via the database /// </summary> private void UpdateList() { var done = true; //Gets server info and puts it into a dataset var ds = NetworkClasses.GetServer(User.PlayerId, User.LocalIp); 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 clients to the listview playerList.Items.Add(listItem); for (var i = 2; i <= 6; i++) { if (string.IsNullOrEmpty(row["Player_" + i].ToString())) { continue; } 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); } }
/// <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); } }
public Options() { InitializeComponent(); groupBox2.Visible = (NetworkClasses.GetPlayer(User.PlayerId).Tables[0].Rows[0]["IsAdmin"].ToString() == "1"); }