mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
api getMaps for create channel
This commit is contained in:
parent
a837d2099c
commit
6bc9b7e32b
8 changed files with 509 additions and 1658 deletions
|
|
@ -4,7 +4,7 @@ define([
|
|||
"fs"
|
||||
],
|
||||
|
||||
function (Parent, Settings, fs) {
|
||||
function (Parent, Settings, FileSystem) {
|
||||
|
||||
function Level (uid, engine, gameObjects) {
|
||||
Parent.call(this, uid, engine, gameObjects);
|
||||
|
|
@ -15,7 +15,7 @@ function (Parent, Settings, fs) {
|
|||
Level.prototype.loadLevelDataFromPath = function (path, callback) {
|
||||
// overwriting parent
|
||||
|
||||
fs.readFile(path, "utf8", function (err, data) {
|
||||
FileSystem.readFile(path, "utf8", function (err, data) {
|
||||
if (err) throw err;
|
||||
callback(JSON.parse(data));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ define(function() {
|
|||
CHANNEL_END_ROUND_TIME: 4, //10,
|
||||
CHANNEL_DEFAULT_MAX_USERS: 40,
|
||||
CHANNEL_DEFAULT_SCORE_LIMIT: 10,
|
||||
CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug'],
|
||||
CHANNEL_DEFAULT_LEVELS: ['debug'],
|
||||
|
||||
// ME STATE
|
||||
ME_STATE_MAX_DIFFERENCE_METERS: 1,
|
||||
|
|
|
|||
|
|
@ -3,10 +3,11 @@ define([
|
|||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/Validate",
|
||||
"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) {
|
||||
this.coordinator = coordinator;
|
||||
|
|
@ -31,12 +32,15 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
|
|||
|
||||
switch(command) {
|
||||
case "getChannels":
|
||||
output = this.coordinator.getChannels();
|
||||
output = this.getChannels();
|
||||
break;
|
||||
case "createChannel":
|
||||
// FIXME: sanitize input
|
||||
output = this.createChannel(message.options);
|
||||
break;
|
||||
case "getMaps":
|
||||
output = this.getMaps();
|
||||
break;
|
||||
default:
|
||||
this.isError = true;
|
||||
output = "Command not found";
|
||||
|
|
@ -46,6 +50,10 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
|
|||
this.output = output;
|
||||
}
|
||||
|
||||
Api.prototype.getChannels = function() {
|
||||
return this.coordinator.getChannels();
|
||||
};
|
||||
|
||||
Api.prototype.createChannel = function(options) {
|
||||
|
||||
var allowedOptionKeys = [
|
||||
|
|
@ -73,7 +81,7 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
|
|||
}
|
||||
|
||||
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;
|
||||
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 + ").";
|
||||
}
|
||||
|
||||
|
||||
|
||||
var defaultOptions = {
|
||||
maxUsers: Settings.CHANNEL_DEFAULT_MAX_USERS,
|
||||
minUsers: 0,
|
||||
|
|
@ -114,7 +120,6 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
|
|||
|
||||
options = Options.merge(options, defaultOptions);
|
||||
|
||||
|
||||
var result = this.coordinator.createChannel(options);
|
||||
if(result !== false) {
|
||||
return result;
|
||||
|
|
@ -134,14 +139,24 @@ function (Nc, ProtocolHelper, validate, Options, Settings) {
|
|||
}
|
||||
|
||||
return JSON.stringify(output);
|
||||
|
||||
};
|
||||
|
||||
Api.prototype.getContentType = function() {
|
||||
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;
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,10 @@ input, button {
|
|||
display: none;
|
||||
}
|
||||
|
||||
#createform #maps {
|
||||
text-transform: capitalize;
|
||||
}
|
||||
|
||||
article#menu {
|
||||
margin: 4em auto;
|
||||
background: #1a1a1a;
|
||||
|
|
|
|||
|
|
@ -21,12 +21,12 @@
|
|||
<p><label>Score limit:<br> <input id="scoreLimit" type="number" value="5"></label></p>
|
||||
<fieldset>
|
||||
<legend>Maps</legend>
|
||||
<ul>
|
||||
<ul id="maps">
|
||||
<li>
|
||||
<label><input name="maps" value="debug" type="checkbox" checked> Debug</label>
|
||||
</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>
|
||||
</ul>
|
||||
</fieldset>
|
||||
|
|
|
|||
|
|
@ -41,6 +41,23 @@ if(!Chuck) var Chuck = {};
|
|||
|
||||
var lastRefreshResponse;
|
||||
function refresh(callback) {
|
||||
|
||||
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();
|
||||
xhr.onreadystatechange = function() {
|
||||
|
|
@ -69,9 +86,37 @@ if(!Chuck) var Chuck = {};
|
|||
} catch(e) {
|
||||
console.error(e)
|
||||
}
|
||||
*/
|
||||
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) {
|
||||
var html = "";
|
||||
if(list.length > 0) {
|
||||
|
|
@ -89,6 +134,24 @@ if(!Chuck) var Chuck = {};
|
|||
$("#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) {
|
||||
try {
|
||||
var nickname = $("#nick").value;
|
||||
|
|
@ -293,6 +356,16 @@ if(!Chuck) var Chuck = {};
|
|||
|
||||
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();
|
||||
xhr.onreadystatechange = function() {
|
||||
if(xhr.readyState == 4) {
|
||||
|
|
@ -308,11 +381,13 @@ if(!Chuck) var Chuck = {};
|
|||
}
|
||||
xhr.open("POST", "/api", true);
|
||||
xhr.send(JSON.stringify({command:"createChannel", options: options}));
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
$("#refresh").onclick = refresh;
|
||||
refresh();
|
||||
populateMaps();
|
||||
var channelDestructionTimeout = null;
|
||||
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
Loading…
Add table
Add a link
Reference in a new issue