Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function executeCommand(console, command)
{
// tokenize the command string
let tokens = command.match(/'.*?'|".*?"|\S+/g);
if (tokens == null)
return;
for (let i = 0; i < tokens.length; ++i) {
tokens[i] = tokens[i].replace(/'(.*)'/, "$1");
tokens[i] = tokens[i].replace(/"(.*)"/, "$1");
}
let objectName = tokens[0];
let instruction = tokens[1];
// check that the instruction is valid
if (!from.Array(console.commands)
.any(it => it.entity == objectName))
{
console.log(`unrecognized object name '${objectName}'`);
return;
}
if (tokens.length < 2) {
console.log(`missing instruction for '${objectName}'`);
return;
}
if (!from.Array(console.commands)
.where(it => it.entity == objectName)
.any(it => it.instruction == instruction))
{
console.log(`instruction '${instruction}' not valid for '${objectName}'`);
return;
}
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
'use strict';
const from = require('from');
// historically, Sphere requires a gamepad with at least 2 axes (X/Y) and
// 5 buttons (A, B, X, Y, Start) for full operation.
const Devices = from.Array(Joystick.getDevices())
.where(it => it.numAxes >= 2)
.where(it => it.numButtons >= 5)
.toArray();
class Joypad
{
constructor()
{
throw new TypeError(`'${new.target.name}' is a static class and cannot be instantiated`);
}
static get P1() { return Devices[0] || Joystick.Null; }
static get P2() { return Devices[1] || Joystick.Null; }
static get P3() { return Devices[2] || Joystick.Null; }
static get P4() { return Devices[3] || Joystick.Null; }
}
}
var entity = tokens[0];
var instruction = tokens[1];
// check that the instruction is valid
if (!from.Array(commands)
.any(function(c) { return entity == c.entity; }))
{
log("unrecognized object name '" + entity + "'");
return;
}
if (tokens.length < 2) {
log("missing instruction for '" + entity + "'");
return;
}
if (!from.Array(commands)
.where(function(c) { return entity == c.entity; })
.any(function(c) { return instruction == c.instruction; }))
{
log("instruction '" + instruction + "' not valid for '" + entity + "'");
return;
}
// parse arguments
for (var i = 2; i < tokens.length; ++i) {
var maybeNumber = parseFloat(tokens[i]);
tokens[i] = !isNaN(maybeNumber) ? maybeNumber : tokens[i];
}
// execute the command
from.Array(commands)
.where(function(c) { return entity == c.entity; })
function stop()
{
from.Array(tasks).each(function(tid) {
Thread.kill(tid);
});
};
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
'bool', 'raw',
];
const Attributes = {
'fstring': [ 'length' ],
'raw': [ 'size' ],
};
for (const key of Object.keys(desc)) {
let fieldDesc = desc[key];
let fieldType = fieldDesc.type;
if (!from.Array(FieldTypes).any(it => it === fieldType))
throw new TypeError(`invalid field type '${fieldType}'`);
if (fieldType in Attributes) {
let haveAttributes = from.Array(Attributes[fieldType])
.all(it => it in fieldDesc);
if (!haveAttributes)
throw new TypeError(`missing attributes for '${fieldType}'`);
}
}
}
static kill(threadID)
{
from.Array(threads)
.where(it => it.id == threadID)
.besides(it => it.isValid = false)
.remove();
}
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
'bool', 'raw',
];
const Attributes = {
'fstring': [ 'length' ],
'raw': [ 'size' ],
};
for (const key of Object.keys(desc)) {
let fieldDesc = desc[key];
let fieldType = fieldDesc.type;
if (!from.Array(FieldTypes).any(it => it === fieldType))
throw new TypeError(`invalid field type '${fieldType}'`);
if (fieldType in Attributes) {
let haveAttributes = from.Array(Attributes[fieldType])
.all(it => it in fieldDesc);
if (!haveAttributes)
throw new TypeError(`missing attributes for '${fieldType}'`);
}
}
}
const FieldTypes = [
'float32be', 'float32le', 'float64be', 'float64le',
'int8', 'int16be', 'int16le', 'int32be', 'int32le',
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
'bool', 'raw',
];
const Attributes = {
'fstring': [ 'length' ],
'raw': [ 'size' ],
};
for (const key of Object.keys(desc)) {
let fieldDesc = desc[key];
let fieldType = fieldDesc.type;
if (!from.Array(FieldTypes).any(it => it === fieldType))
throw new TypeError(`invalid field type '${fieldType}'`);
if (fieldType in Attributes) {
let haveAttributes = from.Array(Attributes[fieldType])
.all(it => it in fieldDesc);
if (!haveAttributes)
throw new TypeError(`missing attributes for '${fieldType}'`);
}
}
}
function updateAllThreads()
{
let activeThreads = from.Array(threads.slice())
.where(it => it.isValid && !it.isBusy)
let threadsEnding = [];
for (let thread of activeThreads) {
let lastSelf = currentSelf;
thread.isBusy = true;
currentSelf = thread.id;
let isRunning = thread.updater(thread.id);
if (thread.inputHandler !== undefined && isRunning)
thread.inputHandler();
currentSelf = lastSelf;
thread.isBusy = false;
if (!isRunning)
threadsEnding.push(thread.id);
}
for (let threadID of threadsEnding)
Thread.kill(threadID);
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
'bool', 'raw',
];
const Attributes = {
'fstring': [ 'length' ],
'raw': [ 'size' ],
};
for (const key of Object.keys(desc)) {
let fieldDesc = desc[key];
let fieldType = fieldDesc.type;
if (!from.Array(FieldTypes).any(it => it === fieldType))
throw new TypeError(`invalid field type '${fieldType}'`);
if (fieldType in Attributes) {
let haveAttributes = from.Array(Attributes[fieldType])
.all(it => it in fieldDesc);
if (!haveAttributes)
throw new TypeError(`missing attributes for '${fieldType}'`);
}
}
}