Program.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Threading;
  3. using System.Data;
  4. using StackExchange.Redis;
  5. using CSharpUtil;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using System.Diagnostics;
  11. using DataTransfer.tongji;
  12. namespace DataTransfer
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. Console.WriteLine("言灵数据传输开始........");
  19. Thread t1 = new Thread(transfer);
  20. t1.Start();
  21. }
  22. public static int mask = 0;
  23. public static int mask_lose = 0;
  24. //public static int mask_usernum = 0;
  25. public static void transfer()
  26. {
  27. Console.WriteLine("transfer............");
  28. while (true)
  29. {
  30. try
  31. {
  32. int hour = DateTime.Now.Hour;
  33. if (hour == 2 && mask == 0)//正式的是晚上2点进行数据传输
  34. {
  35. Console.WriteLine("数据传输的时间到了开始执行程序.......");
  36. //进行数据传输
  37. mask = 1;
  38. dataTransfer();
  39. }
  40. if (hour != 2 && mask != 0)
  41. {
  42. mask = 0;
  43. }
  44. #region------流失玩家指标分析
  45. if (hour == 12 && mask_lose == 0)//正式的是晚上2点进行数据传输
  46. {
  47. Console.WriteLine("统计信息程序开始执行............");
  48. //进行数据传输
  49. mask_lose = 1;
  50. UserStatistics.DataDeal();
  51. }
  52. if (hour != 12 && mask_lose != 0)
  53. {
  54. mask_lose = 0;
  55. }
  56. #endregion
  57. }
  58. catch (Exception ex)
  59. {
  60. Console.WriteLine(ex.Message); // 提示套接字监听异常
  61. break;
  62. }
  63. }
  64. }
  65. public static void dataTransfer()
  66. {
  67. Console.WriteLine("进入dataTransfer方法..............");
  68. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  69. float day = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
  70. Console.WriteLine("当前天是:" + day);
  71. var mem = Redis.Ins.GetDatabase(0);
  72. //int zoneid = 1;//暂时先这样
  73. HashEntry[] zoneidList = mem.HashGetAll(MemKey_Statistics.Zonelist());
  74. foreach (var item in zoneidList)
  75. {
  76. string[] sList = item.ToString().Split(':');
  77. int zoneid = int.Parse(sList[0]);
  78. Taskprogressrecord_RedisToSql(zoneid);
  79. if (mem.KeyExists(MemKey_Statistics.GamerunLoginUser(zoneid, day)))
  80. {
  81. string[] list = mem.HashKeys(MemKey_Statistics.GamerunLoginUser(zoneid, day)).ToStringArray();
  82. foreach (string k in list)
  83. {
  84. Console.WriteLine("uid:" + k);
  85. redisToSql(k, zoneid);
  86. }
  87. }
  88. else
  89. {
  90. Console.WriteLine(day+"-没有玩家登录,以玩家为单位的数据不做传输");
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// redis数据传输到mysql中
  96. /// </summary>
  97. /// <param name="uid"></param>
  98. /// <param name="zoneid"></param>
  99. public static void redisToSql(string uid,int zoneid)
  100. {
  101. Console.WriteLine("以玩家为单位数据传输开始..........:" );
  102. Userbaseparams_RedisToSql(uid,zoneid);
  103. TaskcompleteTsrecord_RedisToSql(uid,zoneid);
  104. }
  105. const string Userbaseparams_TableName = "tab_userbaseparams";
  106. const string TaskcompleteTsrecord_TableName = "tab_taskcompleteTsrecord";
  107. const string Taskprogressrecord_TableName = "tab_taskprogressrecord";
  108. /// <summary>
  109. /// 界面内数值变化数据传输
  110. /// </summary>
  111. /// <param name="uid"></param>
  112. /// <param name="zoneid"></param>
  113. public static void Userbaseparams_RedisToSql(string uid, int zoneid)
  114. {
  115. Console.WriteLine("玩家基础数值变化方法开始.........");
  116. var mem = Redis.Ins.GetDatabase(0);
  117. string key = MemKey_Statistics.UserBaseParams(uid, zoneid);
  118. if (mem.KeyExists(key))
  119. {
  120. long length = mem.ListLength(key);
  121. var ctxList = mem.ListRange(key, 0, length);
  122. int num = 0;
  123. foreach (var item in ctxList)
  124. {
  125. num += 1;
  126. JObject dic = JObject.Parse(item.ToString());
  127. int cmd = int.Parse(dic["cmd"].ToString());
  128. int type = int.Parse(dic["type"].ToString());
  129. string curVal = dic["curVal"].ToString();
  130. string changeVal = dic["changeVal"].ToString();
  131. string endVal = dic["endVal"].ToString();
  132. string desc = dic["desc"].ToString();
  133. //Console.WriteLine("desc..........:"+ desc);
  134. string ts = dic["ts"].ToString();
  135. //continue;
  136. var sql = $" Insert Into {Userbaseparams_TableName} (`uid`, `zoneid`, `cmd`, `type`, `curVal`, `endVal`, `changeVal`, `desc`, `ts`) " +
  137. $"values('{uid}',{zoneid},{cmd},{type},'{curVal}','{endVal}','{changeVal}','{desc}','{ts}')";
  138. var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
  139. //Debug.Assert(n > 0, $"{sql} 执行失败!");
  140. if (num >= length)
  141. {
  142. Console.WriteLine("总共执行条数:" + num);
  143. mem.KeyDelete(key);
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// 个人数据 包括以下两点:
  151. ///1.通过玩家ID查询指定玩家的任务进度,包括任务ID和任务STEP ID----【直接从玩家数据里取数据展示】
  152. ///2.可查询玩家每日任务进度变化---【单一记录玩家自己的任务信息就可以了,每次做任务时候要记录时间,展示的时候需要以天为单位展示任务进度】
  153. /// </summary>
  154. /// <param name="uid"></param>
  155. /// <param name="zoneid"></param>
  156. public static void TaskcompleteTsrecord_RedisToSql(string uid, int zoneid)
  157. {
  158. var mem = Redis.Ins.GetDatabase(0);
  159. string key = MemKey_Statistics.SelfTaskDataCollect(uid, zoneid);
  160. if (mem.KeyExists(key))
  161. {
  162. Console.WriteLine("玩家任务完成时间记录方法开始.........");
  163. long length = mem.ListLength(key);
  164. var ctxList = mem.ListRange(key, 0, length);
  165. int num = 0;
  166. foreach (var item in ctxList)
  167. {
  168. num += 1;
  169. string[] sList = item.ToString().Split('-');
  170. string taskId = sList[0];
  171. string time = sList[1];
  172. var sql = $" Insert Into {TaskcompleteTsrecord_TableName} (`uid`, `zoneid`, `taskId`, `time`) " +
  173. $"values('{uid}',{zoneid},'{taskId}','{time}')";
  174. var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
  175. //Debug.Assert(n > 0, $"{sql} 执行失败!");
  176. if (num >= length)
  177. {
  178. Console.WriteLine("总共执行条数:" + num);
  179. mem.KeyDelete(key);
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// 所有玩家最新任务id记录
  187. /// </summary>
  188. /// <param name="uid"></param>
  189. /// <param name="zoneid"></param>
  190. public static void Taskprogressrecord_RedisToSql(int zoneid)
  191. {
  192. Console.WriteLine("玩家任务进度方式开始......");
  193. var mem = Redis.Ins.GetDatabase(0);
  194. string key = MemKey_Statistics.TaskDataCollect(zoneid);
  195. if (mem.KeyExists(key))
  196. {
  197. HashEntry[] ctxList = mem.HashGetAll(key);
  198. int length = ctxList.Length;
  199. int num = 0;
  200. foreach (var item in ctxList)
  201. {
  202. num += 1;
  203. string[] sList = item.ToString().Split(':');
  204. string uid = sList[0];
  205. string taskId = sList[1];
  206. var sql = $" Insert Into {Taskprogressrecord_TableName} (`uid`, `zoneid`, `taskId`) " +
  207. $"values('{uid}',{zoneid},'{taskId}')";
  208. var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
  209. //Debug.Assert(n > 0, $"{sql} 执行失败!");
  210. if (num >= length)
  211. {
  212. Console.WriteLine("总共执行条数:" + num);
  213. mem.KeyDelete(key);
  214. break;
  215. }
  216. }
  217. }
  218. else
  219. {
  220. Console.WriteLine("玩家任务进度redis-key不存在所以不在数据传输处理");
  221. }
  222. }
  223. }
  224. }