1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use stdClass;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class WhompahController {
22:
23: 24: 25: 26:
27: public $moduleName;
28:
29:
30: public $db;
31:
32:
33: public $text;
34:
35:
36: public $commandAlias;
37:
38: 39: 40: 41:
42: public function setup() {
43: $this->db->loadSQLFile($this->moduleName, 'whompah_cities');
44:
45: $this->commandAlias->register($this->moduleName, 'whompah', 'whompahs');
46: $this->commandAlias->register($this->moduleName, 'whompah', 'whompa');
47: $this->commandAlias->register($this->moduleName, 'whompah', 'whompas');
48: }
49:
50: 51: 52: 53:
54: public function whompahListCommand($message, $channel, $sender, $sendto, $args) {
55: $sql = "SELECT * FROM `whompah_cities` ORDER BY city_name ASC";
56: $data = $this->db->query($sql);
57:
58: $blob = '';
59: forEach ($data as $row) {
60: $cityLink = $this->text->makeChatcmd($row->short_name, "/tell <myname> whompah {$row->short_name}");
61: $blob .= "{$row->city_name} ({$cityLink})\n";
62: }
63: $blob .= "\nWritten By Tyrence (RK2)\nDatabase from a Bebot module written by POD13";
64:
65: $msg = $this->text->makeBlob('Whompah Cities', $blob);
66:
67: $sendto->reply($msg);
68: }
69:
70: 71: 72: 73:
74: public function whompahTravelCommand($message, $channel, $sender, $sendto, $args) {
75: $startCity = $this->findCity($args[1]);
76: $endCity = $this->findCity($args[2]);
77:
78: if ($startCity === null) {
79: $msg = "Error! Could not find city '$args[1]'!";
80: $sendto->reply($msg);
81: return;
82: }
83: if ($endCity === null) {
84: $msg = "Error! Could not find city '$args[2]'!";
85: $sendto->reply($msg);
86: return;
87: }
88:
89: $whompahs = $this->buildWhompahNetwork();
90:
91: $whompah = new stdClass;
92: $whompah->id = $endCity->id;
93: $whompah->city_name = $whompahs[$endCity->id]->city_name;
94: $whompah->previous = null;
95: $whompah->visited = true;
96: $obj = $this->findWhompahPath($q = array($whompah), $whompahs, $startCity->id);
97:
98: if ($obj === false) {
99: $msg = "There was an error while trying to find the whompah path.";
100: } else {
101: while ($obj->previous !== null) {
102: $msg .= "$obj->city_name -> ";
103: $obj = $obj->previous;
104: }
105: $msg .= "$obj->city_name";
106: }
107:
108: $sendto->reply($msg);
109: }
110:
111: 112: 113: 114:
115: public function whompahDestinationsCommand($message, $channel, $sender, $sendto, $args) {
116: $city = $this->findCity($args[1]);
117:
118: if ($city === null) {
119: $msg = "Error! Could not find city '$args[1]'!";
120: $sendto->reply($msg);
121: return;
122: }
123:
124: $sql = "SELECT w2.* FROM whompah_cities_rel w1 JOIN whompah_cities w2 ON w1.city2_id = w2.id WHERE w1.city1_id = ?";
125: $data = $this->db->query($sql, $city->id);
126:
127: $msg = "From {$city->city_name} you can get to: " .
128: implode(", ", array_map(function($row) { return "<highlight>{$row->city_name}<end> ({$row->short_name})"; }, $data));
129:
130: $sendto->reply($msg);
131: }
132:
133: public function findWhompahPath($queue, $whompahs, $endCity) {
134: $currentWhompah = array_shift($queue);
135:
136: if ($currentWhompah == false) {
137: return false;
138: }
139:
140: if ($currentWhompah->id == $endCity) {
141: return $currentWhompah;
142: }
143:
144: forEach ($whompahs[$currentWhompah->id]->connections as $city2Id) {
145: if ($whompahs[$city2Id]->visited !== true) {
146: $whompahs[$city2Id]->visited = true;
147: $nextWhompah = new stdClass;
148: $nextWhompah->id = $city2Id;
149: $nextWhompah->city_name = $whompahs[$city2Id]->city_name;
150: $nextWhompah->previous = $currentWhompah;
151: $queue []= $nextWhompah;
152: }
153: }
154:
155: return $this->findWhompahPath($queue, $whompahs, $endCity);
156: }
157:
158: public function findCity($search) {
159: $sql = "SELECT * FROM whompah_cities WHERE city_name LIKE ? OR short_name LIKE ?";
160: return $this->db->queryRow($sql, $search, $search);
161: }
162:
163: public function buildWhompahNetwork() {
164: $whompahs = array();
165:
166: $sql = "SELECT * FROM `whompah_cities`";
167: $data = $this->db->query($sql);
168: forEach ($data as $row) {
169: $whompahs[$row->id] = $row;
170: $whompahs[$row->id]->connections = array();
171: $whompahs[$row->id]->visited = false;
172: }
173:
174: $sql = "SELECT city1_id, city2_id FROM whompah_cities_rel";
175: $data = $this->db->query($sql);
176: forEach ($data as $row) {
177: $whompahs[$row->city1_id]->connections[] = $row->city2_id;
178: }
179:
180: return $whompahs;
181: }
182: }
183: