public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理是否添加好友的反馈"); // 是否同意 bool isAccept = (bool)request.Parameters.ExTryGet((byte)ParameterCode.AcceptAddFriend); // 请求添加的玩家数据 int fromId = (int)request.Parameters.ExTryGet((byte)ParameterCode.PlayerId); string fromName = (string)request.Parameters.ExTryGet((byte)ParameterCode.PlayerName); // 请求的客户端 MobaPeer fromPeer = Caches.Player.GetPeer(fromId); OperationResponse response = new OperationResponse((byte)OperationCode.PlayerAddResult); if (isAccept) { response.ReturnCode = (short)ReturnCode.Suceess; // 自身的数据 DataBase.Model.Player player = UserManager.GetPlayer(peer.Username); // 好友的数据 DataBase.Model.Player friend = PlayerManager.GetById(fromId); // 添加好友 PlayerManager.AddFriend(player.Identification, fromId); // 发送更新后的数据 DtoPlayer dtoPlayer = player.ConvertToDot(); response.Parameters = new Dictionary <byte, object>(); response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dtoPlayer.Friends.Last()); peer.SendOperationResponse(response, sendParameters); dtoPlayer = friend.ConvertToDot(); response.Parameters = new Dictionary <byte, object>(); response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dtoPlayer.Friends.Last()); fromPeer.SendOperationResponse(response, sendParameters); return; } else { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = fromName + " 拒绝添加你为好友"; } fromPeer.SendOperationResponse(response, sendParameters); }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理获取玩家角色的请求"); OperationResponse response = new OperationResponse(request.OperationCode); // 验证连接对象 if (Caches.User.VerifyPeer(peer)) { // 检测是否存在角色 if (UserManager.HasPlayer(peer.Username)) { response.ReturnCode = (short)ReturnCode.Suceess; } else { response.ReturnCode = (short)ReturnCode.Empty; } } else { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "非法登陆"; } peer.SendOperationResponse(response, sendParameters); }
/// <summary> /// 下线移除缓存 /// </summary> /// <param name="username"></param> public void Offline(MobaPeer peer) { Player player = UserManager.GetPlayer(peer.Username); if (!m_OnlineDict.ContainsKey(player.Identification)) { return; } // 通知所有好友下线 if (!string.IsNullOrEmpty(player.FriendIdList)) { OperationResponse response = new OperationResponse((byte)OperationCode.FriendStateChange); string[] friendList = player.FriendIdList.Split(','); foreach (string friendId in friendList) { int id = int.Parse(friendId); if (IsOnline(id)) { response.Parameters = new Dictionary <byte, object>(); MobaPeer tempPeer = GetPeer(id); DtoFriend dto = new DtoFriend(id, player.Name, false); response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dto); tempPeer.SendOperationResponse(response, new SendParameters()); } } } m_OnlineDict.Remove(player.Identification); }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理进入选择房间的请求"); DataBase.Model.Player player = UserManager.GetPlayer(peer.Username); SelectRoom room = Caches.Select.EnterRoom(player.Identification, peer); if (room == null) { return; } OperationResponse response = new OperationResponse(); // 先给客户端发送已经进入房间的玩家数据(房间模型) response.OperationCode = (byte)OperationCode.SelectGetInfo; response.Parameters = new Dictionary <byte, object>(); response[(byte)ParameterCode.TeamOneSelectData] = JsonMapper.ToJson(room.TeamOneDict.Values.ToArray()); response[(byte)ParameterCode.TeamTwoSelectData] = JsonMapper.ToJson(room.TeamTwoDict.Values.ToArray()); peer.SendOperationResponse(response, sendParameters); // 再给房间内的其他客户端发一条消息:有人进入了 Dictionary <byte, object> data = new Dictionary <byte, object>(); data.Add((byte)(ParameterCode.PlayerId), player.Identification); room.Brocast(OperationCode.EnterSelect, data); }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理选择的请求"); int playerId = UserManager.GetPlayer(peer.Username).Identification; int heroId = (int)request.Parameters.ExTryGet((byte)ParameterCode.HeroId); SelectRoom room = Caches.Select.Select(playerId, heroId); if (room == null) { // 告诉自己选择失败 OperationResponse response = new OperationResponse(request.OperationCode); response.ReturnCode = (short)ReturnCode.Falied; peer.SendOperationResponse(response, sendParameters); return; } // 给房间内的所有人发一条消息:谁选择了什么英雄 Dictionary <byte, object> data = new Dictionary <byte, object>(); data.Add((byte)ParameterCode.PlayerId, playerId); data.Add((byte)ParameterCode.HeroId, heroId); room.Brocast(OperationCode.Selected, data); }
// 处理注册请求 public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理注册请求"); string username = request.Parameters.ExTryGet((byte)ParameterCode.Username) as string; string password = request.Parameters.ExTryGet((byte)ParameterCode.Password) as string; // 无效检测 if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password)) { return; } OperationResponse response = new OperationResponse(request.OperationCode); if (UserManager.GetByUsername(username) == null) { // 添加新用户 UserManager.Add(new User(username, password)); response.ReturnCode = (byte)ReturnCode.Suceess; } else { response.ReturnCode = (byte)ReturnCode.Falied; response.DebugMessage = "账号已存在"; } peer.SendOperationResponse(response, sendParameters); }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理玩家上线的请求"); // 获取自身的Player数据 DataBase.Model.Player player = UserManager.GetPlayer(peer.Username); if (player == null) { MobaServer.LogError(">>>>>>>>>> 数据异常 : PlayerOnlineHandler"); return; } // 防止重复上线 if (Caches.Player.IsOnline(player.Identification)) { MobaServer.LogError(">>>>>>>>> Player:" + player.Name + " 已上线"); return; } Caches.Player.Online(player.Identification, peer); OperationResponse response = new OperationResponse(); // 上线通知好友 if (!String.IsNullOrEmpty(player.FriendIdList)) { string[] friends = player.FriendIdList.Split(','); MobaPeer tempPeer = null; response.OperationCode = (byte)OperationCode.FriendStateChange; foreach (string friend in friends) { if (string.IsNullOrEmpty(friend)) { continue; } int id = int.Parse(friend); if (!Caches.Player.IsOnline(id)) { continue; } tempPeer = Caches.Player.GetPeer(id); // 发送好友上线的消息 response.Parameters = new Dictionary <byte, object>(); DtoFriend dto = new DtoFriend(player.Identification, player.Name, true); response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dto); tempPeer.SendOperationResponse(response, sendParameters); } } // 发送自身的玩家数据给客户端 response.OperationCode = request.OperationCode; response.Parameters = new Dictionary <byte, object>(); response[(byte)ParameterCode.DtoPlayer] = JsonMapper.ToJson(player.ConvertToDot()); peer.SendOperationResponse(response, sendParameters); }
/// <summary> /// 发送服务器的响应 /// </summary> /// <param name="peer">接收响应的客户端</param> /// <param name="opCode">操作码</param> /// <param name="data">发送的数据</param> /// <param name="retCode">返回码</param> /// <param name="debugMgr">调试信息</param> public void SendResponse(MobaPeer peer, byte opCode, Dictionary <byte, object> data = null, ReturnCode retCode = ReturnCode.Suceess, string debugMgr = "") { OperationResponse response = new OperationResponse(opCode); response.Parameters = data; response.ReturnCode = (short)retCode; response.DebugMessage = debugMgr; peer.SendOperationResponse(response, new SendParameters()); }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理停止匹配的请求"); int playerId = (int)request.Parameters[(byte)ParameterCode.PlayerId]; OperationResponse response = new OperationResponse(request.OperationCode); if (Caches.Match.StopMatch(peer, playerId)) { // 告诉客户端离开成功 response.ReturnCode = (short)ReturnCode.Suceess; peer.SendOperationResponse(response, sendParameters); } }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理创建角色的请求"); string playerName = request.Parameters.ExTryGet((byte)ParameterCode.PlayerName) as string; // 无效检测 if (String.IsNullOrEmpty(playerName)) { return; } User user = UserManager.GetByUsername(peer.Username); DataBase.Model.Player player = new DataBase.Model.Player(playerName, user); PlayerManager.Add(peer.Username, player); OperationResponse response = new OperationResponse(request.OperationCode); peer.SendOperationResponse(response, sendParameters); }
// 处理登陆请求 public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理登陆请求"); string username = request.Parameters.ExTryGet((byte)ParameterCode.Username) as string; string password = request.Parameters.ExTryGet((byte)ParameterCode.Password) as string; // 无效检测 if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password)) { return; } OperationResponse response = new OperationResponse(request.OperationCode); // 验证在线 if (Caches.User.IsOnline(username)) { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "账号已登陆"; } else { // 验证用户名,密码是否正确 if (UserManager.VerifyUser(username, password)) { Caches.User.Online(username, peer); response.ReturnCode = (short)ReturnCode.Suceess; } else { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "用户名或密码不正确"; } } peer.SendOperationResponse(response, sendParameters); }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理添加好友的请求"); // 请求添加的玩家名字 string name = request.Parameters.ExTryGet((byte)ParameterCode.PlayerName) as string; // 无效判断 if (String.IsNullOrEmpty(name)) { return; } // 获取添加好友的数据 OperationResponse response = new OperationResponse(request.OperationCode); DataBase.Model.Player player = PlayerManager.GetByName(name); if (player == null) { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "没有此玩家"; peer.SendOperationResponse(response, sendParameters); return; } // 如果添加的玩家为自身 DataBase.Model.Player selfPlayer = UserManager.GetPlayer(peer.Username); if (selfPlayer.Identification == player.Identification) { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "不能添加自身"; peer.SendOperationResponse(response, sendParameters); return; } // 如果已经是好友 string[] friends = UserManager.GetPlayer(peer.Username).FriendIdList.Split(','); foreach (string friend in friends) { if (String.IsNullOrEmpty(friend)) { continue; } if (int.Parse(friend) == player.Identification) { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "玩家已经是你的好友"; peer.SendOperationResponse(response, sendParameters); return; } } // 判断是否在线 这里不提供离线处理 直接返回失败 // TODO 不能给离线的玩家发送信息 bool isOnline = Caches.Player.IsOnline(player.Identification); if (!isOnline) { response.ReturnCode = (short)ReturnCode.Falied; response.DebugMessage = "玩家离线"; peer.SendOperationResponse(response, sendParameters); return; } // 玩家在线 向玩家发送是否同意的响应 MobaPeer toPeer = Caches.Player.GetPeer(player.Identification); response.OperationCode = (byte)OperationCode.PlayerAddToClient; response.Parameters = new Dictionary <byte, object>(); // 传输玩家名和id response[(byte)(ParameterCode.PlayerName)] = selfPlayer.Name; response[(byte)(ParameterCode.PlayerId)] = selfPlayer.Identification; toPeer.SendOperationResponse(response, sendParameters); }