corrade-http-templates – Blame information for rev 77

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