Example #1
0
        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 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;
        }