feat: Implement SQLite backend and configuration UI
This commit is contained in:
69
game.php
69
game.php
@@ -1,5 +1,10 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'database.php';
|
||||
|
||||
// Fetch configuration from the database
|
||||
$stmt = $db->query('SELECT * FROM configuration');
|
||||
$config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
|
||||
if (!isset($_SESSION['scores'])) {
|
||||
$_SESSION['scores'] = [
|
||||
@@ -12,15 +17,21 @@ if (!isset($_SESSION['scores'])) {
|
||||
$action = $_GET['action'] ?? '';
|
||||
|
||||
if ($action === 'roll_dice') {
|
||||
$event = roll_dice();
|
||||
$gameState = check_game_state();
|
||||
$event = roll_dice($config);
|
||||
$gameState = check_game_state($config);
|
||||
if ($gameState !== 'ongoing') {
|
||||
log_game($gameState);
|
||||
}
|
||||
echo json_encode([
|
||||
'scores' => $_SESSION['scores'],
|
||||
'event' => $event,
|
||||
'gameState' => $gameState,
|
||||
]);
|
||||
} elseif ($action === 'assassinate') {
|
||||
$assassinationResult = assassinate();
|
||||
$assassinationResult = assassinate($config);
|
||||
if ($assassinationResult['gameState'] !== 'ongoing') {
|
||||
log_game($assassinationResult['gameState']);
|
||||
}
|
||||
echo json_encode([
|
||||
'event' => $assassinationResult['event'],
|
||||
'gameState' => $assassinationResult['gameState'],
|
||||
@@ -36,14 +47,14 @@ if ($action === 'roll_dice') {
|
||||
]);
|
||||
}
|
||||
|
||||
function roll_dice() {
|
||||
function roll_dice($config) {
|
||||
$roll = rand(1, 6);
|
||||
$event = '';
|
||||
|
||||
if ($roll <= 3) {
|
||||
$event = 'In the Imperial Palace: ' . imperial_palace_event();
|
||||
$event = 'In the Imperial Palace: ' . imperial_palace_event($config);
|
||||
} elseif ($roll <= 5) {
|
||||
$event = 'Relative Unrest: ' . relative_unrest_event();
|
||||
$event = 'Relative Unrest: ' . relative_unrest_event($config);
|
||||
} else {
|
||||
$_SESSION['scores']['flames']++;
|
||||
$event = 'The fires spread. +1 Flames.';
|
||||
@@ -51,96 +62,102 @@ function roll_dice() {
|
||||
return $event;
|
||||
}
|
||||
|
||||
function imperial_palace_event() {
|
||||
function imperial_palace_event($config) {
|
||||
$roll = rand(1, 6);
|
||||
$event = '';
|
||||
switch ($roll) {
|
||||
case 1:
|
||||
$_SESSION['scores']['flames']++;
|
||||
$event = 'The emperor fiddles with manic glee. +1 Flames.';
|
||||
$event = $config['imperial_palace_event_1'];
|
||||
break;
|
||||
case 2:
|
||||
$_SESSION['scores']['desolation']++;
|
||||
$_SESSION['scores']['relocation']++;
|
||||
$event = 'The emperor raises another horse to the position of senator. +1 Desolation, +1 Relocation.';
|
||||
$event = $config['imperial_palace_event_2'];
|
||||
break;
|
||||
case 3:
|
||||
$_SESSION['scores']['relocation']++;
|
||||
$event = 'It\'s execution night at the palace. +1 Relocation.';
|
||||
$event = $config['imperial_palace_event_3'];
|
||||
break;
|
||||
case 4:
|
||||
$_SESSION['scores']['desolation']++;
|
||||
$event = 'The emperor screams like a baby. +1 Desolation.';
|
||||
$event = $config['imperial_palace_event_4'];
|
||||
break;
|
||||
case 5:
|
||||
$_SESSION['scores']['flames']++;
|
||||
$_SESSION['scores']['relocation']++;
|
||||
$event = 'The emperor sits in front of the flame and commands it to obey. It does not. +1 Flames, +1 Relocation.';
|
||||
$event = $config['imperial_palace_event_5'];
|
||||
break;
|
||||
case 6:
|
||||
$_SESSION['scores']['flames']++;
|
||||
$event = 'Work continues on a house made of pure gold. It keeps melting. +1 Flames.';
|
||||
$event = $config['imperial_palace_event_6'];
|
||||
break;
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
|
||||
function relative_unrest_event() {
|
||||
function relative_unrest_event($config) {
|
||||
$roll = rand(1, 6);
|
||||
$event = '';
|
||||
switch ($roll) {
|
||||
case 1:
|
||||
$_SESSION['scores']['relocation']++;
|
||||
$event = 'People complain - this is unacceptable. Then they go about their business. +1 Relocation.';
|
||||
$event = $config['relative_unrest_event_1'];
|
||||
break;
|
||||
case 2:
|
||||
$_SESSION['scores']['desolation']++;
|
||||
$event = 'There are no goods at market. +1 Desolation.';
|
||||
$event = $config['relative_unrest_event_2'];
|
||||
break;
|
||||
case 3:
|
||||
$_SESSION['scores']['relocation']++;
|
||||
$event = 'You receive a letter asking you to reassert your faith in the emperor. In writing. +1 Relocation.';
|
||||
$event = $config['relative_unrest_event_3'];
|
||||
break;
|
||||
case 4:
|
||||
$_SESSION['scores']['desolation']++;
|
||||
$_SESSION['scores']['flames']++;
|
||||
$event = 'Lions are released onto the streets. +1 Desolation, +1 Flames.';
|
||||
$event = $config['relative_unrest_event_4'];
|
||||
break;
|
||||
case 5:
|
||||
$_SESSION['scores']['desolation']++;
|
||||
$event = 'Is Rome really over? +1 Desolation.';
|
||||
$event = $config['relative_unrest_event_5'];
|
||||
break;
|
||||
case 6:
|
||||
$_SESSION['scores']['flames']++;
|
||||
$event = 'The burnings will continue until morale improves. +1 Flames.';
|
||||
$event = $config['relative_unrest_event_6'];
|
||||
break;
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
|
||||
function assassinate() {
|
||||
function assassinate($config) {
|
||||
$desolation = $_SESSION['scores']['desolation'];
|
||||
$total = 0;
|
||||
for ($i = 0; $i < $desolation; $i++) {
|
||||
$total += rand(1, 6);
|
||||
}
|
||||
|
||||
if ($total >= 20) {
|
||||
if ($total >= $config['assassination_win']) {
|
||||
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'];
|
||||
}
|
||||
}
|
||||
|
||||
function check_game_state() {
|
||||
if ($_SESSION['scores']['flames'] >= 10) {
|
||||
function check_game_state($config) {
|
||||
if ($_SESSION['scores']['flames'] >= $config['flames_win']) {
|
||||
return 'loss_flames';
|
||||
}
|
||||
if ($_SESSION['scores']['desolation'] >= 10) {
|
||||
if ($_SESSION['scores']['desolation'] >= $config['desolation_win']) {
|
||||
return 'loss_desolation';
|
||||
}
|
||||
if ($_SESSION['scores']['relocation'] >= 10) {
|
||||
if ($_SESSION['scores']['relocation'] >= $config['relocation_win']) {
|
||||
return 'win';
|
||||
}
|
||||
return 'ongoing';
|
||||
}
|
||||
|
||||
function log_game($end_condition) {
|
||||
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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user