private void InitGame(MsgSys msg) { // 初始化所有玩家的状态 int[] gameStart = (int[])msg.sysContent; StartPlayerShow(gameStart); myLogic.Enable(); }
public void StartGame()//发送游戏开始数据 { if (keepalive) { MsgSys sysMsg = new MsgSys(); sysMsg.sysType = MsgSysType.GameStart; sysMsg.sysContent = null; Message conn = new Message(sysMsg); userSend(conn); } }
public void UserReady(int total)//发送用户准备请求数据 { if (keepalive) { MsgSys sysMsg = new MsgSys(); sysMsg.sysType = MsgSysType.Ready; sysMsg.sysContent = total; Message conn = new Message(sysMsg); userSend(conn); } }
public void CloseConn() { if (keepalive) { MsgSys sysMsg = new MsgSys(); sysMsg.sysType = MsgSysType.Offline; sysMsg.sysContent = myLogic.myClientName; userSend(new Message(sysMsg)); receiveThread = null; keepalive = false; } }
// // // 处理房间中的逻辑 // // private void SendPlayerSitDown(int tableIdx, int seatIdx) { this.tableIdx = tableIdx; this.seatIdx = seatIdx; MsgSys seatMsg = new MsgSys(); seatMsg.sysType = MsgSysType.Seat; seatMsg.sysContent = new int[] { tableIdx, seatIdx }; Message msg = new Message(seatMsg); userSend(msg); }
// // 网络通信的功能 // public bool ConnectNet(Message msg) { if (myClientSoc.connected == false) { myClientSoc.InitialSoc(); // 如果网络连接失败 if (myClientSoc.connected == false) { return(false); } } userSend(msg); Message serverMsg = myClientSoc.RecieveMsg(); if (serverMsg.msgType == MsgType.Sys) { MsgSys loginMsg = (MsgSys)serverMsg.msgContent; if (loginMsg.sysType == MsgSysType.Judge) { bool ret = (bool)loginMsg.sysContent; // 登录失败 直接返回 if (ret != true) { MessageBox.Show("登录失败!"); return(false); } // 登录成功 keepalive = true; myStatus = PlayerStatus.OnLine; } else { // 如果登录不成功,主动结束socket // 下次会重新连接。 myClientSoc.CloseConn(); return(false); } } this.myStatus = PlayerStatus.OnLine; return(true); }
public bool PlayerLogin(string user, string pwd) { // 现在do nothing,任何登录都能成功 // ConnectNet() always return true; MsgSys sysMsg = new MsgSys(); sysMsg.sysType = MsgSysType.Login; string[] user_pwd = new string[2] { user, EncodePwd(pwd) }; sysMsg.sysContent = user_pwd; if (ConnectNet(new Message(sysMsg))) { myLogic.myClientName = user; // 登录成功 keepalive = true; //启动后台线程接受服务器端发送的消息 if (receiveThread == null) { receiveThread = new Thread(new ThreadStart(NetRuning)); receiveThread.Start(); msgProcessor = new Thread(new ThreadStart(MsgProcessor)); msgProcessor.Start(); } // // 准备发送一个online的命令,服务器把当前所有的table的情况发回来。 MsgSys s = new MsgSys(); s.sysType = MsgSysType.Online; s.sysContent = myLogic.myClientName; Message conn = new Message(s); conn.userSender = myLogic.myClientName; myClientSoc.SendMsg(conn); return(true); } return(false); }
// // 玩家退出游戏房间 // public void QuitGameArea() { // 模拟再次登录 //SendPlayerSitDown(this.tableIdx, this.seatIdx); MsgSys seatMsg = new MsgSys(); seatMsg.sysType = MsgSysType.LeaveRoom; seatMsg.sysContent = new int[] { tableIdx, seatIdx }; Message msg = new Message(seatMsg); userSend(msg); foreach (Logic lg in otherPlayersLogic) { lg.UserQuit(); lg.HideArea(); } // 然后将当前的table信息清空 this.tableIdx = -1; this.seatIdx = -1; }
private void ProcessSysMsg(Message _sysMsg) { MsgSys sysMsg = (MsgSys)_sysMsg.msgContent; switch (sysMsg.sysType) { case MsgSysType.Judge: //登录请求响应 { bool judged = (bool)sysMsg.sysContent; if (!judged) { MessageBox.Show("登录失败!"); myClientSoc.CloseConn(); } if (judged) { myStatus = PlayerStatus.OnLine; // myLogic.myClientName = judged[1]; MsgSys s = new MsgSys(); s.sysType = MsgSysType.Online; s.sysContent = myLogic.myClientName; Message conn = new Message(s); userSend(conn); } } break; case MsgSysType.Join: //有玩家上线 { //某玩家加入,大厅中在线列表更新 string userName = (string)sysMsg.sysContent; if (!userName.Equals(myLogic.myClientName)) { hall.AddOnePlayers(userName); } // Logic lg = new Logic(2); // lg.myClientName = userName; // otherPlayersLogic.Add(new Logic(2)); } break; case MsgSysType.List: //在线玩家列表 { //在线玩家列表 List <string> userList = (List <string>)sysMsg.sysContent; hall.AddPlayers(userList); } break; case MsgSysType.Exit: { //某玩家退出 string userName = (string)sysMsg.sysContent; //MessageBox.Show("玩家-" + userName + "-退出!"); hall.RemovePlayers(userName); } break; case MsgSysType.CanStart: { //通知 全部准备好 List <string> userList = (List <string>)sysMsg.sysContent; //所有准备好的玩家的名字 for (int i = 0; i < userList.Count; i++) { if (userList[i] != myLogic.myClientName) { Logic lg = new Logic(2); lg.myClientName = userList[i]; otherPlayersLogic.Add(new Logic(2)); } } if (userList[0] == myLogic.myClientName) { MsgSys msgStart = new MsgSys(); msgStart.sysType = MsgSysType.GameStart; msgStart.sysContent = null; } } break; case MsgSysType.Begin: { InitGame(sysMsg); //游戏开始 } break; case MsgSysType.NewRound: //完成一幅新的一轮开始 { string userRefresh = (string)_sysMsg.userSender; int[] gameStatus = (int[])sysMsg.sysContent; if (userRefresh == myLogic.myClientName) { myLogic.InitGame(gameStatus); } else { foreach (Logic lg in otherPlayersLogic) { if (lg.myClientName == userRefresh) { lg.InitGame(gameStatus); break; } } } } break; case MsgSysType.Seat: //游戏座位响应 { int[] seatInfo = (int[])sysMsg.sysContent; string userSender = (string)_sysMsg.userSender; //System.Console.WriteLine("seat {0} {1} {2}", userSender, seatInfo[0], seatInfo[1]); hall.PlayerSeatDown(seatInfo[0], seatInfo[1], userSender); // 如果当前桌子有人发来信息,且不是本人说明有后来的玩家进入 // 则更新先进入者的信息 if (userSender != myLogic.myClientName && seatInfo[0] == this.tableIdx) { int otherLogicPos = (seatInfo[1] - seatIdx + 4) % 4 - 1; otherPlayersLogic[otherLogicPos].SetPlayer(userSender); } } break; // 通知玩家有人进入某房间 case MsgSysType.LeaveRoom: { int[] seatInfo = (int[])sysMsg.sysContent; string userSender = (string)_sysMsg.userSender; hall.PlayerLeaveTable(seatInfo[0], seatInfo[1], userSender); if (userSender != myLogic.myClientName && seatInfo[0] == this.tableIdx) { int otherLogicPos = (seatInfo[1] - seatIdx + 4) % 4 - 1; otherPlayersLogic[otherLogicPos].UserQuit(); } } break; case MsgSysType.ReadyMsg: { //gameRoom.AddSysMsg(userSender); } break; // 此消息是为了通知其他玩家,某个房间已经开始游戏,不能再坐下去了 case MsgSysType.GameOn: { int tableIdx = (int)sysMsg.sysContent; hall.TableGameOn(tableIdx); } break; // 此消息是为了通知其他玩家,某个房间已经结束游戏,可以继续参与了 case MsgSysType.GameOver: { int tableIdx = (int)sysMsg.sysContent; hall.TableGameOver(tableIdx); string user = _sysMsg.userSender; if (tableIdx == this.tableIdx) { myLogic.gameArea.UnGameStatus(); foreach (Logic lg in otherPlayersLogic) { lg.gameArea.UnGameStatus(); } if (gameRoom.InvokeRequired) { gameRoom.Invoke(new showFun(gameRoom.ControlAdjustNO)); } else { gameRoom.ControlAdjustNO(); } if (MessageBox.Show(user + "获胜!", "游戏提示消息!", MessageBoxButtons.OK, MessageBoxIcon.Question) == DialogResult.OK) { if (gameRoom.InvokeRequired) { gameRoom.Invoke(new showFun(gameRoom.ControlAdjustYes)); } else { gameRoom.ControlAdjustYes(); } } else { if (gameRoom.InvokeRequired) { gameRoom.Invoke(new showFun(gameRoom.ControlAdjustYes)); } else { gameRoom.ControlAdjustYes(); } } } } break; } }
public Message(MsgSys msg) : this() { msgType = MsgType.Sys; msgContent = msg; }