corrade-http-templates – Diff between revs 2 and 12
?pathlinks?
Rev 2 | Rev 12 | |||
---|---|---|---|---|
1 | <?php |
1 | <?php |
|
2 | |
2 | |
|
3 | ########################################################################### |
3 | ########################################################################### |
|
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
|
5 | ########################################################################### |
5 | ########################################################################### |
|
6 | ## This is a script that uses Corrade to get the avatar positions and ## |
6 | ## This is a script that uses Corrade to get the avatar positions and ## |
|
7 | ## then aggregates that data to a JSON object that will be read by AJAX. ## |
7 | ## then aggregates that data to a JSON object that will be read by AJAX. ## |
|
8 | ## ## |
8 | ## ## |
|
9 | ## The output JSON object that will be read via AJAX is of the form: ## |
9 | ## The output JSON object that will be read via AJAX is of the form: ## |
|
10 | ## { ## |
10 | ## { ## |
|
11 | ## header: ["x", "y", "z"], ## |
11 | ## header: ["x", "y", "z"], ## |
|
12 | ## positions: [[x1, y1, z1], [x2, y2, z2], ...], ## |
12 | ## positions: [[x1, y1, z1], [x2, y2, z2], ...], ## |
|
13 | ## names: ["First Resident", "Second Resident", ...], ## |
13 | ## names: ["First Resident", "Second Resident", ...], ## |
|
14 | ## colors: [[0.5, 1, 0.75], [0.3, 0.55, 0.34], ...] ## |
14 | ## colors: [[0.5, 1, 0.75], [0.3, 0.55, 0.34], ...] ## |
|
15 | ## } ## |
15 | ## } ## |
|
16 | ########################################################################### |
16 | ########################################################################### |
|
17 | |
17 | |
|
18 | ########################################################################### |
18 | ########################################################################### |
|
19 | ## CONFIGURATION ## |
19 | ## CONFIGURATION ## |
|
20 | ########################################################################### |
20 | ########################################################################### |
|
21 | |
21 | |
|
22 | // The configuration file for this script containing the settings. |
22 | # The configuration file for this script containing the settings. |
|
- | 23 | require_once("config.php"); |
||
23 | include_once("config.php"); |
24 | require_once("functions.php") |
|
24 | |
25 | |
|
25 | ########################################################################### |
26 | ########################################################################### |
|
26 | ## INTERNALS ## |
27 | ## INTERNALS ## |
|
27 | ########################################################################### |
28 | ########################################################################### |
|
28 | |
- | ||
29 | ########################################################################### |
- | ||
30 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
- | ||
31 | ########################################################################### |
- | ||
32 | function wasKeyValueGet($key, $data) { |
- | ||
33 | return array_reduce( |
- | ||
34 | explode( |
- | ||
35 | "&", |
- | ||
36 | $data |
- | ||
37 | ), |
- | ||
38 | function($o, $p) { |
- | ||
39 | $x = explode("=", $p); |
- | ||
40 | return array_shift($x) != $o ? $o : array_shift($x); |
- | ||
41 | }, |
- | ||
42 | $key |
- | ||
43 | ); |
- | ||
44 | } |
- | ||
45 | |
- | ||
46 | /////////////////////////////////////////////////////////////////////////// |
- | ||
47 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
- | ||
48 | /////////////////////////////////////////////////////////////////////////// |
- | ||
49 | function wasCSVToArray($csv) { |
- | ||
50 | $l = array(); |
- | ||
51 | $s = array(); |
- | ||
52 | $m = ""; |
- | ||
53 | for ($i = 0; $i < strlen($csv); ++$i) { |
- | ||
54 | switch ($csv{$i}) { |
- | ||
55 | case ',': |
- | ||
56 | if (sizeof($s) == 0 || !current($s) == '"') { |
- | ||
57 | array_push($l, $m); |
- | ||
58 | $m = ""; |
- | ||
59 | break; |
- | ||
60 | } |
- | ||
61 | $m .= $csv{$i}; |
- | ||
62 | continue; |
- | ||
63 | case '"': |
- | ||
64 | if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { |
- | ||
65 | $m .= $csv{$i}; |
- | ||
66 | ++$i; |
- | ||
67 | break; |
- | ||
68 | } |
- | ||
69 | if (sizeof($s) == 0|| !current($s) == $csv[$i]) { |
- | ||
70 | array_push($s, $csv{$i}); |
- | ||
71 | continue; |
- | ||
72 | } |
- | ||
73 | array_pop($s); |
- | ||
74 | break; |
- | ||
75 | default: |
- | ||
76 | $m .= $csv{$i}; |
- | ||
77 | break; |
- | ||
78 | } |
- | ||
79 | } |
- | ||
80 | array_push($l, $m); |
- | ||
81 | return $l; |
- | ||
82 | } |
- | ||
83 | |
- | ||
84 | /////////////////////////////////////////////////////////////////////////// |
- | ||
85 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
- | ||
86 | /////////////////////////////////////////////////////////////////////////// |
- | ||
87 | function wasArrayStride($a, $s) { |
- | ||
88 | return array_filter($a, |
- | ||
89 | function($e, $i) use($s) { |
- | ||
90 | return $i % $s == 0; |
- | ||
91 | }, |
- | ||
92 | ARRAY_FILTER_USE_BOTH |
- | ||
93 | ); |
- | ||
94 | } |
- | ||
95 | |
- | ||
96 | /////////////////////////////////////////////////////////////////////////// |
- | ||
97 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
- | ||
98 | /////////////////////////////////////////////////////////////////////////// |
- | ||
99 | function wasColorHash($a) { |
- | ||
100 | $hash = sha1($a); |
- | ||
101 | $size = strlen($hash); |
- | ||
102 | |
- | ||
103 | return substr($hash, 0, 2). |
- | ||
104 | substr($hash, $size/2, 2). |
- | ||
105 | substr($hash, $size-2, 2); |
- | ||
106 | } |
- | ||
107 | |
- | ||
108 | /////////////////////////////////////////////////////////////////////////// |
- | ||
109 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
- | ||
110 | /////////////////////////////////////////////////////////////////////////// |
- | ||
111 | function mapValueToRange($value, $xMin, $xMax, $yMin, $yMax) { |
- | ||
112 | return $yMin + ( |
- | ||
113 | ( |
- | ||
114 | $yMax - $yMin |
- | ||
115 | ) |
- | ||
116 | * |
- | ||
117 | ( |
- | ||
118 | $value - $xMin |
- | ||
119 | ) |
- | ||
120 | / |
- | ||
121 | ( |
- | ||
122 | $xMax - $xMin |
- | ||
123 | ) |
- | ||
124 | ); |
- | ||
125 | } |
- | ||
126 | |
- | ||
127 | function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') { |
- | ||
128 | $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); |
- | ||
129 | $rgbArray = array(); |
- | ||
130 | switch(strlen($hexStr)) { |
- | ||
131 | case 6: |
- | ||
132 | $colorVal = hexdec($hexStr); |
- | ||
133 | $rgbArray['red'] = 0xFF & ($colorVal >> 0x10); |
- | ||
134 | $rgbArray['green'] = 0xFF & ($colorVal >> 0x8); |
- | ||
135 | $rgbArray['blue'] = 0xFF & $colorVal; |
- | ||
136 | break; |
- | ||
137 | case 3: |
- | ||
138 | $rgbArray['red'] = hexdec( |
- | ||
139 | str_repeat(substr($hexStr, 0, 1), 2)); |
- | ||
140 | $rgbArray['green'] = hexdec( |
- | ||
141 | str_repeat(substr($hexStr, 1, 1), 2)); |
- | ||
142 | $rgbArray['blue'] = hexdec( |
- | ||
143 | str_repeat(substr($hexStr, 2, 1), 2)); |
- | ||
144 | break; |
- | ||
145 | default: |
- | ||
146 | return false; |
- | ||
147 | } |
- | ||
148 | return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; |
- | ||
149 | } |
- | ||
150 | |
29 | |
|
151 | #### |
30 | #### |
|
152 | # I. Get the avatar positions. |
31 | # I. Get the avatar positions. |
|
153 | $params = array( |
32 | $params = array( |
|
154 | 'command' => 'getavatarpositions', |
33 | 'command' => 'getavatarpositions', |
|
155 | 'group' => $GROUP, |
34 | 'group' => $GROUP, |
|
156 | 'entity' => 'region', |
35 | 'entity' => 'region', |
|
157 | 'password' => $PASSWORD |
36 | 'password' => $PASSWORD |
|
158 | ); |
37 | ); |
|
159 | |
38 | |
|
160 | # We now escape each key and value: this is very important, because the |
39 | # We now escape each key and value: this is very important, because the |
|
161 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
40 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
|
162 | # in this example though but this will not hurt anyone). |
41 | # in this example though but this will not hurt anyone). |
|
163 | array_walk($params, |
42 | array_walk($params, |
|
164 | function(&$value, $key) { |
43 | function(&$value, $key) { |
|
165 | $value = urlencode($key)."=".urlencode($value); |
44 | $value = urlencode($key)."=".urlencode($value); |
|
166 | } |
45 | } |
|
167 | ); |
46 | ); |
|
168 | $postvars = implode('&', $params); |
47 | $postvars = implode('&', $params); |
|
169 | |
48 | |
|
170 | # Set the options, send the request and then display the outcome |
49 | # Set the options, send the request and then display the outcome |
|
171 | if (!($curl = curl_init())) { |
50 | if (!($curl = curl_init())) { |
|
172 | print 0; |
51 | print 0; |
|
173 | return; |
52 | return; |
|
174 | } |
53 | } |
|
175 | |
54 | |
|
176 | curl_setopt($curl, CURLOPT_URL, $URL); |
55 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
177 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
56 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
178 | curl_setopt($curl, CURLOPT_POST, true); |
57 | curl_setopt($curl, CURLOPT_POST, true); |
|
179 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
58 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
180 | $result = curl_exec($curl); |
59 | $result = curl_exec($curl); |
|
181 | curl_close($curl); |
60 | curl_close($curl); |
|
182 | |
61 | |
|
183 | $success = urldecode( |
62 | $success = urldecode( |
|
184 | wasKeyValueGet( |
63 | wasKeyValueGet( |
|
185 | "success", |
64 | "success", |
|
186 | $result |
65 | $result |
|
187 | ) |
66 | ) |
|
188 | ); |
67 | ); |
|
189 | |
68 | |
|
190 | // Unable to get avatar positions? |
69 | // Unable to get avatar positions? |
|
191 | if($success == 'False') return -1; |
70 | if($success == 'False') return -1; |
|
192 | |
71 | |
|
193 | $data = wasCSVToArray( |
72 | $data = wasCSVToArray( |
|
194 | urldecode( |
73 | urldecode( |
|
195 | wasKeyValueGet( |
74 | wasKeyValueGet( |
|
196 | "data", |
75 | "data", |
|
197 | $result |
76 | $result |
|
198 | ) |
77 | ) |
|
199 | ) |
78 | ) |
|
200 | ); |
79 | ); |
|
201 | |
80 | |
|
202 | #### |
81 | #### |
|
203 | # II. Construct and echo the JSON object. |
82 | # II. Construct and echo the JSON object. |
|
204 | echo json_encode( |
83 | echo json_encode( |
|
205 | array( |
84 | array( |
|
206 | 'header' => array('x', 'y', 'z'), |
85 | 'header' => array('x', 'y', 'z'), |
|
207 | 'positions' => array_values( |
86 | 'positions' => array_values( |
|
208 | array_map( |
87 | array_map( |
|
209 | function($e) { |
88 | function($e) { |
|
210 | $e = preg_replace('/</', '', $e); |
89 | $e = preg_replace('/</', '', $e); |
|
211 | $e = preg_replace('/>/', '', $e); |
90 | $e = preg_replace('/>/', '', $e); |
|
212 | $e = explode(',', $e); |
91 | $e = explode(',', $e); |
|
213 | return array($e[0], $e[1], $e[2]); |
92 | return array($e[0], $e[1], $e[2]); |
|
214 | }, |
93 | }, |
|
215 | wasArrayStride( |
94 | wasArrayStride( |
|
216 | array_slice($data, 2), |
95 | array_slice($data, 2), |
|
217 | 3 |
96 | 3 |
|
218 | ) |
97 | ) |
|
219 | ) |
98 | ) |
|
220 | ), |
99 | ), |
|
221 | 'names' => array_values( |
100 | 'names' => array_values( |
|
222 | wasArrayStride( |
101 | wasArrayStride( |
|
223 | $data, |
102 | $data, |
|
224 | 3 |
103 | 3 |
|
225 | ) |
104 | ) |
|
226 | ), |
105 | ), |
|
227 | 'colors' => array_values( |
106 | 'colors' => array_values( |
|
228 | array_map( |
107 | array_map( |
|
229 | function($e) { |
108 | function($e) { |
|
230 | $e = hex2RGB(wasColorHash($e)); |
109 | $e = hex2RGB(wasColorHash($e)); |
|
231 | return array( |
110 | return array( |
|
232 | round ( |
111 | round ( |
|
233 | mapValueToRange($e['red'], 0, 255, 0, 1), 2 |
112 | mapValueToRange($e['red'], 0, 255, 0, 1), 2 |
|
234 | ), |
113 | ), |
|
235 | round( |
114 | round( |
|
236 | mapValueToRange($e['green'], 0, 255, 0, 1), 2 |
115 | mapValueToRange($e['green'], 0, 255, 0, 1), 2 |
|
237 | ), |
116 | ), |
|
238 | round( |
117 | round( |
|
239 | mapValueToRange($e['blue'], 0, 255, 0, 1), 2 |
118 | mapValueToRange($e['blue'], 0, 255, 0, 1), 2 |
|
240 | ) |
119 | ) |
|
241 | ); |
120 | ); |
|
242 | }, |
121 | }, |
|
243 | wasArrayStride( |
122 | wasArrayStride( |
|
244 | $data, |
123 | $data, |
|
245 | 3 |
124 | 3 |
|
246 | ) |
125 | ) |
|
247 | ) |
126 | ) |
|
248 | ) |
127 | ) |
|
249 | ), |
128 | ), |
|
250 | JSON_NUMERIC_CHECK |
129 | JSON_NUMERIC_CHECK |
|
251 | ); |
130 | ); |
|
252 | |
131 | |
|
253 | return 0; |
132 | return 0; |
|
254 | |
133 | |
|
255 | ?> |
134 | ?> |
|
256 | |
135 | |
|
257 | |
136 | |