Skip to content

Commit

Permalink
Merge branch 'cli'
Browse files Browse the repository at this point in the history
  • Loading branch information
dunstad committed Feb 4, 2020
2 parents f6bedfc + 0d66df6 commit aa06895
Show file tree
Hide file tree
Showing 26 changed files with 458 additions and 339 deletions.
33 changes: 13 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"package-all": "electron-packager . --overwrite --all --icon=public/assets/placeholder_icon.icns --prune=true --out=release-builds"
},
"dependencies": {
"ajv": "^5.2.2",
"ajv": "^6.10.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.15.2",
"cookie-parser": "~1.4.3",
Expand Down
2 changes: 1 addition & 1 deletion public/js/client/GUI.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export class GUI {

let startPointWorld = startPoint.world();
let endPointWorld = endPoint.world();
let commandParameters = [startPointWorld.x, startPointWorld.y, startPointWorld.z, endPointWorld.x, endPointWorld.y, endPointWorld.z, selectionIndex, scanLevel];
let commandParameters = [startPointWorld.x, startPointWorld.y, startPointWorld.z, endPointWorld.x, endPointWorld.y, endPointWorld.z, scanLevel, selectionIndex];
this.sendCommand(commandName, commandParameters);

this.selectStart.clear();
Expand Down
1 change: 1 addition & 0 deletions public/js/server/SocketToAccountMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SocketToAccountMap {
* @returns {boolean}
*/
removeClient(accountName, clientSocket) {
clientSocket.removeAllListeners();
var result = false;
var account = this.accounts[accountName];
if (account && account.clients) {
Expand Down
125 changes: 84 additions & 41 deletions public/js/server/TestClient.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const net = require('net');
const InventoryData = require('../shared/InventoryData');
const MapData = require('../shared/MapData');
const validators = require('../shared/fromRobotSchemas.js');
const keyToValidatorMap = require('../shared/fromRobotSchemas.js').keyToValidatorMap;

/**
* Used to make sure the server is working properly. Attempts to replicate
Expand Down Expand Up @@ -32,7 +32,12 @@ class TestClient {
this.equipped;
this.map = new MapData();
this.map.setFromMapData(this.testData.map);
this.position = this.testData.position;
this.position = {
x: this.testData.config.posX,
y: this.testData.config.posY,
z: this.testData.config.posZ,
};
this.config = this.testData.config;
this.map.set(this.position.x, this.position.y, this.position.z, {"hardness": 2});
this.components = this.testData.components;

Expand All @@ -57,12 +62,12 @@ class TestClient {

this.commandMap = {

scanArea: (scanLevel)=>{
scanArea: (scanLevel, times)=>{
for (let i = -2; i <= 5; i++) {
let scan = this.geolyzerScan(-3, -3, i, 8, 8, 1);
this.send('map data', scan);
}
this.sendWithCost('command result', [true, 'area scanned']);
this.sendWithCost('command result', ['map data', true]);
},

viewInventory: ()=>{
Expand All @@ -71,11 +76,11 @@ class TestClient {
this.send('inventory data', inventoryMeta);

for (let slotNum in this.inventory.slots) {
let slotData = this.inventory.serializeSlot(slotNum);
let slotData = this.inventory.serializeSlot(parseInt(slotNum));
this.send('slot data', slotData);
}

this.sendWithCost('command result', [true, 'all slot data sent']);
this.sendWithCost('command result', ['inventory data', true]);

},

Expand All @@ -89,31 +94,56 @@ class TestClient {
let slotData = this.inventory.serializeSlot(this.inventory.selected);
this.send('slot data', slotData);

this.sendWithCost('command result', [true, {label: 'test object', size: 4}]);
this.sendWithCost('command result', ['equip', {label: 'test object', size: 4}]);

},

dig: (x1, y1, z1, x2, y2, z2, selectionIndex, scanLevel)=>{
dig: (x1, y1, z1, x2, y2, z2, relative, scanLevel, selectionIndex,)=>{
if (relative) {
x1 += this.position.x;
y1 += this.position.y;
z1 += this.position.z;
x2 += this.position.x;
y2 += this.position.y;
z2 += this.position.z;
}
let points = this.getBoxPoints(x1, y1, z1, x2, y2, z2);
for (let point of points) {
this.dig(point.x, point.y, point.z);
this.send('dig success', point);
}
this.send('delete selection', selectionIndex);
this.sendWithCost('command result', [true, 'digging done']);
if (selectionIndex !== undefined) {
this.send('delete selection', selectionIndex);
}
this.sendWithCost('command result', ['dig', true]);
},

place: (x1, y1, z1, x2, y2, z2, selectionIndex, scanLevel)=>{
place: (x1, y1, z1, x2, y2, z2, relative, scanLevel, selectionIndex,)=>{
if (relative) {
x1 += this.position.x;
y1 += this.position.y;
z1 += this.position.z;
x2 += this.position.x;
y2 += this.position.y;
z2 += this.position.z;
}
let points = this.getBoxPoints(x1, y1, z1, x2, y2, z2);
for (let point of points) {
let blockData = this.place(point.x, point.y, point.z);
this.send('block data', blockData);
}
this.send('delete selection', selectionIndex);
this.sendWithCost('command result', [true, 'placing done']);
if (selectionIndex !== undefined) {
this.send('delete selection', selectionIndex);
}
this.sendWithCost('command result', ['place', true]);
},

move: (x, y, z, scanLevel)=>{
move: (x, y, z, relative, scanLevel)=>{
if (relative) {
x += this.position.x;
y += this.position.y;
z += this.position.z;
}
let result = this.move(x, y, z);
if (result) {
this.commandMap.scanArea();
Expand All @@ -126,26 +156,36 @@ class TestClient {
}
},

interact: (x, y, z, scanLevel)=>{
interact: (x, y, z, relative, scanLevel)=>{
if (relative) {
x += this.position.x;
y += this.position.y;
z += this.position.z;
}
let blockData = this.inspect(x, y, z);
if (blockData.name == 'minecraft:chest') {
this.send('inventory data', this.testData.externalInventory.meta);
for (let slotNum in this.inventories[3].slots) {
let slot = this.inventories[3].serializeSlot(slotNum);
let slot = this.inventories[3].serializeSlot(parseInt(slotNum));
this.send('slot data', slot);
}
}
this.sendWithCost('command result', [true, 'interact complete']);
this.sendWithCost('command result', ['interact', true]);
},

inspect: (x, y, z, scanLevel)=>{
inspect: (x, y, z, relative, scanLevel)=>{
if (relative) {
x += this.position.x;
y += this.position.y;
z += this.position.z;
}
let blockData = this.inspect(x, y, z);
this.sendWithCost('block data', blockData);
},

select: (slotNum)=>{
this.select(slotNum);
this.sendWithCost('command result', [true, 'select complete']);
this.sendWithCost('command result', ['select', true]);
},

transfer: (fromSlotIndex, fromSide, toSlotIndex, toSide, amount)=>{
Expand All @@ -169,20 +209,20 @@ class TestClient {
let toSlotData = toInv.serializeSlot(toSlotIndex);
this.send('slot data', toSlotData);

this.sendWithCost('command result', [true, "transfer successful"]);
this.sendWithCost('command result', ['transfer', true]);
}
else {
this.sendWithCost('command result', [false, "transfer failed"]);
this.sendWithCost('command result', ['transfer', false]);
}

},

craft: (itemName)=>{
this.sendWithCost('command result', [false, 'crafting not implemented']);
this.sendWithCost('command result', ['craft', 'crafting not implemented']);
},

raw: (commandString)=>{
let resultData = [true, 'received command: ' + commandString];
let resultData = ['raw', 'received command: ' + commandString];
this.sendWithCost('command result', resultData);
},

Expand All @@ -196,6 +236,21 @@ class TestClient {
this.sendWithCost('available components', components);
},

config: (optionName, optionValue)=>{
if (optionName && (optionValue !== undefined)) {
this.config[optionName] = optionValue;
this.sendWithCost('command result', ['config', true]);
}
else if (optionName) {
let result = {};
result[optionName] = this.config[optionName];
this.sendWithCost('config', result);
}
else {
this.sendWithCost('config', this.config);
}
},

};

this.socket.on('data', (rawMessages)=>{
Expand Down Expand Up @@ -458,21 +513,7 @@ class TestClient {
* @param {any} value
*/
validate(key, value) {
let keyToValidatorMap = {
'inventory data': validators.inventoryMeta,
'slot data': validators.inventorySlot,
'command result': validators.commandResult,
'robot position': validators.position,
'available components': validators.components,
'map data': validators.geolyzerScan,
'id': validators.id,
'message': validators.message,
'power level': validators.powerLevel,
'dig success': validators.digSuccess,
'delete selection': validators.deleteSelection,
'block data': validators.blockData,
};
keyToValidatorMap[key](value);
return keyToValidatorMap[key](value);
}

/**
Expand All @@ -482,7 +523,9 @@ class TestClient {
*/
send(key, value) {

this.validate(key, value);
if (!this.validate(key, value)) {
throw Error(`command ${key} failed to validate with value ${JSON.stringify(value)}`);
};

const data = {
[key]: value,
Expand Down Expand Up @@ -520,14 +563,14 @@ class TestClient {
* so it can be checked in the unit tests.
*/
getID() {
return {robot: this.testData.robotName, account: this.testData.accountName};
return {robot: this.testData.config.robotName, account: this.testData.config.accountName};
}

/**
* Used to identify the test client to the server and open the socket connection.
*/
connect() {
this.socket.connect(this.testData.port, this.testData.host);
this.socket.connect(this.testData.config.tcpPort, this.testData.config.serverIP);
}

/**
Expand Down
Loading

0 comments on commit aa06895

Please sign in to comment.