强化系统界面
强化系统数据表
数据XML解析方式 强化系统数据表的ID是没有参考意义的,所以使用嵌套字典,第一个字典使用pos当key,第二个字典使用starlv当key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #region 强化升级配置 private Dictionary<int ,Dictionary<int , StrongCfg>> strongCfgDic = new Dictionary<int ,Dictionary<int , StrongCfg>>();private void InitStrongCfg (string path ){ TextAsset strongCfgAsset = Resources.Load<TextAsset>(path); if (strongCfgAsset != null ) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(strongCfgAsset.text); XmlNodeList xmlNodeList = xmlDoc.SelectSingleNode("root" ).ChildNodes; for (int i = 0 ; i < xmlNodeList.Count; i++) { XmlElement ele = xmlNodeList[i] as XmlElement; if (ele.GetAttributeNode("ID" ) == null ) { continue ; } int ID = Convert.ToInt32(ele.GetAttributeNode("ID" ).InnerText); StrongCfg strongCfg = new StrongCfg { ID = ID }; foreach (XmlElement element in ele.ChildNodes) { int val = int .Parse(element.InnerText); switch (element.Name) { case "pos" : strongCfg.pos = val; break ; } } Dictionary<int , StrongCfg> dic = null ; if (strongCfgDic.TryGetValue(strongCfg.pos,out dic)) { dic.Add(strongCfg.starlv, strongCfg); } else { dic = new Dictionary<int , StrongCfg>(); dic.Add(strongCfg.starlv , strongCfg); strongCfgDic.Add(strongCfg.pos , dic); } } } } public StrongCfg GetStrongData (int pos,int starlv ){ StrongCfg strongCfg = null ; Dictionary<int ,StrongCfg> dic = null ; if (strongCfgDic.TryGetValue(pos,out dic)) { if (dic.ContainsKey(starlv)) { strongCfg = dic[starlv]; } } return strongCfg; } #endregion
强化系统通信协议 强化系统的通信只需要关键的点,一个是pos,一个是starlv
首先在服务器的GameMsg
——PlayerData
里面声明一个int数组,这样声明能够节省通信流量
1 2 3 4 5 6 7 8 9 10 11 12 13 public class PlayerData { public int guideid; public int [] strongArr; }
而在数据库中,没有存储数组的数据格式,除非使用二进制,我们这里将这个数组转化为一串字符串来存储,形式为:
字符串用Split分割后再转化即可。使用字符串可以能够让我们在数据库管理工具中更加直观的看到数据,二进制是看不到的。
商业项目中,配置数据类是自动生成的,不需要自己写解析。
商业项目中,数据库也是使用ORM框架自动配置,不需要自己写服务器配置方法。
添加强化的Req和Rsp类 每当增加一个模块,就需要定制新的数据交互类:ReqStrong
和ReqGuide
回忆一下我们在上一章中学到的,客户端只需要向服务端发送guideid
,服务端会自己读取配置文件——更新服务器端的playerdata(更新缓存和数据库)——并将奖励封装成RspGuide
发送给客户端。具体的奖励都是由服务器端发送的,客户端读取配置文件中的奖励只是为了UI显示或校验数据(比如判断请求是否符合条件)用的 。
在这一章,当客户端强化时只需要将强化的部位id(pos)发送给服务端,而服务端需要更新的消息比较多,见下面的代码
修改服务器的GameMsg
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #region 引导相关 [System.Serializable ] public class ReqGuide { public int guideid; } [System.Serializable ] public class RspGuide { public int guideid; public int coin; public int lv; public int exp; } #endregion #region 强化相关 [System.Serializable ] public class ReqStrong { public int pos; } [System.Serializable ] public class RspStrong { public int coinoin; public int crystal; public int hp; public int ad; public int ap; public int addef; public int apdef; public int [] stringArr; } #endregion