static void RandomPoolTest() { RandomWeightPool <Item> rp = new RandomWeightPool <Item>(); rp.SetItemWeight(new Item() { Name = "5" }, 5); rp.SetItemWeight(new Item() { Name = "10" }, 10); Dictionary <Item, int> counter = new Dictionary <Item, int>(); for (int i = 0; i < 100000; i++) { var it = rp.GetItem(); if (!counter.ContainsKey(it)) { counter.Add(it, 0); } counter[it]++; } foreach (var p in counter) { Console.WriteLine(p.Key.Name + "__" + p.Value); } Console.ReadKey(); }
/// <summary> /// 运行时等所有SLK数据载入后,可以把ID引用变成真的对象引用 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="idPool"></param> /// <returns></returns> public static RandomWeightPool <T> ParseIdPool <T>(RandomWeightPool <string> idPool) where T : SlkDataObject { RandomWeightPool <T> pool = new RandomWeightPool <T>(); foreach (var pair in idPool.GetMapCopy()) { //TODO } return(pool); }
public static RandomWeightPool <string> Config2IdPool(string data) { RandomWeightPool <string> pool = new RandomWeightPool <string>(); if (string.IsNullOrEmpty(data)) { return(pool); } string[] srr = data.Split(SplitChar); foreach (var s in srr) { string[] pair = s.Split('|'); if (pair.Length > 1) { pool.SetItemWeight(pair[0], float.Parse(pair[1])); } } return(pool); }
public static string IdPool2Config(RandomWeightPool <string> idPool) { StringBuilder sb = new StringBuilder(); bool isFirst = true; foreach (var pair in idPool.GetMapCopy()) { if (!isFirst) { sb.Append(SplitChar); } else { isFirst = false; } sb.Append(string.Format("{0}|{1}", pair.Key, pair.Value)); } return(sb.ToString()); }