RoomService.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Config;
  5. use App\Models\Game;
  6. use App\Models\Room;
  7. use App\Models\RoomUser;
  8. use App\Models\User;
  9. use App\Models\UserGame;
  10. use App\Models\Wallet;
  11. use Telegram\Bot\Api;
  12. use Telegram\Bot\Exceptions\TelegramSDKException;
  13. use Illuminate\Support\Facades\Cache;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\Log;
  16. //房间服务
  17. class RoomService
  18. {
  19. public static function model(): string
  20. {
  21. return Room::class;
  22. }
  23. public static function enum(): string
  24. {
  25. return '';
  26. }
  27. public static function getWhere(array $search = []): array
  28. {
  29. $where = [];
  30. if (isset($search['id']) && !empty($search['id'])) {
  31. $where[] = ['id', '=', $search['id']];
  32. }
  33. if (isset($search['member_id']) && !empty($search['member_id'])) {
  34. $where[] = ['member_id', '=', $search['member_id']];
  35. }
  36. if (isset($search['room_id']) && !empty($search['room_id'])) {
  37. if(isset($search['like_room_id']) && $search['like_room_id'] == true){
  38. $where[] = ['room_id', 'like', '%' . $search['room_id'] . '%'];
  39. }else{
  40. $where[] = ['room_id', '=', $search['room_id']];
  41. }
  42. }
  43. if (isset($search['old_room_id']) && !empty($search['old_room_id'])) {
  44. $where[] = ['old_room_id', '=', $search['old_room_id']];
  45. }
  46. if (isset($search['base_score']) && !empty($search['base_score'])) {
  47. $where[] = ['base_score', '=', $search['base_score']];
  48. }
  49. if (isset($search['not_status']) && $search['not_status'] != '') {
  50. $where[] = ['status', '<>', $search['not_status']];
  51. }
  52. if (isset($search['status']) && $search['status'] != '') {
  53. $where[] = ['status', '=', $search['status']];
  54. }
  55. return $where;
  56. }
  57. public static function findOne(array $search): ?Room
  58. {
  59. return self::model()::where(self::getWhere($search))->first();
  60. }
  61. public static function findAll(array $search = [])
  62. {
  63. return self::model()::where(self::getWhere($search))->get();
  64. }
  65. public static function paginate(array $search = [])
  66. {
  67. $limit = isset($search['limit']) ? $search['limit'] : 10;
  68. $paginator = self::model()::where(self::getWhere($search))
  69. ->orderBy('created_at', 'desc')
  70. ->paginate($limit);
  71. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  72. }
  73. private $telegram;
  74. public function __construct(Api $tg)
  75. {
  76. $this->telegram = $tg;
  77. }
  78. public static function getGameIdMessage($chatId, $gameName, $messageId)
  79. {
  80. $text = "请设置游戏ID\n";
  81. $keyboard = [];
  82. $ug = UserGame::where('member_id', $chatId)
  83. ->where('game_name', $gameName)->first();
  84. if ($ug) {
  85. $text = "游戏ID:{$ug->game_id}\n";
  86. $text .= "-------------------\n";
  87. $text .= "请确认或直接输入新的游戏ID\n";
  88. $keyboard[] = [['text' => '确认', 'callback_data' => "inputGameID@@{$ug->game_id}"]];
  89. }
  90. $text .= "❓如何获取我的ID\n";
  91. $text .= "1.登录微信小程序:如 财神十三张\n";
  92. $text .= "2.登录成功后,点击用户头像,查看我的资料\n";
  93. $text .= "3.复制对应的ID\n";
  94. $data = [
  95. 'chat_id' => $chatId,
  96. 'text' => $text
  97. ];
  98. if ($messageId) {
  99. $data['message_id'] = $messageId;
  100. Cache::put("{$chatId}_GAME_ID_MESSAGE_ID", $messageId);
  101. }
  102. if ($keyboard) {
  103. $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  104. }
  105. return $data;
  106. }
  107. //提前开始游戏
  108. public function start($chatId, $roomId, $messageId)
  109. {
  110. $room = Room::where('room_id', $roomId)->first();
  111. if ($room->status != 1) {
  112. return [['chat_id' => $chatId, 'text' => '已经开始,无需重复开始', 'message_id' => $messageId]];
  113. }
  114. $list = RoomUser::where('room_id', $roomId)->get();
  115. if (count($list) < 2) {
  116. return [['chat_id' => $chatId, 'text' => '最少需要2人才可以开始游戏']];
  117. }
  118. $readyCount = RoomUser::where('room_id', $roomId)->where('status', 1)->count();
  119. if ($readyCount != count($list)) {
  120. return [['chat_id' => $chatId, 'text' => '还有人员未准备,请等待所有人员准备']];
  121. }
  122. $room->status = 2;// 游戏中
  123. $room->save();
  124. RoomUser::where('room_id', $roomId)->update(['status' => 2]);//游戏中
  125. foreach ($list as $item) {
  126. $text = "✅ 房主提前开始游戏 \n";
  127. $text .= "房号 {$room->room_id} \n";
  128. $text .= "‼️请回到微信小程序搜索《{$room->game_name}》\n输入房间号({$room->room_id})加入游戏\n";
  129. $arr[] = [
  130. 'chat_id' => $item->member_id,
  131. 'text' => $text
  132. ];
  133. if ($item->member_id == $chatId) {
  134. $arr[] = $this->gameHome($roomId, $item->member_id, $messageId);
  135. } else {
  136. $arr[] = $this->gameHome($roomId, $item->member_id);
  137. }
  138. }
  139. return $arr;
  140. }
  141. //返回游戏首页
  142. public function gameHome($roomId, $chatId, $messageId = null)
  143. {
  144. $ru = RoomUser::where('member_id', $chatId)
  145. ->where('room_id', $roomId)->first();
  146. if (!$ru->game_id) {
  147. $room = Room::where('room_id', $roomId)->first();
  148. $data = RoomService::getGameIdMessage($chatId, $room->game_name, $messageId);
  149. Cache::put(get_step_key($chatId), StepStatus::INPUT_GAME_ID);
  150. return $data;
  151. }
  152. $room = Room::where('room_id', $roomId)->first();
  153. $data = $this->getGameInfo($room, $chatId, $room->member_id == $chatId);
  154. $data['chat_id'] = $chatId;
  155. if ($messageId != null) $data['message_id'] = $messageId;
  156. Cache::delete(get_step_key($chatId));
  157. return $data;
  158. }
  159. //获取最终的房间信息和确认按钮
  160. private function getCreateButton($room, $chatId)
  161. {
  162. $text = "房间信息\n";
  163. $text .= "🗂 {$room->game_name} {$room->participants}人 {$room->rounds}局\n";
  164. $text .= "房间号:{$room->room_id}\n";
  165. $text .= "底分:{$room->base_score} USDT\n";
  166. $text .= "中途加入:".($room->midway?'允许':'不允许')." \n";
  167. $text .= "\n";
  168. $keyboard[] = [
  169. ['text' => '创建', 'callback_data' => 'room@@done'],
  170. ['text' => '❌取消', 'callback_data' => "games@@cancel"]
  171. ];
  172. return [
  173. 'chat_id' => $chatId,
  174. 'text' => $text,
  175. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  176. ];
  177. }
  178. /**
  179. * 获取游戏房间信息和房间的操作按钮
  180. * @param $roomId string 房间ID
  181. * @param $isHouseOwner bool 是否是房主
  182. *
  183. */
  184. public function getGameInfo($room, $chatId, $isHouseOwner, $text = '')
  185. {
  186. if($isHouseOwner){
  187. $text .= "游戏:{$room->game_name} {$room->rounds}局 房号:{$room->room_id} \n";
  188. }else{
  189. $text .= "游戏:{$room->game_name} {$room->rounds}局 \n";
  190. }
  191. $text .= "底分:{$room->base_score} USDT\n";
  192. $text .= "中途加入:".($room->midway?'允许':'不允许')." \n";
  193. $text .= "人数:{$room->participants}人\n";
  194. $text .= "{$room->introduction}\n";
  195. $text .= "\n";
  196. $text .= "----------------------------------------------\n";
  197. $text .= "\n";
  198. $text .= "‼️1.开始游戏前,非房主请点击准备,房主等待\n大家准备后,点击开始游戏。\n";
  199. $text .= "‼️2.游戏结束后,请务必上传双方和第三方的战\n绩截图。如出现争议,平台将以三方截图为依据\n进行审核,以确保比赛的公平与公正\n";
  200. // $text .= "‼️3.请回到微信小程序搜索《{$room->game_name}》\n输入房间号({$room->room_id})加入游戏\n";
  201. $keyboard = $this->getGameKeyboard($room->room_id, $chatId, $isHouseOwner);
  202. return [
  203. 'text' => $text,
  204. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  205. ];
  206. }
  207. //获取选择底分的按钮列表
  208. private function getChooseBaseScoreMsg()
  209. {
  210. $config = Config::where('field', 'base_score')->first();
  211. $baseScore = json_decode($config->val, true);
  212. $keyboard = [];
  213. foreach ($baseScore as $item) {
  214. if (count($keyboard) == 0) {
  215. $keyboard = [[
  216. ['text' => "💵 {$item} USDT", 'callback_data' => "roomMin@@" . $item],
  217. ]];
  218. } else {
  219. if (count($keyboard[count($keyboard) - 1]) >= 3) $keyboard[] = [];
  220. $keyboard[count($keyboard) - 1][] = ['text' => "💵 {$item} USDT", 'callback_data' => "roomMin@@" . $item];
  221. }
  222. }
  223. return $keyboard;
  224. }
  225. private function getGameKeyboard($roomId, $chatId, $isHouseOwner)
  226. {
  227. $keyboard = [
  228. [
  229. ['text' => '准备', 'callback_data' => "games@@ready{$roomId}"],
  230. ['text' => '人员/状态', 'callback_data' => "games@@status{$roomId}"],
  231. ],
  232. ];
  233. $ru = RoomUser::where('room_id', $roomId)->where('member_id', $chatId)
  234. ->first();
  235. if ($ru->status != 0) {
  236. $keyboard[0][0]['text'] = "✅ 已准备";
  237. $keyboard[0][0]['callback_data'] = "games@@readyS";
  238. }
  239. $room = Room::where('room_id', $roomId)->first();
  240. if ($room->status == 2) {
  241. if (in_array($ru->status, [2, 3])) {
  242. $keyboard[] = [['text' => '结算游戏', 'callback_data' => "games@@settle{$roomId}"]];
  243. } else if (in_array($ru->status, [0, 1])) {
  244. $keyboard[] = [['text' => '退出', 'callback_data' => "games@@exit{$roomId}"]];
  245. }
  246. } else if (in_array($room->status, [0, 1])) {
  247. if ($isHouseOwner) {
  248. $keyboard[] = [
  249. ['text' => '解散', 'callback_data' => "games@@Disband{$roomId}"],
  250. ['text' => '开始游戏', 'callback_data' => "games@@start{$roomId}"],
  251. ];
  252. } else {
  253. $keyboard[] = [['text' => '退出', 'callback_data' => "games@@exit{$roomId}"]];
  254. }
  255. }
  256. return $keyboard;
  257. }
  258. //人员状态
  259. public function userStatus($roomId, $chatId, $messageId)
  260. {
  261. $list = RoomUser::where('room_id', $roomId)
  262. ->orderBy('id')
  263. ->get();
  264. if (count($list) == 0) {
  265. return [
  266. 'chat_id' => $chatId,
  267. 'text' => '房间不存在,请先创建房间'
  268. ];
  269. }
  270. $status = ['待准备', '已准备', '游戏中', '待结算', '已结算'];
  271. $text = "编号 角色 状态 游戏ID\n";
  272. foreach ($list as $index => $item) {
  273. $no = $index + 1;
  274. $gameId = $item->game_id;
  275. if (!$gameId) $gameId = '-------';
  276. if ($no == 1) {
  277. $text .= "{$no} 房主 {$status[$item->status]} {$gameId}\n";
  278. // $text .= "{$no}\t\t{$item->game_id}\t\t{$status[$item->status]}\t\t房主\n";
  279. } else {
  280. $text .= "{$no} 玩家 {$status[$item->status]} {$gameId}\n";
  281. // $text .= "{$no}\t\t{$item->game_id}\t\t{$status[$item->status]}\t\t玩家\n";
  282. }
  283. }
  284. $keyboard = [
  285. [
  286. ['text' => '返回', 'callback_data' => "games@@home{$roomId}"],
  287. ],
  288. ];
  289. return [
  290. 'chat_id' => $chatId,
  291. 'text' => $text,
  292. 'parse_mode' => 'Markdown',
  293. 'message_id' => $messageId,
  294. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  295. ];
  296. }
  297. //准备
  298. public function ready($roomId, $chatId, $messageId)
  299. {
  300. $ru = RoomUser::whereIn('status', [0, 1, 2, 3])
  301. ->where('member_id', $chatId)
  302. ->where('room_id', $roomId)
  303. ->first();
  304. if (!$ru) {
  305. return [[
  306. 'chat_id' => $chatId,
  307. 'text' => "❌您还没有选择房间,请先通过在线房间进入房间"
  308. ]];
  309. }
  310. if (in_array($ru->status, [2, 3])) {
  311. return [[
  312. 'chat_id' => $chatId,
  313. 'text' => "❌您已在游戏中,无需准备"
  314. ]];
  315. }
  316. Cache::put(get_step_key($chatId), StepStatus::INPUT_GAME_ID);
  317. if (!$ru->game_id) {
  318. $room = Room::where('room_id', $roomId)->first();
  319. $data = RoomService::getGameIdMessage($chatId, $room->game_name, $messageId);
  320. return [$data];
  321. }
  322. $room = Room::where('room_id', $roomId)->first();
  323. $ru->status = 1;
  324. $arr = [];
  325. if ($room->status == 2) {
  326. $ru->status = 2;
  327. $text = "✅ 游戏已开始 \n";
  328. $text .= "房号 {$room->room_id} \n";
  329. $text .= "‼️请回到微信小程序搜索《{$room->game_name}》\n输入房间号({$room->room_id})加入游戏\n";
  330. $arr[] = [
  331. 'chat_id' => $chatId,
  332. 'text' => $text
  333. ];
  334. }
  335. $ru->save();
  336. $ready = RoomUser::where('room_id', $roomId)->where('status', 1)->count();
  337. $data = $this->getGameInfo($room, $chatId, $room->member_id == $chatId);
  338. $data['chat_id'] = $chatId;
  339. $data['message_id'] = $messageId;
  340. $arr[] = $data;
  341. if ($room->participants == $ready) {
  342. $list = RoomUser::where('room_id', $roomId)->get();
  343. $room->status = 2;// 游戏中
  344. $room->save();
  345. RoomUser::where('room_id', $roomId)->update(['status' => 2]);//游戏中
  346. foreach ($list as $item) {
  347. $text = "✅ 所有人员都已准备,自动开始游戏 \n";
  348. $text .= "房号 {$room->room_id} \n";
  349. $text .= "‼️请回到微信小程序搜索《{$room->game_name}》\n输入房间号({$room->room_id})加入游戏\n";
  350. $arr[] = [
  351. 'chat_id' => $item->member_id,
  352. 'text' => $text
  353. ];
  354. if ($item->member_id == $chatId) {
  355. $arr[] = $this->gameHome($roomId, $item->member_id, $messageId);
  356. } else {
  357. $arr[] = $this->gameHome($roomId, $item->member_id);
  358. }
  359. }
  360. }
  361. return $arr;
  362. }
  363. //玩家加入房间
  364. public function join($chatId, $roomId, $messageId = null)
  365. {
  366. $room = Room::where('room_id', $roomId)->first();
  367. if (!$room) {
  368. return [[
  369. 'chat_id' => $chatId,
  370. 'text' => '❌ 房间已关闭,请重新选择房间!'
  371. ]];
  372. }
  373. if ($room->status !== 1 && $room->status !== 2) {
  374. return [[
  375. 'chat_id' => $chatId,
  376. 'text' => '❌ 房间已开始游戏,请重新选择房间!'
  377. ]];
  378. }
  379. if($room->status == 2 && !$room->midway){
  380. return [[
  381. 'chat_id' => $chatId,
  382. 'text' => '❌ 房间已开始游戏,不允许中途加入!'
  383. ]];
  384. }
  385. $wallet = Wallet::where('member_id', $chatId)->first();
  386. $zuiDi = $room->base_score * 100;
  387. if ($wallet->available_balance < $zuiDi) {
  388. return [[
  389. 'chat_id' => $chatId,
  390. 'text' => "❌您选择的是:{$room->base_score}USDT的房间,最低余额:{$zuiDi}USDT\n请重新选择或充值余额!"
  391. ]];
  392. }
  393. $ru = RoomUser::whereIn('status', [0, 1, 2, 3])
  394. ->where('member_id', $chatId)
  395. ->first();
  396. if ($ru) {
  397. if ($room->join_count >= $room->participants && $ru->member_id != $chatId) {
  398. return [[
  399. 'chat_id' => $chatId,
  400. 'text' => '❌加入失败,房间人数已满'
  401. ]];
  402. }
  403. $text = '';
  404. if ($room->room_id !== $ru->room_id) {
  405. $text .= "❌加入失败,您已经加入了其他房间,\n以下是您加入的房间信息\n";
  406. $text .= "\n";
  407. $text .= "------------------------------\n";
  408. $text .= "\n";
  409. $room = Room::where('room_id', $ru->room_id)->first();
  410. }
  411. $data = $this->getGameInfo($room, $chatId, $room->member_id == $chatId, $text);
  412. $data['chat_id'] = $chatId;
  413. $data['message_id'] = $messageId;
  414. return [$data];
  415. }
  416. $user = User::where('member_id', $chatId)->first();
  417. $ru = new RoomUser();
  418. $ru->room_id = $room->room_id;
  419. $ru->member_id = $chatId;
  420. $ru->first_name = $user->first_name;
  421. $ru->status = 0;
  422. $ru->save();
  423. $room->join_count += 1;
  424. $room->save();
  425. $arr = [];
  426. $list = RoomUser::where('room_id', $ru->room_id)->where('member_id', '<>', $chatId)->get();
  427. foreach ($list as $item) {
  428. $arr[] = [
  429. 'chat_id' => $item->member_id,
  430. 'text' => "新用户进入房间",
  431. ];
  432. if ($room->join_count == $room->participants) {
  433. $arr[] = [
  434. 'chat_id' => $item->member_id,
  435. 'text' => "房间已满员,请所有玩家准备",
  436. ];
  437. }
  438. }
  439. $data = RoomService::getGameIdMessage($chatId, $room->game_name, $messageId);
  440. Cache::put(get_step_key($chatId), StepStatus::INPUT_GAME_ID);
  441. $arr[] = $data;
  442. return $arr;
  443. }
  444. /**
  445. * 取消创建房间
  446. * @param $chatId
  447. * @throws TelegramSDKException
  448. */
  449. public function cancel($chatId, $messageId)
  450. {
  451. Room::where('status', 0)
  452. ->where('member_id', $chatId)->delete();
  453. return [
  454. 'chat_id' => $chatId,
  455. 'text' => '🚫游戏创建流程已取消',
  456. 'message_id' => $messageId
  457. ];
  458. }
  459. public function exit($data, $chatId, $messageId)
  460. {
  461. $roomId = preg_replace('/^games@@exit/', '', $data);
  462. $roomId = intval($roomId);
  463. $room = Room::where('room_id', $roomId)->first();
  464. if (!$room) return ['chat_id' => $chatId, 'text' => '房间已解散,无需退出'];
  465. $ru = RoomUser::where('room_id', $roomId)
  466. ->where('member_id', $chatId)->first();
  467. if (!$ru) {
  468. return ['chat_id' => $chatId, 'text' => '不在房间内,无需退出'];
  469. }
  470. if ($ru->member_id == $room->member_id) {
  471. return ['chat_id' => $chatId, 'text' => '房主不可退出房间'];
  472. }
  473. if (!in_array($room->status, [1, 2]) || !in_array($ru->status, [0, 1])) {
  474. return ['chat_id' => $chatId, 'text' => '游戏已开局,退出失败'];
  475. }
  476. $ru->delete();
  477. $room->join_count -= 1;
  478. $room->save();
  479. $data = (new OnLineService())->getList($chatId, null, 1);
  480. $data['message_id'] = $messageId;
  481. return $data;
  482. // return [
  483. // 'chat_id' => $chatId,
  484. // 'text' => "已退出房间",
  485. // 'message_id' => $messageId
  486. // ];
  487. }
  488. /**
  489. * 解散房间
  490. * @return array
  491. */
  492. public function disband($roomId, $chatId, $messageId)
  493. {
  494. $room = Room::whereIn('status', [0, 1, 2, 3])
  495. ->where('member_id', $chatId)
  496. ->where('room_id', $roomId)
  497. ->first();
  498. if (!$room) {
  499. return [[
  500. 'chat_id' => $chatId,
  501. 'text' => '房间不存在,请先创建房间 /room',
  502. 'message_id' => $messageId
  503. ]];
  504. } else {
  505. if (in_array($room->status, [2, 3])) {
  506. return [[
  507. 'chat_id' => $chatId,
  508. 'text' => '游戏已开始,不可解散房间'
  509. ]];
  510. }
  511. $list = RoomUser::where('room_id', $room->room_id)->get();
  512. $arr = [];
  513. foreach ($list as $item) {
  514. $a = [
  515. 'chat_id' => $item->member_id,
  516. 'text' => "❌房主已解散房间",
  517. ];
  518. if ($item->member_id == $room->member_id) {
  519. $a['message_id'] = $messageId;
  520. }
  521. $arr[] = $a;
  522. }
  523. $room->delete();
  524. RoomUser::where('room_id', $room->room_id)->delete();
  525. return $arr;
  526. }
  527. }
  528. /**
  529. * 设置游戏ID
  530. * @param $chatId string 用户的tg ID
  531. * @param $array array 设置房间号的前缀【房间号】
  532. * @throws TelegramSDKException
  533. */
  534. public function setGameID($chatId, $gameId, $messageId)
  535. {
  536. if (!preg_match('/^\d+$/', $gameId)) return [['chat_id' => $chatId, 'text' => '输入错误,请发送游戏ID', 'reply_to_message_id' => $messageId]];
  537. $ru = RoomUser::where('member_id', $chatId)
  538. ->where('status', 0)->first();
  539. if (!$ru) return [["chat_id" => $chatId, 'text' => '设置失败,没有房间', 'reply_to_message_id' => $messageId]];
  540. $room = Room::where('room_id', $ru->room_id)->whereIn('status', [0, 1, 2])->first();
  541. if (!$room) return [["chat_id" => $chatId, 'text' => '设置失败,没有房间', 'reply_to_message_id' => $messageId]];
  542. $ru->game_id = $gameId;
  543. $ru->save();
  544. if ($room->status != 2) {
  545. $room->status = 1;
  546. $room->save();
  547. }
  548. UserGame::updateOrCreate(
  549. ['member_id' => $chatId, 'game_name' => $room->game_name],
  550. ['game_id' => $gameId]
  551. );
  552. $notice = null;
  553. $text = "";
  554. if ($chatId == $room->member_id) {
  555. $notice = ChannelService::createRoomNotice($room->room_id);
  556. $text = "✅ 房间创建成功\n";
  557. $text .= "\n";
  558. }
  559. $messageId1 = Cache::get("{$chatId}_GAME_ID_MESSAGE_ID", $messageId);
  560. Cache::delete("{$chatId}_GAME_ID_MESSAGE_ID");
  561. $data = $this->getGameInfo($room, $chatId, $room->member_id == $chatId, $text);
  562. $data['chat_id'] = $chatId;
  563. if ($messageId1) $data['message_id'] = $messageId1;
  564. Cache::delete(get_step_key($chatId));
  565. if ($notice) return [$data, $notice];
  566. return [$data];
  567. }
  568. /** 确认创建房间
  569. * @param $chatId
  570. * @return array
  571. */
  572. public function done($chatId, $messageId)
  573. {
  574. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  575. if (!$room) return [['chat_id' => $chatId, 'text' => '房间不存在,请先创建房间 /room', 'message_id' => $messageId]];
  576. $text = "";
  577. if ($room->room_id == '') $text = '请设置房间号';
  578. if ($room->base_score == 0) $text = '请选择底分';
  579. if ($room->participants == 0 || $room->rounds == 0) $text = '请设置人数/局数';
  580. if ($text) return [['chat_id' => $chatId, 'text' => $text]];
  581. $user = User::where('member_id', $chatId)->first();
  582. $ru = new RoomUser();
  583. $ru->room_id = $room->room_id;
  584. $ru->member_id = $chatId;
  585. $ru->status = 0;
  586. $ru->first_name = $user->first_name;
  587. $ru->save();
  588. $room->join_count += 1;
  589. $room->save();
  590. $data = RoomService::getGameIdMessage($chatId, $room->game_name, $messageId);
  591. Cache::put(get_step_key($chatId), StepStatus::INPUT_GAME_ID);
  592. Cache::delete("{$chatId}_BASE_SCORE");
  593. return [$data];
  594. }
  595. //设置游戏介绍
  596. public function setIntroduction($chatId, $introduction, $messageId)
  597. {
  598. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  599. if (!$room) return ['chat_id' => $chatId, 'text' => '设置失败,没有正在创建的房间', 'reply_to_message_id' => $messageId];
  600. $room->introduction = $introduction;
  601. $room->save();
  602. Cache::delete(get_step_key($chatId));
  603. $messageId1 = Cache::get("{$chatId}_CREATE_ROOM_MESSAGE_ID");
  604. $res = $this->getCreateButton($room, $chatId);
  605. $res['message_id'] = $messageId1;
  606. return $res;
  607. }
  608. /**
  609. * 设置人数和局数
  610. * @param $chatId string 用户的tg ID
  611. * @param $array array 设置房间号的前缀【房间号】
  612. * @throws TelegramSDKException
  613. */
  614. public function setNumberOfGames($chatId, $array, $messageId)
  615. {
  616. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  617. if (!$room) return ['chat_id' => $chatId, 'text' => '设置失败,没有正在创建的房间', 'reply_to_message_id' => $messageId];
  618. if (count($array) != 2) return ['chat_id' => $chatId, 'text' => '设置失败,请发送游戏人数', 'reply_to_message_id' => $messageId];
  619. $array[0] = intval($array[0]);
  620. $array[1] = intval($array[1]);
  621. if (!is_int($array[1]) || $array[1] < 1) {
  622. return ['chat_id' => $chatId, 'text' => '设置失败,请发送游戏局数', 'reply_to_message_id' => $messageId];
  623. }
  624. if (!is_int($array[0]) || $array[0] < 2) {
  625. return ['chat_id' => $chatId, 'text' => '设置失败,请发送游戏人数', 'reply_to_message_id' => $messageId];
  626. }
  627. $room->participants = $array[0];
  628. $room->rounds = $array[1];
  629. $room->save();
  630. // $text = "{$room->game_name} {$room->participants}人 {$room->rounds}局\n";
  631. // $text .= "房间号:{$room->room_id}\n";
  632. // $text .= "底分:{$room->base_score} USDT\n";
  633. // $text .= "中途加入:".($room->midway?'允许':'不允许')." \n";
  634. // $text .= "-------------------\n";
  635. // $text .= "请输入游戏介绍\n";
  636. // $messageId1 = Cache::get("{$chatId}_CREATE_ROOM_MESSAGE_ID");
  637. // Cache::put(get_step_key($chatId), StepStatus::INPUT_GAME_INTRODUCTION);
  638. // Cache::put("{$chatId}_CREATE_ROOM_MESSAGE_ID", $messageId1);
  639. // return [
  640. // 'chat_id' => $chatId,
  641. // 'text' => $text,
  642. // 'message_id' => $messageId1,
  643. // 'reply_markup' => json_encode(['inline_keyboard' => [[['text' => '❌取消', 'callback_data' => "games@@cancel"]]]])
  644. // ];
  645. Cache::delete(get_step_key($chatId));
  646. $messageId1 = Cache::get("{$chatId}_CREATE_ROOM_MESSAGE_ID");
  647. $res = $this->getCreateButton($room, $chatId);
  648. $res['message_id'] = $messageId1;
  649. return $res;
  650. }
  651. /**
  652. * 设置房间号
  653. * @param $chatId string 用户的tg ID
  654. * @param $roomId string 房间号
  655. * @throws TelegramSDKException
  656. */
  657. public function setNum($chatId, $roomId, $messageId)
  658. {
  659. if (!preg_match('/^\d+$/', $roomId)) return ['chat_id' => $chatId, 'text' => '房间号错误', 'reply_to_message_id' => $messageId];
  660. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  661. if (!$room) return ['chat_id' => $chatId, 'text' => "禁止设置,没有正在创建的房间"];
  662. $isRoom = Room::where('room_id', $roomId)
  663. ->where('room_id', '<>', $roomId)
  664. ->first();
  665. if ($isRoom) return ['chat_id' => $chatId, 'text' => '房间号已存在,请检查房间号', 'reply_to_message_id' => $messageId];
  666. $room->room_id = $roomId;
  667. $room->old_room_id = $roomId;
  668. $room->save();
  669. Cache::put(get_step_key($chatId), StepStatus::INPUT_ROOM_NUM);
  670. $text = "游戏:{$room->game_name}\n";
  671. $text .= "底分:{$room->base_score} USDT\n";
  672. $text .= "中途加入:".($room->midway?'允许':'不允许')." \n";
  673. $text .= "房间号:$roomId\n";
  674. $text .= "-------------------\n";
  675. $text .= "请输入游戏人数和局数\n";
  676. $text .= "例如:4人5局\n";
  677. $text .= "那么请发送:4/5\n";
  678. $messageId1 = Cache::get("{$chatId}_CREATE_ROOM_MESSAGE_ID");
  679. return [
  680. 'chat_id' => $chatId,
  681. 'text' => $text,
  682. 'message_id' => $messageId1,
  683. 'reply_markup' => json_encode(['inline_keyboard' => [[['text' => '❌取消', 'callback_data' => "games@@cancel"]]]])
  684. ];
  685. }
  686. public function setGameName($chatId, $gameName, $messageId)
  687. {
  688. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  689. if (!$room) {
  690. return ['chat_id' => $chatId, 'text' => "禁止设置,没有正在创建的房间", 'reply_to_message_id' => $messageId];
  691. }
  692. $room->game_name = $gameName;
  693. $room->save();
  694. Cache::put(get_step_key($chatId), StepStatus::INPUT_MIDWAY);
  695. $text = "游戏:{$room->game_name}\n";
  696. $text .= "底分:{$room->base_score} USDT\n";
  697. $text .= "-------------------\n";
  698. $text .= "请选择是否允许中途加入游戏\n";
  699. $messageId1 = Cache::get("{$chatId}_CREATE_ROOM_MESSAGE_ID");
  700. return [
  701. 'chat_id' => $chatId,
  702. 'text' => $text,
  703. 'message_id' => $messageId1,
  704. 'reply_markup' => json_encode(['inline_keyboard' => [[
  705. ['text' => '✅ 允许', 'callback_data' => "setGameMidway@@1"],
  706. ['text' => '❌ 不允许', 'callback_data' => "setGameMidway@@0"]
  707. ], [['text' => '❌取消', 'callback_data' => "games@@cancel"]]]])
  708. ];
  709. }
  710. /**
  711. * 设置游戏是否允许中途加入
  712. * @param $chatId 用户TG ID
  713. * @param $midway 中途加入 1允许 0 不允许
  714. * @param $messageId 消息ID
  715. * @throws TelegramSDKException
  716. */
  717. public function setGameMidway($chatId, $midway, $messageId)
  718. {
  719. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  720. if (!$room) {
  721. return ['chat_id' => $chatId, 'text' => "禁止设置,没有正在创建的房间", 'reply_to_message_id' => $messageId];
  722. }
  723. $room->midway = $midway;
  724. $room->save();
  725. Cache::put(get_step_key($chatId), StepStatus::INPUT_ROOM_ID);
  726. $text = "游戏:{$room->game_name}\n";
  727. $text .= "底分:{$room->base_score} USDT\n";
  728. $text .= "中途加入:".($room->midway?'允许':'不允许')." \n";
  729. $text .= "-------------------\n";
  730. $text .= "请发送三方游戏的房间号码";
  731. $messageId1 = Cache::get("{$chatId}_CREATE_ROOM_MESSAGE_ID");
  732. return [
  733. 'chat_id' => $chatId,
  734. 'text' => $text,
  735. 'message_id' => $messageId1,
  736. 'reply_markup' => json_encode(['inline_keyboard' => [[['text' => '❌取消', 'callback_data' => "games@@cancel"]]]])
  737. ];
  738. }
  739. /**
  740. * 选择房间低分
  741. * @param $chatId
  742. * @throws TelegramSDKException
  743. */
  744. public function setMin($data, $chatId, $messageId)
  745. {
  746. $roomMin = preg_replace('/^roomMin@@/', '', $data);
  747. $roomMin = intval($roomMin);
  748. $user = User::where('member_id', $chatId)->first();
  749. $zuiDi = $roomMin * 100;
  750. $wallet = Wallet::where('member_id', $chatId)->first();
  751. if ($wallet->available_balance < $zuiDi) {
  752. $text = "👤 {$user->first_name}({$user->member_id})\n";
  753. $text .= "\n";
  754. $text .= "💰钱包余额\n";
  755. $temp = floatval($wallet->available_balance);
  756. $text .= "USDT:{$temp}\n";
  757. $text .= "\n";
  758. $text .= "您创建的是{$roomMin}USDT的房间,最低余额{$zuiDi}USDT,请重新选择或充值余额后再创建房间!\n";
  759. return ['chat_id' => $chatId, 'text' => $text];
  760. } else {
  761. $room = Room::where('status', 0)->where('member_id', $chatId)->first();
  762. if (!$room) {
  763. return ['chat_id' => $chatId, 'text' => "禁止设置,没有正在创建的房间"];
  764. }
  765. $room->base_score = $roomMin;
  766. $room->save();
  767. $text = "底分:{$roomMin} USDT\n";
  768. $text .= "-------------------\n";
  769. $text .= "请选择游戏";
  770. $games = Game::all();
  771. $keyboard = [];
  772. foreach ($games as $index => $item) {
  773. if ($index % 2 == 0) {
  774. $keyboard[] = [['text' => $item['game_name'], 'callback_data' => "inputGameName@@{$item['game_name']}"]];
  775. } else {
  776. $keyboard[count($keyboard) - 1][] = ['text' => $item['game_name'], 'callback_data' => "inputGameName@@{$item['game_name']}"];
  777. }
  778. }
  779. if (count($keyboard[count($keyboard) - 1]) > 1) {
  780. $keyboard[] = [['text' => '❌取消', 'callback_data' => "games@@cancel"]];
  781. } else {
  782. $keyboard[count($keyboard) - 1][] = ['text' => '❌取消', 'callback_data' => "games@@cancel"];
  783. }
  784. // Cache::put(get_step_key($chatId), StepStatus::INPUT_GAME_NAME);
  785. Cache::put("{$chatId}_CREATE_ROOM_MESSAGE_ID", $messageId);
  786. return [
  787. 'chat_id' => $chatId,
  788. 'text' => $text,
  789. 'message_id' => $messageId,
  790. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  791. ];
  792. }
  793. }
  794. /**
  795. * 创建房间
  796. * @param $chatId
  797. * @throws TelegramSDKException
  798. */
  799. public function create($chatId)
  800. {
  801. $room = Room::whereIn('status', [0, 1, 2, 4])
  802. ->where('member_id', $chatId)->first();
  803. $ru = RoomUser::where('member_id', $chatId)
  804. ->whereIn('status', [0, 1, 2, 3])->first();
  805. if (($room && $room->status !== 0) || $ru) {
  806. return ['chat_id' => $chatId, 'text' => "您有未结算对局,禁止创建房间"];
  807. }
  808. if (!$room) {
  809. $room = new Room();
  810. $room->member_id = $chatId;
  811. $room->game_name = '';
  812. $room->save();
  813. }
  814. $keyboard = $this->getChooseBaseScoreMsg();
  815. $keyboard[] = [['text' => '❌取消', 'callback_data' => "games@@cancel"]];
  816. return [
  817. 'chat_id' => $chatId,
  818. 'text' => "👇请选择你房间底分",
  819. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  820. ];
  821. }
  822. /**
  823. * 重置房间号
  824. * @param $roomId string 房间号
  825. */
  826. public static function resetRoomId($roomId)
  827. {
  828. $num = self::model()::where('old_room_id', $roomId)->count();
  829. $room = Room::where('room_id', $roomId)->first();
  830. $newRoomId = $roomId . '_' . $num;
  831. if ($room) {
  832. $room->room_id = $newRoomId;
  833. $room->save();
  834. }
  835. RoomUser::where('room_id', $roomId)->update(['room_id' => $newRoomId]);
  836. return $newRoomId;
  837. }
  838. /**
  839. * @description: 结算通知
  840. * @return {*}
  841. */
  842. public static function noticeSettle()
  843. {
  844. try {
  845. $time = Carbon::now()->subMinutes(20)->format('Y-m-d H:i:s');
  846. $roomList = self::model()::where('status', 2)
  847. ->where('settle_status', 0)
  848. ->where('updated_at', '<', $time)
  849. ->get();
  850. $telegram = new Api(config('services.telegram.token'));
  851. foreach ($roomList as $room) {
  852. try {
  853. $list = RoomUser::where('room_id', $room->room_id)
  854. ->where('status', 2)
  855. ->get();
  856. if ($list->isEmpty()) continue;
  857. $text = "❗️房间号:{$room->room_id} \n";
  858. $text .= "‼️您有待结算的游戏,请及时进行结算。\n";
  859. foreach ($list as $item) {
  860. try {
  861. $keyboard = [[
  862. ['text' => '结算游戏', 'callback_data' => "games@@settle{$room->room_id}"]
  863. ]];
  864. $message = [
  865. 'chat_id' => $item->member_id,
  866. 'text' => $text,
  867. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  868. ];
  869. $telegram->sendMessage($message);
  870. } catch (\Exception $e) {
  871. Log::error("发送消息失败:房间 {$room->room_id}, 用户 {$item->member_id}, 错误:" . $e->getMessage());
  872. continue; // 跳过当前用户
  873. }
  874. }
  875. } catch (\Exception $e) {
  876. Log::error("处理房间 {$room->room_id} 失败:" . $e->getMessage());
  877. continue; // 跳过当前房间
  878. }
  879. $room->settle_status = 1; // 已通知
  880. $room->save();
  881. }
  882. } catch (\Exception $e) {
  883. Log::error("noticeSettle 整体执行失败:" . $e->getMessage());
  884. }
  885. }
  886. }