BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / JealousItemEvent.php

JealousItemEvent.php

JealousItemEvent.php 2.8 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/MonthEndEvent.php';
4
5class JealousItemEvent extends MonthEndEvent
6{
7 public function getMinAge()
8 {
9 return 10;
10 }
11
12 public function getMinParticipants()
13 {
14 return 2;
15 }
16
17 public function getMaxParticipants()
18 {
19 return 2;
20 }
21
22 public function getCost()
23 {
24 return 10;
25 }
26
27 public function isAvailable()
28 {
29 $eligible = array_filter($this->tribe->getKobolds(), function ($kobold) {
30 return $kobold->getAge() >= $this->getMinAge();
31 });
32
33 if (count($eligible) < 2) {
34 return false;
35 }
36
37 foreach ($eligible as $kobold) {
38 $items = $kobold->getItems() ?? [];
39 if (!empty($items)) {
40 return true;
41 }
42 }
43
44 return false;
45 }
46
47 private function ensureLikes($kobold)
48 {
49 if (!isset($kobold->dataStore)) {
50 $kobold->dataStore = new stdClass();
51 }
52 if (!isset($kobold->dataStore->likes) || !is_array($kobold->dataStore->likes)) {
53 $kobold->dataStore->likes = [];
54 }
55 }
56
57 public function execute()
58 {
59 $kobolds = array_values($this->tribe->getKobolds());
60 if (count($kobolds) < 2) {
61 return;
62 }
63
64 $targets = array_values(array_filter($kobolds, function ($kobold) {
65 $items = $kobold->getItems() ?? [];
66 return !empty($items);
67 }));
68
69 if (empty($targets)) {
70 return;
71 }
72
73 $target = $targets[array_rand($targets)];
74 $jealousOptions = array_values(array_filter($kobolds, fn($k) => $k->getId() !== $target->getId()));
75 if (empty($jealousOptions)) {
76 return;
77 }
78
79 $jealous = $jealousOptions[array_rand($jealousOptions)];
80 $items = $target->getItems() ?? [];
81 $item = $items[array_rand($items)];
82 $itemName = is_array($item) ? ($item['name'] ?? $item['id'] ?? 'an item') : (is_object($item) && method_exists($item, 'getName') ? $item->getName() : 'an item');
83
84 $this->ensureLikes($jealous);
85 $current = $jealous->dataStore->likes[$target->getId()] ?? 0;
86 $jealous->dataStore->likes[$target->getId()] = $current - 2;
87
88 $variations = [
89 "{$jealous->getName()} grew jealous of {$target->getName()}'s {$itemName}. The envy lingered, souring {$jealous->his()} opinion.",
90 "Seeing {$target->getName()} with {$itemName} stirred jealousy in {$jealous->getName()}. {$jealous->his()} feelings toward {$target->getName()} cooled.",
91 "{$jealous->getName()} fixated on {$target->getName()}'s {$itemName} and felt a sharp pang of envy, weakening the bond between them."
92 ];
93
94 $this->story = $variations[array_rand($variations)];
95 $jealous->save();
96 }
97}