1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class PlayfieldController {
26:
27: 28: 29: 30:
31: public $moduleName;
32:
33:
34: public $db;
35:
36:
37: public $commandAlias;
38:
39:
40: public $text;
41:
42:
43: public $util;
44:
45: 46: 47: 48:
49: public function setup() {
50: $this->db->loadSQLFile($this->moduleName, 'playfields');
51:
52: $this->commandAlias->register($this->moduleName, "playfields", "playfield");
53: }
54:
55: 56: 57: 58:
59: public function playfieldListCommand($message, $channel, $sender, $sendto, $args) {
60: $blob = '';
61:
62: $sql = "SELECT * FROM playfields ORDER BY long_name";
63: $data = $this->db->query($sql);
64: forEach ($data as $row) {
65: $blob .= "[<highlight>{$row->id}<end>] {$row->long_name} ({$row->short_name})\n";
66: }
67:
68: $msg = $this->text->makeBlob("Playfields", $blob);
69: $sendto->reply($msg);
70: }
71:
72: 73: 74: 75:
76: public function playfieldShowCommand($message, $channel, $sender, $sendto, $args) {
77: $search = strtolower(trim($args[1]));
78:
79: list($longQuery, $longParams) = $this->util->generateQueryFromParams(explode(' ', $search), 'long_name');
80: list($shortQuery, $shortParams) = $this->util->generateQueryFromParams(explode(' ', $search), 'short_name');
81:
82: $params = array_merge($longParams, $shortParams);
83:
84: $data = $this->db->query("SELECT * FROM playfields WHERE ($longQuery) OR ($shortQuery)", $params);
85:
86: $count = count($data);
87:
88: if ($count > 1) {
89: $blob = "Result of Playfield Search for '$search'\n\n";
90: forEach ($data as $row) {
91: $blob .= "[<highlight>$row->id<end>] $row->long_name\n\n";
92: }
93:
94: $msg = $this->text->makeBlob("Playfields ($count)", $blob);
95: } else if ($count == 1) {
96: $row = $data[0];
97: $msg = "[<highlight>$row->id<end>] $row->long_name";
98: } else {
99: $msg = "There were no matches for your search.";
100: }
101: $sendto->reply($msg);
102: }
103:
104: 105: 106: 107:
108: public function waypoint1Command($message, $channel, $sender, $sendto, $args) {
109:
110: $xCoords = $args[1];
111: $yCoords = $args[2];
112:
113: $playfieldName = $args[4];
114:
115: $playfield = $this->getPlayfieldByName($playfieldName);
116: $sendto->reply($this->processWaypointCommand($xCoords, $yCoords, $playfield->short_name, $playfield->id));
117: }
118:
119: 120: 121: 122:
123: public function waypoint2Command($message, $channel, $sender, $sendto, $args) {
124: $xCoords = $args[1];
125: $yCoords = $args[2];
126: $playfieldId = $args[4];
127:
128: $playfield = $this->getPlayfieldById($playfieldId);
129: if ($playfield === null) {
130: $playfieldName = $playfieldId;
131: } else {
132: $playfieldName = $playfield->short_name;
133: }
134:
135: $sendto->reply($this->processWaypointCommand($xCoords, $yCoords, $playfieldName, $playfieldId));
136: }
137:
138: 139: 140: 141:
142: public function waypoint3Command($message, $channel, $sender, $sendto, $args) {
143: $xCoords = $args[1];
144: $yCoords = $args[3];
145: $playfieldId = $args[5];
146:
147: $playfield = $this->getPlayfieldById($playfieldId);
148: if ($playfield === null) {
149: $playfieldName = $playfieldId;
150: } else {
151: $playfieldName = $playfield->short_name;
152: }
153:
154: $sendto->reply($this->processWaypointCommand($xCoords, $yCoords, $playfieldName, $playfieldId));
155: }
156:
157: 158: 159: 160:
161: public function waypoint4Command($message, $channel, $sender, $sendto, $args) {
162: $xCoords = $args[1];
163: $yCoords = $args[3];
164: $playfieldName = $args[5];
165:
166: $playfield = $this->getPlayfieldByName($playfieldName);
167: if ($playfield === null) {
168: $sendto->reply("Could not find playfield '$playfieldName'.");
169: } else {
170: $playfieldId = $playfield->id;
171: $playfieldName = $playfield->short_name;
172: $sendto->reply($this->processWaypointCommand($xCoords, $yCoords, $playfieldName, $playfieldId));
173: }
174: }
175:
176: private function processWaypointCommand($xCoords, $yCoords, $playfieldName, $playfieldId) {
177: $link = $this->text->makeChatcmd("waypoint: {$xCoords}x{$yCoords} {$playfieldName}", "/waypoint {$xCoords} {$yCoords} {$playfieldId}");
178: $blob = "Click here to use waypoint: $link";
179: return $this->text->makeBlob("waypoint: {$xCoords}x{$yCoords} {$playfieldName}", $blob);
180: }
181:
182: public function getPlayfieldByName($playfieldName) {
183: $sql = "SELECT * FROM playfields WHERE `long_name` LIKE ? OR `short_name` LIKE ? LIMIT 1";
184:
185: return $this->db->queryRow($sql, $playfieldName, $playfieldName);
186: }
187:
188: public function getPlayfieldById($playfieldId) {
189: $sql = "SELECT * FROM playfields WHERE `id` = ?";
190:
191: return $this->db->queryRow($sql, $playfieldId);
192: }
193: }
194: