1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: class TrickleController {
20:
21: 22: 23: 24:
25: public $moduleName;
26:
27:
28: public $text;
29:
30:
31: public $util;
32:
33:
34: public $db;
35:
36: 37: 38: 39:
40: public function setup() {
41: $this->db->loadSQLFile($this->moduleName, 'trickle');
42: }
43:
44: 45: 46: 47: 48: 49:
50: public function trickle1Command($message, $channel, $sender, $sendto, $args) {
51: $abilities = array('agi' => 0, 'int' => 0, 'psy' => 0, 'sta' => 0, 'str' => 0, 'sen' => 0);
52:
53: $array = explode(" ", $message);
54: array_shift($array);
55: for ($i = 0; isset($array[$i]); $i += 2) {
56: $ability = $this->util->getAbility($array[$i]);
57: if ($ability == null) {
58: return false;
59: }
60:
61: $abilities[$ability] += $array[1 + $i];
62: }
63:
64: $msg = $this->processAbilities($abilities);
65: $sendto->reply($msg);
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function trickle2Command($message, $channel, $sender, $sendto, $args) {
75: $abilities = array('agi' => 0, 'int' => 0, 'psy' => 0, 'sta' => 0, 'str' => 0, 'sen' => 0);
76:
77: $array = explode(" ", $message);
78: array_shift($array);
79: for ($i = 0; isset($array[$i]); $i += 2) {
80: $ability = $this->util->getAbility($array[1 + $i]);
81: if ($ability == null) {
82: return false;
83: }
84:
85: $abilities[$ability] += $array[$i];
86: }
87:
88: $msg = $this->processAbilities($abilities);
89: $sendto->reply($msg);
90: }
91:
92: 93: 94: 95:
96: public function trickleSkillCommand($message, $channel, $sender, $sendto, $args) {
97: $search = $args[1];
98:
99: $data = $this->db->query("SELECT * FROM trickle WHERE name LIKE ?", "%" . str_replace(" ", "%", $search) . "%");
100: $count = count($data);
101: if ($count == 0) {
102: $msg = "Could not find any skills for search '$search'";
103: } else if ($count == 1) {
104: $row = $data[0];
105: $msg = $this->getTrickleAmounts($row);
106: } else {
107: $blob = "";
108: forEach ($data as $row) {
109: $blob .= $this->getTrickleAmounts($row) . "\n";
110: }
111: $msg = $this->text->makeBlob("Trickle Info: $search", $blob);
112: }
113:
114: $sendto->reply($msg);
115: }
116:
117: public function getTrickleAmounts($row) {
118: $arr = ['agi', 'int', 'psy', 'sta', 'str', 'sen'];
119: $msg = "<highlight>$row->name<end> ";
120: forEach ($arr as $ability) {
121: $fieldName = "amount" . ucfirst($ability);
122: if ($row->$fieldName > 0) {
123: $abilityName = $this->util->getAbility($ability, true);
124: $value = round(4 / ($row->$fieldName), 2);
125: $msg .= "($abilityName: ${value}) ";
126: }
127: }
128: return $msg;
129: }
130:
131: private function processAbilities($abilities) {
132: $msg = "";
133:
134:
135: $that = $this;
136: $abilitiesHeader = $this->util->mapFilterCombine($abilities, ", ", function($ability, $value) use ($that) {
137: if ($value == 0) {
138: return null;
139: } else {
140: return $that->util->getAbility($ability, true) . " <highlight>" . $value . "<end>";
141: }
142: });
143:
144: $results = $this->getTrickleResults($abilities);
145: $blob .= $this->formatOutput($results, $amount, $abilities);
146: $blob .= "\nBy Tyrence (RK2), inspired by the Bebot command of the same name";
147: return $this->text->makeBlob("Trickle Results: $abilitiesHeader", $blob);
148: }
149:
150: function getTrickleResults($abilities) {
151: $sql = "
152: SELECT
153: groupName,
154: name,
155: amountAgi,
156: amountInt,
157: amountPsy,
158: amountSta,
159: amountStr,
160: amountSen,
161: (amountAgi * {$abilities['agi']}
162: + amountInt * {$abilities['int']}
163: + amountPsy * {$abilities['psy']}
164: + amountSta * {$abilities['sta']}
165: + amountStr * {$abilities['str']}
166: + amountSen * {$abilities['sen']}) AS amount
167: FROM
168: trickle
169: GROUP BY
170: groupName,
171: name,
172: amountAgi,
173: amountInt,
174: amountPsy,
175: amountSta,
176: amountStr,
177: amountSen
178: HAVING
179: amount > 0
180: ORDER BY
181: id";
182:
183: return $this->db->query($sql);
184: }
185:
186: function formatOutput($results, $amount, $abilities) {
187: $msg = "";
188: $groupName = "";
189: forEach($results as $result) {
190:
191: if ($result->groupName != $groupName) {
192: $groupName = $result->groupName;
193: $msg .= "\n<header2>$groupName<end>\n";
194: }
195:
196: $amount = $result->amount / 4;
197: $msg .= "$result->name <highlight>$amount<end>";
198:
199: 200: 201: 202: 203: 204: 205: 206: 207: 208:
209:
210: $msg .= "\n";
211: }
212:
213: return $msg;
214: }
215: }
216: