public Form1(LoginResultDTO opptInfo) { this._opptInfo = opptInfo; InitializeComponent(); _connector = ServerConnector.GetInstance(); _connector.OnConnectedEvent += ConnectedCallBack; _connector.OnRecivedMessageEvent += RecivedMessage; _connector.OnDisconnectedEvent += DisconnectedCallBack; Control.CheckForIllegalCrossThreadCalls = false; EnableGameUi(false); _playerIndex = 0; _opptNick = ""; }
private void RecivedMessage(byte[] data) { var tmp = new byte[data.Length - 4]; var lengthByte = new byte[4]; Array.Copy(data, 0, lengthByte, 0, 4); Array.Copy(data, 4, tmp, 0, tmp.Length); Stream stream = new MemoryStream(tmp); MobileSuiteModel msm = ProtoBuf.Serializer.Deserialize<MobileSuiteModel>(stream); switch (msm.type) { case (int)MessageType.MSG_TYPE_LOGIN_RES: if (msm.message == null) { MessageBox.Show("无此用户"); return; } stream = new MemoryStream(msm.message); LoginResultDTO loginResult = ProtoBuf.Serializer.Deserialize<LoginResultDTO>(stream); txtIn.Text = loginResult.uName; _playerInfo = loginResult; EnableGameUi(true); break; case (int)MessageType.MSG_TYPE_LOGOUT_RES: case (int)MessageType.MSG_TYPE_END_GAME_RES: MessageBox.Show("玩家退出了游戏"); break; case (int)MessageType.MSG_TYPE_CREATE_USER_RES: stream = new MemoryStream(msm.message); CreateResultDTO createResult = ProtoBuf.Serializer.Deserialize<CreateResultDTO>(stream); lbInfo.Items.Add(createResult.userId); txtIn.Text = createResult.userId.ToString(CultureInfo.InvariantCulture); break; case (int)MessageType.MSG_TYPE_CHAT_MESSAGE_RES: stream = new MemoryStream(msm.message); ChatMsg chat = ProtoBuf.Serializer.Deserialize<ChatMsg>(stream); lbInfo.Items.Add(chat.chatContext); break; case (int)MessageType.MSG_TYPE_START_RES: stream = new MemoryStream(msm.message); GameStartDTO gsDto = ProtoBuf.Serializer.Deserialize<GameStartDTO>(stream); _playerIndex = gsDto.playerIndex; _opptNick = gsDto.opptName; //StartGame(); Start = new StartNewGameDelegate(StartGame); Invoke(Start); break; case (int)MessageType.MSG_TYPE_LINE_A_POINT_TO_REQUEST_RES: break; case (int)MessageType.MSG_TYPE_LINE_A_POINT_RES: stream = new MemoryStream(msm.message); LineAPointDTO lpDto = ProtoBuf.Serializer.Deserialize<LineAPointDTO>(stream); int result = _game.Line(lpDto.row, lpDto.col, lpDto.playerIndex); String btnName = String.Format("{0}_{1}", lpDto.row, lpDto.col); Button currentButton = null; foreach (Control c in Controls) { if (c.Name == btnName) { currentButton = (Button)c; break; } } if (result == 0 && lpDto.playerIndex == 1) { Color color = Color.FromArgb(255, 0, 0); currentButton.BackColor = color; } else if (result == 0) { Color color = Color.FromArgb(0, 0, 255); currentButton.BackColor = color; } updateState(); if (_game.GameState == 2) { MessageBox.Show("游戏结束"); _isEnd = true; } break; } }