corrade-http-templates – Diff between revs 53 and 77
?pathlinks?
Rev 53 | Rev 77 | |||
---|---|---|---|---|
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 | require_once('config.php'); |
|
- | 24 | require_once('vendor/was/utilities/src/formats/kvp/kvp.php'); |
||
- | 25 | require_once('vendor/was/utilities/src/formats/csv/csv.php'); |
||
- | 26 | require_once('vendor/was/utilities/src/collections/arrays/arrays.php'); |
||
- | 27 | require_once('vendor/was/utilities/src/mathematics/algebra.php'); |
||
24 | require_once("functions.php"); |
28 | require_once('functions.php'); |
|
25 | |
29 | |
|
26 | ########################################################################### |
30 | ########################################################################### |
|
27 | ## INTERNALS ## |
31 | ## INTERNALS ## |
|
28 | ########################################################################### |
32 | ########################################################################### |
|
29 | |
33 | |
|
30 | #### |
34 | #### |
|
31 | # I. Get the avatar positions. |
35 | # I. Get the avatar positions. |
|
32 | $params = array( |
36 | $params = array( |
|
33 | 'command' => 'getavatarpositions', |
37 | 'command' => 'getavatarpositions', |
|
34 | 'group' => $GROUP, |
38 | 'group' => $GROUP, |
|
35 | 'entity' => 'region', |
39 | 'entity' => 'region', |
|
36 | 'password' => $PASSWORD |
40 | 'password' => $PASSWORD |
|
37 | ); |
41 | ); |
|
38 | |
42 | |
|
39 | # We now escape each key and value: this is very important, because the |
43 | # 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 |
44 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
|
41 | # in this example though but this will not hurt anyone). |
45 | # in this example though but this will not hurt anyone). |
|
42 | array_walk($params, |
46 | array_walk($params, |
|
43 | function(&$value, $key) { |
47 | function(&$value, $key) { |
|
44 | $value = urlencode($key)."=".urlencode($value); |
48 | $value = urlencode($key)."=".urlencode($value); |
|
45 | } |
49 | } |
|
46 | ); |
50 | ); |
|
47 | $postvars = implode('&', $params); |
51 | $postvars = implode('&', $params); |
|
48 | |
52 | |
|
49 | # Set the options, send the request and then display the outcome |
53 | # Set the options, send the request and then display the outcome |
|
50 | if (!($curl = curl_init())) { |
54 | if (!($curl = curl_init())) { |
|
51 | print 0; |
55 | print 0; |
|
52 | return; |
56 | return; |
|
53 | } |
57 | } |
|
54 | |
58 | |
|
55 | curl_setopt($curl, CURLOPT_URL, $URL); |
59 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
56 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
60 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
57 | curl_setopt($curl, CURLOPT_POST, true); |
61 | curl_setopt($curl, CURLOPT_POST, true); |
|
58 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
62 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
59 | $result = curl_exec($curl); |
63 | $result = curl_exec($curl); |
|
60 | curl_close($curl); |
64 | curl_close($curl); |
|
61 | |
65 | |
|
62 | $success = urldecode( |
66 | $success = urldecode( |
|
63 | wasKeyValueGet( |
67 | wasKeyValueGet( |
|
64 | "success", |
68 | "success", |
|
65 | $result |
69 | $result |
|
66 | ) |
70 | ) |
|
67 | ); |
71 | ); |
|
68 | |
72 | |
|
69 | // Unable to get avatar positions? |
73 | // Unable to get avatar positions? |
|
70 | if($success == 'False') return -1; |
74 | if($success == 'False') return -1; |
|
71 | |
75 | |
|
72 | $data = wasCSVToArray( |
76 | $data = wasCSVToArray( |
|
73 | urldecode( |
77 | urldecode( |
|
74 | wasKeyValueGet( |
78 | wasKeyValueGet( |
|
75 | "data", |
79 | "data", |
|
76 | $result |
80 | $result |
|
77 | ) |
81 | ) |
|
78 | ) |
82 | ) |
|
79 | ); |
83 | ); |
|
80 | |
84 | |
|
81 | #### |
85 | #### |
|
82 | # II. Construct and echo the JSON object. |
86 | # II. Construct and echo the JSON object. |
|
83 | echo json_encode( |
87 | echo json_encode( |
|
84 | array( |
88 | array( |
|
85 | 'header' => array('x', 'y', 'z'), |
89 | 'header' => array('x', 'y', 'z'), |
|
86 | 'positions' => array_values( |
90 | 'positions' => array_values( |
|
87 | array_map( |
91 | array_map( |
|
88 | function($e) { |
92 | function($e) { |
|
89 | $e = preg_replace('/</', '', $e); |
93 | $e = preg_replace('/</', '', $e); |
|
90 | $e = preg_replace('/>/', '', $e); |
94 | $e = preg_replace('/>/', '', $e); |
|
91 | $e = explode(',', $e); |
95 | $e = explode(',', $e); |
|
92 | return array($e[0], $e[1], $e[2]); |
96 | return array($e[0], $e[1], $e[2]); |
|
93 | }, |
97 | }, |
|
94 | wasArrayStride( |
98 | wasArrayStride( |
|
95 | array_slice($data, 2), |
99 | array_slice($data, 2), |
|
96 | 3 |
100 | 3 |
|
97 | ) |
101 | ) |
|
98 | ) |
102 | ) |
|
99 | ), |
103 | ), |
|
100 | 'names' => array_values( |
104 | 'names' => array_values( |
|
101 | wasArrayStride( |
105 | wasArrayStride( |
|
102 | $data, |
106 | $data, |
|
103 | 3 |
107 | 3 |
|
104 | ) |
108 | ) |
|
105 | ), |
109 | ), |
|
106 | 'colors' => array_values( |
110 | 'colors' => array_values( |
|
107 | array_map( |
111 | array_map( |
|
108 | function($e) { |
112 | function($e) { |
|
109 | $e = hex2RGB(wasColorHash($e)); |
113 | $e = hex2RGB(wasColorHash($e)); |
|
110 | return array( |
114 | return array( |
|
111 | round ( |
115 | round ( |
|
112 | mapValueToRange($e['red'], 0, 255, 0, 1), 2 |
116 | mapValueToRange($e['red'], 0, 255, 0, 1), 2 |
|
113 | ), |
117 | ), |
|
114 | round( |
118 | round( |
|
115 | mapValueToRange($e['green'], 0, 255, 0, 1), 2 |
119 | mapValueToRange($e['green'], 0, 255, 0, 1), 2 |
|
116 | ), |
120 | ), |
|
117 | round( |
121 | round( |
|
118 | mapValueToRange($e['blue'], 0, 255, 0, 1), 2 |
122 | mapValueToRange($e['blue'], 0, 255, 0, 1), 2 |
|
119 | ) |
123 | ) |
|
120 | ); |
124 | ); |
|
121 | }, |
125 | }, |
|
122 | wasArrayStride( |
126 | wasArrayStride( |
|
123 | $data, |
127 | $data, |
|
124 | 3 |
128 | 3 |
|
125 | ) |
129 | ) |
|
126 | ) |
130 | ) |
|
127 | ) |
131 | ) |
|
128 | ), |
132 | ), |
|
129 | JSON_NUMERIC_CHECK |
133 | JSON_NUMERIC_CHECK |
|
130 | ); |
134 | ); |
|
131 | |
135 | |
|
132 | return 0; |
136 | return 0; |
|
133 | |
137 | |
|
134 | ?> |
138 | ?> |
|
135 | |
139 | |
|
136 | |
140 | |