UserData.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Globalization;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class UserData
  6. {
  7. public enum From
  8. {
  9. ios,
  10. }
  11. public UnityEvent NickChanged = new UnityEvent ();
  12. public UnityEvent ClanChanged = new UnityEvent();
  13. public UnityEvent CoinChanged = new UnityEvent();
  14. public UnityEvent DiamondChanged = new UnityEvent ();
  15. public int id;
  16. public string snsId;
  17. public string email;
  18. public string deviceId;
  19. private string _nick = "";
  20. public int exp;
  21. public int win;
  22. public int lose;
  23. public string from = "ios";
  24. private int _rank;
  25. private int _coin;
  26. private int _diamond;
  27. public int rankIndex;
  28. public bool vipNextRound;
  29. private double _vipExpireDay;
  30. private bool _isVIP;
  31. private bool _isFullVIP;
  32. private IFormatProvider culture = new CultureInfo("zh-cn", true);
  33. public int[] craftSelectLimit;
  34. public bool isLogin;
  35. public UserData ()
  36. {
  37. }
  38. public int GetBadge()
  39. {
  40. return GetBadgeByRank(rank);
  41. }
  42. public static int GetBadgeByRank(int rank)
  43. {
  44. int badge = (int)Mathf.Floor((float)rank/1500f);
  45. badge += 1;
  46. if(badge > 30)
  47. {
  48. badge = 30;
  49. }
  50. return badge;
  51. }
  52. public static Sprite GetBadgeIcon(int rank)
  53. {
  54. return Resources.Load<Sprite>("Textures/BadgeIcon/badge"+StringUtil.FillZero(GetBadgeByRank(rank), 4));
  55. }
  56. public string idStr
  57. {
  58. get{
  59. return "#" + id;
  60. }
  61. }
  62. private int m_ClanId;
  63. public int clanId
  64. {
  65. set{
  66. m_ClanId = value;
  67. ClanChanged.Invoke ();
  68. }
  69. get{
  70. return m_ClanId;
  71. }
  72. }
  73. public string nick
  74. {
  75. set{
  76. _nick = NickFormat(value, id);
  77. NickChanged.Invoke ();
  78. }
  79. get{
  80. return _nick;
  81. }
  82. }
  83. public static string NickFormat(string nick, int userId)
  84. {
  85. if (StringUtil.Empty (nick))
  86. return "#"+userId;
  87. return nick;
  88. }
  89. public static string NickFormat(LitJson.JsonData jsonNick, int userId)
  90. {
  91. if (jsonNick != null)
  92. return NickFormat (WWW.UnEscapeURL(jsonNick.ToString()), userId);
  93. return NickFormat(null, userId);
  94. }
  95. public int rank
  96. {
  97. set{
  98. _rank = value;
  99. if (id != Session.GetInstance ().myUserData.id)
  100. return;
  101. RankNum[] rankNums = GameObject.FindObjectsOfType<RankNum>();
  102. for(int i=0; i<rankNums.Length; i++)
  103. {
  104. rankNums[i].UpdateRank(value);
  105. }
  106. }
  107. get{
  108. return _rank;
  109. }
  110. }
  111. public int coin
  112. {
  113. set{
  114. _coin = value;
  115. CoinChanged.Invoke ();
  116. }
  117. get{
  118. return _coin;
  119. }
  120. }
  121. public int diamond
  122. {
  123. set{
  124. _diamond = value;
  125. DiamondChanged.Invoke ();
  126. }
  127. get{
  128. return _diamond;
  129. }
  130. }
  131. public string vipExpireTime
  132. {
  133. set
  134. {
  135. DateTime dt = DateTime.ParseExact(value, "yyyy-MM-dd HH:mm:ss", culture);
  136. TimeSpan ts = dt.Subtract(DateTime.Now);
  137. _isFullVIP = _isVIP = (ts.TotalSeconds > 0);
  138. _vipExpireDay = ts.TotalDays;
  139. // _isFullVIP = (ts.TotalDays > 30);
  140. if (id != Session.GetInstance ().myUserData.id)
  141. return;
  142. CraftSelectionItem[] craftItems = GameObject.FindObjectsOfType<CraftSelectionItem>();
  143. for(int i=0; i<craftItems.Length; i++)
  144. {
  145. craftItems[i].Refresh();
  146. }
  147. ShopItem[] shopItems = GameObject.FindObjectsOfType<ShopItem>();
  148. for(int i=0; i<shopItems.Length; i++)
  149. {
  150. shopItems[i].Refresh();
  151. }
  152. }
  153. }
  154. public void RemoveVIPExpireTime()
  155. {
  156. _isVIP = false;
  157. _vipExpireDay = 0;
  158. _isFullVIP = false;
  159. }
  160. public bool IsVIP()
  161. {
  162. return _isVIP || vipNextRound;
  163. }
  164. public double GetVipExpireDay()
  165. {
  166. return _vipExpireDay;
  167. }
  168. public bool IsFullVIP()
  169. {
  170. return _isFullVIP;
  171. }
  172. public bool CanBuy(BuyItemData data)
  173. {
  174. if(data.GetCurrency() == BuyUtil.Currency.Diamond)
  175. {
  176. if(data.GetPrice() > diamond)
  177. {
  178. BuyDiamondPanel.Show();
  179. return false;
  180. }
  181. }
  182. else if(data.GetCurrency() == BuyUtil.Currency.Coin)
  183. {
  184. if(data.GetPrice() > coin)
  185. {
  186. BuyCoinPanel.Show();
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. public override string ToString ()
  193. {
  194. return string.Format ("[UserData: nick={0}, rank={1}, id={2}", nick, rank, id);
  195. }
  196. }