public Packet HandlePacket(Packet packetReceived, Client mClient) { switch (packetReceived.type) { case PacketType.CHATMESSAGE: ChatMessagePacket pMessage = (ChatMessagePacket)packetReceived; if (pMessage.recipient == "server") { SendMessageAll(mClient.username + " - " + pMessage.message); } else { CreateMessage(mClient.username + " whispers to you: " + pMessage.message, pMessage.recipient); } break; case PacketType.NICKNAME: NickNamePacket pNick = (NickNamePacket)packetReceived; mClient.username = pNick.nickName; nicknameList.Add(mClient.username); SendClientList(nicknameList); break; case PacketType.ENDPOINT: LoginPacket pLogin = (LoginPacket)packetReceived; localEP = pLogin.endPoint; mClient.UDPSendLocalEP(localEP); Thread t = new Thread(new ParameterizedThreadStart(UDPClientMethod)); t.Start(mClient); break; case PacketType.GAMEREQ: GameRequestPacket pGameReqPacket = (GameRequestPacket)packetReceived; int packetState = (int)pGameReqPacket.requestState; if (packetState == 0) { CheckUserExists(clientList, pGameReqPacket.sender, pGameReqPacket); } else if (packetState == 1) { if (CheckGameAccepted(Games, pGameReqPacket.sender) == false) { GameLogic game = new GameLogic(pGameReqPacket.recipient, pGameReqPacket.sender); Games.Add(game); CheckUserExists(clientList, pGameReqPacket.sender, pGameReqPacket); CheckUserExists(clientList, pGameReqPacket.recipient, pGameReqPacket); } else { packetReceived = new Packet(); } } else if (packetState == 2) { CheckUserExists(clientList, pGameReqPacket.sender, pGameReqPacket); } break; case PacketType.GAME: GamePacket pGamePacket = (GamePacket)packetReceived; int gameNumber = CheckGameExists(Games, pGamePacket.sender); if (gameNumber == -1) { break; } else if (Games[gameNumber].CheckMove(pGamePacket.x, pGamePacket.y)) { //Need to send a nought and a cross together if (pGamePacket.text == Games[gameNumber].Player1) { SendGamePacket(pGamePacket.sender, pGamePacket.recipient, pGamePacket.x, pGamePacket.y, pGamePacket.value, "X"); SendGamePacket(pGamePacket.recipient, pGamePacket.sender, pGamePacket.x, pGamePacket.y, pGamePacket.value, "O"); } else if (pGamePacket.text == Games[gameNumber].Player2) { SendGamePacket(pGamePacket.recipient, pGamePacket.sender, pGamePacket.x, pGamePacket.y, pGamePacket.value, "O"); SendGamePacket(pGamePacket.sender, pGamePacket.recipient, pGamePacket.x, pGamePacket.y, pGamePacket.value, "X"); } } break; } return(packetReceived); }
private void HandlePacket(Packet packetFromServer) { switch (packetFromServer.type) { case PacketType.GAME: GamePacket pGamePacket = (GamePacket)packetFromServer; int selection = UpdateButtons(pGamePacket.x, pGamePacket.y); switch (selection) { case 1: noughtsAndCrosses.button1.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button1.Text = pGamePacket.text; noughtsAndCrosses.button1.Enabled = false; })); break; case 2: noughtsAndCrosses.button2.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button2.Text = pGamePacket.text; noughtsAndCrosses.button2.Enabled = false; })); break; case 3: noughtsAndCrosses.button3.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button3.Text = pGamePacket.text; noughtsAndCrosses.button3.Enabled = false; })); break; case 4: noughtsAndCrosses.button4.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button4.Text = pGamePacket.text; noughtsAndCrosses.button4.Enabled = false; })); break; case 6: noughtsAndCrosses.button6.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button6.Text = pGamePacket.text; noughtsAndCrosses.button6.Enabled = false; })); break; case 7: noughtsAndCrosses.button7.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button7.Text = pGamePacket.text; noughtsAndCrosses.button7.Enabled = false; })); break; case 8: noughtsAndCrosses.button8.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button8.Text = pGamePacket.text; noughtsAndCrosses.button8.Enabled = false; })); break; case 9: noughtsAndCrosses.button9.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button9.Text = pGamePacket.text; noughtsAndCrosses.button9.Enabled = false; })); break; case 10: noughtsAndCrosses.button10.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button10.Text = pGamePacket.text; noughtsAndCrosses.button10.Enabled = false; })); break; } break; case PacketType.CHATMESSAGE: ChatMessagePacket messagePacket = (ChatMessagePacket)packetFromServer; _form.UpdateChatWindow(messagePacket.message); break; case PacketType.NICKNAME: NickNamePacket nicknamePacket = (NickNamePacket)packetFromServer; _form.UpdateChatWindow(nicknamePacket.nickName); break; case PacketType.ENDPOINT: LoginPacket serverLogInDetails = (LoginPacket)packetFromServer; _udpClient.Connect((IPEndPoint)serverLogInDetails.endPoint); _udpReaderThread = new Thread(UDPRead); _udpReaderThread.Start(); break; case PacketType.USERLIST: UserListPacket userListPacket = (UserListPacket)packetFromServer; nicknameList = userListPacket.userList; _form.UpdateClientListBox(userListPacket.userList); break; case PacketType.GAMEREQ: GameRequestPacket gameReqPacket = (GameRequestPacket)packetFromServer; int packetState = (int)gameReqPacket.requestState; switch (packetState) { case 0: _form.UpdateChatWindow("Game was declined!"); break; case 1: gameRecipient = gameReqPacket.recipient; gameSender = gameReqPacket.sender; _form.UpdateChatWindow("Game accepted!"); RequestGame(gameReqPacket.sender, 1, gameReqPacket.recipient); noughtsAndCrosses.Text = _form.clientNickName; noughtsAndCrosses.ShowDialog(); break; case 2: _form.CreateMessageBox(gameReqPacket.sender, gameReqPacket.recipient); break; } break; } }