work in progress... worked on channel attributes and game goal. fixes #45 and references #48

This commit is contained in:
logsol 2014-03-30 00:12:04 +01:00
parent 039213cf50
commit 55256ada95
23 changed files with 409 additions and 110 deletions

View file

@ -1,9 +1,12 @@
define([
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Protocol/Helper"
"Lib/Utilities/Protocol/Helper",
"Lib/Utilities/Validate",
"Lib/Utilities/Options",
"Game/Config/Settings"
],
function (Nc, ProtocolHelper) {
function (Nc, ProtocolHelper, validate, Options, Settings) {
function Api(coordinator) {
this.coordinator = coordinator;
@ -14,15 +17,16 @@ function (Nc, ProtocolHelper) {
Api.prototype.handleCall = function(queryParameters) {
var command,
output = null;
output = null;
try {
var message = JSON.parse(queryParameters);
command = message.command;
} catch(e) {
this.isError = true;
output = "JSON syntax error";
console.error(e)
this.output = "JSON syntax error";
console.error(e);
return;
}
switch(command) {
@ -43,6 +47,74 @@ function (Nc, ProtocolHelper) {
}
Api.prototype.createChannel = function(options) {
var allowedOptionKeys = [
"channelName",
"maps",
"maxUsers",
"minUsers",
"scoreLimit"
];
var newOptions = {};
// Channelname
if(validate(options.channelName, {type: 'string', minLength: 3, maxLength: 30})) {
newOptions.channelName = options.channelName;
} else {
this.isError = true;
return "Could not create channel, invalid channel name.";
}
// Maps / levelUids
if( ! validate(options.levelUids, {type: 'array', minLength: 1, maxLength: 999})) {
this.isError = true;
return "Could not create channel, invalid maps.";
}
for(var i = 0; i < options.levelUids.length; i++) {
if(!validate(options.levelUids[i], {type: 'string', in: ['stones2', 'debug']})) {
this.isError = true;
return "Could not create channel, invalid map (" + options.levelUids[i] + ").";
}
}
newOptions.levelUids = options.levelUids;
// Users
if(validate(options.maxUsers, {optional: true, type: 'number', min: 1, max: Settings.CHANNEL_MAX_USERS})) {
newOptions.maxUsers = options.maxUsers;
} else {
this.isError = true;
return "Could not create channel, Max users invalid. (Limited to " + Settings.CHANNEL_MAX_USERS + " users)";
}
if(validate(options.minUsers, {optional: true, type: 'number', min: 0, max: Settings.CHANNEL_MAX_USERS})) {
newOptions.minUsers = options.minUsers;
} else {
this.isError = true;
return "Could not create channel, Max users too high. Limited to: " + Settings.CHANNEL_MAX_USERS;
}
// Limits
if(validate(options.scoreLimit, {type: 'number', min: 1, max: 999})) {
newOptions.scoreLimit = options.scoreLimit;
} else {
this.isError = true;
return "Could not create channel, score limit (" + options.scoreLimit + ").";
}
var defaultOptions = {
maxUsers: Settings.CHANNEL_DEFAULT_MAX_USERS,
minUsers: 0,
scoreLimit: Settings.CHANNEL_DEFAULT_SCORE_LIMIT
};
options = Options.merge(options, defaultOptions);
var result = this.coordinator.createChannel(options);
if(result !== false) {
return result;
@ -56,6 +128,11 @@ function (Nc, ProtocolHelper) {
var output = {};
var key = this.isError ? "error" : "success";
output[key] = this.output;
if(this.isError) {
console.warn("API Error: " + this.output);
}
return JSON.stringify(output);
};
@ -63,6 +140,8 @@ function (Nc, ProtocolHelper) {
Api.prototype.getContentType = function() {
return "application/json";
};
return Api;