0, 'desolation' => 0, 'relocation' => 0, ]; } $action = $_GET['action'] ?? ''; if ($action === 'roll_dice') { $event = roll_dice(); $gameState = check_game_state(); echo json_encode([ 'scores' => $_SESSION['scores'], 'event' => $event, 'gameState' => $gameState, ]); } elseif ($action === 'assassinate') { $assassinationResult = assassinate(); 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() { $roll = rand(1, 6); $event = ''; if ($roll <= 3) { $event = 'In the Imperial Palace: ' . imperial_palace_event(); } elseif ($roll <= 5) { $event = 'Relative Unrest: ' . relative_unrest_event(); } else { $_SESSION['scores']['flames']++; $event = 'The fires spread. +1 Flames.'; } return $event; } function imperial_palace_event() { $roll = rand(1, 6); $event = ''; switch ($roll) { case 1: $_SESSION['scores']['flames']++; $event = 'The emperor fiddles with manic glee. +1 Flames.'; break; case 2: $_SESSION['scores']['desolation']++; $_SESSION['scores']['relocation']++; $event = 'The emperor raises another horse to the position of senator. +1 Desolation, +1 Relocation.'; break; case 3: $_SESSION['scores']['relocation']++; $event = 'It\'s execution night at the palace. +1 Relocation.'; break; case 4: $_SESSION['scores']['desolation']++; $event = 'The emperor screams like a baby. +1 Desolation.'; 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.'; break; case 6: $_SESSION['scores']['flames']++; $event = 'Work continues on a house made of pure gold. It keeps melting. +1 Flames.'; break; } return $event; } function relative_unrest_event() { $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.'; break; case 2: $_SESSION['scores']['desolation']++; $event = 'There are no goods at market. +1 Desolation.'; break; case 3: $_SESSION['scores']['relocation']++; $event = 'You receive a letter asking you to reassert your faith in the emperor. In writing. +1 Relocation.'; break; case 4: $_SESSION['scores']['desolation']++; $_SESSION['scores']['flames']++; $event = 'Lions are released onto the streets. +1 Desolation, +1 Flames.'; break; case 5: $_SESSION['scores']['desolation']++; $event = 'Is Rome really over? +1 Desolation.'; break; case 6: $_SESSION['scores']['flames']++; $event = 'The burnings will continue until morale improves. +1 Flames.'; break; } return $event; } function assassinate() { $desolation = $_SESSION['scores']['desolation']; $total = 0; for ($i = 0; $i < $desolation; $i++) { $total += rand(1, 6); } if ($total >= 20) { 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) { return 'loss_flames'; } if ($_SESSION['scores']['desolation'] >= 10) { return 'loss_desolation'; } if ($_SESSION['scores']['relocation'] >= 10) { return 'win'; } return 'ongoing'; }