123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class ChatMsg
- {
- public enum Category
- {
- System = 0,
- Clan = 1,
- Follow = 2,
- Request = 3,
- Replay = 4,
- }
- private static Dictionary<int, Category> catDict;
- public static Category GetCategoryByCode(int code)
- {
- if (catDict == null) {
- catDict = new Dictionary<int, Category> ();
- System.Array arr = System.Enum.GetValues (typeof(Category));
- for(int i=0; i<arr.Length; i++)
- {
- Category cat = (Category)arr.GetValue (i);
- catDict.Add (cat.GetHashCode (), cat);
- }
- }
- return catDict[code];
- }
- public int id;
- public Category categovy;
- public int catParam;
- public int sender;
- public int receiver;
- public int contentType;
- public string content = "";
- public string extra = "";
- public System.DateTime dateTime;
- private JsonData json;
- public ChatMsg()
- {
- }
- public ChatMsg(JsonData json)
- {
- this.json = json;
- ParseJson (json);
- }
- public ChatMsg (string jsonStr)
- {
- JsonData json = JsonMapper.ToObject (jsonStr);
- ParseJson (json);
- }
- public void SetDateTime(string time)
- {
- dateTime = System.Convert.ToDateTime(time);
- }
- private void ParseJson(JsonData json)
- {
- id = JsonUtil.ToInt (json["id"]);
- categovy = GetCategoryByCode (JsonUtil.ToInt(json["category"]));
- catParam = JsonUtil.ToInt (json["catparam"]);
- sender = JsonUtil.ToInt (json["sender"]);
- receiver = JsonUtil.ToInt (json["receiver"]);
- contentType = JsonUtil.ToInt (json["type"]);
- content = WWW.UnEscapeURL (json["content"].ToString());
- extra = json ["extra"].ToString ();
- SetDateTime (json ["time"].ToString ());
- }
- public JsonData GetJson()
- {
- if (json == null) {
- json = JsonMapper.ToObject ("{}");
- json["id"] = id;
- json["category"] = categovy.GetHashCode();
- json["catparam"] = catParam;
- json["sender"] = sender;
- json["receiver"] = receiver;
- json["type"] = contentType.GetHashCode();
- json["content"] = content;
- json ["extra"] = extra;
- json ["time"] = dateTime.ToString ();
- }
- return json;
- }
- public string GetTimeStr()
- {
- long nowTicks = loginDateTime.Ticks + (long) (GameTime.realtimeSinceStartup - loginTime) * 10000000;
- long ticks = dateTime.Ticks;
- int deltaTime = (int)((nowTicks - ticks)/10000000f);
- int day = deltaTime / (24 * 60 * 60);
- int temp = deltaTime % (24 * 60 * 60);
- int hour = temp / (60 * 60);
- temp = temp % (60 * 60);
- int min = temp / 60;
- string str = "";
- if(day > 0)
- str += day.ToString () + Language.GetStr ("Time", "d") + " ";
- if(hour > 0)
- str += hour.ToString () + Language.GetStr ("Time", "h") + " ";
- if(min > 0)
- str += min.ToString () + Language.GetStr ("Time", "min") + " ";
- if (StringUtil.Empty (str))
- str = Language.GetStr ("Time", "justNow");
- else
- str += Language.GetStr ("Time", "ago");
- return str;
- }
- private static float loginTime;
- private static System.DateTime loginDateTime;
- public static void InitDateTime(string loginDateTime)
- {
- ChatMsg.loginDateTime = System.Convert.ToDateTime(loginDateTime);
- loginTime = GameTime.realtimeSinceStartup;
- }
- public static System.DateTime GetNowDateTime()
- {
- long ticks = loginDateTime.Ticks + (long) (GameTime.realtimeSinceStartup - loginTime) * 10000000;
- return new System.DateTime(ticks);
- }
- private ReplayInfo replayInfo;
- public ReplayInfo GetReplayInfo()
- {
- if (replayInfo == null) {
- replayInfo = new ReplayInfo (content);
- }
- return replayInfo;
- }
- }
|