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: 22:
23: class RecipeController {
24:
25: 26: 27: 28:
29: public $moduleName;
30:
31:
32: public $db;
33:
34:
35: public $util;
36:
37:
38: public $text;
39:
40:
41: public $itemsController;
42:
43: private $path;
44:
45: 46: 47: 48: 49: 50: 51:
52: public function connectEvent() {
53: $this->db->loadSQLFile($this->moduleName, "recipes", true);
54:
55: $this->path = getcwd() . "/modules/" . $this->moduleName . "/recipes/";
56: if ($handle = opendir($this->path)) {
57: while (false !== ($fileName = readdir($handle))) {
58:
59: if (preg_match("/(\d+)\.txt/", $fileName, $args)) {
60: $id = $args[1];
61: $lines = file($this->path . $fileName);
62: $name = substr(trim(array_shift($lines)), 6);
63: $author = substr(trim(array_shift($lines)), 8);
64: $data = implode("", $lines);
65: $this->db->exec("INSERT INTO recipes (id, name, author, recipe) VALUES (?, ?, ?, ?)", $id, $name, $author, $data);
66: } else if (preg_match("/(\d+)\.json/", $fileName, $args)) {
67: $recipe = json_decode(file_get_contents($this->path . $fileName));
68: if ($recipe === null) {
69: throw new Exception("Could not read '$fileName', invalid JSON");
70: }
71:
72: $id = $args[1];
73: $name = $recipe->name;
74: $author = $recipe->author;
75: $items = array();
76: forEach($recipe->items as $item) {
77: $dbItem = $this->itemsController->findById($item->item_id);
78: if ($dbItem === null) {
79: throw Exception("Could not find item '{$item->item_id}'");
80: }
81: $items[$item->alias] = $dbItem;
82: $items[$item->alias]->ql = $item->ql;
83: }
84:
85: $data = "#C16------------------------------\n";
86: $data .= "#C12Ingredients #C20\n";
87: $data .= "#C16------------------------------\n\n";
88: $ingredients = $items;
89: forEach($recipe->steps as $step) {
90: unset($ingredients[$step->result]);
91: }
92: forEach($ingredients as $ingredient) {
93: $data .= $this->text->makeImage($ingredient->icon) . "\n";
94: $data .= $this->text->makeItem($ingredient->lowid, $ingredient->highid, $ingredient->ql, $ingredient->name) . "\n\n\n";
95: }
96:
97: $data .= "#C16------------------------------\n";
98: $data .= "#C12Recipe #C16\n";
99: $data .= "#C16------------------------------#C20\n\n";
100: forEach($recipe->steps as $step) {
101: $source = $items[$step->source];
102: $target = $items[$step->target];
103: $result = $items[$step->result];
104: $data .= $source->name . "\n";
105: $data .= "#C15+#C20" . "\n";
106: $data .= $target->name . "\n";
107: $data .= "#C15=#C20" . "\n";
108: $data .= $this->text->makeImage($result->icon) . "\n";
109: $data .= $this->text->makeItem($result->lowid, $result->highid, $result->ql, $result->name) . "\n";
110: if ($step->skills) {
111: $data .= "#C16Skills: | {$step->skills} |#C20\n";
112: }
113: $data .= "\n\n";
114: }
115:
116: $this->db->exec("INSERT INTO recipes (id, name, author, recipe) VALUES (?, ?, ?, ?)", $id, $name, $author, $data);
117: }
118: }
119: closedir($handle);
120: } else {
121: throw new Exception("Could not open '$this->path' for loading recipes");
122: }
123: }
124:
125: 126: 127: 128:
129: public function recipeShowCommand($message, $channel, $sender, $sendto, $args) {
130: $id = $args[1];
131:
132: $row = $this->db->queryRow("SELECT * FROM recipes WHERE id = ?", $id);
133:
134: if ($row === null) {
135: $msg = "Could not find recipe with id <highlight>$id<end>.";
136: } else {
137: $msg = $this->createRecipeBlob($row);
138: }
139: $sendto->reply($msg);
140: }
141:
142: 143: 144: 145:
146: public function recipeSearchCommand($message, $channel, $sender, $sendto, $args) {
147: if (preg_match('|<a href="itemref://(\d+)/(\d+)/(\d+)">([^<]+)</a>|', $args[1], $matches)) {
148: $lowId = $matches[1];
149: $search = $matches[4];
150:
151: $data = $this->db->query("SELECT * FROM recipes WHERE recipe LIKE ? OR recipe LIKE ? ORDER BY name ASC", $search, "%" . $lowId . "%");
152: } else {
153: $search = $args[1];
154:
155: list($query, $queryParams) = $this->util->generateQueryFromParams(explode(" ", $search), "recipe");
156: $data = $this->db->query("SELECT * FROM recipes WHERE $query ORDER BY name ASC", $queryParams);
157: }
158:
159: $count = count($data);
160:
161: if ($count == 0) {
162: $msg = "Could not find any recipes matching your search criteria.";
163: } else if ($count == 1) {
164: $msg = $this->createRecipeBlob($data[0]);
165: } else {
166: $blob = '';
167: forEach ($data as $row) {
168: $blob .= $this->text->makeChatcmd($row->name, "/tell <myname> recipe $row->id") . "\n";
169: }
170:
171: $msg = $this->text->makeBlob("Recipes matching '$search' ($count)", $blob);
172: }
173:
174: $sendto->reply($msg);
175: }
176:
177: public function formatRecipeText($input) {
178: $input = str_replace("\\n", "\n", $input);
179: $input = preg_replace_callback('/#L "([^"]+)" "([0-9]+)"/', array($this, 'replaceItem'), $input);
180: $input = preg_replace('/#L "([^"]+)" "([^"]+)"/', "<a href='chatcmd://\\2'>\\1</a>", $input);
181:
182:
183:
184:
185: $input = str_replace("{myname}", "<myname>", $input);
186:
187: $input = str_replace("#C09","</font><font color=#FFFFFF>", $input);
188: $input = str_replace("#C12","</font><font color=#FF0000>", $input);
189: $input = str_replace("#C13","</font><font color=#FFFFFF>", $input);
190: $input = str_replace("#C14","</font><font color=#FFFFFF>", $input);
191: $input = str_replace("#C15","</font><font color=#FFFFFF>", $input);
192: $input = str_replace("#C16","</font><font color=#FFFF00>", $input);
193: $input = str_replace("#C18","</font><font color=#AAFF00>", $input);
194: $input = str_replace("#C20","</font><font color=#009B00>", $input);
195:
196: return $input;
197: }
198:
199: public function createRecipeBlob($row) {
200: $recipe_name = $row->name;
201: $author = empty($row->author) ? "Unknown" : $row->author;
202:
203: $recipeText = "Recipe Id: <highlight>$row->id<end>\n";
204: $recipeText .= "Author: <highlight>$author<end>\n\n";
205: $recipeText .= $this->formatRecipeText($row->recipe);
206:
207: return $this->text->makeBlob("Recipe for $recipe_name", $recipeText);
208: }
209:
210: private function replaceItem($arr) {
211: $id = $arr[2];
212: $row = $this->itemsController->findById($id);
213: if ($row !== null) {
214: $output = $this->text->makeItem($row->lowid, $row->highid, $row->highql, $row->name);
215: } else {
216: $output = "#L \"{$arr[1]}\" \"/tell <myname> itemid {$arr[2]}\"";
217: }
218: return $output;
219: }
220: }
221:
222: