| 1 | <?php |
| 2 | |
| 3 | namespace TribeGen\Events; |
| 4 | |
| 5 | use TribeGen\Database\Database; |
| 6 | use TribeGen\Classes\Tribe; |
| 7 | |
| 8 | class EventManager |
| 9 | { |
| 10 | private $tribe; |
| 11 | private $enabledEvents = []; |
| 12 | private $eventClasses = []; |
| 13 | |
| 14 | public function __construct(Tribe $tribe) |
| 15 | { |
| 16 | $this->tribe = $tribe; |
| 17 | $this->loadEnabledEvents(); |
| 18 | $this->registerEventClasses(); |
| 19 | } |
| 20 | |
| 21 | private function loadEnabledEvents() |
| 22 | { |
| 23 | $db = Database::getInstance(); |
| 24 | $pdo = $db->getPDO(); |
| 25 | |
| 26 | $stmt = $pdo->prepare("SELECT event_class FROM events_enabled WHERE save_id = ? AND enabled = 1"); |
| 27 | $stmt->execute([$this->tribe->getSaveId()]); |
| 28 | $rows = $stmt->fetchAll(); |
| 29 | |
| 30 | foreach ($rows as $row) { |
| 31 | $this->enabledEvents[] = $row['event_class']; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private function registerEventClasses() |
| 36 | { |
| 37 | // Register available event classes here |
| 38 | // This list should be expandable |
| 39 | $this->eventClasses = [ |
| 40 | 'hunting' => HuntingEvent::class, |
| 41 | 'exploration' => ExplorationEvent::class, |
| 42 | 'farming' => FarmingEvent::class, |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | public function getAvailableEvents() |
| 47 | { |
| 48 | $available = []; |
| 49 | |
| 50 | foreach ($this->eventClasses as $key => $class) { |
| 51 | if (in_array($class, $this->enabledEvents)) { |
| 52 | $available[$key] = $class; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $available; |
| 57 | } |
| 58 | |
| 59 | public function executeEvent($eventClass) |
| 60 | { |
| 61 | if (!class_exists($eventClass)) { |
| 62 | throw new \Exception("Event class not found: $eventClass"); |
| 63 | } |
| 64 | |
| 65 | $event = new $eventClass($this->tribe); |
| 66 | return $event->execute(); |
| 67 | } |
| 68 | |
| 69 | public function getRandomMonthEvent() |
| 70 | { |
| 71 | // Get random month progression event based on season |
| 72 | // This should be expandable |
| 73 | $seasonEvents = [ |
| 74 | 'spring' => [], |
| 75 | 'summer' => [], |
| 76 | 'autumn' => [], |
| 77 | 'winter' => [] |
| 78 | ]; |
| 79 | |
| 80 | $season = $this->tribe->getCurrentSeason(); |
| 81 | $events = $seasonEvents[$season] ?? []; |
| 82 | |
| 83 | if (empty($events)) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | $eventClass = $events[array_rand($events)]; |
| 88 | return $this->executeEvent($eventClass); |
| 89 | } |
| 90 | } |