using Newtonsoft.Json.Linq; using ProtoCSStruct; using Sog; using System; using System.Collections.Generic; public class RewardItemRW : RewardItem { public new long amount; public new int repeat; public RewardItemRW() { } public RewardItemRW(RewardItem r) { this.type = r.type; this.code = r.code; this.amount = r.amount; this.repeat = r.repeat; } public override string ToString() { return "{\"type\":" + type + ",\"code\":\"" + code + "\",\"amount\":" + amount + "\",\"repeat\":" + repeat + "}"; } public bool IsValid() { return type > 0 && !string.IsNullOrEmpty(code) && amount > 0; } } public class RewardItem { public int type;//99-link public string code; public int amount { get; } public int repeat { get; } public RewardItem() { } public RewardItem(int type, string code, int amount, int repeat) { this.type = type; this.code = code; this.amount = amount; this.repeat = repeat; } public RewardItem(int amount, int repeat) { this.amount = amount; this.repeat = repeat; } public RewardItem(RewardItem r) { this.type = r.type; this.code = r.code; this.amount = r.amount; this.repeat = r.repeat; } public override string ToString() { return "{\"type\":" + type + ",\"code\":\"" + code + "\",\"amount\":" + amount + "\",\"repeat\":" + repeat + "}"; } //public bool IsValid() //{ // return type > 0 && !string.IsNullOrEmpty(code) && amount > 0; //} } public class RewardRangeItem { public int rangeMin; public int rangeMax; public List list; } public class RewardWeightItem { public string key; public int weight; public List list; } public class RewardItemTalent { public int max; public int other; public Dictionary> typeList; } public partial class RewardDesc { //public JArray array; public List list;//fix //public Dictionary> map; public List rangeList; //仅针对level zzone和TimesResetByMaxValue的有效 public List weightList;//针对once, arrays public List talentList;//针对talent public List GetAsItemsList(JArray ja) { List list = new List(); for (int i = 0; i < ja.Count; i++) { var a = ja[i] as JObject; int amount = 0, repeat = 0; if (a.ContainsKey("amount")) { amount = a["amount"].Value(); } if (a.ContainsKey("repeat")) { repeat = a["repeat"].Value(); } RewardItem item = new RewardItem(amount, repeat); item.type = a["type"].Value(); item.code = a["code"].Value(); list.Add(item); //TraceLog.Trace("{0}", item); //check if(item.type == (int)GoodsType.Props) { var d = EquipAttrDescMgr.Instance.GetConfig(item.code); if(d == null) { TraceLog.Error(" EquipAttr {0} not exist", item.code); throw new Exception("EquipAttr not exist id=" + item.code); } } } //TraceLog.Trace("{0}", list.Count); return list; } public Dictionary> GetAsItemsMap(JArray array) { Dictionary> dict = new Dictionary>(); //[{"15":[{"type":9,"code":"reward1","amount":99}]},{"3":[{"type":9,"code":"reward2","amount":999}]}] for (int i = 0; i < array.Count; i++) { JObject ja = array[i] as JObject; //{"15":[{"type":9,"code":"reward1","amount":99}]} var it = ja.GetEnumerator(); it.MoveNext(); var pair = it.Current; //TraceLog.Trace("{0} {1}", pair.Key, pair.Value); if (pair.Value.Type == JTokenType.String) { dict[pair.Key] = new List(); } else { dict[pair.Key] = GetAsItemsList(pair.Value.Value()); } } return dict; } public List ParseToItemsList(JArray array) { List list = new List(); //[{"15":[{"type":9,"code":"reward1","amount":99}]},{"3":[{"type":9,"code":"reward2","amount":999}]}] for (int i = 0; i < array.Count; i++) { RewardWeightItem rw = new RewardWeightItem(); JObject ja = array[i] as JObject; //{"15":[{"type":9,"code":"reward1","amount":99}]} var it = ja.GetEnumerator(); it.MoveNext(); var pair = it.Current; //TraceLog.Trace("{0} {1}", pair.Key, pair.Value); rw.key = pair.Key; rw.weight = 0;//需要实时计算 if (pair.Value.Type == JTokenType.String) { rw.list = new List(); } else { rw.list = GetAsItemsList(pair.Value.Value()); } list.Add(rw); } return list; } } public partial class RewardDescMgr { public override void ReadComplete() { foreach (RewardDesc desc in ItemTable.Values) { //TraceLog.Trace("parse json {0}", desc.content); try { JArray array = JArray.Parse(desc.content); if (desc.randomType == RewardRandomType.Fixed) { desc.list = desc.GetAsItemsList(array); } else if (desc.randomType == RewardRandomType.Level || desc.randomType == RewardRandomType.Zone || desc.randomType == RewardRandomType.Appoint || desc.randomType == RewardRandomType.ContinueBattle ) { var map = desc.GetAsItemsMap(array); desc.rangeList = new List(); RewardRangeItem last = null; foreach (var item in map) { RewardRangeItem rli = new RewardRangeItem(); rli.rangeMin = int.Parse(item.Key); if (last != null) { last.rangeMax = rli.rangeMin; } rli.list = item.Value; desc.rangeList.Add(rli); last = rli; } last.rangeMax = int.MaxValue; desc.rangeList.Sort((a, b) => a.rangeMin - b.rangeMin); } else if (desc.randomType == RewardRandomType.TimesResetByMaxValue) { desc.rangeList = new List(); RewardRangeItem last = null; for (int i = 0; i < array.Count; i++) { JObject ja = array[i] as JObject; var it = ja.GetEnumerator(); it.MoveNext(); var pair = it.Current; string key = pair.Key; RewardRangeItem rli = new RewardRangeItem(); rli.rangeMin = int.Parse(key); if (last != null) { last.rangeMax = rli.rangeMin; } if (pair.Value.Type == JTokenType.String ) { //if (pair.Value.Equals("Reset")) rli.list = new List(); rli.rangeMax = last.rangeMin; break; } else { rli.list = desc.GetAsItemsList(pair.Value.Value()); if(i == array.Count - 1) { rli.rangeMax = int.MaxValue; } } desc.rangeList.Add(rli); last = rli; } //desc.rangeList.Sort((a, b) => a.rangeMin - b.rangeMin); } else if(desc.randomType == RewardRandomType.Talent) { desc.talentList = new List(); for (int i = 0; i < array.Count; i++) { JObject ja = array[i] as JObject; RewardItemTalent rli = new RewardItemTalent(); rli.max = ja["Max"].Value(); rli.other = ja["Other"].Value(); rli.typeList = new Dictionary>(); var lmg = ja["LMG"] as JArray; rli.typeList["LMG"] = desc.GetAsItemsList(lmg); var sr = ja["SR"] as JArray; rli.typeList["SR"] = desc.GetAsItemsList(sr); var ar = ja["AR"] as JArray; rli.typeList["AR"] = desc.GetAsItemsList(ar); desc.talentList.Add(rli); } } else//once arrays { desc.weightList = desc.ParseToItemsList(array); } } catch (Exception ex) { TraceLog.Error("Parse Reward Json error {0}", desc.content); throw new Exception("Parse Reward Json error id="+desc.id); } } } }