public void SendRequest(int boardNum, bool challengerGoesFirst) { OutgoingRequest = new NetGameRequestEventArgs( boardNum, challengerGoesFirst ); Send(Packet.Create(NetMessageType.GameRequest, boardNum, challengerGoesFirst)); }
public void Receive(Packet packet) { if (Seen(packet)) { return; } seenTokens.Add(packet.Token); string result = packet.Data.Aggregate("", (current, data) => current + (" " + data)); TraceHelper.WriteLine("{2}: {0}: {1}", packet.Type, result, packet.Token); switch (packet.Type) { case NetMessageType.InLobby: bool newLobby = (bool)packet.Data[0]; if (InLobby == newLobby) { break; } InLobby = newLobby; if (OnLobbyUpdate != null) { OnLobbyUpdate(this, new LobbyEventArgs(InLobby)); } break; case NetMessageType.Chat: if (OnChatMessage != null) { OnChatMessage(this, new NetChatEventArgs((string)packet.Data[0])); } break; case NetMessageType.Name: string newName = (string)packet.Data[0]; if (Name == newName) { break; } Name = newName; if (OnNameUpdate != null) { OnNameUpdate(this, new NetNameEventArgs(Name)); } break; case NetMessageType.GameRequest: IncomingRequest = new NetGameRequestEventArgs( (int)packet.Data[0], (bool)packet.Data[1] ); if (OnGameRequest != null) { OnGameRequest(this, IncomingRequest); } break; case NetMessageType.GameResponse: if (OnGameResponse != null) { bool challengeAccepted = (bool)packet.Data[0]; NetGameResponseEventArgs response; if (OutgoingRequest == null) { response = new NetGameResponseEventArgs(0, true, challengeAccepted); } else { response = new NetGameResponseEventArgs( OutgoingRequest.BoardNum, OutgoingRequest.ChallengerGoesFirst, challengeAccepted ); } OnGameResponse(this, response); } break; case NetMessageType.PlayerMove: if (OnPlayerMove != null) { OnPlayerMove(this, new NetPlayerMoveEventArgs( (Move)packet.Data[0] )); } break; case NetMessageType.Leave: if (OnPlayerLeave != null) { OnPlayerLeave(this, new EventArgs()); } break; } }