UK
apps/caramail-chat / src / Form / Demo.php · commit 6eebcdb
<?phpnamespace App\Form;use App\Net\FakeLink;/** * A chat without a server: the frames a 2002 server would have sent, one after the other (the smoke test, the * screenshots, the playground's page when no server is set). What the window writes is answered when it matters. */final class Demo{    private float $time = 0;    private int $next = 0;    /** @var list<array{0: float, 1: \Closure(FakeLink, ChatForm): void}> */    private array $script;    private const ME = '1234:56:78901:0';    private const LEA = '4321:12:34567:0';    private const MAX = '777:88:99999:0';    private const SOPHIE = '555:44:33333:0';    public function __construct(private readonly FakeLink $link)    {        $member = static fn (string $id, string $nick, int $age, int $sex, string $city, int $admin = 0) => [$id, (string)$admin, $nick, '0', (string)$age, (string)$sex, $city];        $this->script = [            [0.0, static fn (FakeLink $l) => $l->open()],            [0.1, static fn (FakeLink $l) => $l->serverSays('100', 'NewCaraChat v1.00 By Duch')],            [0.2, static fn (FakeLink $l) => $l->serverSays('0', self::ME, '197', '0', 'Vous')],            [0.3, static fn (FakeLink $l) => $l->serverSays('20', '0', 'Salon', '?', 'admin')],            [0.4, static fn (FakeLink $l) => $l->serverSays('85', '-1', ...$member(self::ME, 'Vous', 25, 1, 'Paris'),                ...$member(self::LEA, 'Lea', 23, 0, 'Lyon'), ...$member(self::MAX, 'Max', 31, 1, 'Lille'),                ...$member(self::SOPHIE, 'Sophie', 0, 0, '?', 1))],            [0.5, static fn (FakeLink $l) => $l->serverSays('94', 'Salon', '?', 'admin', '44', '10 - 15 ans', '?', 'admin', '0', '15 - 20 ans', '?', 'admin', '6', '20 - 25 ans', '?', 'admin', '9', '25 - 30 ans', '?', 'admin', '4', 'Belgique', '?', 'admin', '2', 'plus de 50 ans', '?', 'admin', '3', 'Salon : 2', '?', 'admin', '12')],            [0.8, static fn (FakeLink $l) => $l->serverSays('70', self::LEA, '0', 'Lea', '0100Bonsoir tout le monde :-)')],            [1.3, static fn (FakeLink $l) => $l->serverSays('70', self::MAX, '0', 'Max', '0101Salut Lea ;-) ça va ? @11')],            [1.8, static fn (FakeLink $l) => $l->serverSays('70', self::LEA, '0', 'Lea', "0011Super bien, et toi ? :-D")],            [2.3, static fn (FakeLink $l) => $l->serverSays('70', self::SOPHIE, '0', 'Sophie', '0508Bienvenue sur CaraChat ! 8-) @05')],            [2.8, static fn (FakeLink $l) => $l->serverSays('70', self::MAX, '0', 'Max', '0302Qui vient au ciné ce soir ? :o)')],            [3.3, static fn (FakeLink $l) => $l->serverSays('72', self::LEA, '0', 'Lea', '0103Coucou Vous, tu viens de Paris ? :-)2')],            [3.8, static fn (FakeLink $l) => $l->serverSays('85', '0', ...$member('999:1:2:0', 'Karim', 27, 1, 'Nantes'))],            [4.2, static fn (FakeLink $l) => $l->serverSays('70', '999:1:2:0', '0', 'Karim', '0108Bonsoir ! @00 @09 :-P')],            [4.6, static fn (FakeLink $l) => $l->serverSays('56', '-2:-2:-2', self::LEA, 'Lea', '0', '23', '0', 'Lyon', 'Salon', '0',                self::MAX, 'Max', '0', '31', '1', 'Lille', 'Salon', '0', '999:1:2:0', 'Karim', '0', '27', '1', 'Nantes', 'Salon', '0',                '31:4:1:0', 'Julie', '0', '19', '0', 'Brest', 'Musique', '0', '32:5:2:0', 'Paul', '0', '45', '1', 'Lyon', '?', '1')],            [4.7, static fn (FakeLink $l) => $l->serverSays('199', '67')],            [5.0, static function (FakeLink $l, ChatForm $f): void {                $f->line->text = 'Salut tout le monde :-) #13';                $f->onSendClick();            }],            [5.4, static function (FakeLink $l, ChatForm $f): void {                if ($f->demoTab === 'typing') {                    $f->line->text = 'Je tape une phrase avec un smiley @12';                    $f->setFocus($f->line);                } elseif ($f->demoTab === 'members') {                    $f->roomSwitch->choose('members');                } elseif ($f->demoTab === 'smileys') {                    $f->sideTabs->choose('smileys');                } elseif ($f->demoTab !== '') {                    $f->show($f->demoTab === 'private' ? 'pv:' . self::LEA : $f->demoTab);                }            }],        ];    }    public function tick(float $dt, ChatForm $form): void    {        $this->time += $dt;        while ($this->next < count($this->script) && $this->script[$this->next][0] <= $this->time) {            ($this->script[$this->next][1])($this->link, $form);            $this->next++;        }        // the "server" answers what the window sends: its own sentences come back in the room        foreach ($this->link->take() as $line) {            $f = explode("\t", $line);            if ($f[0] === '70' && isset($f[1])) {                $this->link->serverSays('70', self::ME, '0', 'Vous', $f[1]);            }        }    }}