在终端中运行php脚本,直到按下某个键

时间:2018-01-02 20:58:44

标签: php terminal console-application readline

我目前正在用PHP写一个终端游戏,我遇到了真正的困难:如果我按下(特定)密钥,我怎样才能使我的脚本自由运行?

我知道我可以使用readline()等方式接收用户输入但是如何通过按键暂停已经运行的脚本?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用PHP generators来多任务游戏。 看看这篇文章Cooperative multitasking using coroutines (in PHP!)。还有库https://github.com/recoilphp/recoil - PHP 7的异步协程内核,可以帮助您编写异步任务。

作为概念的证明,您可以尝试这个脚本,这远非完美。任务和调度程序的实现取自文章。

//Tested on PHP 7.1.11 and MacOS


class Task {
    protected $taskId;
    protected $coroutine;
    protected $sendValue = null;
    protected $beforeFirstYield = true;

    public function __construct($taskId, Generator $coroutine) {
        $this->taskId = $taskId;
        $this->coroutine = $coroutine;
    }

    public function getTaskId() {
        return $this->taskId;
    }

    public function setSendValue($sendValue) {
        $this->sendValue = $sendValue;
    }

    public function run() {
        if ($this->beforeFirstYield) {
            $this->beforeFirstYield = false;
            return $this->coroutine->current();
        } else {
            $retval = $this->coroutine->send($this->sendValue);
            $this->sendValue = null;
            return $retval;
        }
    }

    public function isFinished() {
        return !$this->coroutine->valid();
    }
}


class Scheduler {
    protected $maxTaskId = 0;
    protected $taskMap = []; // taskId => task
    protected $taskQueue;

    public function __construct() {
        $this->taskQueue = new SplQueue();
    }

    public function newTask(Generator $coroutine) {
        $tid = ++$this->maxTaskId;
        $task = new Task($tid, $coroutine);
        $this->taskMap[$tid] = $task;
        $this->schedule($task);
        return $tid;
    }

    public function schedule(Task $task) {
        $this->taskQueue->enqueue($task);
    }

    public function run() {
        while (!$this->taskQueue->isEmpty()) {
            $task = $this->taskQueue->dequeue();
            $task->run();

            if ($task->isFinished()) {
                unset($this->taskMap[$task->getTaskId()]);
            } else {
                $this->schedule($task);
            }
        }
    }
}



function game($state) {
    while (true) {

        if($state->isTheGamePaused === true) {
            echo "The game is paused\n";
        } else {
            echo "Game is running\n";
        }
        yield;
    }
}

function pauseKeyListener($state) {
    readline_callback_handler_install('', function() { });
    while (true) {
        $r = [STDIN];
        $w = NULL;
        $e = NULL;
        $n = stream_select($r, $w, $e, null);
        if ($n && in_array(STDIN, $r)) {
            $pressedChar = stream_get_contents(STDIN, 1);

            // Pause the game if the 'p' is pressed
            if($pressedChar === 'p') {
                $state->isTheGamePaused = true;
            //Resume the game if the 'r' is pressed
            } elseif ($pressedChar === 'r') {
                $state->isTheGamePaused = false;
            }

            echo "Char read: $pressedChar\n";
        }
        yield;
    }
}

$state = new stdClass();
$state->isTheGamePaused = false;

$scheduler = new Scheduler;

$scheduler->newTask(game($state));
$scheduler->newTask(pauseKeyListener($state));

$scheduler->run();