瀏覽代碼

增加清理脚本

郑晓宇 6 年之前
父節點
當前提交
c4a56394d6
共有 2 個文件被更改,包括 77 次插入1 次删除
  1. 73 0
      app/Console/Commands/gameclear.php
  2. 4 1
      app/Home/Controllers/GameController.php

+ 73 - 0
app/Console/Commands/gameclear.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Model\User;
+use App\Model\Room;
+use App\Model\RoomUser;
+use Illuminate\Support\Facades\Redis;
+
+class gameclear extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'game:clear';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '清理异常房间跟用户、机器人状态';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        while (1) {
+            $start_at = time() - 300;
+            $rooms = Room::where([
+                ["is_end", 0]
+                ["start_at", "<", date("Y-m-d H:i:s", $start_at)]
+            ])->pluck("room_id");
+            if($rooms){
+                // 更新房间用户状态
+                foreach ($rooms as $k => $room) {
+                    RoomUser::where("room_id",$room)->udpate(["state"=>2]);
+                }
+            }
+            // 获取在线机器人
+            $robots = User::where(["is_robot"=>1, "is_login"=>1])->pluck("room_id");
+            if($robots){
+                // 获取机器人游戏状态
+                foreach ($robots as $k => $robot) {
+                    // 判断是否在游戏中
+                    $is_in_game = RoomUser::where(["user_id"=>$robot,"state"=>1])->count();
+                    if(!$is_in_game){
+                        // 更新登录状态
+                        User::where("user_id",$robot)->update(["is_login"=>0]);
+                    }
+                }
+            }
+            // 每 5 分钟执行一次
+            sleep(300); 
+        }
+    }
+}

+ 4 - 1
app/Home/Controllers/GameController.php

@@ -36,7 +36,10 @@ class GameController extends Controller
         //注册gateway地址
         Gateway::$registerAddress = env("WS_REGEDIT_ADDR");
         //获取房间号
-        $room_id = RoomUser::where(["user_id"=>$user_id,"state"=>1])->value('room_id');
+        $room_id = RoomUser::where([
+            ["user_id", $user_id],
+            ["state", 1]
+        ])->value('room_id');
 
         if(!$room_id){
             $response['code'] = 400;