corrade-http-templates – Rev 2
?pathlinks?
<?php
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
## This is a script that uses Corrade's: ##
## * getregiondata ##
## * getavatarpositions ##
## * download ##
## commands in order to display the positions of avatars on the region. ##
###########################################################################
###########################################################################
## CONFIGURATION ##
###########################################################################
# Set this to the name of the group.
$GROUP = 'My Group';
# Set this to the group password.
$PASSWORD = 'mypassword';
# Set this to Corrade's HTTP Server URL.
$URL = 'http://corrade.internal.site:8080';
###########################################################################
## INTERNALS ##
###########################################################################
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function wasKeyValueGet($key, $data) {
return array_reduce(
explode(
"&",
$data
),
function($o, $p) {
$x = explode("=", $p);
return array_shift($x) != $o ? $o : array_shift($x);
},
$key
);
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
function wasCSVToArray($csv) {
$l = array();
$s = array();
$m = "";
for ($i = 0; $i < strlen($csv); ++$i) {
switch ($csv{$i}) {
case ',':
if (sizeof($s) == 0 || !current($s) == '"') {
array_push($l, $m);
$m = "";
break;
}
$m .= $csv{$i};
continue;
case '"':
if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) {
$m .= $csv{$i};
++$i;
break;
}
if (sizeof($s) == 0|| !current($s) == $csv[$i]) {
array_push($s, $csv{$i});
continue;
}
array_pop($s);
break;
default:
$m .= $csv{$i};
break;
}
}
array_push($l, $m);
return $l;
}
##########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function mapValueToRange($value, $xMin, $xMax, $yMin, $yMax) {
return $yMin + (
(
$yMax - $yMin
)
*
(
$value - $xMin
)
/
(
$xMax - $xMin
)
);
}
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function wasLSLVectorToArray($vector) {
$components = array();
if(!preg_match(
"/^<\s*([0-9\.]+?)\s*,\s*([0-9\.]+?)\s*,\s*([0-9\.]+?)\s*>$/",
$vector,
$components
)) return;
array_shift($components);
return $components;
}
####
# I. Get the UUID of the map image for the current region
$params = array(
'command' => 'getgridregiondata',
'group' => $GROUP,
'password' => $PASSWORD,
'data' => 'MapImageID'
);
array_walk($params,
function(&$value, $key) {
$value = rawurlencode($key)."=".rawurlencode($value);
}
);
$postvars = implode('&', $params);
if (!($curl = curl_init())) {
print 0;
return;
}
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
$return = curl_exec($curl);
curl_close($curl);
$success = urldecode(
wasKeyValueGet(
"success",
$return
)
);
if($success == 'False') {
echo 'Unable to get the region map image.';
die;
}
$mapUUID = wasCSVToArray(
urldecode(
wasKeyValueGet(
"data",
$return
)
)
)[1];
####
# II. Download the map image as a PNG file.
$params = array(
'command' => 'download',
'group' => $GROUP,
'password' => $PASSWORD,
'item' => $mapUUID,
'type' => 'Texture',
'format' => 'Png'
);
array_walk($params,
function(&$value, $key) {
$value = rawurlencode($key)."=".rawurlencode($value);
}
);
$postvars = implode('&', $params);
if (!($curl = curl_init())) {
print 0;
return;
}
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
$return = curl_exec($curl);
curl_close($curl);
if($success == 'False') {
echo 'Unable to download the region map texture.';
die;
}
####
# III. Convert the image data to a PNG of size 512x512
$im = imagescale(
imagecreatefromstring(
base64_decode(
urldecode(
wasKeyValueGet(
"data",
$return
)
)
)
),
512,
512
);
####
# IV. Get the avatar positions on the map.
$params = array(
'command' => 'getavatarpositions',
'group' => $GROUP,
'password' => $PASSWORD,
'entity' => 'region'
);
array_walk($params,
function(&$value, $key) {
$value = rawurlencode($key)."=".rawurlencode($value);
}
);
$postvars = implode('&', $params);
if (!($curl = curl_init())) {
print 0;
return;
}
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
$return = curl_exec($curl);
curl_close($curl);
$success = urldecode(
wasKeyValueGet(
"success",
$return
)
);
if($success == 'False') {
echo 'Unable to get the avatars positions.';
die;
}
####
# V. Display the coordinates on the map.
array_walk(
array_chunk(
wasCSVToArray(
urldecode(
wasKeyValueGet(
"data",
$return
)
)
),
3
),
function(&$value, $key) use(&$im) {
if(count($value) != 3) return;
$components = wasLSLVectorToArray($value[2]);
if(count($components) != 3) return;
$x = mapValueToRange($components[0], 0, 255, 0, 512);
$y = 512 - mapValueToRange($components[1], 0, 255, 0, 512);
imagefilledellipse(
$im,
$x,
$y,
8,
8,
imagecolorallocate(
$im,
0,
0,
0
)
);
imagefilledellipse(
$im,
$x,
$y,
6,
6,
imagecolorallocate(
$im,
0,
255,
0
)
);
}
);
####
# VI. Output the Base64 encoded image for AJAX.
ob_start();
imagepng($im);
$png = ob_get_contents();
imagedestroy($im);
ob_end_clean();
echo base64_encode($png);
?>