✨ feat: Add detailed PHPDoc comments and improve code readability
This commit is contained in:
40
config.php
40
config.php
@@ -1,4 +1,17 @@
|
||||
<?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';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
@@ -6,13 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$stmt = $db->prepare('UPDATE configuration SET value = ? WHERE name = ?');
|
||||
$stmt->execute([$value, $name]);
|
||||
}
|
||||
|
||||
header('Location: config.php');
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $db->query('SELECT * FROM configuration');
|
||||
$config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
@@ -22,17 +38,31 @@ $config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="stylesheet" href="config.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Configuration</h1>
|
||||
|
||||
<form method="POST">
|
||||
<?php foreach ($config as $name => $value): ?>
|
||||
<div class="form-group">
|
||||
<label for="<?= $name ?>"><?= ucwords(str_replace('_', ' ', $name)) ?></label>
|
||||
<?php if (strlen($value) > 80): ?>
|
||||
<textarea id="<?= $name ?>" name="<?= $name ?>"><?= htmlspecialchars($value) ?></textarea>
|
||||
<label for="<?php echo $name ?>">
|
||||
<?php echo ucwords(str_replace('_', ' ', $name)) ?>
|
||||
</label>
|
||||
<?php if (strlen($value) > 80) : ?>
|
||||
<textarea
|
||||
id="<?php echo $name ?>"
|
||||
name="<?php echo $name ?>"
|
||||
>
|
||||
<?php echo htmlspecialchars($value) ?>
|
||||
</textarea>
|
||||
<?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; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
@@ -41,4 +71,4 @@ $config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
<a href="index.php">Back to Game</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
88
database.php
88
database.php
@@ -1,22 +1,40 @@
|
||||
<?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->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// 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,
|
||||
date_played DATETIME,
|
||||
flames_score INTEGER,
|
||||
desolation_score INTEGER,
|
||||
relocation_score INTEGER,
|
||||
end_condition TEXT
|
||||
)');
|
||||
)'
|
||||
);
|
||||
|
||||
$db->exec('CREATE TABLE IF NOT EXISTS configuration (
|
||||
$db->exec(
|
||||
'CREATE TABLE IF NOT EXISTS configuration (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT,
|
||||
value TEXT
|
||||
)');
|
||||
)'
|
||||
);
|
||||
|
||||
// Check if configuration is already seeded
|
||||
$stmt = $db->query('SELECT COUNT(*) FROM configuration');
|
||||
@@ -29,23 +47,59 @@ if ($count === 0) {
|
||||
['desolation_win', '10'],
|
||||
['relocation_win', '10'],
|
||||
['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_3', 'It\'s execution night at the palace. +1 Relocation.'],
|
||||
['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.'],
|
||||
[
|
||||
'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_3',
|
||||
'It\'s execution night at the palace. +1 Relocation.'
|
||||
],
|
||||
[
|
||||
'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 (?, ?)');
|
||||
|
||||
foreach ($config as $item) {
|
||||
$stmt->execute($item);
|
||||
}
|
||||
}
|
||||
?>
|
||||
254
game.php
254
game.php
@@ -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
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user