Program.cs 9.0 KB

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