You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

314 lines
10 KiB

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<RewardItem> list;
}
public class RewardWeightItem
{
public string key;
public int weight;
public List<RewardItem> list;
}
public class RewardItemTalent
{
public int max;
public int other;
public Dictionary<string, List<RewardItem>> typeList;
}
public partial class RewardDesc
{
//public JArray array;
public List<RewardItem> list;//fix
//public Dictionary<string, List<RewardItem>> map;
public List<RewardRangeItem> rangeList; //仅针对level zzone和TimesResetByMaxValue的有效
public List<RewardWeightItem> weightList;//针对once, arrays
public List<RewardItemTalent> talentList;//针对talent
public List<RewardItem> GetAsItemsList(JArray ja)
{
List<RewardItem> list = new List<RewardItem>();
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<int>();
}
if (a.ContainsKey("repeat"))
{
repeat = a["repeat"].Value<int>();
}
RewardItem item = new RewardItem(amount, repeat);
item.type = a["type"].Value<int>();
item.code = a["code"].Value<string>();
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<string, List<RewardItem>> GetAsItemsMap(JArray array)
{
Dictionary<string, List<RewardItem>> dict = new Dictionary<string, List<RewardItem>>();
//[{"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<RewardItem>();
}
else
{
dict[pair.Key] = GetAsItemsList(pair.Value.Value<JArray>());
}
}
return dict;
}
public List<RewardWeightItem> ParseToItemsList(JArray array)
{
List<RewardWeightItem> list = new List<RewardWeightItem>();
//[{"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<RewardItem>();
}
else
{
rw.list = GetAsItemsList(pair.Value.Value<JArray>());
}
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>();
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>();
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<RewardItem>();
rli.rangeMax = last.rangeMin;
break;
}
else
{
rli.list = desc.GetAsItemsList(pair.Value.Value<JArray>());
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<RewardItemTalent>();
for (int i = 0; i < array.Count; i++)
{
JObject ja = array[i] as JObject;
RewardItemTalent rli = new RewardItemTalent();
rli.max = ja["Max"].Value<int>();
rli.other = ja["Other"].Value<int>();
rli.typeList = new Dictionary<string, List<RewardItem>>();
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);
}
}
}
}