Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

TicTacToe
in package

Tic-tac-toe multiplayer helpers (Build-the-App capstone).

Moved verbatim out of route/learn.php so the route file stays function-free and hot-reloadable. Behaviour is unchanged — the same room sanitiser, winner detector, and state-broadcast helpers the /ws/tictactoe handler calls.

Game state is stored in the ws_tictactoe_rooms and ws_tictactoe_clients Store tables, which must be created before App::run().

Table of Contents

Methods

ttt_broadcast_state()  : void
Broadcast the current room state to all connected clients in $room.
ttt_broadcast_state_with()  : void
Broadcast the current room state merged with $extras.
ttt_detect_winner()  : array{0: ?string, 1: ?array}
Detect a winner on the given 9-character board string.
ttt_sanitize_room()  : string
Sanitise a room name to a safe, lowercase slug.
asInt()  : int
Coerce a mixed Store value to int.
broadcast()  : void
Shared fan-out core for ttt_broadcast_state() / ttt_broadcast_state_with().

Methods

ttt_broadcast_state()

Broadcast the current room state to all connected clients in $room.

public static ttt_broadcast_state(string $room) : void

Reads the room row from the ws_tictactoe_rooms Store table and pushes a "state" JSON message to every fd found in ws_tictactoe_clients.

Parameters
$room : string

ttt_broadcast_state_with()

Broadcast the current room state merged with $extras.

public static ttt_broadcast_state_with(string $room, array<string, mixed> $extras) : void

Use this variant to piggyback additional fields (e.g. "event", "winner_line") onto the standard state payload in a single push.

Parameters
$room : string
$extras : array<string, mixed>

Extra key-value pairs merged into the payload.

ttt_detect_winner()

Detect a winner on the given 9-character board string.

public static ttt_detect_winner(string $board) : array{0: ?string, 1: ?array}

The board is indexed 08 left-to-right, top-to-bottom; each cell holds 'X', 'O', or '_' (empty). Returns a two-element tuple:

  • [winner_symbol, winning_indices] when a winning line is found (e.g. ['X', [0, 1, 2]]).
  • [null, null] when there is no winner yet.
Parameters
$board : string
Return values
array{0: ?string, 1: ?array}

ttt_sanitize_room()

Sanitise a room name to a safe, lowercase slug.

public static ttt_sanitize_room(string $room) : string

Lowercases, strips anything that is not [a-z0-9-], and truncates to 32 characters so the result is safe as a Store key.

Parameters
$room : string
Return values
string

asInt()

Coerce a mixed Store value to int.

private static asInt(mixed $v) : int

Store columns return string on read even for TYPE_INT columns. Returns 0 for non-numeric values.

Parameters
$v : mixed
Return values
int

broadcast()

Shared fan-out core for ttt_broadcast_state() / ttt_broadcast_state_with().

private static broadcast(string $room, array<string, mixed> $extras) : void

Builds the state payload (optionally merged with $extras) and pushes it to every fd in the room. Single implementation so the two public entry points can't drift in payload shape.

Parameters
$room : string
$extras : array<string, mixed>
On this page