1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use stdClass;
6: use Exception;
7:
8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class CityWaveController {
24:
25: 26: 27: 28:
29: public $moduleName;
30:
31:
32: public $chatBot;
33:
34:
35: public $commandAlias;
36:
37:
38: public $timerController;
39:
40:
41: public $settingManager;
42:
43:
44: public $setting;
45:
46:
47: public $util;
48:
49: const TIMER_NAME = "City Raid";
50:
51: 52: 53:
54: public function setup() {
55: $this->commandAlias->register($this->moduleName, "citywave start", "startwave");
56: $this->commandAlias->register($this->moduleName, "citywave stop", "stopwave");
57:
58: $this->settingManager->add($this->moduleName, 'city_wave_times', 'Times to display timer alerts', 'edit', 'text', '105s 150s 90s 120s 120s 120s 120s 120s 120s', '105s 150s 90s 120s 120s 120s 120s 120s 120s', '', 'mod', 'city_wave_times.txt');
59: $this->settingManager->registerChangeListener('city_wave_times', array($this, 'changeWaveTimes'));
60: }
61:
62: public function changeWaveTimes($settingName, $oldValue, $newValue, $data) {
63: $alertTimes = explode(' ', $newValue);
64: if (count($alertTimes) != 9) {
65: throw new Exception("Error saving setting: must have 9 spawn times. For more info type !help city_wave_times.");
66: }
67: forEach ($alertTimes as $alertTime) {
68: $time = $this->util->parseTime($alertTime);
69: if ($time == 0) {
70:
71: throw new Exception("Error saving setting: invalid alert time('$alertTime'). For more info type !help city_wave_times.");
72: }
73: }
74: }
75:
76: 77: 78: 79:
80: public function citywaveStartCommand($message, $channel, $sender, $sendto, $args) {
81: $wave = $this->getWave();
82: if ($wave !== null) {
83: $sendto->reply("A raid is already in progress.");
84: } else {
85: $this->startWaveCounter($sender);
86: }
87: }
88:
89: 90: 91: 92:
93: public function citywaveStopCommand($message, $channel, $sender, $sendto, $args) {
94: $wave = $this->getWave();
95: if ($wave === null) {
96: $msg = "There is no raid in progress at this time.";
97: } else {
98: $this->timerController->remove(self::TIMER_NAME);
99: $msg = "Wave counter stopped by $sender.";
100: }
101: $sendto->reply($msg);
102: }
103:
104: 105: 106: 107:
108: public function citywaveCommand($message, $channel, $sender, $sendto, $args) {
109: $wave = $this->getWave();
110: if ($wave === null) {
111: $msg = "There is no raid in progress at this time.";
112: } else if ($wave == 9) {
113: $msg = "Waiting for General.";
114: } else {
115: $msg = "Waiting for wave $wave.";
116: }
117: $sendto->reply($msg);
118: }
119:
120: 121: 122: 123:
124: public function autoStartWaveCounterEvent($eventObj) {
125: if (preg_match("/^Your city in (.+) has been targeted by hostile forces.$/i", $eventObj->message)) {
126: $this->startWaveCounter();
127: }
128: }
129:
130: public function getWave() {
131: $timer = $this->timerController->get(self::TIMER_NAME);
132: if ($timer === null) {
133: return null;
134: } else {
135: return $timer->alerts[0]->wave;
136: }
137: }
138:
139: public function startWaveCounter($name = null) {
140: if ($name === null) {
141: $this->chatBot->sendGuild("Wave counter started.");
142: } else {
143: $this->chatBot->sendGuild("Wave counter started by $name.");
144: }
145: $lastTime = time();
146: $wave = 1;
147: $alerts = array();
148: $alertTimes = explode(' ', $this->setting->city_wave_times);
149: forEach ($alertTimes as $alertTime) {
150: $time = $this->util->parseTime($alertTime);
151: $lastTime += $time;
152:
153: $alert = new stdClass;
154: if ($wave == 9) {
155: $alert->message = "General Incoming.";
156: } else {
157: $alert->message = "Wave $wave incoming.";
158: }
159: $alert->time = $lastTime;
160: $alert->wave = $wave;
161: $alerts []= $alert;
162:
163: $wave++;
164: }
165: $this->timerController->remove(self::TIMER_NAME);
166: $this->timerController->add(self::TIMER_NAME, $this->chatBot->vars['name'], "guild", $alerts, 'timercontroller.timerCallback');
167: }
168: }
169: