Exemple #1
0
 static Dictionary<string, int> GetPacketAwards(int maxCount, Variant packate)
 {
     Dictionary<string, int> awards = new Dictionary<string, int>(maxCount);
     // 已获得的物品数
     int currentCount = 0;
     List<string> goods = new List<string>(packate.Count);
     List<int> pList = new List<int>(packate.Count);
     List<int> maxS = new List<int>(packate.Count);
     int totalP = 0;
     foreach (var gg in packate)
     {
         if (gg.Value is Variant)
         {
             Variant v = gg.Value as Variant;
             //int count = v.GetIntOrDefault("Min");
             //if (count > 0)
             //{
             //    awards.SetOrInc(gg.Key, count);
             //    currentCount += count;
             //    if (maxCount <= currentCount)
             //    {
             //        return awards;
             //    }
             //}
             int p = (int)(v.GetDoubleOrDefault("P") * 10000);
             if (p <= 0) p = 1;
             totalP += p;
             pList.Add(totalP);
             goods.Add(gg.Key);
             maxS.Add(v.GetIntOrDefault("Max"));
         }
     }
     while (totalP > 0 && maxCount > currentCount)
     {
         int hit = NumberRandom.Next(totalP);
         for (int i = 0; i < pList.Count; i++)
         {
             int curP = pList[i];
             if (hit < pList[i]) //命中
             {
                 int count = awards.SetOrInc(goods[i], 1);
                 if (count >= maxS[i])
                 {
                     if (count > maxS[i])
                     {
                         currentCount--;
                         awards.SetOrInc(goods[i], -1);
                     }
                     //删除...
                     totalP -= curP;
                     goods.RemoveAt(i);
                     maxS.RemoveAt(i);
                     ReSetPList(ref pList, i);
                     if (pList.Count == 0)
                     {
                         return awards;
                     }
                 }
                 currentCount++;
                 break;
             }
         }
     }
     return awards;
 }
        /// <summary>
        /// 得到指定字段的数量统计
        /// </summary>
        /// <param name="bsonDoc"></param>
        /// <returns></returns>
        BsonDocument LevelDoc(IEnumerable<BsonDocument> bsonDoc)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            foreach (BsonDocument doc in bsonDoc)
            {
                int level = 0;
                int count = 0;
                foreach (BsonElement k in doc)
                {
                    switch (k.Name)
                    {
                        case "Value.PetsLevel"://宠物等级
                        case "Value.PetsRank": //宠物等级
                        case "Value.ZiZhi":    //宠物资质
                        case "Level":          //角色等级

                            level = Convert.ToInt32(k.Value);
                            break;
                        case "Count":
                            count = Convert.ToInt32(k.Value);
                            break;
                    }
                }
                dic.SetOrInc(level.ToString(), count);
            }
            return new BsonDocument(dic);
        }
Exemple #3
0
        /// <summary>
        /// 计算单个奖励包裹
        /// P:  double      //获得的概率..
        /// TMax :6          //最大获得数量
        /// G_*1: P:double, Min:1,Max:2   //物品g1获得的概率为0.8...可获得1-2个..
        /// G_*2: P:double, Min:1,Max:5   //...............
        /// </summary>
        /// <param name="packate">格式:{P:0.8,TMax:6,G_*1:{P:0.2,Min:1,Max:2},G_*2:{P:0.3, Min:1,Max:5},...}</param>
        /// <param name="awards">奖励结果(物品ID:数量)</param>
        public static void GetPacketAwards(Variant packate, Dictionary<string, int> awards)
        {
            //P:  double 获得的概率.
            double p = packate.GetDoubleOrDefault("P");
            if (!NumberRandom.RandomHit(p)) return;

            // TMax 最大获得数量
            int maxCount = packate.GetIntOrDefault("TMax");
            if (maxCount <= 0)
            {
                maxCount = 5;
            }
            maxCount = NumberRandom.Next(maxCount) + 1;

            var awardP = GetPacketAwards(maxCount, packate);
            foreach (var v in awardP)
            {
                awards.SetOrInc(v.Key, v.Value);
            }
            //// 已获得的物品数
            //int currentCount = 0;
            //foreach (var gg in packate)
            //{
            //    if (maxCount <= currentCount)
            //    {
            //        return;
            //    }
            //    if (gg.Value is Variant)
            //    {
            //        int count = GetAwardCount(gg.Value as Variant);
            //        if (count > 0)
            //        {
            //            if (count + currentCount > maxCount)
            //            {
            //                count = maxCount - currentCount;
            //            }
            //            int rc;
            //            awards.TryGetValue(gg.Key, out rc);
            //            awards[gg.Key] = rc + count;
            //            currentCount += count;
            //        }
            //    }
            //}
        }