/// <summary> /// 客户端登录的处理 /// </summary> /// <param name="client"></param> /// <param name="dto"></param> private void Login(ClientPeer client, AccountDto dto) { // 单线程执行,防止多个线程同时访问,数据出错 SingleExecute.Instance.Exeecute(() => { // 用户名不存在 if (DatabaseManager.IsExistUserName(dto.userName) == false) { client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -1); // 向客户端发送消息 return; } // 密码错误 if (DatabaseManager.IsMatch(dto.userName, dto.password) == false) { client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -2); return; } // 用户在线 if (DatabaseManager.IsOnline(dto.userName)) { client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -3); return; } // 验证都通过了,登录成功 DatabaseManager.Login(client, dto.userName); client.SendMsg(OpCode.Account, AccountCode.Login_SRES, 1); }); }
/// <summary> /// 客户端登录的请求 /// </summary> /// <param name="client"></param> /// <param name="dto"></param> private void Login(ClientPeer client, AccountDto dto) { //单线程执行 //防止多个线程同时访问数据出错 SingleExecute.Instance.Execute(() => { if (DatabaseManager.IsExistUserName(dto.userName) == false) { //用户名不存在 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -1); return; } if (DatabaseManager.IsMatch(dto.userName, dto.password) == false) { //密码不正确 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -2); return; } if (DatabaseManager.IsOnline(dto.userName)) { //该账号已在线 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -3); //return; } DatabaseManager.Login(dto.userName, client); client.SendMsg(OpCode.Account, AccountCode.Login_SRES, 0); }); }
/// <summary> /// 客户端注册的处理 /// </summary> /// <param name="dto"></param> private void Register(ClientPeer client, AccountDto dto) { // 单线程执行,防止多个线程同时访问,数据出错 SingleExecute.Instance.Exeecute(() => { // 用户名已被注册 if (DatabaseManager.IsExistUserName(dto.userName)) { client.SendMsg(OpCode.Account, AccountCode.Register_SRES, -1); // 发送一个整型,客户端接收后自己进行判断,比直接发送一个"用户名已被注册"的字符串节约性能 return; } // 添加用户 DatabaseManager.AddUser(dto.userName, dto.password); // 给客户端返回一个消息,0 - 代表注册成功 client.SendMsg(OpCode.Account, AccountCode.Register_SRES, 0); }); }
/// <summary> /// 客户端获取用户信息的请求 /// </summary> /// <param name="client"></param> private void GetUserInfo(ClientPeer client) { SingleExecute.Instance.Exeecute(() => { UserDto dto = DatabaseManager.CreateUserDto(client.Id); client.SendMsg(OpCode.Account, AccountCode.GetUserInfo_SRES, dto); }); }
/// <summary> /// 客户端获取排行榜信息的请求 /// </summary> /// <param name="client"></param> private void GetRank(ClientPeer client) { SingleExecute.Instance.Exeecute(() => { RankListDto dto = DatabaseManager.GetRankListDto(); client.SendMsg(OpCode.Account, AccountCode.GetRank_SRES, dto); }); }
/// <summary> /// 客户端发来的更新金币数量请求 /// </summary> /// <param name="client"></param> /// <param name="coin"></param> private void UpdateCoin(ClientPeer client, int coin) { SingleExecute.Instance.Exeecute(() => { int totalCoin = DatabaseManager.UpdateCoin(client.Id, coin); client.SendMsg(OpCode.Account, AccountCode.UpdateCoin_SRES, totalCoin); }); }
/// <summary> /// 客户端进入房间的请求 /// </summary> /// <param name="client"></param> /// <param name="roomType"></param> private void EnterRoom(ClientPeer client, int roomType) { SingleExecute.Instance.Exeecute(() => { // 判断当前的客户端连接对象是不是在匹配房间里面,如果在,忽略 if (matchCacheList[roomType].IsMatching(client.Id)) { return; } MatchRoom room = matchCacheList[roomType].Enter(client); // 构造 UserDto UserDto userDto = DatabaseManager.CreateUserDto(client.Id); // 广播给房间内的所有玩家,除了自身,有新的玩家进来了,参数:新进用户的 UserDto room.Broadcast(OpCode.Match, MatchCode.Enter_BRO, userDto, client); // 给客户端一个相应,参数:房间传输模型,包含房间内的正在等待的玩家以及准备的玩家id 集合 client.SendMsg(OpCode.Match, MatchCode.Enter_SRES, MakeMatchRoomDto(room)); if (roomType == 0) { Console.WriteLine(userDto.name + "进入底注为 10 的房间"); } if (roomType == 1) { Console.WriteLine(userDto.name + "进入底注为 20 的房间"); } if (roomType == 2) { Console.WriteLine(userDto.name + "进入底注为 50 的房间"); } }); }
/// <summary> /// 客户端注册的处理 /// </summary> /// <param name="dto"></param> private void Register(ClientPeer client, AccountDto dto) { //单线程执行 //防止多个线程同时访问数据出错 SingleExecute.Instance.Execute(() => { //用户名已经被注册 if (DatabaseManager.IsExistUserName(dto.userName)) { client.SendMsg(OpCode.Account, AccountCode.Register_SRES, -1); return; } //创建一条用户数据 DatabaseManager.CreateUser(dto.userName, dto.password); client.SendMsg(OpCode.Account, AccountCode.Register_SRES, 0); }); }
//客户端更新账号信息 private void ModifyPwd(ClientPeer client, AccountDto dto) { SingleExecute.Instance.Execute(() => { int flag = DatabaseManager.ModifyPwd(client, dto.password); client.SendMsg(OpCode.Account, AccountCode.ModifyPwd_SRES, flag); }); }
/// <summary> /// 客户端获取排行榜的请求处理 /// </summary> /// <param name="client"></param> private void GetRankList(ClientPeer client) { SingleExecute.Instance.Execute(() => { Console.WriteLine("请求排行榜信息"); RankListDto dto = DatabaseManager.GetRankListDto(); client.SendMsg(OpCode.Account, AccountCode.GetRankList_SRES, dto); Console.WriteLine("发送排行榜信息" + OpCode.Account + AccountCode.GetRankList_SRES); }); }
/// <summary> /// 广播发消息,给除自己以外的客户端发消息 /// </summary> public void Broadcast(int opCode, int subCode, object value, ClientPeer exceptClient = null) { NetMsg msg = new NetMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in playerList) { ClientPeer client = DatabaseManager.GetClientPeerByUserId(player.id); if (client == exceptClient) { continue; } client.SendMsg(packet); } }
/// <summary> /// 广播发消息 /// </summary> public void Broadcase(int opCode, int subCode, object value, ClientPeer exceptClient = null) { Console.WriteLine("--广播有玩家加入的消息--" + opCode + subCode); NetMsg msg = new NetMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in playerList) { ClientPeer client = Database.DatabaseManager.GetClientPeerByUserId(player.userId); if (client == exceptClient) { Console.WriteLine("--wangzhi--房间里玩家的id--" + client.Id); Console.WriteLine("--wangzhi--忽略的玩家ID--" + exceptClient.Id); continue; } client.SendMsg(packet); } }
/// <summary> /// 客户端进入房间的请求 /// </summary> /// <param name="client"></param> /// <param name="roomType"></param> private void EnterRoom(ClientPeer client, int roomType) { SingleExecute.Instance.Execute(() => { //判断一下当前的客户端连接对象是否在匹配房间里面,如果在则忽略 if (matchCacheList[roomType].IsMatching(client.Id)) { return; } else { Console.WriteLine("--wangzhi--进入的玩家为:--" + client.Id); MatchRoom room = matchCacheList[roomType].Enter(client); //构造用户数据传输模型 UserDto userDto = DatabaseManager.CreateUserDto(client.Id); //广播给房间内的所有玩家,除了自身,有新的玩家进来了,参数:新进用户的userdto room.Broadcase(OpCode.Match, MatchCode.Enter_BRO, userDto, client); //给客户端一个响应 参数:房间传输模型,包含房间内的正在等待的玩家以及准备的玩家id集合 Console.WriteLine("--wangzhi--房间里的玩家个数--" + room.clientList.Count); Console.WriteLine("--wangzhi--房间ID为:--" + room.roomId); foreach (var player in room.clientList) { Console.WriteLine("--wangzhi--玩家id分别为:--" + player.Id); } client.SendMsg(OpCode.Match, MatchCode.Enter_SRES, MakeMatchRoomDto(room, client.Id)); if (roomType == 0) { Console.WriteLine(userDto.UserName + "进入底注为10,顶注为100的房间"); } if (roomType == 1) { Console.WriteLine(userDto.UserName + "进入底注为20,顶注为200的房间"); } if (roomType == 2) { Console.WriteLine(userDto.UserName + "进入底注为50,顶注为500的房间"); } } }); }
public void SendMsg(int opCode, int subCode, object value) { Debug.Log("--wangzhi--发送消息--" + opCode + " " + subCode); client.SendMsg(opCode, subCode, value); }
//用户登出 private void UserLogout(ClientPeer client) { DatabaseManager.OffLine(client); client.SendMsg(OpCode.Account, AccountCode.Logout_SRES, 0); return; }
public void SendMsg(int opCode, int subCode, object value) { Debug.Log("进入SendMsg发送shuju"); client.SendMsg(opCode, subCode, value); }
public void SendMsg(int opCode, int subCode, object value) { client.SendMsg(opCode, subCode, value); }