ChatMsg.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. public class ChatMsg
  6. {
  7. public enum Category
  8. {
  9. System = 0,
  10. Clan = 1,
  11. Follow = 2,
  12. Request = 3,
  13. Replay = 4,
  14. }
  15. private static Dictionary<int, Category> catDict;
  16. public static Category GetCategoryByCode(int code)
  17. {
  18. if (catDict == null) {
  19. catDict = new Dictionary<int, Category> ();
  20. System.Array arr = System.Enum.GetValues (typeof(Category));
  21. for(int i=0; i<arr.Length; i++)
  22. {
  23. Category cat = (Category)arr.GetValue (i);
  24. catDict.Add (cat.GetHashCode (), cat);
  25. }
  26. }
  27. return catDict[code];
  28. }
  29. public int id;
  30. public Category categovy;
  31. public int catParam;
  32. public int sender;
  33. public int receiver;
  34. public int contentType;
  35. public string content = "";
  36. public string extra = "";
  37. public System.DateTime dateTime;
  38. private JsonData json;
  39. public ChatMsg()
  40. {
  41. }
  42. public ChatMsg(JsonData json)
  43. {
  44. this.json = json;
  45. ParseJson (json);
  46. }
  47. public ChatMsg (string jsonStr)
  48. {
  49. JsonData json = JsonMapper.ToObject (jsonStr);
  50. ParseJson (json);
  51. }
  52. public void SetDateTime(string time)
  53. {
  54. dateTime = System.Convert.ToDateTime(time);
  55. }
  56. private void ParseJson(JsonData json)
  57. {
  58. id = JsonUtil.ToInt (json["id"]);
  59. categovy = GetCategoryByCode (JsonUtil.ToInt(json["category"]));
  60. catParam = JsonUtil.ToInt (json["catparam"]);
  61. sender = JsonUtil.ToInt (json["sender"]);
  62. receiver = JsonUtil.ToInt (json["receiver"]);
  63. contentType = JsonUtil.ToInt (json["type"]);
  64. content = WWW.UnEscapeURL (json["content"].ToString());
  65. extra = json ["extra"].ToString ();
  66. SetDateTime (json ["time"].ToString ());
  67. }
  68. public JsonData GetJson()
  69. {
  70. if (json == null) {
  71. json = JsonMapper.ToObject ("{}");
  72. json["id"] = id;
  73. json["category"] = categovy.GetHashCode();
  74. json["catparam"] = catParam;
  75. json["sender"] = sender;
  76. json["receiver"] = receiver;
  77. json["type"] = contentType.GetHashCode();
  78. json["content"] = content;
  79. json ["extra"] = extra;
  80. json ["time"] = dateTime.ToString ();
  81. }
  82. return json;
  83. }
  84. public string GetTimeStr()
  85. {
  86. long nowTicks = loginDateTime.Ticks + (long) (GameTime.realtimeSinceStartup - loginTime) * 10000000;
  87. long ticks = dateTime.Ticks;
  88. int deltaTime = (int)((nowTicks - ticks)/10000000f);
  89. int day = deltaTime / (24 * 60 * 60);
  90. int temp = deltaTime % (24 * 60 * 60);
  91. int hour = temp / (60 * 60);
  92. temp = temp % (60 * 60);
  93. int min = temp / 60;
  94. string str = "";
  95. if(day > 0)
  96. str += day.ToString () + Language.GetStr ("Time", "d") + " ";
  97. if(hour > 0)
  98. str += hour.ToString () + Language.GetStr ("Time", "h") + " ";
  99. if(min > 0)
  100. str += min.ToString () + Language.GetStr ("Time", "min") + " ";
  101. if (StringUtil.Empty (str))
  102. str = Language.GetStr ("Time", "justNow");
  103. else
  104. str += Language.GetStr ("Time", "ago");
  105. return str;
  106. }
  107. private static float loginTime;
  108. private static System.DateTime loginDateTime;
  109. public static void InitDateTime(string loginDateTime)
  110. {
  111. ChatMsg.loginDateTime = System.Convert.ToDateTime(loginDateTime);
  112. loginTime = GameTime.realtimeSinceStartup;
  113. }
  114. public static System.DateTime GetNowDateTime()
  115. {
  116. long ticks = loginDateTime.Ticks + (long) (GameTime.realtimeSinceStartup - loginTime) * 10000000;
  117. return new System.DateTime(ticks);
  118. }
  119. private ReplayInfo replayInfo;
  120. public ReplayInfo GetReplayInfo()
  121. {
  122. if (replayInfo == null) {
  123. replayInfo = new ReplayInfo (content);
  124. }
  125. return replayInfo;
  126. }
  127. }