void OnConnectEvent(int connection) { JoinGameReq req = new JoinGameReq(SystemInfo.deviceName); GameMsg msg = new GameMsg(GameMsg.MsgType.JoinGameReq, req); Client.SendMessage(msg, UnityEngine.Networking.QosType.ReliableSequenced); }
void OnJoinGameReq(JoinGameReq req, Connection conn) { if (conn.state == ConnState.Connected) { JoinGameRsp rsp = new JoinGameRsp(); if (inGameClientPlayerCount >= 4) { rsp.success = false; } else { if (req.name != null) { //名字有效 rsp.success = true; conn.state = ConnState.WaitForReady; conn.playerName = req.name; inGameClientPlayerCount++; } } Server.SendMessage(new GameMsg(GameMsg.MsgType.JoinGameRsp, rsp), conn.connId, UnityEngine.Networking.QosType.ReliableSequenced); } }
/// <summary> /// 进入游戏 初始化角色和场景数据 /// need TODO 场景数据 /// </summary> /// <param name="sessionId"></param> /// <param name="roleId"></param> /// <returns></returns> private static async Task <BaseResponseData> JoinGame() { var result = new JoinGameResult(); var data = new JoinGameReq(); var selfUser = User; string sessionId = selfUser.sessionId; if (selfUser.bytes != null) { data = await InitHelpers.GetPse().DeserializeAsync <JoinGameReq>(selfUser.bytes); if (data == null) { //请求参数为空 result.Result = GameEnum.WsResult.RoleIdIsNull; return(result); } } else { //请求参数为空 result.Result = GameEnum.WsResult.RoleIdIsNull; return(result); } var roleId = new Guid(data.RoleId); var user = await RoleDataHelper.Instance.GetRoleByRoleIdAsync(roleId); if (user != null) //找到角色数据 绑定在线状态 返回数据给用户 设置状态为登录状态 { //查找session用来绑定 //查找用户是否在线 var sid = await SidRoleIdDataHelper.Instance.GetSidByRoleIdAsync(roleId); //查找当前连接是否有角色 if (sid != null) //有旧的session { if (sid.Equals(sessionId)) //检查session保存的roleid是不是和新登入的一致 { //session一致 有可能是断线重连 } else { //不一致 可能是从别的地方连接 await RoleDataHelper.Instance.RemoveRoleBySidAsync(sid); await RoleDataHelper.Instance.RemoveRoleBySidAsync(sessionId); await RoleDataHelper.Instance.SetRoleBySidAsync(sessionId, user); } //用户已经在线 进行通知处理 匹配 imei处理 预防盗号处理 //TODO await SidRoleIdDataHelper.Instance.UpdateSidByRoleIdAsync(roleId, sessionId); } else { //没有旧的session await RoleDataHelper.Instance.RemoveRoleBySidAsync(sessionId); await RoleDataHelper.Instance.SetRoleBySidAsync(sessionId, user); //切记 下线时候要删除 或者在心跳包收不到的情况下 也要删除 await SidRoleIdDataHelper.Instance.SetSidByRoleIdAsync(roleId, sessionId); } //构造返回值 List <Model.ResponseData.UserAttr> u = new List <Model.ResponseData.UserAttr>(); foreach (var item in user.UserAttr) { u.Add(new Model.ResponseData.UserAttr() { Count = item.Count, UserAttrID = item.UserAttrID }); } selfUser.role = user; //Load Bag info var bagInfo = await BagDataHelper.Instance.GetBagByRoleId(roleId); result.CompanyInfo = await CompanyController.Instance.GetCompanyInfoByRoleId(roleId); result.DepartInfoInfo = await DepartmentController.Instance.GetDepartmentInfoByRoleId(roleId); result.FinanceLogInfo = await FinanceLogController.Instance.GetFinanceLog(roleId); result.MapInfo = await LandController.Instance.GetRoleLandShopInfo(roleId); result.Room = await RoomController.Instance.GetRoom(user.Id) as RoomResult; result.UserAttr = u; result.Affinity = user.Affinity; result.Avatar = user.Avatar; result.Certificates = user.Certificates; result.CertificatesExp = user.CertificatesExp; result.Charm = user.Charm; result.Concentration = user.Concentration; result.Constitution = user.Constitution; result.Desc = user.Desc; result.Gold = user.Gold; result.Exp = user.Exp; result.Icon = user.Icon; result.RoleId = user.Id.ToString(); result.Intelligence = user.Intelligence; result.Level = user.Level; result.Name = user.Name; result.Sex = user.Sex; result.SocialStatus = user.SocialStatus; result.Type = user.Type; result.VipLevel = user.VipLevel; if (bagInfo != null) { result.RoleBag = new BagInfo() { CurUsedCell = bagInfo.CurUsedCell, MaxCellNumber = bagInfo.MaxCellNumber, Items = BagController.Instance.GetRoleItems(bagInfo.Items) }; } else { bagInfo = new Bag(); result.RoleBag = new BagInfo() { CurUsedCell = bagInfo.CurUsedCell, MaxCellNumber = bagInfo.MaxCellNumber, Items = BagController.Instance.GetRoleItems(bagInfo.Items) }; } } return(result); }
public async Task InitializeGameHub() { gameHubConnection = new HubConnectionBuilder() .WithUrl(ServerAddress + "/belotHub") .Build(); gameHubConnection.On <PlayerStateVm, string>(ChannelConstants.PlayerStateChange, async(ps, eventName) => { this.playerState = ps; this.cardToBePlayed = null; Console.WriteLine("PlayerStateChange"); Console.WriteLine(eventName); if (eventName == null) { } //Console.WriteLine(ps.ActionRequired); else if (eventName == "GameStart") { //Console.WriteLine("GameStart State Update"); } else if (eventName.Contains("Deal")) { //Console.WriteLine(JsonConvert.SerializeObject(ps)); } else if (eventName.Contains("Play")) { //Console.WriteLine(JsonConvert.SerializeObject(ps)); } else if (eventName.Contains("ViewScore")) { //Console.WriteLine("ViewScore"); //Console.WriteLine(JsonConvert.SerializeObject(ps)); } if (playerState != null) { if (playerState.ActionRequired == "Wait") { return; } else if (playerState.ActionRequired == "Bidding") { Thread.Sleep(1000); await this.Announce(playerState.PossibleBids[0]); } else if (playerState.ActionRequired == "ConfirmDeal") { Thread.Sleep(1000); await this.Deal(); } else if (playerState.ActionRequired == "PlayCard") { Thread.Sleep(1000); await this.PlayCard(); } } }); gameHubConnection.On <JoinGameRes>(ChannelConstants.JoinGameAnswer, (res) => { Console.WriteLine("JoinRespons"); Console.WriteLine(res.Success); }); gameHubConnection.On <TableStateVm>(ChannelConstants.TableUpdate, (tu) => { Console.WriteLine("TableUpdate"); this.tableStateVm = tu; if (tu.ActionRequied == "ViewScore") { Console.WriteLine("ViewScore - Table Update"); Console.WriteLine(JsonConvert.SerializeObject(tu)); } }); await gameHubConnection.StartAsync(); var joinReq = new JoinGameReq() { PlayerName = this.UserId, GameName = this.GameName, Seat = int.Parse(this.Seat) }; Thread.Sleep(int.Parse(this.Seat) * 500); await gameHubConnection.SendAsync("JoinGame", joinReq); }
void OnDateEvent(GameMsg msg, int connId) { if (connDict.ContainsKey(connId)) { Connection conn = connDict[connId]; if (conn == null) { //不应该发生 Debug.LogError("ServerAgent.OnDateEvent>> connection is null"); return; } // 设置conn的lastTime conn.lastRecvTime = DateTime.Now; if (msg.type == GameMsg.MsgType.JoinGameReq) { JoinGameReq req = msg.content as JoinGameReq; if (req != null) { OnJoinGameReq(req, conn); } } else if (msg.type == GameMsg.MsgType.ClientReady) { ClientReady ready = msg.content as ClientReady; if (ready != null) { OnClientReady(ready, conn); } } else if (msg.type == GameMsg.MsgType.QuitGameReq) { QuitGameReq quit = msg.content as QuitGameReq; if (quit != null) { OnClientQuit(quit, conn); } } else if (msg.type == GameMsg.MsgType.ClientLocalPlayerInfo) { ClientLocalPlayerInfo state = msg.content as ClientLocalPlayerInfo; if (state != null) { OnClientLocalState(state, conn); } } else if (msg.type == GameMsg.MsgType.Damage) { HitPlayer hit = msg.content as HitPlayer; if (hit != null) { OnDamage(hit, conn); } } else if (msg.type == GameMsg.MsgType.Shoot) { PlayerShoot shoot = msg.content as PlayerShoot; if (shoot != null) { OnShoot(shoot, conn); } } } }
public async Task JoinGame(JoinGameReq joinGameReq) { try { if (!this.belotGameManager.Games.ContainsKey(joinGameReq.GameName)) { lock (mangaerLock) { this.belotGameManager.CreateGame(joinGameReq.GameName); } } if (this.belotGameManager.Games[joinGameReq.GameName].GameStarted) { var clientToDisconect = this.belotGameManager.AddPlayerToGame(joinGameReq.GameName, joinGameReq.PlayerName, this.Context.ConnectionId, joinGameReq.Seat); await this.Groups.AddToGroupAsync(this.Context.ConnectionId, joinGameReq.GameName); var rejoinResult = new JoinGameRes { Success = true }; await this.Clients.Caller.SendAsync(ChannelConstants.JoinGameAnswer, rejoinResult); await this.SendDisconectMessage(clientToDisconect, joinGameReq.GameName); await this.UpdateTabelState(joinGameReq.GameName); await this.UpdateIndividualPlayerStates(joinGameReq.GameName, "NewPlayer"); return; } this.belotGameManager.AddPlayerToGame(joinGameReq.GameName, joinGameReq.PlayerName, this.Context.ConnectionId, joinGameReq.Seat); await this.Groups.AddToGroupAsync(this.Context.ConnectionId, joinGameReq.GameName); var result = new JoinGameRes { Success = true }; await this.Clients.Caller.SendAsync(ChannelConstants.JoinGameAnswer, result); if (!this.belotGameManager.Games[joinGameReq.GameName].Players.Any(x => string.IsNullOrEmpty(x.Id)) && !this.belotGameManager.Games[joinGameReq.GameName].CanStart) { this.belotGameManager.Games[joinGameReq.GameName].StartGame(); await this.UpdateTabelState(joinGameReq.GameName); this.belotGameManager.Games[joinGameReq.GameName].CanStart = false; await this.UpdateIndividualPlayerStates(joinGameReq.GameName, "GameStart"); return; } await this.UpdateIndividualPlayerStates(joinGameReq.GameName); } catch (Exception ex) { var s = 4; } }