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

@ -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;