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: 26:
27: class BankController {
28:
29: 30: 31: 32:
33: public $moduleName;
34:
35:
36: public $db;
37:
38:
39: public $text;
40:
41:
42: public $util;
43:
44:
45: public $settingManager;
46:
47: 48: 49:
50: public function setup() {
51: $this->db->loadSQLFile($this->moduleName, 'bank');
52:
53: $this->settingManager->add($this->moduleName, 'bank_file_location', 'Location of the AO Items Assistant csv dump file', 'edit', 'text', './modules/BANK_MODULE/import.csv');
54: $this->settingManager->add($this->moduleName, 'max_bank_items', 'Number of items shown in search results', 'edit', 'number', '50', '20;50;100');
55: }
56:
57: 58: 59: 60:
61: public function bankBrowseCommand($message, $channel, $sender, $sendto, $args) {
62: $blob = '';
63: $data = $this->db->query("SELECT DISTINCT player FROM bank ORDER BY player ASC");
64: forEach ($data as $row) {
65: $character_link = $this->text->makeChatcmd($row->player, "/tell <myname> bank browse {$row->player}");
66: $blob .= $character_link . "\n";
67: }
68:
69: $msg = $this->text->makeBlob('Bank Characters', $blob);
70: $sendto->reply($msg);
71: }
72:
73: 74: 75: 76:
77: public function bankBrowsePlayerCommand($message, $channel, $sender, $sendto, $args) {
78: $name = ucfirst(strtolower($args[1]));
79:
80: $blob = '';
81: $data = $this->db->query("SELECT DISTINCT container_id, container, player FROM bank WHERE player = ? ORDER BY container ASC", $name);
82: if (count($data) > 0) {
83: forEach ($data as $row) {
84: $container_link = $this->text->makeChatcmd($row->container, "/tell <myname> bank browse {$row->player} {$row->container_id}");
85: $blob .= "{$container_link}\n";
86: }
87:
88: $msg = $this->text->makeBlob("Containers for $name", $blob);
89: } else {
90: $msg = "Could not find bank character <highlight>$name<end>.";
91: }
92: $sendto->reply($msg);
93: }
94:
95: 96: 97: 98:
99: public function bankBrowseContainerCommand($message, $channel, $sender, $sendto, $args) {
100: $name = ucfirst(strtolower($args[1]));
101: $containerId = $args[2];
102: $limit = $this->settingManager->get('max_bank_items');
103:
104: $blob = '';
105: $sql = "SELECT * FROM bank WHERE player = ? AND container_id = ? ORDER BY name ASC, ql ASC LIMIT ?";
106: $data = $this->db->query($sql, $name, $containerId, intval($limit));
107:
108: if (count($data) > 0) {
109: forEach ($data as $row) {
110: $item_link = $this->text->makeItem($row->lowid, $row->highid, $row->ql, $row->name);
111: $blob .= "{$item_link} ({$row->ql})\n";
112: }
113:
114: $msg = $this->text->makeBlob("Contents of $row->container", $blob);
115: } else {
116: $msg = "Could not find container with id <highlight>{$containerId}</highlight> on bank character <highlight>{$name}<end>.";
117: }
118: $sendto->reply($msg);
119: }
120:
121: 122: 123: 124:
125: public function bankSearchCommand($message, $channel, $sender, $sendto, $args) {
126: $search = htmlspecialchars_decode($args[1]);
127: $words = explode(' ', $search);
128: $limit = $this->settingManager->get('max_bank_items');
129:
130: list($where_sql, $params) = $this->util->generateQueryFromParams($words, 'name');
131: array_push($params, intval($limit));
132:
133: $blob = '';
134: $data = $this->db->query("SELECT * FROM bank WHERE {$where_sql} ORDER BY name ASC, ql ASC LIMIT ?", $params);
135:
136: if (count($data) > 0) {
137: forEach ($data as $row) {
138: $item_link = $this->text->makeItem($row->lowid, $row->highid, $row->ql, $row->name);
139: $blob .= "{$item_link} ({$row->ql}) (<highlight>{$row->player}<end>, {$row->container})\n";
140: }
141:
142: $msg = $this->text->makeBlob("Bank Search Results for {$args[1]}", $blob);
143: } else {
144: $msg = "Could not find any search results for <highlight>{$args[1]}<end>.";
145: }
146: $sendto->reply($msg);
147: }
148:
149: 150: 151: 152:
153: public function bankUpdateCommand($message, $channel, $sender, $sendto, $args) {
154: $lines = file($this->settingManager->get('bank_file_location'));
155:
156: if ($lines === false) {
157: $msg = "Could not open file: '" . $this->settingManager->get('bank_file_location') . "'";
158: $sendto->reply($msg);
159: return;
160: }
161:
162:
163: array_shift($lines);
164:
165: $this->db->beginTransaction();
166: $this->db->exec("DELETE FROM bank");
167:
168: forEach ($lines as $line) {
169:
170:
171: list($name, $ql, $player, $container, $location, $lowId, $highId, $containerId) = str_getcsv($line);
172: if ($location != 'Bank' && $location != 'Inventory') {
173: continue;
174: }
175: if ($container == '') {
176: $container = $location;
177: }
178:
179: $sql = "INSERT INTO bank (name, lowid, highid, ql, player, container, container_id, location) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
180: $this->db->exec($sql, $name, $lowId, $highId, $ql, $player, $container, $containerId, $location);
181: }
182: $this->db->commit();
183:
184: $msg = "The bank database has been updated.";
185: $sendto->reply($msg);
186: }
187: }
188: