Commit 1c75764d authored by Stuart John Watson's avatar Stuart John Watson

Moved js out of index. Added starting of opentTTD

parent 3262d2fd
0 <?php namespace openTTD;
function getInfo(){
$ADDRESS = "games.sucs.org";
$PORT = 3979;
if (($sock = socket_create(AF_INET,SOCK_DGRAM,0)) === false){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
if(socket_connect($sock,$ADDRESS,$PORT) === false){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
socket_write($sock,"\x03\x00\x00",3);
$out = socket_read($sock,2048);
socket_close($sock);
$length = unpack("v",substr($out,0,2))[0];
$info1 = unpack("CCC",substr($out,2,3));
$packetId = $info1[0];
if ($packetId != 1){
return ["online"=>false,"error"=>"server sent unexpected response"];
}
$protocolVersion = $info1[1];
if ($protocolVersion != 4) {
return ["online"=>false,"error"=>"server sent unsported packet version"];
}
$grfsCount = $info1[2];
$data = substr($out,5);
$grfs = readGrfs($data,$grfsCount);
$info2 = unpack("VVCCC",substr($data,0,7));
$gameDate = $info2[0];
$startDate = $info2[1];
$companiesMax = $info2[2];
$companiesOn = $info2[3];
$spectatorsMax = $info2[4];
$partsT = explode("\x00",substr($data,7));
$serverName = $partsT[0];
$serverRevision = $partsT[1];
$info3 = unpack("CCCCC",substr($partsT[2]));
$serverLang = $info3[0];
$passworded = $info3[1];
$clientsMax = $info3[2];
$clientsOn = $info3[3];
$spectatorsOn = $info3[4];
$mapName = $partsT[3];
$info4 = unpack("vvCC",$partsT[4]);
$mapWidth = $info4[0];
$mapHeight = $info4[1];
$mapSet = $info4[2];
$dedicated = $info4[3];
return [
"online"=>true,
"description"=>$serverName,
"map"=>$mapName,
"players"=>[
"current"=>$clientsOn,
"max"=>$clientsMax
]
];
}
function readGrfs(&$data,$number){
$rtn = [];
for($i=0;$i<number;$i++){
$rtn[] = unpack("VB128",substr($data,i*130,130));
}
$data = substr($data,$number*130);
return $rtn;
}
echo json_encode(getInfo());
?>
......@@ -106,136 +106,6 @@
</footer>
<script src="js/jquery.min.js"></script>
<script>
var ERR_MAP = {
"BAD_LOGIN":"You have entered invalid credentials.",
"MISSING_USERNAME_OR_PASSWORD":"Please enter a username and password.",
"BANNED":"Sorry you are banned. For more information contact games@sucs.org",
"ERR_UNKNOWN_AUTH_TYPE":"An unexpected error occoured - Bad Auth Type.",
"UNI_DISALLOWED":"Only SUCS members are currentlly allowed access."
},
SUCCESS = "You are now logged into the SUCS Game Server system, and can connect to any of the servers we have running by simply specifying the hostname/IP address 'games.sucs.org'. This page must be left open while you are playing. When you close this window, you will no longer have access to the games server, and will have to login again if you wish to play some more.",
SIGNUP_INFO = "Thank you for taking an interest in playing on the SUCS game server. Unfortunately the game server is currently only available to SUCS members, you can <a href=\"https://sucs.org/join\">sign up</a> to SUCS and get 24/7 access to the server plus all the other benefits that come with SUCS membership.";
function loginRefresh(){
console.log()
$.post("controll.php",{renew:1},onPostResponse);
}
var REFRESH_ID;
function scheduleRefresh(){
REFRESH_ID = setTimeout(loginRefresh,30*1000);
}
function cancelRefresh(){
clearTimeout(REFRESH_ID);
}
function populateExtraData(data,domain){
if(domain === undefined){
domain = $("body")
}
$.each(data,function(key,value){
var target = domain.find("[data-target='"+key+"']");
if (target.length == 0){
console.warn("failed to find target",key,"under",domain);
return;
}
if (key == "_online"){
if (value){
target.css("color","green").text("Online")
} else {
target.css("color","red").text("Offline")
}
} else if (value == null){
target.empty();
} else if (value instanceof Array){
target.empty();
for(var i=0;i<value.length;i++){
$("<li>").text(value[i]).appendTo(target);
}
} else if (typeof value == "object") {
populateExtraData(value,target)
} else {
target.text(value);
}
})
}
function onPostResponse(response){
console.log(response)
//When this response comes back it will be 1 of 5 diffrent state we care about
//DEFAULT|BANNED|UNI-NO|GAME-ACCESS|BAD-CREDENTIALS
//Populate extra payload data
populateExtraData(response.extraPayload)
//if the response is AS_BEFORE nothing changes, just schedle a refresh
if (response.level == "AS_BEFORE") {
scheduleRefresh();
return
}
//Show an error if there is one
if (response.loginError){
$("#loginErrorWrap").show();
$("#loginError").text(ERR_MAP[response.loginError]||response.loginError);
} else {
$("#loginErrorWrap").hide();
}
//Display username if we have it
if(response.username){
$("#username").show().text("Hello "+response.username);
} else {
$("#username").hide();
}
//Display login details if not logged in
if (response.level == "NO_LOGIN"){
$("#login, #signup").show();
$("#logout").hide();
} else {
$("#login, #signup").hide();
$("#logout").show();
}
//Now lets take care of the other messages we end up having to display
var welcomeMessage
if (response.level == "GAME_ACCESS"){
welcomeMessage = SUCCESS;
} else if (response.loginError == "UNI_DISALLOWED"){
welcomeMessage = SIGNUP_INFO;
$("#signup").show();
}
if (welcomeMessage){
$("#loginMessage").show().html(welcomeMessage);
} else {
$("#loginMessage").hide();
}
//Finally if they logged in set the refresh up to happen
if (response.level != "NO_LOGIN"){
scheduleRefresh();
} else {
cancelRefresh();
}
}
onPostResponse({level:"NO_LOGIN",extraPayload:{}});
$("form").submit(function(event){
var data={}
$(this).find("input").each(function(){
var t = $(this);
data[t.attr("name")] = t.val();
if(t.attr("type") == "password"){
t.val("")
}
});
$.post("controll.php",data,onPostResponse);
return false;
});
</script>
<script src=refresh.js></script>
</body>
</html>
var ERR_MAP = {
"BAD_LOGIN":"You have entered invalid credentials.",
"MISSING_USERNAME_OR_PASSWORD":"Please enter a username and password.",
"BANNED":"Sorry you are banned. For more information contact games@sucs.org",
"ERR_UNKNOWN_AUTH_TYPE":"An unexpected error occoured - Bad Auth Type.",
"UNI_DISALLOWED":"Only SUCS members are currentlly allowed access."
},
SUCCESS = "You are now logged into the SUCS Game Server system, and can connect to any of the servers we have running by simply specifying the hostname/IP address 'games.sucs.org'. This page must be left open while you are playing. When you close this window, you will no longer have access to the games server, and will have to login again if you wish to play some more.",
SIGNUP_INFO = "Thank you for taking an interest in playing on the SUCS game server. Unfortunately the game server is currently only available to SUCS members, you can <a href=\"https://sucs.org/join\">sign up</a> to SUCS and get 24/7 access to the server plus all the other benefits that come with SUCS membership.";
function loginRefresh(){
console.log()
$.post("controll.php",{renew:1},onPostResponse);
}
var REFRESH_ID;
function scheduleRefresh(){
REFRESH_ID = setTimeout(loginRefresh,30*1000);
}
function cancelRefresh(){
clearTimeout(REFRESH_ID);
}
function populateExtraData(data,domain){
if(domain === undefined){
domain = $("body")
}
$.each(data,function(key,value){
var target = domain.find("[data-target='"+key+"']");
if (target.length == 0){
console.warn("failed to find target",key,"under",domain);
return;
}
if (key == "_online"){
if (value){
target.css("color","green").text("Online")
} else {
target.css("color","red").text("Offline")
}
} else if (value == null){
target.empty();
} else if (value instanceof Array){
target.empty();
for(var i=0;i<value.length;i++){
$("<li>").text(value[i]).appendTo(target);
}
} else if (typeof value == "object") {
populateExtraData(value,target)
} else {
target.text(value);
}
})
}
function onPostResponse(response){
console.log(response)
//When this response comes back it will be 1 of 5 diffrent state we care about
//DEFAULT|BANNED|UNI-NO|GAME-ACCESS|BAD-CREDENTIALS
//Populate extra payload data
populateExtraData(response.extraPayload)
//if the response is AS_BEFORE nothing changes, just schedle a refresh
if (response.level == "AS_BEFORE") {
scheduleRefresh();
return
}
//Show an error if there is one
if (response.loginError){
$("#loginErrorWrap").show();
$("#loginError").text(ERR_MAP[response.loginError]||response.loginError);
} else {
$("#loginErrorWrap").hide();
}
//Display username if we have it
if(response.username){
$("#username").show().text("Hello "+response.username);
} else {
$("#username").hide();
}
//Display login details if not logged in
if (response.level == "NO_LOGIN"){
$("#login, #signup").show();
$("#logout").hide();
} else {
$("#login, #signup").hide();
$("#logout").show();
}
//Now lets take care of the other messages we end up having to display
var welcomeMessage
if (response.level == "GAME_ACCESS"){
welcomeMessage = SUCCESS;
} else if (response.loginError == "UNI_DISALLOWED"){
welcomeMessage = SIGNUP_INFO;
$("#signup").show();
}
if (welcomeMessage){
$("#loginMessage").show().html(welcomeMessage);
} else {
$("#loginMessage").hide();
}
//Finally if they logged in set the refresh up to happen
if (response.level != "NO_LOGIN"){
scheduleRefresh();
} else {
cancelRefresh();
}
}
onPostResponse({level:"NO_LOGIN",extraPayload:{}});
$("form").submit(function(event){
var data={}
$(this).find("input").each(function(){
var t = $(this);
data[t.attr("name")] = t.val();
if(t.attr("type") == "password"){
t.val("")
}
});
$.post("controll.php",data,onPostResponse);
return false;
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment