protected override async void Run(Session session, C2G_UpdateAchievement message, Action <G2C_UpdateAchievement> reply) { G2C_UpdateAchievement response = new G2C_UpdateAchievement(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } if (message.Info.Progress == 0) { response.Error = ProtocolErrorCode.ERR_InputParams; reply(response); return; } DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); UpdateAchievement(userInfo, message.Info); await dbProxyComponent.Save(userInfo, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_Unlock message, Action <G2C_Unlock> reply) { G2C_Unlock response = new G2C_Unlock(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); userInfo.UnlockPoint = message.DataId; await dbProxyComponent.Save(userInfo, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_GameStart message, Action <G2C_GameStart> reply) { G2C_GameStart response = new G2C_GameStart(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); userInfo.LastGameStartTime = DateTime.UtcNow.Ticks; await dbProxyComponent.Save(userInfo, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2R_Register message, Action <R2C_Register> reply) { R2C_Register response = new R2C_Register(); try { //验证账号格式 if (!Regex.IsMatch(message.Account, "^[A-Za-z0-9]*$")) { response.Error = ProtocolErrorCode.ERR_IllegalCharacter; reply(response); return; } //验证密码格式 if (!Regex.IsMatch(message.Password, "^[A-Za-z0-9]*$")) { response.Error = ProtocolErrorCode.ERR_IllegalCharacter; reply(response); return; } //数据库操作对象 DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>(); //查询账号是否存在 var result = await dbProxy.Query <DB_Account>((p) => p.Account == message.Account); if (result.Count > 0) { response.Error = ProtocolErrorCode.ERR_AccountAlreadyRegister; reply(response); return; } //新建账号 DB_Account newAccount = ComponentFactory.CreateWithId <DB_Account>(IdGenerater.GenerateId()); newAccount.Account = message.Account; newAccount.Password = message.Password; Log.Info($"注册新账号:{MongoHelper.ToJson(newAccount)}"); //新建用户信息 DB_UserInfo newUser = ComponentFactory.CreateWithId <DB_UserInfo>(newAccount.Id); newUser.Account = $"用户{message.Account}"; newUser.RoleDataList.Add(1001); //保存到数据库 await dbProxy.Save(newAccount); await dbProxy.Save(newUser, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_UpdateCollection message, Action <G2C_UpdateCollection> reply) { G2C_UpdateCollection response = new G2C_UpdateCollection(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); bool change = false; switch (message.Type) { case 1: if (addToList(userInfo.MonsterList, message.DataId)) { change = true; } break; case 2: if (addToList(userInfo.HadOwnItemList, message.DataId)) { change = true; } break; case 3: if (addToList(userInfo.UnSeeOwnItemList, message.DataId)) { change = true; } break; } if (change) { await dbProxyComponent.Save(userInfo, false); } reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
private void UpdateAchievement(DB_UserInfo userInfo, AchievementInfo info) { for (int i = 0; i < userInfo.AchievementList.Count; i++) { DB_AchievementInfo dbAchievementInfo = userInfo.AchievementList[i]; if (dbAchievementInfo.DataId == info.DataId) { dbAchievementInfo.Progress = info.Progress; return; } } userInfo.AchievementList.Add(new DB_AchievementInfo() { DataId = info.DataId, Progress = info.Progress }); }
protected override async void Run(Session session, C2G_GetUserInfo message, Action <G2C_GetUserInfo> reply) { G2C_GetUserInfo response = new G2C_GetUserInfo(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } //查询用户信息 DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); response.Crystal = userInfo.Crystal; response.CurrRoleDataId = userInfo.CurrRoleDataId; response.RoleDataList = userInfo.RoleDataList; response.UnlockedTechList = userInfo.UnlockedTechList; response.UnlockPoint = userInfo.UnlockPoint; response.MonsterList = userInfo.MonsterList; response.HadOwnItemList = userInfo.HadOwnItemList; response.SeeOwnItemList = userInfo.UnSeeOwnItemList; List <AchievementInfo> list = new List <AchievementInfo>(userInfo.AchievementList.Count); for (int i = 0; i < userInfo.AchievementList.Count; i++) { DB_AchievementInfo info = userInfo.AchievementList[i]; AchievementInfo achievementInfo = new AchievementInfo() { DataId = info.DataId, Progress = info.Progress }; list.Add(achievementInfo); } response.AchievementList = list; reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_CostCrystal message, Action <G2C_CostCrystal> reply) { G2C_CostCrystal response = new G2C_CostCrystal(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } short crystal = getCost(message.Type, message.DataId); if (crystal <= 0) { response.Error = ProtocolErrorCode.ERR_InputParams; reply(response); return; } DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); if (userInfo.Crystal < crystal) { response.Error = ProtocolErrorCode.ERR_CostCrystalFail; reply(response); return; } userInfo.Crystal -= crystal; await dbProxyComponent.Save(userInfo, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_GameOver message, Action <G2C_GameOver> reply) { G2C_GameOver response = new G2C_GameOver(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } if (message.CrystalReward <= 0) { response.Error = ProtocolErrorCode.ERR_InputParams; reply(response); return; } DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); long gameUseMin = (DateTime.UtcNow.Ticks - userInfo.LastGameStartTime) / 1000 / 60; //花费分钟数 long average = message.CrystalReward / gameUseMin; if (average > 20) //每分钟允许获得至多20个海钻 { response.Error = ProtocolErrorCode.ERR_GameOverError; reply(response); return; } userInfo.Crystal += message.CrystalReward; await dbProxyComponent.Save(userInfo, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_ChangeRole message, Action <G2C_ChangeRole> reply) { G2C_ChangeRole response = new G2C_ChangeRole(); try { User user = GateHelper.GetUserBySession(session); //验证Session if (user == null) { response.Error = ProtocolErrorCode.ERR_SignError; reply(response); return; } //查询用户信息 DBProxyComponent dbProxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DB_UserInfo userInfo = await dbProxyComponent.Query <DB_UserInfo>(user.UserID, false); //暂不验证,角色那里目前是成就验证 /* * if (!userInfo.RoleDataList.Contains(message.DataId)) * { * response.Error = ProtocolErrorCode.ERR_DataNotExsit; * reply(response); * return; * }*/ userInfo.CurrRoleDataId = message.DataId; await dbProxyComponent.Save(userInfo, false); reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }