public void Send(ushort mainid, ushort subid, string msg) { simpledata.SimpleString simpleStr = new simpledata.SimpleString(); simpleStr.simple = msg; byte[] data = NetUtil.ProtobufSerialize(simpleStr); SendAsync(mainid, subid, data); }
public void DoJion(Conn client, byte[] buffer) { var simpleInt = NetUtil.ProtobufDeserialize <simpledata.SimpleInt>(buffer); int jroomid = simpleInt.simple; if (!nnGames.ContainsKey(jroomid)) { var simpleStr = new simpledata.SimpleString(); simpleStr.simple = string.Format("加入房间失败:房间号{0}不存在!", jroomid); byte[] data = NetUtil.ProtobufSerialize(simpleStr); client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.JionFailure, data); return; } NiuNiuGame jGame = nnGames[jroomid]; jGame.RoomJion(client); }
public void RoomJion(Conn client) { if (isPlaying) { simpledata.SimpleString simpleStr = new simpledata.SimpleString(); simpleStr.simple = "加入房间失败:游戏已开始,请稍后。"; byte[] data = NetUtil.ProtobufSerialize(simpleStr); client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.JionFailure, data); //游戏开始 加入失败 return; } if (dictNNPlayers.Count == GamePlayerNumber) { simpledata.SimpleString simpleStr = new simpledata.SimpleString(); simpleStr.simple = "加入房间失败:游戏人数已满。"; byte[] data = NetUtil.ProtobufSerialize(simpleStr); client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.JionFailure, data); //游戏开始 加入失败 return; } NiuNiuPlayer newNNPlayer = null; for (int i = 0; i < GamePlayerNumber; i++) { if (!dictNNPlayers.ContainsKey(i)) { var userid = client.ClientUseID; var userinfo = LobbyServer.Ins.GetUserInfo(userid); newNNPlayer = new NiuNiuPlayer(i, userinfo, client); dictNNPlayers.Add(i, newNNPlayer); break; } } //所有玩家数据(包括新加入的玩家),发送给新加入的玩家 GameSeatedUPlayers otherSeatedPlayers = new GameSeatedUPlayers(); //刚加入玩家的数据,发给已经加入的玩家 GamePlayer newGamePlayer = new GamePlayer(); newGamePlayer.seatid = newNNPlayer.SeatID; newGamePlayer.user = newNNPlayer.UserInfo; byte[] newPlayerData = NetUtil.ProtobufSerialize(newGamePlayer); foreach (var temp in dictNNPlayers.Values) { GamePlayer player = new GamePlayer(); player.seatid = temp.SeatID; player.state = temp.GState; player.user = temp.UserInfo; otherSeatedPlayers.players.Add(player); otherSeatedPlayers.roomid = GameID; otherSeatedPlayers.hostid = hostid; if (temp.SeatID != newNNPlayer.SeatID) { temp.conn.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.PlayerJion, newPlayerData); } } LogUtil.LogInfo(string.Format("****** 用户ID{0} 加入房间{1} 座位{2}", client.ClientUseID, GameID, newNNPlayer.SeatID)); byte[] allPlayerData = NetUtil.ProtobufSerialize(otherSeatedPlayers); client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.JionSuccess, allPlayerData); }
public bool RoomLeave(Conn client) { if (isPlaying) { simpledata.SimpleString simpleStr = new simpledata.SimpleString(); simpleStr.simple = "离开房间失败:游戏已开始,请稍后。"; byte[] data = NetUtil.ProtobufSerialize(simpleStr); client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.LeaveFailure, data); //游戏开始 加入失败 return(false); } int userid = client.ClientUseID; int seatid = 0; foreach (var kvp in dictNNPlayers) { if (kvp.Value.UserInfo.id == userid) { seatid = kvp.Key; break; } } //客户端更新数据 // var userinfo = dictNNPlayers[seatid].UserInfo; // byte[] data = NetUtil.ProtobufSerialize(userinfo); // client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.LeaveSuccess, data); //移除玩家 foreach (var v in dictNNPlayers.Values) { simpledata.SimpleInt msg = new simpledata.SimpleInt(); msg.simple = seatid; byte[] data = NetUtil.ProtobufSerialize(msg); v.conn.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.LeaveSuccess, data); } dictNNPlayers.Remove(seatid); //判断是否还有玩家,没有就解散 if (dictNNPlayers.Count == 0) { return(true); } //若果离开的是坐庄的 则换下一个 if (hostid == seatid) { for (int i = 1; i < GamePlayerNumber; i++) { int nhostid = (seatid + 1) % GamePlayerNumber; if (dictNNPlayers.ContainsKey(nhostid)) { SetHost(nhostid); break; } } } return(false); }