corrade-http-templates – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 eva 1 <?php
2  
3 ###########################################################################
4 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
5 ###########################################################################
6 ## This is a script that uses Corrade's: ##
7 ## * getregiondata ##
8 ## * getavatarpositions ##
9 ## * download ##
10 ## commands in order to display the positions of avatars on the region. ##
11 ###########################################################################
12  
13 ###########################################################################
14 ## CONFIGURATION ##
15 ###########################################################################
16  
17 # Set this to the name of the group.
18 $GROUP = 'My Group';
19 # Set this to the group password.
20 $PASSWORD = 'mypassword';
21 # Set this to Corrade's HTTP Server URL.
22 $URL = 'http://corrade.internal.site:8080';
23  
24 ###########################################################################
25 ## INTERNALS ##
26 ###########################################################################
27  
28 ###########################################################################
29 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
30 ###########################################################################
31 function wasKeyValueGet($key, $data) {
32 return array_reduce(
33 explode(
34 "&",
35 $data
36 ),
37 function($o, $p) {
38 $x = explode("=", $p);
39 return array_shift($x) != $o ? $o : array_shift($x);
40 },
41 $key
42 );
43 }
44 ///////////////////////////////////////////////////////////////////////////
45 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
46 ///////////////////////////////////////////////////////////////////////////
47 function wasCSVToArray($csv) {
48 $l = array();
49 $s = array();
50 $m = "";
51 for ($i = 0; $i < strlen($csv); ++$i) {
52 switch ($csv{$i}) {
53 case ',':
54 if (sizeof($s) == 0 || !current($s) == '"') {
55 array_push($l, $m);
56 $m = "";
57 break;
58 }
59 $m .= $csv{$i};
60 continue;
61 case '"':
62 if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) {
63 $m .= $csv{$i};
64 ++$i;
65 break;
66 }
67 if (sizeof($s) == 0|| !current($s) == $csv[$i]) {
68 array_push($s, $csv{$i});
69 continue;
70 }
71 array_pop($s);
72 break;
73 default:
74 $m .= $csv{$i};
75 break;
76 }
77 }
78 array_push($l, $m);
79 return $l;
80 }
81  
82 ##########################################################################
83 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
84 ###########################################################################
85 function mapValueToRange($value, $xMin, $xMax, $yMin, $yMax) {
86 return $yMin + (
87 (
88 $yMax - $yMin
89 )
90 *
91 (
92 $value - $xMin
93 )
94 /
95 (
96 $xMax - $xMin
97 )
98 );
99 }
100  
101 ###########################################################################
102 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
103 ###########################################################################
104 function wasLSLVectorToArray($vector) {
105 $components = array();
106 if(!preg_match(
107 "/^<\s*([0-9\.]+?)\s*,\s*([0-9\.]+?)\s*,\s*([0-9\.]+?)\s*>$/",
108 $vector,
109 $components
110 )) return;
111 array_shift($components);
112 return $components;
113 }
114  
115 ####
116 # I. Get the UUID of the map image for the current region
117 $params = array(
118 'command' => 'getgridregiondata',
119 'group' => $GROUP,
120 'password' => $PASSWORD,
121 'data' => 'MapImageID'
122 );
123 array_walk($params,
124 function(&$value, $key) {
125 $value = rawurlencode($key)."=".rawurlencode($value);
126 }
127 );
128 $postvars = implode('&', $params);
129 if (!($curl = curl_init())) {
130 print 0;
131 return;
132 }
133  
134 curl_setopt($curl, CURLOPT_URL, $URL);
135 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
136 curl_setopt($curl, CURLOPT_POST, true);
137 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
138 $return = curl_exec($curl);
139 curl_close($curl);
140  
141 $success = urldecode(
142 wasKeyValueGet(
143 "success",
144 $return
145 )
146 );
147  
148 if($success == 'False') {
149 echo 'Unable to get the region map image.';
150 die;
151 }
152  
153 $mapUUID = wasCSVToArray(
154 urldecode(
155 wasKeyValueGet(
156 "data",
157 $return
158 )
159 )
160 )[1];
161  
162 ####
163 # II. Download the map image as a PNG file.
164 $params = array(
165 'command' => 'download',
166 'group' => $GROUP,
167 'password' => $PASSWORD,
168 'item' => $mapUUID,
169 'type' => 'Texture',
170 'format' => 'Png'
171 );
172 array_walk($params,
173 function(&$value, $key) {
174 $value = rawurlencode($key)."=".rawurlencode($value);
175 }
176 );
177 $postvars = implode('&', $params);
178 if (!($curl = curl_init())) {
179 print 0;
180 return;
181 }
182 curl_setopt($curl, CURLOPT_URL, $URL);
183 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
184 curl_setopt($curl, CURLOPT_POST, true);
185 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
186 $return = curl_exec($curl);
187 curl_close($curl);
188  
189 if($success == 'False') {
190 echo 'Unable to download the region map texture.';
191 die;
192 }
193  
194  
195 ####
196 # III. Convert the image data to a PNG of size 512x512
197 $im = imagescale(
198 imagecreatefromstring(
199 base64_decode(
200 urldecode(
201 wasKeyValueGet(
202 "data",
203 $return
204 )
205 )
206 )
207 ),
208 512,
209 512
210 );
211  
212 ####
213 # IV. Get the avatar positions on the map.
214 $params = array(
215 'command' => 'getavatarpositions',
216 'group' => $GROUP,
217 'password' => $PASSWORD,
218 'entity' => 'region'
219 );
220 array_walk($params,
221 function(&$value, $key) {
222 $value = rawurlencode($key)."=".rawurlencode($value);
223 }
224 );
225 $postvars = implode('&', $params);
226 if (!($curl = curl_init())) {
227 print 0;
228 return;
229 }
230 curl_setopt($curl, CURLOPT_URL, $URL);
231 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
232 curl_setopt($curl, CURLOPT_POST, true);
233 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
234 $return = curl_exec($curl);
235 curl_close($curl);
236  
237 $success = urldecode(
238 wasKeyValueGet(
239 "success",
240 $return
241 )
242 );
243  
244 if($success == 'False') {
245 echo 'Unable to get the avatars positions.';
246 die;
247 }
248  
249  
250 ####
251 # V. Display the coordinates on the map.
252 array_walk(
253 array_chunk(
254 wasCSVToArray(
255 urldecode(
256 wasKeyValueGet(
257 "data",
258 $return
259 )
260 )
261 ),
262 3
263 ),
264 function(&$value, $key) use(&$im) {
265 if(count($value) != 3) return;
266 $components = wasLSLVectorToArray($value[2]);
267 if(count($components) != 3) return;
268 $x = mapValueToRange($components[0], 0, 255, 0, 512);
269 $y = 512 - mapValueToRange($components[1], 0, 255, 0, 512);
270 imagefilledellipse(
271 $im,
272 $x,
273 $y,
274 8,
275 8,
276 imagecolorallocate(
277 $im,
278 0,
279 0,
280  
281 )
282 );
283 imagefilledellipse(
284 $im,
285 $x,
286 $y,
287 6,
288 6,
289 imagecolorallocate(
290 $im,
291 0,
292 255,
293  
294 )
295 );
296 }
297 );
298  
299 ####
300 # VI. Output the Base64 encoded image for AJAX.
301 ob_start();
302 imagepng($im);
303 $png = ob_get_contents();
304 imagedestroy($im);
305 ob_end_clean();
306  
307 echo base64_encode($png);
308  
309 ?>
310