Hi Epic, I agree with you, to a point, on castles just being big, fancy homes. If you look at Bodiam, it looks fortified (the real castle had ultra-modern gun loops at the gatehouse, murder holes in the gates, flanking towers, a big moat, etc) but when you look further you see that many of these features don’t work in practice. But, if you look at some of Edward I’s castles in Wales, such as Harlech, you’ll see very functioning military fortress installations which were used as such many times. So, the debate continues, which is part of the fun…
kosmopol, here are the scripts:
This is the javascript, externalPosLogger, attached to a controller object in-game which logs a position every second and passes it to another javascript function called cacheVals running in the browser page:
var playerObject : Transform;
InvokeRepeating("logPoint", 1, 1);
function logPoint () {
var pointPosition = playerObject.position;
var posx = pointPosition.x.ToString();
var posy = pointPosition.y.ToString();
var posz = pointPosition.z.ToString();
var posAll = posx + "," + posz + "," + posy;
Application.ExternalCall ("cacheVals", posAll);
}
Now here’s the javascript function in the external page that receives the positions from Unity. This function or set of functions uses some Ajax/jquery stuff so that the page doesn’t have to reload in order to update - it’s mainly used for the status monitor which you may have noticed working away under the game window on the page - the Sending, Sent! thing.:
var vals = new Array();
var maxCache;
var pending;
// Number of entries to trigger AJAX request
maxCache = 10;
function cacheVals (val) {
vals.push (val);
if ((vals.length >= maxCache) (pending != true)) {
$.post ("masterpage.php", { 'xyz': vals }, function (msgBack) {
if (msgBack = '1') {
vals = [];
pending = false;
$('#ajaxStatus').text ('Sent!');
}
else {
$('#ajaxStatus').html ('Error :-(
' + msgBack);
pending = false;
}
})
pending = true;
}
else {
$('#ajaxStatus').append ('.');
}
if (pending)
$('#ajaxStatus').text ('Sending!');
}
And this is all governed by the masterpage.php PHP script running as its own page but including the actual login, maze, and logout pages. This is the master key that glues everything together and allows Unity to write csv files to the server. This script and the one above were built by our partner David Harker, BTW:
<?php
session_start();
usleep (100);
define ('FILE_START', 'placetostorefiles/mazepath');
define ('FILE_EXT', '.csv');
define ('MAX_FILESIZE', '10000');
// name of form field with 123,456,789 etc.
$ipfield = 'xyz';
// list of field IDs in order
$split_fields = array ('x', 'y', 'z');
if (isset ($_POST['logout'])) {
session_destroy ();
// echo "reload page!";
include ("logoutpage.inc");
}
elseif (isset ($_SESSION['username']) AND (isset ($_POST[$ipfield]) )) {
//if (isset ($_SESSION['username'])) {
// get values first
// ok to record values
$filename = FILE_START . $_SESSION['username'] . "_ts" . $_SESSION['timestamp'] . FILE_EXT;
//print_r ($_POST);
//die();
// Split input
$in = (is_array ($_POST[$ipfield])) ? $_POST[$ipfield] : array ($_POST[$ipfield]);
foreach ($in as $n => $xyz)
$split[$n] = explode (",",$xyz);
foreach ($split as $vnum => $arr_xyz)
foreach ($split_fields as $pos => $fname)
$save[$vnum][$fname] = numinput ($arr_xyz[$pos]);
$outt = array ();
foreach ($save as $n => $arr_f)
$outt[] = implode (",",$arr_f);
$append = implode ("\n", $outt);
// create CSV file if we need to
if (!file_exists ($filename))
file_put_contents ($filename, implode (",", $split_fields));
echo (append_to_file ($append, $filename)) ? '1' : '0';
}
elseif (isset ($_POST['username'])) {
$_SESSION['username'] = preg_replace ("/^[^a-z]|[^a-z0-9]/i", '', strtolower ($_POST['username']));
$_SESSION['timestamp'] = time();
include ("castlepage.inc");
}
elseif (isset ($_SESSION['username'])) { // user has reloaded page, give them maze since we know username already
include ("castlepage.inc");
}
else {
include ("loginpage.inc");
}
function append_to_file ($append, $filename) {
if (!$fh = fopen ($filename, 'a'))
return FALSE;
fwrite ($fh, "\n" . $append) or die ("Can't write to file: $filename !");
return fclose ($fh);
}
function numinput ($mi) {
return (is_numeric ($mi)) ? (round ($mi, 3) + 0.00) : FALSE;
}
?>
Hope that is helpful!