* @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 $stmt = $db->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 = checkGameState($config); if ($gameState !== 'ongoing') { logGame($gameState); } echo json_encode( [ 'scores' => $_SESSION['scores'], 'event' => $event, 'gameState' => $gameState, ] ); } elseif ($action === 'assassinate') { $assassinationResult = assassinate($config); if ($assassinationResult['gameState'] !== 'ongoing') { logGame($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'], ] ); } /** * 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: ' . imperialPalaceEvent($config); } elseif ($roll <= 5) { $event = 'Relative Unrest: ' . relativeUnrestEvent($config); } else { $_SESSION['scores']['flames']++; $event = 'The fires spread. +1 Flames.'; } return $event; } /** * 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; } return $event; } /** * 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; } 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' ]; } else { return [ 'event' => 'Your attempt to assassinate the emperor has failed. You are executed.', 'gameState' => 'loss_assassination' ]; } } /** * 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'; } /** * 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'], $endCondition ] ); }