api getMaps for create channel

This commit is contained in:
Jeena 2014-07-03 20:28:18 +02:00
parent a837d2099c
commit 6bc9b7e32b
8 changed files with 509 additions and 1658 deletions

View file

@ -4,7 +4,7 @@ define([
"fs" "fs"
], ],
function (Parent, Settings, fs) { function (Parent, Settings, FileSystem) {
function Level (uid, engine, gameObjects) { function Level (uid, engine, gameObjects) {
Parent.call(this, uid, engine, gameObjects); Parent.call(this, uid, engine, gameObjects);
@ -15,7 +15,7 @@ function (Parent, Settings, fs) {
Level.prototype.loadLevelDataFromPath = function (path, callback) { Level.prototype.loadLevelDataFromPath = function (path, callback) {
// overwriting parent // overwriting parent
fs.readFile(path, "utf8", function (err, data) { FileSystem.readFile(path, "utf8", function (err, data) {
if (err) throw err; if (err) throw err;
callback(JSON.parse(data)); callback(JSON.parse(data));
}); });

View file

@ -71,7 +71,7 @@ define(function() {
CHANNEL_END_ROUND_TIME: 4, //10, CHANNEL_END_ROUND_TIME: 4, //10,
CHANNEL_DEFAULT_MAX_USERS: 40, CHANNEL_DEFAULT_MAX_USERS: 40,
CHANNEL_DEFAULT_SCORE_LIMIT: 10, CHANNEL_DEFAULT_SCORE_LIMIT: 10,
CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug'], CHANNEL_DEFAULT_LEVELS: ['debug'],
// ME STATE // ME STATE
ME_STATE_MAX_DIFFERENCE_METERS: 1, ME_STATE_MAX_DIFFERENCE_METERS: 1,

View file

@ -3,10 +3,11 @@ define([
"Lib/Utilities/Protocol/Helper", "Lib/Utilities/Protocol/Helper",
"Lib/Utilities/Validate", "Lib/Utilities/Validate",
"Lib/Utilities/Options", "Lib/Utilities/Options",
"Game/Config/Settings" "Game/Config/Settings",
"fs"
], ],
function (Nc, ProtocolHelper, validate, Options, Settings) { function (Nc, ProtocolHelper, validate, Options, Settings, FileSystem) {
function Api(coordinator) { function Api(coordinator) {
this.coordinator = coordinator; this.coordinator = coordinator;
@ -31,12 +32,15 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
switch(command) { switch(command) {
case "getChannels": case "getChannels":
output = this.coordinator.getChannels(); output = this.getChannels();
break; break;
case "createChannel": case "createChannel":
// FIXME: sanitize input // FIXME: sanitize input
output = this.createChannel(message.options); output = this.createChannel(message.options);
break; break;
case "getMaps":
output = this.getMaps();
break;
default: default:
this.isError = true; this.isError = true;
output = "Command not found"; output = "Command not found";
@ -46,6 +50,10 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
this.output = output; this.output = output;
} }
Api.prototype.getChannels = function() {
return this.coordinator.getChannels();
};
Api.prototype.createChannel = function(options) { Api.prototype.createChannel = function(options) {
var allowedOptionKeys = [ var allowedOptionKeys = [
@ -73,7 +81,7 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
} }
for(var i = 0; i < options.levelUids.length; i++) { for(var i = 0; i < options.levelUids.length; i++) {
if(!validate(options.levelUids[i], {type: 'string', in: ['stones2', 'debug']})) { if(!validate(options.levelUids[i], {type: 'string', in: this.getMaps()})) {
this.isError = true; this.isError = true;
return "Could not create channel, invalid map (" + options.levelUids[i] + ")."; return "Could not create channel, invalid map (" + options.levelUids[i] + ").";
} }
@ -104,8 +112,6 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
return "Could not create channel, score limit (" + options.scoreLimit + ")."; return "Could not create channel, score limit (" + options.scoreLimit + ").";
} }
var defaultOptions = { var defaultOptions = {
maxUsers: Settings.CHANNEL_DEFAULT_MAX_USERS, maxUsers: Settings.CHANNEL_DEFAULT_MAX_USERS,
minUsers: 0, minUsers: 0,
@ -114,7 +120,6 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
options = Options.merge(options, defaultOptions); options = Options.merge(options, defaultOptions);
var result = this.coordinator.createChannel(options); var result = this.coordinator.createChannel(options);
if(result !== false) { if(result !== false) {
return result; return result;
@ -134,14 +139,24 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
} }
return JSON.stringify(output); return JSON.stringify(output);
}; };
Api.prototype.getContentType = function() { Api.prototype.getContentType = function() {
return "application/json"; return "application/json";
}; };
Api.prototype.getMaps = function(callback) {
var list = FileSystem.readdirSync(Settings.MAPS_PATH);
var maps = [];
for (var i = 0; i < list.length; i++) {
var fileinfo = list[i].split(".");
if(fileinfo[1] == "json") {
maps.push(fileinfo[0]);
}
};
return maps;
};
return Api; return Api;

View file

@ -36,6 +36,10 @@ input, button {
display: none; display: none;
} }
#createform #maps {
text-transform: capitalize;
}
article#menu { article#menu {
margin: 4em auto; margin: 4em auto;
background: #1a1a1a; background: #1a1a1a;

View file

@ -21,12 +21,12 @@
<p><label>Score limit:<br> <input id="scoreLimit" type="number" value="5"></label></p> <p><label>Score limit:<br> <input id="scoreLimit" type="number" value="5"></label></p>
<fieldset> <fieldset>
<legend>Maps</legend> <legend>Maps</legend>
<ul> <ul id="maps">
<li> <li>
<label><input name="maps" value="debug" type="checkbox" checked> Debug</label> <label><input name="maps" value="debug" type="checkbox" checked> Debug</label>
</li> </li>
<li> <li>
<label><input name="maps" value="stones2" type="checkbox" checked> Stones2</label> <label><input name="maps" value="stones" type="checkbox" checked> Stones</label>
</li> </li>
</ul> </ul>
</fieldset> </fieldset>

View file

@ -41,7 +41,24 @@ if(!Chuck) var Chuck = {};
var lastRefreshResponse; var lastRefreshResponse;
function refresh(callback) { function refresh(callback) {
try {
ajax("getChannels", {}, function(response) {
if(response != lastRefreshResponse) {
lastRefreshResponse = response;
populate(JSON.parse(response).success);
}
document.body.className = "";
if(typeof callback == 'function') {
callback(JSON.parse(response).success)
}
}, function(status, responseText) {
console.error("getChannels error: ", responseText)
});
/*
try {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if(xhr.readyState == 4) { if(xhr.readyState == 4) {
@ -69,9 +86,37 @@ if(!Chuck) var Chuck = {};
} catch(e) { } catch(e) {
console.error(e) console.error(e)
} }
*/
return false; return false;
} }
function ajax(command, options, callback, errorCallback) {
try {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
if(typeof callback == 'function') {
callback(xhr.responseText)
}
} else {
if(typeof errorCallback == 'function' && xhr.status == "400") {
errorCallback(xhr.status, xhr.responseText);
} else {
console.error("Ajax error: " + xhr.status + " " + xhr.responseText)
$("#list").innerHTML = "";
document.body.className = "offline";
}
}
}
}
xhr.open("POST", "/api", true);
xhr.send(JSON.stringify({command:command, options:options}));
} catch(e) {
console.error(e)
}
}
function populate(list) { function populate(list) {
var html = ""; var html = "";
if(list.length > 0) { if(list.length > 0) {
@ -89,6 +134,24 @@ if(!Chuck) var Chuck = {};
$("#list").innerHTML = html; $("#list").innerHTML = html;
} }
function populateMaps() {
ajax("getMaps", {}, function(responseText) {
var maps = JSON.parse(responseText).success;
var html = "";
for (var i = 0; i < maps.length; i++) {
var map = maps[i];
html += "<li><label>";
html += '<input name="maps" value="' + map + '" type="checkbox" checked> ';
html += map;
html += "</label></li>";
};
$("#maps").innerHTML = html;
}, function(status, responseText) {
console.error("getMaps error:", status, responseText);
});
}
$("form#listform").onsubmit = function(e) { $("form#listform").onsubmit = function(e) {
try { try {
var nickname = $("#nick").value; var nickname = $("#nick").value;
@ -293,6 +356,16 @@ if(!Chuck) var Chuck = {};
localStorage["customname"] = channelName; localStorage["customname"] = channelName;
ajax("createChannel", options, function(responseText) {
if(typeof callback == 'function') {
callback(JSON.parse(responseText).success);
}
}, function(status, responseText) {
console.log(responseText)
alert(JSON.parse(responseText).error)
});
/*
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if(xhr.readyState == 4) { if(xhr.readyState == 4) {
@ -308,11 +381,13 @@ if(!Chuck) var Chuck = {};
} }
xhr.open("POST", "/api", true); xhr.open("POST", "/api", true);
xhr.send(JSON.stringify({command:"createChannel", options: options})); xhr.send(JSON.stringify({command:"createChannel", options: options}));
*/
} }
} }
$("#refresh").onclick = refresh; $("#refresh").onclick = refresh;
refresh(); refresh();
populateMaps();
var channelDestructionTimeout = null; var channelDestructionTimeout = null;
var refreshInterval = setInterval(refresh, 5000); var refreshInterval = setInterval(refresh, 5000);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long