feat: Add detailed PHPDoc comments and improve code readability

This commit is contained in:
Keith Solomon
2025-08-15 18:12:19 -05:00
parent b9cb61750d
commit 17e925ec8e
3 changed files with 283 additions and 99 deletions

254
game.php
View File

@@ -1,5 +1,19 @@
<?php
/**
* Config file for the Last Days of Rome.
*
* PHP version: 8.0+
*
* @category API
* @package Rome
* @author Keith Solomon <keith@keithsolmon.net>
* @license MIT License
* @version GIT: $Id$
* @link https://git.keithsolomon.net/keith/Warframe_Shopping_List
*/
session_start();
require_once 'database.php';
// Fetch configuration from the database
@@ -18,146 +32,232 @@ $action = $_GET['action'] ?? '';
if ($action === 'roll_dice') {
$event = roll_dice($config);
$gameState = check_game_state($config);
$gameState = checkGameState($config);
if ($gameState !== 'ongoing') {
log_game($gameState);
logGame($gameState);
}
echo json_encode([
'scores' => $_SESSION['scores'],
'event' => $event,
'gameState' => $gameState,
]);
echo json_encode(
[
'scores' => $_SESSION['scores'],
'event' => $event,
'gameState' => $gameState,
]
);
} elseif ($action === 'assassinate') {
$assassinationResult = assassinate($config);
if ($assassinationResult['gameState'] !== 'ongoing') {
log_game($assassinationResult['gameState']);
logGame($assassinationResult['gameState']);
}
echo json_encode([
'event' => $assassinationResult['event'],
'gameState' => $assassinationResult['gameState'],
]);
echo json_encode(
[
'event' => $assassinationResult['event'],
'gameState' => $assassinationResult['gameState'],
]
);
} elseif ($action === 'reset_game') {
$_SESSION['scores'] = [
'flames' => 0,
'desolation' => 0,
'relocation' => 0,
];
echo json_encode([
'scores' => $_SESSION['scores'],
]);
echo json_encode(
[
'scores' => $_SESSION['scores'],
]
);
}
function roll_dice($config) {
/**
* Rolls a dice and determines the event based on the result.
*
* @param array $config The game configuration.
*
* @return string The event description.
*/
function rollDice($config) {
$roll = rand(1, 6);
$event = '';
if ($roll <= 3) {
$event = 'In the Imperial Palace: ' . imperial_palace_event($config);
$event = 'In the Imperial Palace: ' . imperialPalaceEvent($config);
} elseif ($roll <= 5) {
$event = 'Relative Unrest: ' . relative_unrest_event($config);
$event = 'Relative Unrest: ' . relativeUnrestEvent($config);
} else {
$_SESSION['scores']['flames']++;
$event = 'The fires spread. +1 Flames.';
}
return $event;
}
function imperial_palace_event($config) {
/**
* Handles events occurring in the Imperial Palace based on a dice roll.
*
* @param array $config The game configuration.
*
* @return string The event description.
*/
function imperialPalaceEvent($config) {
$roll = rand(1, 6);
$event = '';
switch ($roll) {
case 1:
$_SESSION['scores']['flames']++;
$event = $config['imperial_palace_event_1'];
break;
case 2:
$_SESSION['scores']['desolation']++;
$_SESSION['scores']['relocation']++;
$event = $config['imperial_palace_event_2'];
break;
case 3:
$_SESSION['scores']['relocation']++;
$event = $config['imperial_palace_event_3'];
break;
case 4:
$_SESSION['scores']['desolation']++;
$event = $config['imperial_palace_event_4'];
break;
case 5:
$_SESSION['scores']['flames']++;
$_SESSION['scores']['relocation']++;
$event = $config['imperial_palace_event_5'];
break;
case 6:
$_SESSION['scores']['flames']++;
$event = $config['imperial_palace_event_6'];
break;
case 1:
$_SESSION['scores']['flames']++;
$event = $config['imperial_palace_event_1'];
break;
case 2:
$_SESSION['scores']['desolation']++;
$_SESSION['scores']['relocation']++;
$event = $config['imperial_palace_event_2'];
break;
case 3:
$_SESSION['scores']['relocation']++;
$event = $config['imperial_palace_event_3'];
break;
case 4:
$_SESSION['scores']['desolation']++;
$event = $config['imperial_palace_event_4'];
break;
case 5:
$_SESSION['scores']['flames']++;
$_SESSION['scores']['relocation']++;
$event = $config['imperial_palace_event_5'];
break;
case 6:
$_SESSION['scores']['flames']++;
$event = $config['imperial_palace_event_6'];
break;
}
return $event;
}
function relative_unrest_event($config) {
/**
* Handles events related to relative unrest based on a dice roll.
*
* @param array $config The game configuration.
*
* @return string The event description.
*/
function relativeUnrestEvent($config) {
$roll = rand(1, 6);
$event = '';
switch ($roll) {
case 1:
$_SESSION['scores']['relocation']++;
$event = $config['relative_unrest_event_1'];
break;
case 2:
$_SESSION['scores']['desolation']++;
$event = $config['relative_unrest_event_2'];
break;
case 3:
$_SESSION['scores']['relocation']++;
$event = $config['relative_unrest_event_3'];
break;
case 4:
$_SESSION['scores']['desolation']++;
$_SESSION['scores']['flames']++;
$event = $config['relative_unrest_event_4'];
break;
case 5:
$_SESSION['scores']['desolation']++;
$event = $config['relative_unrest_event_5'];
break;
case 6:
$_SESSION['scores']['flames']++;
$event = $config['relative_unrest_event_6'];
break;
case 1:
$_SESSION['scores']['relocation']++;
$event = $config['relative_unrest_event_1'];
break;
case 2:
$_SESSION['scores']['desolation']++;
$event = $config['relative_unrest_event_2'];
break;
case 3:
$_SESSION['scores']['relocation']++;
$event = $config['relative_unrest_event_3'];
break;
case 4:
$_SESSION['scores']['desolation']++;
$_SESSION['scores']['flames']++;
$event = $config['relative_unrest_event_4'];
break;
case 5:
$_SESSION['scores']['desolation']++;
$event = $config['relative_unrest_event_5'];
break;
case 6:
$_SESSION['scores']['flames']++;
$event = $config['relative_unrest_event_6'];
break;
}
return $event;
}
/**
* Attempts to assassinate the emperor based on the current desolation score.
*
* @param array $config The game configuration.
*
* @return array An array containing the event description and game state.
*/
function assassinate($config) {
$desolation = $_SESSION['scores']['desolation'];
$total = 0;
for ($i = 0; $i < $desolation; $i++) {
$total += rand(1, 6);
}
if ($total >= $config['assassination_win']) {
return ['event' => 'You have assassinated the emperor! You die a martyr.', 'gameState' => 'win_assassination'];
return [
'event' => 'You have assassinated the emperor! You die a martyr.',
'gameState' => 'win_assassination'
];
} else {
return ['event' => 'Your attempt to assassinate the emperor has failed. You are executed.', 'gameState' => 'loss_assassination'];
return [
'event' => 'Your attempt to assassinate the emperor has failed. You are executed.',
'gameState' => 'loss_assassination'
];
}
}
function check_game_state($config) {
/**
* Checks the current game state based on scores and configuration.
*
* @param array $config The game configuration.
*
* @return string The current game state.
*/
function checkGameState($config) {
if ($_SESSION['scores']['flames'] >= $config['flames_win']) {
return 'loss_flames';
}
if ($_SESSION['scores']['desolation'] >= $config['desolation_win']) {
return 'loss_desolation';
}
if ($_SESSION['scores']['relocation'] >= $config['relocation_win']) {
return 'win';
}
return 'ongoing';
}
function log_game($end_condition) {
/**
* Logs the end of a game with scores and end condition.
*
* @param string $endCondition The condition that ended the game.
*
* @return void
*/
function logGame($endCondition) {
global $db;
$stmt = $db->prepare('INSERT INTO game_logs (date_played, flames_score, desolation_score, relocation_score, end_condition) VALUES (?, ?, ?, ?, ?)');
$stmt->execute([date('Y-m-d H:i:s'), $_SESSION['scores']['flames'], $_SESSION['scores']['desolation'], $_SESSION['scores']['relocation'], $end_condition]);
$stmt = $db->prepare(
'INSERT INTO game_logs (
date_played,
flames_score,
desolation_score,
relocation_score,
end_condition)
VALUES (?, ?, ?, ?, ?)'
);
$stmt->execute(
[
date('Y-m-d H:i:s'),
$_SESSION['scores']['flames'],
$_SESSION['scores']['desolation'],
$_SESSION['scores']['relocation'],
$endCondition
]
);
}