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