1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Exception;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class PremadeImplantController {
22:
23: 24: 25: 26:
27: public $moduleName;
28:
29:
30: public $db;
31:
32:
33: public $text;
34:
35:
36: public $util;
37:
38: private $slots = array('head', 'eye', 'ear', 'rarm', 'chest', 'larm', 'rwrist', 'waist', 'lwrist', 'rhand', 'legs', 'lhand', 'feet');
39:
40: 41: 42:
43: public function setup() {
44: $this->db->loadSQLFile($this->moduleName, "premade_implant");
45: }
46:
47: 48: 49: 50:
51: public function premadeCommand($message, $channel, $sender, $sendto, $args) {
52: $searchTerms = strtolower($args[1]);
53: $results = null;
54:
55: $profession = $this->util->getProfessionName($searchTerms);
56: if ($profession != '') {
57: $searchTerms = $profession;
58: $results = $this->searchByProfession($profession);
59: } else if (in_array($searchTerms, $this->slots)) {
60: $results = $this->searchBySlot($searchTerms);
61: } else {
62: $results = $this->searchByModifier($searchTerms);
63: }
64:
65: if ($results != null) {
66: $blob = $this->formatResults($results);
67: $blob .= "\n\nWritten by Tyrence (RK2)";
68: $msg = $this->text->makeBlob("Implant Search Results for '$searchTerms'", $blob);
69: } else {
70: $msg = "No results found.";
71: }
72:
73: $sendto->reply($msg);
74: }
75:
76: public function searchByProfession($profession) {
77: $sql = "SELECT
78: i.Name AS slot,
79: p2.Name AS profession,
80: a.Name AS ability,
81: CASE WHEN c1.ClusterID = 0 THEN 'N/A' ELSE c1.LongName END AS shiny,
82: CASE WHEN c2.ClusterID = 0 THEN 'N/A' ELSE c2.LongName END AS bright,
83: CASE WHEN c3.ClusterID = 0 THEN 'N/A' ELSE c3.LongName END AS faded
84: FROM premade_implant p
85: JOIN ImplantType i ON p.ImplantTypeID = i.ImplantTypeID
86: JOIN Profession p2 ON p.ProfessionID = p2.ID
87: JOIN Ability a ON p.AbilityID = a.AbilityID
88: JOIN Cluster c1 ON p.ShinyClusterID = c1.ClusterID
89: JOIN Cluster c2 ON p.BrightClusterID = c2.ClusterID
90: JOIN Cluster c3 ON p.FadedClusterID = c3.ClusterID
91: WHERE p2.Name = ?
92: ORDER BY slot";
93: return $this->db->query($sql, $profession);
94: }
95:
96: public function searchBySlot($slot) {
97: $sql = "SELECT
98: i.Name AS slot,
99: p2.Name AS profession,
100: a.Name AS ability,
101: CASE WHEN c1.ClusterID = 0 THEN 'N/A' ELSE c1.LongName END AS shiny,
102: CASE WHEN c2.ClusterID = 0 THEN 'N/A' ELSE c2.LongName END AS bright,
103: CASE WHEN c3.ClusterID = 0 THEN 'N/A' ELSE c3.LongName END AS faded
104: FROM premade_implant p
105: JOIN ImplantType i ON p.ImplantTypeID = i.ImplantTypeID
106: JOIN Profession p2 ON p.ProfessionID = p2.ID
107: JOIN Ability a ON p.AbilityID = a.AbilityID
108: JOIN Cluster c1 ON p.ShinyClusterID = c1.ClusterID
109: JOIN Cluster c2 ON p.BrightClusterID = c2.ClusterID
110: JOIN Cluster c3 ON p.FadedClusterID = c3.ClusterID
111: WHERE i.ShortName = ?
112: ORDER BY shiny, bright, faded";
113: return $this->db->query($sql, $slot);
114: }
115:
116: public function searchByModifier($modifier) {
117: list($shinyQuery, $shinyParams) = $this->util->generateQueryFromParams(explode(' ', $modifier), 'c1.LongName');
118: list($brightQuery, $brightParams) = $this->util->generateQueryFromParams(explode(' ', $modifier), 'c2.LongName');
119: list($fadedQuery, $fadedParams) = $this->util->generateQueryFromParams(explode(' ', $modifier), 'c3.LongName');
120:
121: $params = array_merge($shinyParams, $brightParams, $fadedParams);
122:
123: $sql = "SELECT
124: i.Name AS slot,
125: p2.Name AS profession,
126: a.Name AS ability,
127: CASE WHEN c1.ClusterID = 0 THEN 'N/A' ELSE c1.LongName END AS shiny,
128: CASE WHEN c2.ClusterID = 0 THEN 'N/A' ELSE c2.LongName END AS bright,
129: CASE WHEN c3.ClusterID = 0 THEN 'N/A' ELSE c3.LongName END AS faded
130: FROM premade_implant p
131: JOIN ImplantType i ON p.ImplantTypeID = i.ImplantTypeID
132: JOIN Profession p2 ON p.ProfessionID = p2.ID
133: JOIN Ability a ON p.AbilityID = a.AbilityID
134: JOIN Cluster c1 ON p.ShinyClusterID = c1.ClusterID
135: JOIN Cluster c2 ON p.BrightClusterID = c2.ClusterID
136: JOIN Cluster c3 ON p.FadedClusterID = c3.ClusterID
137: WHERE ($shinyQuery) OR ($brightQuery) OR ($fadedQuery)";
138:
139: return $this->db->query($sql, $params);
140: }
141:
142: public function formatResults($implants) {
143: $count = 0;
144: forEach ($implants as $implant) {
145: $blob .= $this->getFormattedLine($implant);
146: $count++;
147: }
148:
149: return $blob;
150: }
151:
152: public function getFormattedLine($implant) {
153: return "<header2>$implant->profession<end> $implant->slot <highlight>$implant->ability<end> $implant->shiny, $implant->bright, $implant->faded\n";
154: }
155: }
156: