1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?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);
- }
- }
- }
|