//数据变更 private void ChangeValue(Ranking _this, DBRank_One one, long value) { one.Value = value; if (value < 0) { Logger.Error("Ranking.ChangeValue: Id={0},value={1}", one.Guid, value); } }
//尝试添加新记录 public int AddRanking(Ranking _this, int serverId, ulong Id, long value, string name, int fightPoint = -1) { //PlayerLog.WriteLog((int)LogType.AddRanking, "AddRanking s={0},id={1},v={2},n={3}", serverId, Id, value,name); DBRank_One dbRankOnetemp; if (_this.DBRankCache.TryGetValue(Id, out dbRankOnetemp)) { if (dbRankOnetemp.Value == value) { return(dbRankOnetemp.Rank); } _this.Dirty = true; if (!dbRankOnetemp.Name.Equals(name)) { dbRankOnetemp.Name = name; } ChangeValue(_this, dbRankOnetemp, value); return(dbRankOnetemp.Rank); } //新数据 var new_rank = GetRankCount(_this) + 1; dbRankOnetemp = new DBRank_One { Guid = Id, Rank = new_rank, Value = value, OldRank = -1, ServerId = serverId, Name = name, MaxRank = -1, FightPoint = fightPoint }; _this.DBRankCache[Id] = dbRankOnetemp; var key = serverId; if (_this.IsTotalServer) { key = -1; } DBRank_List tempDb; if (_this.mDBData.TryGetValue(key, out tempDb)) { tempDb.mData.Add(dbRankOnetemp); } _this.Dirty = true; return(dbRankOnetemp.Rank); //Sort(); ////排序后玩家可能不存在 //if (DBRankCache.TryGetValue(Id, out dbRankOnetemp)) //{ // return dbRankOnetemp.Rank; //} //return -1; }
//从排行榜移除某个玩家 private void RemoveOne(Ranking _this, DBRank_One one) { _this.DBRankCache.Remove(one.Guid); DBRank_List tempDb; if (_this.mDBData.TryGetValue(one.ServerId, out tempDb)) { tempDb.mData.Remove(one); } }
public IEnumerator BackupTotalRankCoroutine(Coroutine coroutine) { var timeStr = ServerRankBackupManager.FormatDateTimeToKey(DateTime.Now); var rankMgr = ServerRankManager.TotalRank; DBRankBackupServer db = new DBRankBackupServer(); db.ServerId = rankMgr.ServerId; foreach (var rankMgrKV in rankMgr.rank) { DBRankList dbList = new DBRankList(); dbList.Type = rankMgrKV.Key; var rank = rankMgrKV.Value; int i = 0; foreach (var charId in rank.RankUUIDList) { if (dbList.Items.Count >= ServerRankBackupManager.MAXMember) { break; } DBRank_One one = null; if (!rank.DBRankCache.TryGetValue(charId, out one)) { continue; } DBRankItem item = new DBRankItem(); item.CharacterId = one.Guid; item.Name = one.Name; item.Value = one.Value; dbList.Items.Add(item); i++; } db.List.Add(dbList); } var key = string.Format("{0}|{1}", timeStr, db.ServerId); Logger.Trace("BackupAllRankCoroutine-----begin[{0}] key=[{1}]", DateTime.Now, key); var result = RankServer.Instance.DB.Set <DBRankBackupServer>(coroutine, DataCategory.RankBackup, key, db); yield return(result); if (DataStatus.Ok != result.Status) { Logger.Fatal("RankServer.Instance.DB.Set DataStatus.Ok != result.Status [{0}]", key); } Logger.Trace("BackupTotalRankCoroutine-----end[{0}] key=[{1}]", DateTime.Now, key); }
//查询排行榜的某个名次 public List <DBRank_One> GetRankData(RankManager _this, string name, int min_rank, int max_rank) { var templist = new List <DBRank_One>(); int rankType; if (!_this.rank_name.TryGetValue(name, out rankType)) //if (!rank_name.ContainsKey(name)) { return(templist); } var tempranking = _this.rank[rankType]; var count = tempranking.RankUUIDList.Count; if (min_rank < 1) { min_rank = 1; } if (max_rank > count) { max_rank = count; } for (var i = min_rank; i <= max_rank; i++) { var temp = tempranking.GetRankOneByIndex(i - 1); long oldValue; if (tempranking.SaveOldValueByUUID.TryGetValue(temp.Guid, out oldValue)) { var t = new DBRank_One(); t.Guid = temp.Guid; t.Name = temp.Name; t.Value = oldValue; t.Rank = temp.Rank; t.OldRank = temp.OldRank; t.MaxRank = temp.MaxRank; t.FightPoint = temp.FightPoint; t.ServerId = temp.ServerId; templist.Add(t); } else { templist.Add(temp); } } return(templist); }
//交换两个玩家的名次 public bool SwapIndex(Ranking _this, DBRank_One db1, DBRank_One db2) { //ShowRank(_this, 50000); db1.OldRank = db1.Rank; db2.OldRank = db2.Rank; var index1 = db1.Rank - 1; var index2 = db2.Rank - 1; if (!(index1 >= _this.RankUUIDList.Count)) { _this.RankUUIDList[index1] = db2.Guid; } if (!(index2 >= _this.RankUUIDList.Count)) { _this.RankUUIDList[index2] = db1.Guid; } //if (index1 != db1.Rank - 1) //{ // Logger.Warn("SwapIndex index1 index={0},rank={1}", index1, db1.Rank); //} //if (index2 != db2.Rank - 1) //{ // Logger.Warn("SwapIndex index2 index={0},rank={1}", index2, db2.Rank); //} var tempRank = db1.Rank; db1.Rank = db2.Rank; var isNewMax = false; if (db1.Rank < db1.MaxRank || db1.MaxRank == -1) { db1.MaxRank = db1.Rank; isNewMax = true; } db2.Rank = tempRank; _this.Dirty = true; db1.Value = 1000000 / db1.Rank; db2.Value = 1000000 / db2.Rank; //ShowRank(_this, 60000); return(isNewMax); }
public IEnumerator RankP1vP1FightOver(Coroutine coroutine, RankService _this, RankP1vP1FightOverInMessage msg) { var selfId = msg.Request.CharacterId; var pvpId = msg.Request.PvpCharacterId; var ranking = ServerRankManager.GetRankByType(msg.Request.ServerId, P1vP1.P1vP1RankTypeId); if (null == ranking) { Logger.Error("RankP1vP1FightOver not find ranking serverid={0},type={1}", msg.Request.ServerId, P1vP1.P1vP1RankTypeId); yield break; } var self = ranking.GetPlayerData(selfId); var pvp = ranking.GetPlayerData(pvpId); if (self == null) { self = new DBRank_One(); self.Rank = ranking.GetRankCount() + 1; Logger.Error("RankP1vP1FightOver not find selfId={0}", selfId); yield break; } if (pvp == null) { Logger.Error("RankP1vP1FightOver not find pvpId={0}", pvpId); yield break; } var selfIndex = self.Rank; var pvpIndex = pvp.Rank; var result = msg.Request.Result; if (result != 1) { var selfMessage = RankServer.Instance.LogicAgent.PushP1vP1LadderChange(selfId, 0, msg.Request.PvpName, result, selfIndex, selfIndex); yield return(selfMessage.SendAndWaitUntilDone(coroutine)); if (!StaticData.IsRobot(pvpId)) //if (pvpId > 999999) { var pvpMessage = RankServer.Instance.LogicAgent.PushP1vP1LadderChange(pvpId, 1, msg.Request.Name, result, pvpIndex, pvpIndex); yield return(pvpMessage.SendAndWaitUntilDone(coroutine)); } yield break; } if (selfIndex > pvpIndex) { ranking.SwapIndex(self, pvp); var selfMessage = RankServer.Instance.LogicAgent.PushP1vP1LadderChange(selfId, 0, msg.Request.PvpName, result, selfIndex, pvpIndex); yield return(selfMessage.SendAndWaitUntilDone(coroutine)); if (!StaticData.IsRobot(pvpId)) //if (pvpId > 999999) { var pvpMessage = RankServer.Instance.LogicAgent.PushP1vP1LadderChange(pvpId, 1, msg.Request.Name, result, pvpIndex, selfIndex); yield return(pvpMessage.SendAndWaitUntilDone(coroutine)); {//邮件通知对方 var item = new ItemBaseData(); item.ItemId = -1; var args = new StringArray(); args.Items.Add(msg.Request.Name); args.Items.Add(selfIndex.ToString()); var msgMail = RankServer.Instance.LogicAgent.SendMailToCharacterByItems(pvpId, 95, item, args); yield return(msgMail.SendAndWaitUntilDone(coroutine)); } } } else { //selfOverIndex = -1;//-1代表打的人名次比我要差 var selfMessage = RankServer.Instance.LogicAgent.PushP1vP1LadderChange(selfId, 0, msg.Request.PvpName, result, selfIndex, -1); yield return(selfMessage.SendAndWaitUntilDone(coroutine)); if (!StaticData.IsRobot(pvpId)) //if (pvpId > 999999) { var pvpMessage = RankServer.Instance.LogicAgent.PushP1vP1LadderChange(pvpId, 1, msg.Request.Name, result, pvpIndex, -1); yield return(pvpMessage.SendAndWaitUntilDone(coroutine)); } } }
public IEnumerator SSGetRankDataByServerId(Coroutine coroutine, RankService _this, SSGetRankDataByServerIdInMessage msg) { var serverList = msg.Request.ServerList; var time = msg.Request.Time; var type = msg.Request.Ranktype; //请求这个时间点的数据不存在,那就把当前的排行榜数据存成那个时间点,防止下次再取时,又会取下次的排行榜,但排行榜会变化 var needSave = false; var timeStr = ServerRankBackupManager.FormatDateTimeToKey(DateTime.FromBinary(time)); foreach (var sid in serverList.Items) { string tempKey = timeStr + "|" + sid; DBRankBackupServer data = null; if (!ServerRankBackupManager.DictRankDataCache.TryGetValue(tempKey, out data)) { var result = RankServer.Instance.DB.Get <DBRankBackupServer>(coroutine, DataCategory.RankBackup, tempKey); yield return(result); if (DataStatus.Ok == result.Status && null != result.Data) { data = result.Data; if (ServerRankBackupManager.DictRankDataCache.ContainsKey(tempKey)) { ServerRankBackupManager.DictRankDataCache[tempKey] = data; } else { ServerRankBackupManager.DictRankDataCache.Add(tempKey, data); } } else { Logger.Fatal("RankServer.Instance.DB.Get DataStatus.Ok != result.Status [{0}]", tempKey); } } bool ok = false; if (null != data) { foreach (var item in data.List) { if (item.Type != type) { continue; } MsgRankList msgList = new MsgRankList(); msgList.ServerId = sid; msgList.Type = type; foreach (var rankItem in item.Items) { if (msgList.Items.Count >= ServerRankBackupManager.MAXMember) { break; } MsgRankItemData msgItem = new MsgRankItemData(); msgItem.CharacterId = rankItem.CharacterId; msgItem.Name = rankItem.Name; msgItem.Value = rankItem.Value; msgList.Items.Add(msgItem); } msg.Response.Data.Add(msgList); ok = true; break; } } if (!ok) { //数据库没找到就取当前数据 needSave = true; foreach (var rankMgrKV in ServerRankManager.Ranks) { if (rankMgrKV.Value.ServerId != sid) { continue; } foreach (var rank in rankMgrKV.Value.rank) { if (rank.Value.RankType != type) { continue; } ok = true; MsgRankList msgList = new MsgRankList(); msgList.ServerId = sid; msgList.Type = type; foreach (var charId in rank.Value.RankUUIDList) { if (msgList.Items.Count >= ServerRankBackupManager.MAXMember) { break; } DBRank_One one = null; if (!rank.Value.DBRankCache.TryGetValue(charId, out one)) { continue; } MsgRankItemData msgItem = new MsgRankItemData(); msgItem.CharacterId = one.Guid; msgItem.Name = one.Name; msgItem.Value = one.Value; msgList.Items.Add(msgItem); } msg.Response.Data.Add(msgList); break; } break; } } if (!ok) { Logger.Fatal("SSGetRankDataByServerId [{0}]-[{1}]", timeStr, type); } } msg.Reply((int)ErrorCodes.OK); if (needSave) { //需要保存数据库 var passedTime = DateTime.FromBinary(time); var serverl = new List <int>(); foreach (var item in serverList.Items) { serverl.Add(item); } var co = CoroutineFactory.NewSubroutine(ServerRankBackupManager.BackupAllRankCoroutine, coroutine, serverl, passedTime); if (co.MoveNext()) { yield return(co); } } yield break; }
//交换两个玩家的名次 public bool SwapIndex(DBRank_One db1, DBRank_One db2) { return(mImpl.SwapIndex(this, db1, db2)); }