DouyinCallBackHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using Newtonsoft.Json.Linq;
  6. namespace ET.Server
  7. {
  8. /// <summary>
  9. /// 抖音回调帮助类
  10. /// </summary>
  11. public static class DouyinCallBackHelper
  12. {
  13. /// <summary>
  14. /// 评论加入玩家
  15. /// </summary>
  16. /// <param name="domain"></param>
  17. /// <param name="context"></param>
  18. /// <param name="roomId"></param>
  19. public static async void LiveComment(Entity domain, HttpListenerContext context, long roomId)
  20. {
  21. HttpDouyinApiCallbackResponse response = new HttpDouyinApiCallbackResponse();
  22. string bodyStr = await HttpHelper.GetBodyParameter(context);
  23. // 判断body是否为空
  24. if (string.IsNullOrEmpty(bodyStr))
  25. {
  26. response.Error = ErrorCode.ERR_ParameterError;
  27. response.Message = "参数错误";
  28. HttpHelper.Response(context, response);
  29. return;
  30. }
  31. JArray jsonArray = JArray.Parse(bodyStr);
  32. foreach (JToken jToken in jsonArray)
  33. {
  34. JObject jsonObject = (JObject)jToken;
  35. string msgId = (string)jsonObject["msg_id"];
  36. string secOpenId = (string)jsonObject["sec_openid"];
  37. string content = (string)jsonObject["content"];
  38. string avatarUrl = (string)jsonObject["avatar_url"];
  39. string nickname = (string)jsonObject["nickname"];
  40. long timestamp = (long)jsonObject["timestamp"];
  41. Log.Debug($"body参数: openid={secOpenId}, roomId={roomId}, content={content}");
  42. // 判断参数
  43. if (string.IsNullOrEmpty(secOpenId) || roomId <= 0)
  44. {
  45. response.Error = ErrorCode.ERR_ParameterError;
  46. response.Message = "参数错误";
  47. HttpHelper.Response(context, response);
  48. return;
  49. }
  50. List<StartSceneConfig> list = RealmGateAddressHelper.GetAllGame(1);
  51. foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 }))
  52. {
  53. MessageHelper.SendActor(config.InstanceId, new R2G_AddUnitsToMap() { OpenId = secOpenId, RoomId = roomId});
  54. break;
  55. }
  56. HttpHelper.Response(context, response);
  57. }
  58. await ETTask.CompletedTask;
  59. }
  60. /// <summary>
  61. /// 刷礼物
  62. /// </summary>
  63. /// <param name="domain"></param>
  64. /// <param name="context"></param>
  65. /// <param name="roomId"></param>
  66. public static async void LiveGift(Entity domain, HttpListenerContext context, long roomId)
  67. {
  68. HttpDouyinApiCallbackResponse response = new HttpDouyinApiCallbackResponse();
  69. string bodyStr = await HttpHelper.GetBodyParameter(context);
  70. if (string.IsNullOrEmpty(bodyStr))
  71. {
  72. response.Error = ErrorCode.ERR_ParameterError;
  73. response.Message = "参数错误";
  74. HttpHelper.Response(context, response);
  75. return;
  76. }
  77. JArray jsonArray = JArray.Parse(bodyStr);
  78. foreach (JToken jToken in jsonArray)
  79. {
  80. JObject jsonObject = (JObject)jToken;
  81. string msgId = (string)jsonObject["msg_id"];
  82. string secOpenId = (string)jsonObject["sec_openid"];
  83. // 加密的礼物id
  84. string secGiftId = (string)jsonObject["sec_gift_id"];
  85. // 送出的礼物数量
  86. long giftNum = (long)jsonObject["gift_num"];
  87. // 礼物总价值,单位分
  88. long giftValue = (long)jsonObject["gift_value"];
  89. string avatarUrl = (string)jsonObject["avatar_url"];
  90. string nickname = (string)jsonObject["nickname"];
  91. long timestamp = (long)jsonObject["timestamp"];
  92. Log.Debug($"body参数: openid={secOpenId}, roomId={roomId}, secGiftId={secGiftId}, giftNum={giftNum}, giftValue={giftValue}");
  93. // 判断参数
  94. if (string.IsNullOrEmpty(secOpenId) || roomId <= 0)
  95. {
  96. response.Error = ErrorCode.ERR_ParameterError;
  97. response.Message = "参数错误";
  98. HttpHelper.Response(context, response);
  99. return;
  100. }
  101. List<StartSceneConfig> list = RealmGateAddressHelper.GetAllGame(1);
  102. foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 }))
  103. {
  104. MessageHelper.SendActor(config.InstanceId, new R2G_GiveGift() { OpenId = secOpenId, RoomId = roomId, NickName = nickname, GiftId = secGiftId, GiftNum = giftNum, GiftValue = giftValue});
  105. break;
  106. }
  107. HttpHelper.Response(context, response);
  108. }
  109. await ETTask.CompletedTask;
  110. }
  111. /// <summary>
  112. /// 点赞10次增加等级
  113. /// </summary>
  114. /// <param name="domain"></param>
  115. /// <param name="context"></param>
  116. /// <param name="roomId"></param>
  117. public static async void LiveLike(Entity domain, HttpListenerContext context, long roomId)
  118. {
  119. HttpDouyinApiCallbackResponse response = new HttpDouyinApiCallbackResponse();
  120. string bodyStr = await HttpHelper.GetBodyParameter(context);
  121. if (string.IsNullOrEmpty(bodyStr))
  122. {
  123. response.Error = ErrorCode.ERR_ParameterError;
  124. response.Message = "参数错误";
  125. HttpHelper.Response(context, response);
  126. return;
  127. }
  128. JArray jsonArray = JArray.Parse(bodyStr);
  129. foreach (JToken jToken in jsonArray)
  130. {
  131. JObject jsonObject = (JObject)jToken;
  132. string msgId = (string)jsonObject["msg_id"];
  133. string secOpenId = (string)jsonObject["sec_openid"];
  134. long likeNum = (long)jsonObject["like_num"];
  135. string avatarUrl = (string)jsonObject["avatar_url"];
  136. string nickname = (string)jsonObject["nickname"];
  137. long timestamp = (long)jsonObject["timestamp"];
  138. Log.Debug($"body参数: openid={secOpenId}, roomId={roomId}, likes={likeNum}");
  139. // 判断参数
  140. if (string.IsNullOrEmpty(secOpenId) || roomId <= 0 || likeNum < 0)
  141. {
  142. response.Error = ErrorCode.ERR_ParameterError;
  143. response.Message = "参数错误";
  144. HttpHelper.Response(context, response);
  145. return;
  146. }
  147. List<StartSceneConfig> list = RealmGateAddressHelper.GetAllGame(1);
  148. foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 }))
  149. {
  150. MessageHelper.SendActor(config.InstanceId, new R2G_AddLevel() { OpenId = secOpenId, RoomId = roomId, Likes = likeNum});
  151. break;
  152. }
  153. HttpHelper.Response(context, response);
  154. }
  155. await ETTask.CompletedTask;
  156. }
  157. }
  158. }