php fiber 应用

参考

基于 PHP Fiber(纤程)的游戏开发分析-腾讯云开发者社区-腾讯云PHP 8.1 引入的 Fibers 为游戏开发带来新机遇,能管理渲染、物理计算等任务且不阻塞主线程。它支持并发,提升效率,简单易用,但也有局限,如单线程本质、上下文切换开销、调试复杂及生态系统不成熟。https://cloud.tencent.com/developer/article/2509749说明

  • 多个用户参与游戏
  • 每个人猜各自的最终数字
  • 每人做多猜五次

代码:

php 复制代码
function getroundnum() {
    $num = rand(0, 30);
    return $num;
}
class Persion {
    public function __construct(public string $name) {

    }
}
class GuessNum extends Persion {
    private int $resultnum;
    public bool $result;
    private int $num;
    private int $type;
    public function __construct(public string $name, public int $gap = 1) {
        $this->num = getroundnum();
        $this->resultnum = getroundnum();
        $this->result = false;
    }
    //type 1加数字 2减数字
    private function getnextnum() {
        if ($this->num === 0) {
            return $this->num;
        }
        $this->num = match ($this->type) {
            1 => $this->num + $this->gap,
            2 => $this->num - $this->gap,
        };
    }
    public function doguessonce() {
        var_dump("resultnum:" . $this->resultnum . " name:" . $this->name . " num:" . $this->num);
        $result = false;
        $this->type = 0;
        match (true) {
            $this->num == $this->resultnum => $result = true,
            $this->num > $this->resultnum => $this->type = 2, //减
            $this->num < $this->resultnum => $this->type = 1//加
        };
        $this->result = $result;
        if (!$result) {
            $this->getnextnum();
        }
    }
}
php 复制代码
$persion_list = [
    new GuessNum("test1", 1),
    new GuessNum("npc", 2),
];

$fibers = [];

foreach ($persion_list as $entity) {
    $fibers[] = new Fiber(function () use ($entity) {
        while (true) {
            $entity->doguessonce($entity);
            Fiber::suspend($entity);
        }
    });
}

// Start all fibers
$endresult = false;
foreach ($fibers as $fiber) {
    $value = $fiber->start();
    if ($value->result) {
        $endresult = true;
        var_dump("game end success name:" . $value->name);
        break;
    }
}
if (!$endresult) {
    for ($i = 0; $i < 5; $i++) {
        foreach ($fibers as $fiber) {
            $value = $fiber->resume();
            if ($value->result) {
                $endresult = true;
                var_dump("game end success name:" . $value->name);
                break;
            }
        }
        sleep(1);
        var_dump("once end");
    }
}
var_dump("game end ~");

输出

php 复制代码
string(30) "resultnum:12 name:test1 num:22"
string(27) "resultnum:7 name:npc num:17"
string(30) "resultnum:12 name:test1 num:21"
string(27) "resultnum:7 name:npc num:15"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:20"
string(27) "resultnum:7 name:npc num:13"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:19"
string(27) "resultnum:7 name:npc num:11"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:18"
string(26) "resultnum:7 name:npc num:9"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:17"
string(26) "resultnum:7 name:npc num:7"
string(25) "game end success name:npc"
string(8) "once end"
string(10) "game end ~"
相关推荐
weixin_472339463 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击3 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue5 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
m0_555762905 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
浪裡遊6 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
lzb_kkk7 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼7 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
简佐义的博客8 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
程序员爱钓鱼8 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt