UserStatistics.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using System.Threading;
  8. using System.Data;
  9. using StackExchange.Redis;
  10. using CSharpUtil;
  11. using Newtonsoft.Json.Linq;
  12. using System.Diagnostics;
  13. namespace DataTransfer.tongji
  14. {
  15. class UserStatistics
  16. {
  17. public static void DataDeal()
  18. {
  19. LoseUserTargetCollect();
  20. }
  21. /// <summary>
  22. /// 流失玩家的指标的统计
  23. /// </summary>
  24. public static void LoseUserTargetCollect()
  25. {
  26. Console.WriteLine("进入流失玩家的指标的统计方法..............");
  27. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  28. float curday = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
  29. float day = curday - 1;
  30. //float day = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
  31. Console.WriteLine("当前时间day==.............."+ day);
  32. var mem = Redis.Ins.GetDatabase(0);
  33. List<int> retainedlist = new List<int>();
  34. retainedlist.Add(1);
  35. retainedlist.Add(2);
  36. retainedlist.Add(3);
  37. retainedlist.Add(4);
  38. retainedlist.Add(5);
  39. retainedlist.Add(6);
  40. retainedlist.Add(7);
  41. //retainedlist.Add(14);
  42. //retainedlist.Add(30);
  43. HashEntry[] zoneidList = mem.HashGetAll(MemKey_Statistics.Zonelist());
  44. foreach (var item in zoneidList)
  45. {
  46. string[] sList = item.ToString().Split(':');
  47. int zoneid = int.Parse(sList[0]);//区
  48. Console.WriteLine("zoneid............"+zoneid);
  49. userNumRecord(zoneid);//统计下每天的人数
  50. //下面是流失人员指标统计
  51. foreach (int type in retainedlist)
  52. {
  53. Console.WriteLine("type............" + type);
  54. List<string> loseUIdList = loseUserListCollect(type,zoneid,day);
  55. if (loseUIdList.Count <= 0)
  56. {
  57. continue;
  58. }
  59. TargetCollect(loseUIdList,type,zoneid, day);
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// 统计流失玩家uid
  65. /// </summary>
  66. /// <param name="type"></param>
  67. /// <param name="zoneid"></param>
  68. /// <param name="day"></param>
  69. public static List<string> loseUserListCollect(int type,int zoneid, float day)
  70. {
  71. //次日留存
  72. string dayKey = MemKey_Statistics.GamerunLoginUser(zoneid, day);
  73. string lastDayKey = MemKey_Statistics.GamerunLoginUser(zoneid, day - type);
  74. List<string> loseUIdList = new List<string>();
  75. var mem = Redis.Ins.GetDatabase(0);
  76. if (!mem.KeyExists(dayKey) || !mem.KeyExists(lastDayKey))
  77. {
  78. return loseUIdList;
  79. }
  80. string[] list = mem.HashKeys(dayKey).ToStringArray();
  81. string[] nextDaylist = mem.HashKeys(lastDayKey).ToStringArray();
  82. foreach (string uid in nextDaylist)
  83. {
  84. if (!list.Contains(uid))
  85. {
  86. Console.WriteLine("uid............" + uid);
  87. loseUIdList.Add(uid);
  88. }
  89. }
  90. return loseUIdList;
  91. }
  92. /// <summary>
  93. /// 查看次日流失玩家的指标信息
  94. /// </summary>
  95. /// <param name="loseUIdList"></param>
  96. /// <param name="zoneid"></param>
  97. /// <param name="day"></param>
  98. public static void TargetCollect(List<string> loseUIdList,int type,int zoneid,float day)
  99. {
  100. var mem = Redis.Ins.GetDatabase(0);
  101. foreach (string uid in loseUIdList)
  102. {
  103. if (!mem.KeyExists(MemKey_User.gameInfo(uid, zoneid)))
  104. {
  105. continue;
  106. }
  107. #region----------玩家等级
  108. //玩家等级
  109. string baseInfo = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "baseInfo");
  110. JObject baseInfoDic = JObject.Parse(baseInfo);
  111. int level = int.Parse(baseInfoDic["level"].ToString());
  112. string k = "level-" + level;
  113. if (mem.HashExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day-type), k))
  114. {
  115. mem.HashIncrement(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), k, 1);
  116. }
  117. else
  118. {
  119. List<HashEntry> list = new List<HashEntry>();
  120. HashEntry d = new HashEntry(k, 1);
  121. list.Add(d);
  122. mem.HashSet(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), list.ToArray());
  123. }
  124. #endregion
  125. #region--------------主线任务
  126. //主线任务
  127. string privateState = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "privateState");
  128. JObject privateStateDic = JObject.Parse(privateState); //
  129. JObject jtaskCardTracing = JObject.Parse(privateStateDic["taskCardTracing"].ToString());
  130. string taskId = "0";
  131. if (jtaskCardTracing.ContainsKey("1"))//主线
  132. {
  133. taskId = jtaskCardTracing["1"].ToString();
  134. }
  135. string store = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "store");
  136. JObject storeDic = JObject.Parse(store);
  137. JObject taskcardsDic = JObject.Parse(storeDic["taskcards"].ToString());
  138. if (taskId != "0" )
  139. {
  140. JObject dict = JObject.Parse(taskcardsDic[taskId].ToString());
  141. string typeId = dict["typeId"].ToString();
  142. string taskKey = "task-" + typeId;
  143. if (mem.HashExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), taskKey))
  144. {
  145. mem.HashIncrement(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), taskKey, 1);
  146. }
  147. else
  148. {
  149. List<HashEntry> list = new List<HashEntry>();
  150. HashEntry d = new HashEntry(taskKey, 1);
  151. list.Add(d);
  152. mem.HashSet(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), list.ToArray());
  153. }
  154. }
  155. #endregion
  156. #region--------------关卡
  157. string gates = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "gates");
  158. JObject gatesDic = JObject.Parse(gates);
  159. JObject normalsDic = JObject.Parse(gatesDic["normal"].ToString());
  160. JObject lastgatesDic = JObject.Parse(normalsDic["gates"].ToString());
  161. int gateId = 0;
  162. foreach (KeyValuePair<string,JToken>kv in lastgatesDic)
  163. {
  164. gateId = int.Parse(kv.Key);
  165. }
  166. string gateKey = "gateId-" + gateId;
  167. if (mem.HashExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), gateKey))
  168. {
  169. mem.HashIncrement(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), gateKey, 1);
  170. }
  171. else
  172. {
  173. List<HashEntry> list = new List<HashEntry>();
  174. HashEntry d = new HashEntry(gateKey, 1);
  175. list.Add(d);
  176. mem.HashSet(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type), list.ToArray());
  177. }
  178. #endregion
  179. }
  180. loseUserDataTransfer.excelText(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day - type),type,zoneid,day - type);
  181. }
  182. /// <summary>
  183. /// 每天登陆人数纪录----以防刚哥记得的uid的rediskey删除
  184. /// </summary>
  185. /// <param name="zoneid"></param>
  186. public static void userNumRecord(int zoneid)
  187. {
  188. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  189. float day = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
  190. var mem = Redis.Ins.GetDatabase(0);
  191. HashEntry[] ctxList = mem.HashGetAll(MemKey_Statistics.GamerunLoginUser(zoneid, day - 1));
  192. int length = ctxList.Length;
  193. List<HashEntry> list = new List<HashEntry>();
  194. HashEntry d = new HashEntry(day-1, length);
  195. list.Add(d);
  196. mem.HashSet(MemKey_Statistics.userNumRecord(zoneid), list.ToArray());
  197. }
  198. }
  199. }