query('SELECT * FROM configuration'); $config = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); if (!isset($_SESSION['scores'])) { $_SESSION['scores'] = [ 'flames' => 0, 'desolation' => 0, 'relocation' => 0, ]; } $action = $_GET['action'] ?? ''; if ($action === 'roll_dice') { $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($config); if ($assassinationResult['gameState'] !== 'ongoing') { log_game($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'], ]); } function roll_dice($config) { $roll = rand(1, 6); $event = ''; if ($roll <= 3) { $event = 'In the Imperial Palace: ' . imperial_palace_event($config); } elseif ($roll <= 5) { $event = 'Relative Unrest: ' . relative_unrest_event($config); } else { $_SESSION['scores']['flames']++; $event = 'The fires spread. +1 Flames.'; } return $event; } function imperial_palace_event($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; } return $event; } function relative_unrest_event($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; } return $event; } 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']; } else { return ['event' => 'Your attempt to assassinate the emperor has failed. You are executed.', 'gameState' => 'loss_assassination']; } } function check_game_state($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) { 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]); }