-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_query.php
More file actions
205 lines (175 loc) · 5.51 KB
/
process_query.php
File metadata and controls
205 lines (175 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
function CreatePhotoResultWithCard($card, $q, $foil) {
$photoResult = new PhotoResult();
$f = "";
if($foil) $f ="f";
$photoResult->photo_url = "http://www.shadowera.com/cards/" . $card["id"] . $f . ".jpg";
$photoResult->thumb_url = "http://www.shadowera.com/cards/" . $card["id"] . $f . ".jpg";
$photoResult->title = $card["name"];
$photoResult->caption = $q;
return $photoResult;
}
function GetInlineAnswer($q, $db)
{
$response = new InlineQueryAnswer();
$queryWords = explode(" ", $q); // array of query words (ex.: random ally)
// check if is random query
if (in_array("random", $queryWords)) {
$sql = "SELECT * FROM `cards` ";
// type of randomness
if (count($queryWords) == 1 || in_array("card", $queryWords)) {
// random card
}
elseif (in_array("hero", $queryWords)) {
$sql .= "WHERE type = 'Hero' ";
}
elseif (in_array("ally", $queryWords)) {
$sql .= "WHERE type = 'Ally' ";
}
elseif (in_array("item", $queryWords)) {
$sql .= "WHERE type = 'Item' ";
}
elseif (in_array("ability", $queryWords)) {
$sql .= "WHERE type = 'Ability' ";
}
elseif (in_array("location", $queryWords)) {
$sql .= "WHERE type = 'Location' ";
}
elseif (in_array("weapon", $queryWords)) {
$sql .= "WHERE subtype = 'Weapon' ";
}
elseif (in_array("armor", $queryWords)) {
$sql .= "WHERE subtype = 'Armor' ";
}
$sql .= "ORDER BY RAND() LIMIT 1";
$card = $db->GetSingleWithSQL($sql);
if ($card) {
$response->AddResult(CreatePhotoResultWithCard($card, $q, false));
}
// break here, return single random card
$response->cache_time = 0;
return $response;
}
$cardTypes = array(
"Hero",
"Ally",
"Item",
"Ability",
"Location"
);
$cardSubtypes = array(
"Wild",
"Outlaw",
"Twilight",
"Homunculus",
"Templar",
"Ravager",
"Wulven",
"Aldmor",
"Undead",
"Attachment",
"Support",
"Artifact",
"Yari",
"Trap",
"Armor",
"Weapon",
"Balor",
"Scheuth",
"Thriss",
"Tinnal",
"Ogmaga"
);
$cardClasses = array(
"Warrior",
"Hunter",
"Mage",
"Priest",
"Rogue",
"Wulven",
"Elemental"
);
// prepare input array words (add capitalization)
$capWords = array_map(function ($w) {
return ucfirst($w);
}, $queryWords);
// foiled version
$foilIndex = array_search("Foil", $capWords);
$foiled = false;
if($foilIndex > 0) {
array_splice($capWords, $foilIndex, 1);
$foiled = true;
}
$filtersFound = false;
$sql = "SELECT cards.* FROM cards\n"
. "WHERE ";
$classFiletrs = array_intersect($cardClasses, $capWords);
if (count($classFiletrs)) {
$sql = "SELECT cards.* FROM cards_classes\n"
. "LEFT JOIN cards ON cards_classes.card_id = cards.id\n"
. "LEFT JOIN classes ON cards_classes.class_id = classes.id \n"
. "WHERE ";
$filtersFound = true;
$quoted = array_map(function ($t) {
return "'$t'";
}, $classFiletrs);
$sql .= "classes.name = (" . implode(" OR ", $quoted) . ")";
//remove classes keywords
$capWords = array_diff($capWords, $classFiletrs);
}
$typeFiletrs = array_intersect($cardTypes, $capWords);
if (count($typeFiletrs)) {
if ($filtersFound) $sql .= " AND ";
$filtersFound = true;
$quoted = array_map(function ($t) {
return "'$t'";
}, $typeFiletrs);
$sql .= "cards.type = (" . implode(" OR ", $quoted) . ")";
//remove types keywords
$capWords = array_diff($capWords, $typeFiletrs);
}
$subtypeFilters = array_intersect($cardSubtypes, $capWords);
if (count($subtypeFilters)) {
if ($filtersFound) $sql .= " AND ";
$filtersFound = true;
$quoted = array_map(function ($t) {
return "'$t'";
}, $subtypeFilters);
$sql .= "cards.subtype = (" . implode(" OR ", $quoted) . ")";
// remove subtypes keywords
$capWords = array_diff($capWords, $subtypeFilters);
}
$sql .= " LIMIT 50";
if ($filtersFound) {
// get cards by filters if at least one filter was found in query words
$cards = $db->GetManyWithSQL($sql);
if ($cards) {
foreach ($cards as $card) {
$response->AddResult(CreatePhotoResultWithCard($card, $q, $foiled));
}
// stop here, return filtered result
return $response;
}
}
// get by nickname
$q = implode(" ", $capWords);
$sql = "SELECT * FROM `cards` WHERE nick = '$q'";
$card = $db->GetSingleWithSQL($sql);
if ($card)
{
$response->AddResult(CreatePhotoResultWithCard($card, $q, $foiled));
}
else
{
// search by name
$sql = "SELECT * FROM `cards` WHERE name LIKE '%$q%' LIMIT 50";
$cards = $db->GetManyWithSQL($sql);
if ($cards) {
foreach ($cards as $card) {
$response->AddResult(CreatePhotoResultWithCard($card, $q, $foiled));
}
}
}
// return filtered by name or single by nickname here
return $response;
}