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

View File

@@ -1,4 +1,17 @@
<?php <?php
/**
* Config file for the Last Days of Rome.
*
* PHP version: 8.0+
*
* @category Configuration
* @package Rome
* @author Keith Solomon <keith@keithsolmon.net>
* @license MIT License
* @version GIT: $Id$
* @link https://git.keithsolomon.net/keith/Warframe_Shopping_List
*/
require_once 'database.php'; require_once 'database.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
@@ -6,13 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt = $db->prepare('UPDATE configuration SET value = ? WHERE name = ?'); $stmt = $db->prepare('UPDATE configuration SET value = ? WHERE name = ?');
$stmt->execute([$value, $name]); $stmt->execute([$value, $name]);
} }
header('Location: config.php'); header('Location: config.php');
exit; exit;
} }
$stmt = $db->query('SELECT * FROM configuration'); $stmt = $db->query('SELECT * FROM configuration');
$config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@@ -22,17 +38,31 @@ $config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="config.css"> <link rel="stylesheet" href="config.css">
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>Configuration</h1> <h1>Configuration</h1>
<form method="POST"> <form method="POST">
<?php foreach ($config as $name => $value): ?> <?php foreach ($config as $name => $value): ?>
<div class="form-group"> <div class="form-group">
<label for="<?= $name ?>"><?= ucwords(str_replace('_', ' ', $name)) ?></label> <label for="<?php echo $name ?>">
<?php echo ucwords(str_replace('_', ' ', $name)) ?>
</label>
<?php if (strlen($value) > 80) : ?> <?php if (strlen($value) > 80) : ?>
<textarea id="<?= $name ?>" name="<?= $name ?>"><?= htmlspecialchars($value) ?></textarea> <textarea
id="<?php echo $name ?>"
name="<?php echo $name ?>"
>
<?php echo htmlspecialchars($value) ?>
</textarea>
<?php else: ?> <?php else: ?>
<input type="text" id="<?= $name ?>" name="<?= $name ?>" value="<?= htmlspecialchars($value) ?>"> <input
type="text"
id="<?php echo $name ?>"
name="<?php echo $name ?>"
value="<?php echo htmlspecialchars($value) ?>"
>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>

View File

@@ -1,22 +1,40 @@
<?php <?php
/**
* Database configuration for the Last Days of Rome.
*
* PHP version: 8.0+
*
* @category Database
* @package Rome
* @author Keith Solomon <keith@keithsolmon.net>
* @license MIT License
* @version GIT: $Id$
* @link https://git.keithsolomon.net/keith/Warframe_Shopping_List
*/
$db = new PDO('sqlite:game.db'); $db = new PDO('sqlite:game.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create tables if they don't exist // Create tables if they don't exist
$db->exec('CREATE TABLE IF NOT EXISTS game_logs ( $db->exec(
'CREATE TABLE IF NOT EXISTS game_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
date_played DATETIME, date_played DATETIME,
flames_score INTEGER, flames_score INTEGER,
desolation_score INTEGER, desolation_score INTEGER,
relocation_score INTEGER, relocation_score INTEGER,
end_condition TEXT end_condition TEXT
)'); )'
);
$db->exec('CREATE TABLE IF NOT EXISTS configuration ( $db->exec(
'CREATE TABLE IF NOT EXISTS configuration (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, name TEXT,
value TEXT value TEXT
)'); )'
);
// Check if configuration is already seeded // Check if configuration is already seeded
$stmt = $db->query('SELECT COUNT(*) FROM configuration'); $stmt = $db->query('SELECT COUNT(*) FROM configuration');
@@ -29,23 +47,59 @@ if ($count === 0) {
['desolation_win', '10'], ['desolation_win', '10'],
['relocation_win', '10'], ['relocation_win', '10'],
['assassination_win', '20'], ['assassination_win', '20'],
['imperial_palace_event_1', 'The emperor fiddles with manic glee. +1 Flames.'], [
['imperial_palace_event_2', 'The emperor raises another horse to the position of senator. +1 Desolation, +1 Relocation.'], 'imperial_palace_event_1',
['imperial_palace_event_3', 'It\'s execution night at the palace. +1 Relocation.'], 'The emperor fiddles with manic glee. +1 Flames.'
['imperial_palace_event_4', 'The emperor screams like a baby. +1 Desolation.'], ],
['imperial_palace_event_5', 'The emperor sits in front of the flame and commands it to obey. It does not. +1 Flames, +1 Relocation.'], [
['imperial_palace_event_6', 'Work continues on a house made of pure gold. It keeps melting. +1 Flames.'], 'imperial_palace_event_2',
['relative_unrest_event_1', 'People complain - this is unacceptable. Then they go about their business. +1 Relocation.'], 'The emperor raises another horse to the position of senator. +1 Desolation, +1 Relocation.'
['relative_unrest_event_2', 'There are no goods at market. +1 Desolation.'], ],
['relative_unrest_event_3', 'You receive a letter asking you to reassert your faith in the emperor. In writing. +1 Relocation.'], [
['relative_unrest_event_4', 'Lions are released onto the streets. +1 Desolation, +1 Flames.'], 'imperial_palace_event_3',
['relative_unrest_event_5', 'Is Rome really over? +1 Desolation.'], 'It\'s execution night at the palace. +1 Relocation.'
['relative_unrest_event_6', 'The burnings will continue until morale improves. +1 Flames.'], ],
[
'imperial_palace_event_4',
'The emperor screams like a baby. +1 Desolation.'
],
[
'imperial_palace_event_5',
'The emperor sits in front of the flame and commands it to obey. It does not. +1 Flames, +1 Relocation.'
],
[
'imperial_palace_event_6',
'Work continues on a house made of pure gold. It keeps melting. +1 Flames.'
],
[
'relative_unrest_event_1',
'People complain - this is unacceptable. Then they go about their business. +1 Relocation.'
],
[
'relative_unrest_event_2',
'There are no goods at market. +1 Desolation.'
],
[
'relative_unrest_event_3',
'You receive a letter asking you to reassert your faith in the emperor. In writing. +1 Relocation.'
],
[
'relative_unrest_event_4',
'Lions are released onto the streets. +1 Desolation, +1 Flames.'
],
[
'relative_unrest_event_5',
'Is Rome really over? +1 Desolation.'
],
[
'relative_unrest_event_6',
'The burnings will continue until morale improves. +1 Flames.'
],
]; ];
$stmt = $db->prepare('INSERT INTO configuration (name, value) VALUES (?, ?)'); $stmt = $db->prepare('INSERT INTO configuration (name, value) VALUES (?, ?)');
foreach ($config as $item) { foreach ($config as $item) {
$stmt->execute($item); $stmt->execute($item);
} }
} }
?>

140
game.php
View File

@@ -1,5 +1,19 @@
<?php <?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(); session_start();
require_once 'database.php'; require_once 'database.php';
// Fetch configuration from the database // Fetch configuration from the database
@@ -18,53 +32,80 @@ $action = $_GET['action'] ?? '';
if ($action === 'roll_dice') { if ($action === 'roll_dice') {
$event = roll_dice($config); $event = roll_dice($config);
$gameState = check_game_state($config); $gameState = checkGameState($config);
if ($gameState !== 'ongoing') { if ($gameState !== 'ongoing') {
log_game($gameState); logGame($gameState);
} }
echo json_encode([
echo json_encode(
[
'scores' => $_SESSION['scores'], 'scores' => $_SESSION['scores'],
'event' => $event, 'event' => $event,
'gameState' => $gameState, 'gameState' => $gameState,
]); ]
);
} elseif ($action === 'assassinate') { } elseif ($action === 'assassinate') {
$assassinationResult = assassinate($config); $assassinationResult = assassinate($config);
if ($assassinationResult['gameState'] !== 'ongoing') { if ($assassinationResult['gameState'] !== 'ongoing') {
log_game($assassinationResult['gameState']); logGame($assassinationResult['gameState']);
} }
echo json_encode([
echo json_encode(
[
'event' => $assassinationResult['event'], 'event' => $assassinationResult['event'],
'gameState' => $assassinationResult['gameState'], 'gameState' => $assassinationResult['gameState'],
]); ]
);
} elseif ($action === 'reset_game') { } elseif ($action === 'reset_game') {
$_SESSION['scores'] = [ $_SESSION['scores'] = [
'flames' => 0, 'flames' => 0,
'desolation' => 0, 'desolation' => 0,
'relocation' => 0, 'relocation' => 0,
]; ];
echo json_encode([
echo json_encode(
[
'scores' => $_SESSION['scores'], '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); $roll = rand(1, 6);
$event = ''; $event = '';
if ($roll <= 3) { if ($roll <= 3) {
$event = 'In the Imperial Palace: ' . imperial_palace_event($config); $event = 'In the Imperial Palace: ' . imperialPalaceEvent($config);
} elseif ($roll <= 5) { } elseif ($roll <= 5) {
$event = 'Relative Unrest: ' . relative_unrest_event($config); $event = 'Relative Unrest: ' . relativeUnrestEvent($config);
} else { } else {
$_SESSION['scores']['flames']++; $_SESSION['scores']['flames']++;
$event = 'The fires spread. +1 Flames.'; $event = 'The fires spread. +1 Flames.';
} }
return $event; 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); $roll = rand(1, 6);
$event = ''; $event = '';
switch ($roll) { switch ($roll) {
case 1: case 1:
$_SESSION['scores']['flames']++; $_SESSION['scores']['flames']++;
@@ -93,12 +134,21 @@ function imperial_palace_event($config) {
$event = $config['imperial_palace_event_6']; $event = $config['imperial_palace_event_6'];
break; break;
} }
return $event; 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); $roll = rand(1, 6);
$event = ''; $event = '';
switch ($roll) { switch ($roll) {
case 1: case 1:
$_SESSION['scores']['relocation']++; $_SESSION['scores']['relocation']++;
@@ -126,38 +176,88 @@ function relative_unrest_event($config) {
$event = $config['relative_unrest_event_6']; $event = $config['relative_unrest_event_6'];
break; break;
} }
return $event; 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) { function assassinate($config) {
$desolation = $_SESSION['scores']['desolation']; $desolation = $_SESSION['scores']['desolation'];
$total = 0; $total = 0;
for ($i = 0; $i < $desolation; $i++) { for ($i = 0; $i < $desolation; $i++) {
$total += rand(1, 6); $total += rand(1, 6);
} }
if ($total >= $config['assassination_win']) { 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 { } 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']) { if ($_SESSION['scores']['flames'] >= $config['flames_win']) {
return 'loss_flames'; return 'loss_flames';
} }
if ($_SESSION['scores']['desolation'] >= $config['desolation_win']) { if ($_SESSION['scores']['desolation'] >= $config['desolation_win']) {
return 'loss_desolation'; return 'loss_desolation';
} }
if ($_SESSION['scores']['relocation'] >= $config['relocation_win']) { if ($_SESSION['scores']['relocation'] >= $config['relocation_win']) {
return 'win'; return 'win';
} }
return 'ongoing'; 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; 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
]
);
} }