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 BuffPerksController {
22: 23: 24: 25:
26: public $moduleName;
27:
28:
29: public $text;
30:
31:
32: public $util;
33:
34:
35: public $db;
36:
37:
38: public $playerManager;
39:
40:
41: public function setup() {
42: $this->db->loadSQLFile($this->moduleName, "perks");
43:
44: $perkInfo = $this->getPerkInfo();
45:
46: $this->db->exec("DELETE FROM perk");
47: $this->db->exec("DELETE FROM perk_level");
48: $this->db->exec("DELETE FROM perk_level_prof");
49: $this->db->exec("DELETE FROM perk_level_buffs");
50:
51: $perkId = 1;
52: $perkLevelId = 1;
53: forEach ($perkInfo as $perk) {
54: $this->db->exec("INSERT INTO perk (id, name) VALUES (?, ?)", $perkId, $perk->name);
55:
56: forEach ($perk->levels as $level) {
57: $this->db->exec("INSERT INTO perk_level (id, perk_id, number, min_level) VALUES (?, ?, ?, ?)", $perkLevelId, $perkId, $level->perkLevel, $level->minLevel);
58:
59: forEach ($level->professions as $profession) {
60: $this->db->exec("INSERT INTO perk_level_prof (perk_level_id, profession) VALUES (?, ?)", $perkLevelId, $profession);
61: }
62:
63: forEach ($level->buffs as $buff => $amount) {
64: $this->db->exec("INSERT INTO perk_level_buffs (perk_level_id, skill, amount) VALUES (?, ?, ?)", $perkLevelId, $buff, $amount);
65: }
66:
67: $perkLevelId++;
68: }
69:
70: $perkId++;
71: }
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81:
82: public function buffPerksCommand($message, $channel, $sender, $sendto, $args) {
83: if (count($args) == 1) {
84: $whois = $this->playerManager->getByName($sender);
85: if (empty($whois)) {
86: $msg = "Could not retrieve whois info for you.";
87: $sendto->reply($msg);
88: return;
89: } else {
90: $profession = $whois->profession;
91: $minLevel = $whois->level;
92: }
93: } else {
94: $first = $this->util->getProfessionName($args[1]);
95: $second = $this->util->getProfessionName($args[2]);
96: if (!empty($first)) {
97: $profession = $first;
98: $minLevel = $args[2];
99: } else if (!empty($second)) {
100: $profession = $second;
101: $minLevel = $args[1];
102: } else {
103: return false;
104: }
105: }
106:
107: $params = array ($profession, $minLevel);
108:
109: if (count($args) == 4) {
110: $tmp = explode(" ", $args[3]);
111: list($skillQuery, $newParams) = $this->util->generateQueryFromParams($tmp, 'plb.skill');
112: $params = array_merge($params, $newParams);
113: $skillQuery = "AND " . $skillQuery;
114: }
115:
116: $sql = "SELECT
117: p.name AS perk_name,
118: MAX(pl.number) AS max_perk_level,
119: SUM(plb.amount) AS buff_amount,
120: plb.skill
121: FROM
122: perk_level_prof plp
123: JOIN perk_level pl ON plp.perk_level_id = pl.id
124: JOIN perk_level_buffs plb ON pl.id = plb.perk_level_id
125: JOIN perk p ON pl.perk_id = p.id
126: WHERE
127: plp.profession = ?
128: AND pl.min_level <= ?
129: $skillQuery
130: GROUP BY
131: p.name,
132: plb.skill
133: ORDER BY
134: p.name";
135:
136: $data = $this->db->query($sql, $params);
137:
138: if (empty($data)) {
139: $msg = "Could not find any perks for level $minLevel $profession.";
140: } else {
141: $currentPerk = '';
142: $buffs = array();
143: $blob = '';
144: $numPerks = 0;
145: forEach ($data as $row) {
146: if ($row->perk_name != $currentPerk) {
147: $blob .= "\n<header2>$row->perk_name {$row->max_perk_level}<end>\n";
148: $currentPerk = $row->perk_name;
149: $numPerks += $row->max_perk_level;
150: }
151:
152: $blob .= "$row->skill <highlight>$row->buff_amount<end>\n";
153: $buffs[$row->skill] += $row->buff_amount;
154: }
155:
156: $msg = $this->text->makeBlob("Buff Perks for $minLevel $profession", $blob);
157: }
158: $sendto->reply($msg);
159: }
160:
161: public function getPerkInfo() {
162: $path = getcwd() . "/modules/" . $this->moduleName . "/perks.csv";
163: $lines = explode("\n", file_get_contents($path));
164: $perks = array();
165: forEach ($lines as $line) {
166: $line = trim($line);
167:
168: if (empty($line)) {
169: continue;
170: }
171:
172: list($name, $perkLevel, $minLevel, $profs, $buffs) = explode("|", $line);
173: $perk = $perks[$name];
174: if (empty($perk)) {
175: $perk = new stdClass;
176: $perks[$name] = $perk;
177: $perk->name = $name;
178: }
179:
180: $level = new stdClass;
181: $perk->levels[$perkLevel] = $level;
182:
183: $level->perkLevel = $perkLevel;
184: $level->minLevel = $minLevel;
185:
186: $level->professions = array();
187: $professions = explode(",", $profs);
188: forEach ($professions as $prof) {
189: $profession = $this->util->getProfessionName(trim($prof));
190: if (empty($profession)) {
191: echo "Error parsing profession: '$prof'\n";
192: } else {
193: $level->professions []= $profession;
194: }
195: }
196:
197: $level->buffs = array();
198: $buffs = explode(",", $buffs);
199: forEach ($buffs as $buff) {
200: $buff = trim($buff);
201: $pos = strrpos($buff, " ");
202:
203: $skill = substr($buff, 0, $pos + 1);
204: $amount = substr($buff, $pos + 1);
205:
206: $level->buffs[$skill] = $amount;
207: }
208: }
209:
210: return $perks;
211: }
212: }