<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\Redis;

class recylerobots extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'game:recylerobots';

    /**
     * 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()
    {
        //注册地址
        Gateway::$registerAddress = '127.0.0.1:1238';
        while(1){
            //获取所有机器人
            $robots = Redis::lrange('online_robot_list', 0, -1);
            if($robots){
                foreach ($robots as $robot) {
                    $message['type'] = 'checkrobot';
                    //检测是否在线
                    if(Gateway::isOnline($robot)){
                        Gateway::sendToClient($robot, json_encode($message));
                    }else{
                        Redis::lrem('online_robot_list', 0, $robot);
                        Redis::srem('robot_list', $robot);
                    }
                }
            }
            sleep(10);
        }

    }
}
