public void RoomCreate(Conn client) { var userid = client.ClientUseID; var userinfo = LobbyServer.Ins.GetUserInfo(userid); NiuNiuPlayer player = new NiuNiuPlayer(0, userinfo, client); dictNNPlayers.Add(0, player); simpledata.SimpleInt simpleInt = new simpledata.SimpleInt(); simpleInt.simple = GameID; client.SendAsync((ushort)MainID.NiuNiu, (ushort)NiuNiuID.CreateSuccess, NetUtil.ProtobufSerialize(simpleInt)); }
public void DoVistorLogin(Conn client, byte[] buff) { MemoryStream recvms = new MemoryStream(buff); simpledata.SimpleInt simpleInt = Serializer.Deserialize <simpledata.SimpleInt>(recvms); int vid = simpleInt.simple; User tempUser = new User(); if (vid == 0) { // tempUser.name = GenerateRandom(8); tempUser.gold = 100000; tempUser.sex = 0; string cmd = string.Format("insert into user(name,sex,gold) values('{0}',{1},{2})", tempUser.name, tempUser.sex, tempUser.gold); tempUser.id = SqlManager.Ins.SqlInsert(cmd); dictUsers.Add(tempUser.id, tempUser); } else { if (dictUsers.ContainsKey(vid)) { tempUser = dictUsers[vid]; } else { // string cmd = string.Format("select * from user where id = {0} ", vid); // var dr = SqlManager.Ins.SqlSelect(cmd); // dr.Read(); // tempUser.id = vid; // tempUser.name = (string)dr["name"]; // tempUser.sex = (int)dr["sex"]; // tempUser.gold = (int)dr["gold"]; // dictUsers.Add(tempUser.id, tempUser); // SqlManager.Ins.CloseMySqlDataReader(); } } client.ClientUseID = tempUser.id; //返回客户端数据 MemoryStream ms = new MemoryStream(); Serializer.Serialize <User>(ms, tempUser); byte[] sendBytes = ms.ToArray(); client.SendAsync((ushort)MainID.Lobby, (ushort)LobbyID.LoginSuccess, sendBytes); }
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); }