<?php declare(strict_types = 0);
/*
** Copyright (C) 2021-2026 initMAX s.r.o.
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/


namespace Modules\TetrisMAX\Includes;

/**
 * Everything the browser needs to run a game, built ONCE for both trees.
 *
 * The 6.4 - 7.4 controller (actions/WidgetView.php) and the 6.0/6.2 one
 * (legacy/actions/widgets/tetrisMAXView.php) differ only in the base class
 * Zabbix hands them. What they answer with comes from here, so the game cannot
 * deal a different set of pieces on one Zabbix line than on another.
 *
 * This is also the ONLY thing the widget ever asks the server for, and it asks
 * once: the shapes, the board size and the starting pace. After that the game
 * is entirely in the browser.
 */
final class GamePayload {

	/** Board size in cells. The client lays the grid out from these. */
	private const BOARD_SIZE_X = 15;
	private const BOARD_SIZE_Y = 20;

	/**
	 * @param array $fields_values  The widget's stored configuration.
	 *
	 * @return array  game_parameters - the whole of it
	 */
	public static function build(array $fields_values): array {
		return [
			'game_parameters' => [
				'board' => [
					'size_x' => self::BOARD_SIZE_X,
					'size_y' => self::BOARD_SIZE_Y
				],
				'fall_speed' => Speed::intervalMs($fields_values['speed'] ?? Speed::NORMAL),
				'tetrominoes' => Tetrominoes::definitionMap()
			]
		];
	}
}
