corrade-http-templates – Blame information for rev 12
?pathlinks?
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 to get the avatar positions and ## |
||
7 | ## then aggregates that data to a JSON object that will be read by AJAX. ## |
||
8 | ## ## |
||
9 | ## The output JSON object that will be read via AJAX is of the form: ## |
||
10 | ## { ## |
||
11 | ## header: ["x", "y", "z"], ## |
||
12 | ## positions: [[x1, y1, z1], [x2, y2, z2], ...], ## |
||
13 | ## names: ["First Resident", "Second Resident", ...], ## |
||
14 | ## colors: [[0.5, 1, 0.75], [0.3, 0.55, 0.34], ...] ## |
||
15 | ## } ## |
||
16 | ########################################################################### |
||
17 | |||
18 | ########################################################################### |
||
19 | ## CONFIGURATION ## |
||
20 | ########################################################################### |
||
21 | |||
12 | eva | 22 | # The configuration file for this script containing the settings. |
23 | require_once("config.php"); |
||
24 | require_once("functions.php") |
||
2 | eva | 25 | |
26 | ########################################################################### |
||
27 | ## INTERNALS ## |
||
28 | ########################################################################### |
||
29 | |||
30 | #### |
||
31 | # I. Get the avatar positions. |
||
32 | $params = array( |
||
33 | 'command' => 'getavatarpositions', |
||
34 | 'group' => $GROUP, |
||
35 | 'entity' => 'region', |
||
36 | 'password' => $PASSWORD |
||
37 | ); |
||
38 | |||
39 | # We now escape each key and value: this is very important, because the |
||
40 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
||
41 | # in this example though but this will not hurt anyone). |
||
42 | array_walk($params, |
||
43 | function(&$value, $key) { |
||
44 | $value = urlencode($key)."=".urlencode($value); |
||
45 | } |
||
46 | ); |
||
47 | $postvars = implode('&', $params); |
||
48 | |||
49 | # Set the options, send the request and then display the outcome |
||
50 | if (!($curl = curl_init())) { |
||
51 | print 0; |
||
52 | return; |
||
53 | } |
||
54 | |||
55 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
56 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
57 | curl_setopt($curl, CURLOPT_POST, true); |
||
58 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
59 | $result = curl_exec($curl); |
||
60 | curl_close($curl); |
||
61 | |||
62 | $success = urldecode( |
||
63 | wasKeyValueGet( |
||
64 | "success", |
||
65 | $result |
||
66 | ) |
||
67 | ); |
||
68 | |||
69 | // Unable to get avatar positions? |
||
70 | if($success == 'False') return -1; |
||
71 | |||
72 | $data = wasCSVToArray( |
||
73 | urldecode( |
||
74 | wasKeyValueGet( |
||
75 | "data", |
||
76 | $result |
||
77 | ) |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | #### |
||
82 | # II. Construct and echo the JSON object. |
||
83 | echo json_encode( |
||
84 | array( |
||
85 | 'header' => array('x', 'y', 'z'), |
||
86 | 'positions' => array_values( |
||
87 | array_map( |
||
88 | function($e) { |
||
89 | $e = preg_replace('/</', '', $e); |
||
90 | $e = preg_replace('/>/', '', $e); |
||
91 | $e = explode(',', $e); |
||
92 | return array($e[0], $e[1], $e[2]); |
||
93 | }, |
||
94 | wasArrayStride( |
||
95 | array_slice($data, 2), |
||
96 | 3 |
||
97 | ) |
||
98 | ) |
||
99 | ), |
||
100 | 'names' => array_values( |
||
101 | wasArrayStride( |
||
102 | $data, |
||
103 | 3 |
||
104 | ) |
||
105 | ), |
||
106 | 'colors' => array_values( |
||
107 | array_map( |
||
108 | function($e) { |
||
109 | $e = hex2RGB(wasColorHash($e)); |
||
110 | return array( |
||
111 | round ( |
||
112 | mapValueToRange($e['red'], 0, 255, 0, 1), 2 |
||
113 | ), |
||
114 | round( |
||
115 | mapValueToRange($e['green'], 0, 255, 0, 1), 2 |
||
116 | ), |
||
117 | round( |
||
118 | mapValueToRange($e['blue'], 0, 255, 0, 1), 2 |
||
119 | ) |
||
120 | ); |
||
121 | }, |
||
122 | wasArrayStride( |
||
123 | $data, |
||
124 | 3 |
||
125 | ) |
||
126 | ) |
||
127 | ) |
||
128 | ), |
||
129 | JSON_NUMERIC_CHECK |
||
130 | ); |
||
131 | |||
132 | return 0; |
||
133 | |||
134 | ?> |
||
135 |