private void CheckStartGame() { foreach (var p in room.Player_List) { Console.WriteLine(p.Player_name + " " + p.Status); if (!p.Status) { return; } } try { Command c = new Command(CommandCode.Start_Game, room); byte[] data2 = c.Serialize(); for (int i = 0; i < tcpServerClient.Count; i++) { try { if (i == Player_Index) { continue; } tcpServerClient[i].GetStream().Write(data2, 0, data2.Length); } catch { } } } catch { } ScreenEvent.Invoke(this, new SivEventArgs(1)); }
private void CharacterDistribute() { for (int i = 0; i < (tcpServerClient.Count); i++) { List <Character> listTemp = new List <Character>(); bool flag = true; while (flag) { listTemp.Clear(); for (int j = 0; j < 7; j++) { playerRandomChar[0] = rand.Next(characterList.Count); listTemp.Add(characterList[playerRandomChar[0]]); characterList.RemoveAt(playerRandomChar[0]); } for (int j = 0; j < (listTemp.Count - 1); j++) { if (listTemp[j].CharType != listTemp[j + 1].CharType) { flag = false; break; } } if (flag) { for (int j = 0; j < (listTemp.Count - 1); j++) { characterList.Add(listTemp[j]); } } else { break; } } //for (int k = 0; k < listTemp.Count; k++) //{ // Console.WriteLine(listTemp[k].CharName + " " + characterList[0].MaxHealth); //} try { Command c = new Command(CommandCode.Character_Distribute, listTemp); byte[] data = c.Serialize(); if (i == Player_Index) { characterPlayerList = listTemp; UpdateSelectImageList(); } else { tcpServerClient[i].GetStream().Write(data, 0, data.Length); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private void ServerRespond() { while (synchronizeRun) { Command c = new Command(CommandCode.Standby); byte[] data = c.Serialize(); int Player_Index = room.findByID(Player_ID); bool update_room = false; for (int i = 0; i < tcpServerClient.Count; i++) { try { if (i == Player_Index) { continue; } tcpServerClient[i].GetStream().Write(data, 0, data.Length); } //couldn't send message because the client has disconnected //so we remove that tcp client and player from the list catch (Exception e) { Console.WriteLine(e.Message); string s = room.Player_List[i].Player_name + " had left the room!" + "\n"; this.tcpServerClient.RemoveAt(i); this.room.Player_List.RemoveAt(i); i--; UpdateRoom(); //room.Player_List[i].Status = false; //UpdateRoom(room.findByID_ExcludeMainPlayer(room.Player_List[i].id, Player_ID), i); update_room = true; //SendChatMessage(s); } } if (update_room) { Command c2 = new Command(CommandCode.Update_Room, room); byte[] data2 = c2.Serialize(); for (int i = 0; i < tcpServerClient.Count; i++) { try { if (i == Player_Index) { continue; } tcpServerClient[i].GetStream().Write(data2, 0, data2.Length); } catch { } } } Thread.Sleep(1000); } }
private void ClientJoin() { Command c = new Command(CommandCode.Join_Game, room.Player_List.Last()); byte[] data = c.Serialize(); tcpClient = new TcpClient(room.Player_List[room.owner_index].Address, 51002); networkStream = tcpClient.GetStream(); networkStream.Write(data, 0, data.Length); tcpClient.Close(); }
private void Cancel_button_clicked(object sender, FormEventData e) { try { Command c = new Command(CommandCode.Cancel, this.Player_ID); byte[] data = c.Serialize(); tcpClient = new TcpClient(room.Player_List[room.owner_index].Address, 51002); networkStream = tcpClient.GetStream(); networkStream.Write(data, 0, data.Length); tcpClient.Close(); } catch { } }
private void RequestJoin(Room room) { Command c = new Command(CommandCode.Can_I_Join); byte[] data = c.Serialize(); try { tcpClient = new TcpClient(room.Player_List[room.owner_index].Address, 51002); networkStream = tcpClient.GetStream(); networkStream.Write(data, 0, data.Length); Byte[] bytes = new Byte[1024 * 16]; DateTime dt = DateTime.Now; while (true) { int i; while ((i = networkStream.Read(bytes, 0, bytes.Length)) != 0) { Command c2 = new Command(bytes); if (c2.Command_Code == CommandCode.OK_to_Join) { tcpClient.Close(); Player player = new Player(playerName.Text, Game1.getLocalIP()); room.Player_List.Add(player); ScreenEvent.Invoke(this, new SivEventArgs(4, room)); return; } else if (c2.Command_Code == CommandCode.Cant_Join_Room_Full) { tcpClient.Close(); Game1.MessageBox(new IntPtr(0), "Can't join, the room is full.", "Can not Join", 0); return; } } if ((DateTime.Now - dt) > new TimeSpan(0, 0, 3)) { tcpClient.Close(); Game1.MessageBox(new IntPtr(0), "Host is not responding", "Can not Join", 0); return; } } } catch (Exception ex) { Console.WriteLine(ex.Message); Game1.MessageBox(new IntPtr(0), ex.Message, "Can not Join", 0); } }
private void SelectCharacter() { NetworkStream networkStream2; TcpClient tcpClient2; if (isHost()) { try { int Player_Index = room.findByID(Player_ID); room.Player_List[Player_Index].Status = true; Command c = new Command(CommandCode.Update_Room, this.room); byte[] data2 = c.Serialize(); for (int i = 0; i < tcpServerClient.Count; i++) { if (i == Player_Index) { continue; } tcpServerClient[i].GetStream().Write(data2, 0, data2.Length); } UpdateRoom(); CheckStartGame(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { try { int Player_Index = room.findByID(Player_ID); Command c = new Command(CommandCode.Character_Select, room.Player_List[Player_Index].Character1, room.Player_List[Player_Index].Character2, this.Player_ID); byte[] data = c.Serialize(); tcpClient2 = new TcpClient(room.Player_List[room.owner_index].Address, 51002); networkStream2 = tcpClient2.GetStream(); networkStream2.Write(data, 0, data.Length); tcpClient2.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private void ChatBox_EnterPressed(object sender) { string s = room.findPlayerByID(Player_ID).Player_name + ": " + chat.Text + "\n"; if (room.isHost(Player_ID)) { SendChatMessage(s); } else { Command c = new Command(CommandCode.Chat_Message, s); byte[] data = c.Serialize(); tcpClient = new TcpClient(room.Player_List[room.owner_index].Address, 51002); networkStream = tcpClient.GetStream(); networkStream.Write(data, 0, data.Length); tcpClient.Close(); } this.chat.Text = ""; }
private void SendChatMessage(string message) { Command c = new Command(CommandCode.Chat_Message, message); byte[] data = c.Serialize(); for (int i = 0; i < tcpServerClient.Count; i++) { try { if (room.Player_List[i].id == this.Player_ID) { continue; } tcpServerClient[i].GetStream().Write(data, 0, data.Length); } catch { } } chatDisplay.Text += message; }
private void Start_button_clicked(object sender, FormEventData e) { bool isAllReady = true; foreach (Player p in room.Player_List) { if (!p.Status) { isAllReady = false; break; } } if (isAllReady) { try { int Player_Index = room.findByID(Player_ID); Command c = new Command(CommandCode.Start_Game, room); byte[] data2 = c.Serialize(); for (int i = 0; i < tcpServerClient.Count; i++) { try { if (i == Player_Index) { continue; } tcpServerClient[i].GetStream().Write(data2, 0, data2.Length); } catch { } } } catch { } ScreenEvent.Invoke(this, new SivEventArgs(1)); } else { Game1.MessageBox(new IntPtr(0), "All Players in this room are not ready!", "Warning!", 0); } }
private void ServerReceiver() { Byte[] bytes = new Byte[1024 * 16]; while (receiverRun) { if (StoppedTcp) { break; } else if (receiveTcp.Pending()) { TcpClient client = receiveTcp.AcceptTcpClient(); NetworkStream stream = client.GetStream(); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { Command c = new Command(bytes); if (c.Command_Code == CommandCode.Chat_Message) { //SendChatMessage(c.Message); } else if (c.Command_Code == CommandCode.Update_Room) { //Room room = (Room)c.Data1; //int Player_Index = room.findByID(Player_ID); //for (i = 0; i < room.Player_List.Count; i++) //{ // if (i == Player_Index) continue; // if (!room.Player_List[i].Status) // { // //UpdateRoom(room.findByID_ExcludeMainPlayer(room.Player_List[i].id, Player_ID), Player_Index); // } //} } else if (c.Command_Code == CommandCode.Character_Select) { Character character1 = (Character)c.Data1; Character character2 = (Character)c.Data2; Guid id = (Guid)c.Data3; int playerIndex = room.findByID(id); room.Player_List[playerIndex].Character1 = character1; room.Player_List[playerIndex].Character2 = character2; room.Player_List[playerIndex].Status = true; UpdateRoom(); int Player_Index = room.findByID(Player_ID); Command c2 = new Command(CommandCode.Update_Room, room); byte[] data2 = c2.Serialize(); for (int j = 0; j < tcpServerClient.Count; j++) { try { if (j == Player_Index) { continue; } tcpServerClient[j].GetStream().Write(data2, 0, data2.Length); } catch { } } CheckStartGame(); } } client.Close(); } } }
private void ServerReceiver() { Byte[] bytes = new Byte[1024 * 16]; Player _player = null; while (receiverRun) { if (StoppedTcp) { break; } else if (receiveTcp.Pending()) { TcpClient client = receiveTcp.AcceptTcpClient(); _player = null; NetworkStream stream = client.GetStream(); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { Command c = new Command(bytes); if (c.Command_Code == CommandCode.Standby) { //do nothing } if (c.Command_Code == CommandCode.Can_I_Join) { Command temp_c; if (room.Player_List.Count >= room.Number_of_Player) { temp_c = new Command(CommandCode.Cant_Join_Room_Full); } else { temp_c = new Command(CommandCode.OK_to_Join); } byte[] data = temp_c.Serialize(); stream.Write(data, 0, data.Length); } if (c.Command_Code == CommandCode.Join_Game) { _player = (Player)c.Data1; _player.Address = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); bool found = false; foreach (Player p in room.Player_List) { if (p.Address == _player.Address && p.id == _player.id) { found = true; break; } } bool update_room = false; if (!found) { this.room.Player_List.Add(_player); this.tcpServerClient.Add(new TcpClient(_player.Address, 51003)); this.UpdateRoom(); SendChatMessage(_player.Player_name + " had joined the room!" + "\n"); update_room = true; } if (update_room) { int Player_Index = room.findByID(Player_ID); Command c2 = new Command(CommandCode.Update_Room, room); byte[] data2 = c2.Serialize(); for (int i2 = 0; i2 < tcpServerClient.Count; i2++) { try { if (i2 == Player_Index) { continue; } tcpServerClient[i2].GetStream().Write(data2, 0, data2.Length); } catch { } } } } else if (c.Command_Code == CommandCode.Chat_Message) { SendChatMessage(c.Message); } else if (c.Command_Code == CommandCode.Ready) { string ipPlayer = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); foreach (Player p in room.Player_List) { if (p.Address == ipPlayer && p.id == (Guid)c.Data1) { p.Status = true; break; } } int Player_Index = room.findByID(Player_ID); Command c2 = new Command(CommandCode.Update_Room, room); byte[] data2 = c2.Serialize(); for (int j = 0; j < tcpServerClient.Count; j++) { try { if (j == Player_Index) { continue; } tcpServerClient[j].GetStream().Write(data2, 0, data2.Length); } catch { } } this.UpdateRoom(); } else if (c.Command_Code == CommandCode.Cancel) { string ipPlayer = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); foreach (Player p in room.Player_List) { if (p.Address == ipPlayer && p.id == (Guid)c.Data1) { p.Status = false; break; } } int Player_Index = room.findByID(Player_ID); Command c2 = new Command(CommandCode.Update_Room, room); byte[] data2 = c2.Serialize(); for (int j = 0; j < tcpServerClient.Count; j++) { try { if (j == Player_Index) { continue; } tcpServerClient[j].GetStream().Write(data2, 0, data2.Length); } catch { } } this.UpdateRoom(); } } client.Close(); } } }