Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
c3093f0
added uuidManager.js
shundroid Feb 5, 2017
d8f4340
added stateChange callback
shundroid Feb 5, 2017
760d49e
made publisher.js
shundroid Feb 5, 2017
43cb737
added publisher's test
shundroid Feb 5, 2017
2c24372
added .travis.yml
shundroid Feb 5, 2017
ccd5ef0
sphero-websocket to optional
shundroid Feb 5, 2017
01f418c
supported eventPublisher in dashboard.js
shundroid Feb 6, 2017
13a2622
supported es6 scoreboard
shundroid Feb 6, 2017
fab8c07
added componentBase.js
shundroid Feb 6, 2017
bd63afb
made spheroServerManager.js
shundroid Feb 6, 2017
f297b6e
use componentBase in dashboard.js, scoreboard.js
shundroid Feb 6, 2017
20fcaf8
fixed asserting
shundroid Feb 6, 2017
40db315
made virtualSpheroManager.js
shundroid Feb 7, 2017
1ed9d81
comment out spheroServerManager.test.js
shundroid Feb 7, 2017
baeac23
fixed tests
shundroid Feb 7, 2017
ae28aa2
pasted badge
shundroid Feb 7, 2017
5facfcc
remove bind
shundroid Feb 7, 2017
ad1e49f
fixed components
shundroid Feb 7, 2017
d3c9ba1
made controllerManager
shundroid Feb 7, 2017
9b14589
fixed virtualSpheroManager.js
shundroid Feb 7, 2017
1af894a
fixed commandRunner
shundroid Feb 7, 2017
58b05d6
added appModel.js
shundroid Feb 8, 2017
c8b2fd3
added orbModel / orbController
shundroid Feb 10, 2017
36a592e
fixed commandRunner
shundroid Feb 10, 2017
ecdea2a
fixed controllerManager.js
shundroid Feb 10, 2017
0f988cb
fixed virtualSpheroManager.test.js
shundroid Feb 10, 2017
e84f393
added warn
shundroid Feb 10, 2017
5b20aa4
fixed error
shundroid Feb 10, 2017
f2356c7
fixed typeof
shundroid Feb 10, 2017
d39c067
added defaultColor to config
shundroid Feb 10, 2017
4e32d70
fixed orbController
shundroid Feb 11, 2017
f17e4a7
uuidManager -> uuidModel
shundroid Feb 11, 2017
799e653
made main.js smaller
shundroid Feb 11, 2017
c59e2f9
fixed rankingMaker to component
shundroid Feb 15, 2017
334c805
named to controllerManager
shundroid Feb 15, 2017
3a4c5a1
removed reconnect
shundroid Feb 15, 2017
0d7c58f
cleaned main.js
shundroid Feb 15, 2017
04fc578
fixed dashboard link -> updateLink
shundroid Feb 15, 2017
19832b9
fixed passing over spheroWS timing
shundroid Feb 15, 2017
b4a9e96
fixed orbController
shundroid Feb 15, 2017
f0bfb8c
fixed to subscribe color at controllerManager
shundroid Feb 15, 2017
3e57963
fixed removeOrb -> removedOrb
shundroid Feb 15, 2017
507a75c
added tests
shundroid Feb 15, 2017
c8c0115
fixed commandRunner
shundroid Feb 16, 2017
4f5596f
set collisionConfig in config.js
shundroid Feb 16, 2017
f35c98a
key -> name
shundroid Feb 16, 2017
08a594a
fixed command method's test
shundroid Feb 16, 2017
78c5670
testで衝突しにくい形に
shundroid Feb 16, 2017
c770246
fixed that each test clears observeFunctions before test
shundroid Feb 17, 2017
b1bf606
fixed timeoutDelay to const
shundroid Feb 18, 2017
46f7553
fixed assert's timing at componentBase.subscribe
shundroid Feb 18, 2017
1c53e5a
fixed to new config of collision
shundroid Feb 19, 2017
0900c7a
Object.keys -> for in
shundroid Feb 19, 2017
c0317bf
fixed dashboard's test
shundroid Feb 19, 2017
f17894a
split before, beforeEach
shundroid Feb 19, 2017
6afd545
fixed #63
shundroid Feb 19, 2017
106c96c
fixed uuidModel
shundroid Feb 19, 2017
55dbd79
log -> logAsClientMessage
shundroid Feb 19, 2017
9ae5d8e
console.error -> throw
shundroid Feb 19, 2017
6bac2e4
uuidModel -> uuidManager
shundroid Feb 19, 2017
4103ba6
console.error wrap to connector
shundroid Feb 19, 2017
7be4cbd
改行を修正
shundroid Feb 20, 2017
c0f8b27
supported testing of spheroServerManager
shundroid Feb 20, 2017
ffdadcf
let 抜けを修正
shundroid Feb 20, 2017
9ff07a9
updated null check
shundroid Feb 22, 2017
6a4766b
fixed connector's bug
shundroid Feb 24, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 6.6

173 changes: 86 additions & 87 deletions commandRunner.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,100 @@
var EventEmitter = require("events").EventEmitter;
var util = require("util");
import ComponentBase from "./componentBase";

function CommandRunner(key) {
EventEmitter.call(this);
export default class CommandRunner extends ComponentBase {
constructor(name) {
super();

this.key = key;
this.commands = [];
this.baseSpeed = 0;
this.angle = 0;
this.loopTimeoutId = null;
this.customTimeoutIds = {};
this.backgroundTimeoutIds = {};
this.commandFunctions = {
rotate: (config, turn) => {
if (config.isBuiltIn) {
this.stopLoop();
this.clearCustomTimeoutIds();
this.name = name;
this.commands = [];
this.baseSpeed = 0;
this.angle = 0;
this.loopTimeoutId = null;
this.customTimeoutIds = {};
this.backgroundTimeoutIds = {};
this.commandFunctions = {
rotate: (config, turn) => {
if (config.isBuiltIn) {
this.stopLoop();
this.clearCustomTimeoutIds();
}
const rotateFunction = () => {
this.angle = (this.angle + turn) % 360;
this.publish("command", name, "roll", [0, this.angle]);
this.customTimeoutIds.rotate = setTimeout(rotateFunction, 500);
};
rotateFunction();
},
stop: config => {
if (config.isBuiltIn) {
this.stopLoop();
this.clearCustomTimeoutIds();
}
this.publish("command", name, "roll", [0, this.angle]);
},
dash: (config, baseSpeed, dashTime) => {
if (this.backgroundTimeoutIds.dash) {
clearTimeout(this.backgroundTimeoutIds.dash);
}
this.baseSpeed = baseSpeed;
this.backgroundTimeoutIds.dash = setTimeout(() => {
this.baseSpeed = 0;
}, dashTime * 1000);
},
roll: (config, speed, degree) => {
this.publish("command", name, "roll", [
this.baseSpeed + speed,
(360 + degree + this.angle) % 360
]);
}
const rotateFunction = () => {
this.angle = (this.angle + turn) % 360;
this.emit("command", "roll", [0, this.angle]);
this.customTimeoutIds.rotate = setTimeout(rotateFunction, 500);
};
rotateFunction();
},
stop: config => {
if (config.isBuiltIn) {
this.stopLoop();
this.clearCustomTimeoutIds();
}
this.emit("command", "roll", [0, this.angle]);
},
dash: (config, baseSpeed, dashTime) => {
if (this.backgroundTimeoutIds.dash !== null) {
clearTimeout(this.backgroundTimeoutIds.dash);
}
this.baseSpeed = baseSpeed;
this.backgroundTimeoutIds.dash = setTimeout(() => {
this.baseSpeed = 0;
}, dashTime * 1000);
},
roll: (config, speed, degree) => {
this.emit("command", "roll", [
this.baseSpeed + speed,
(360 + degree + this.angle) % 360
]);
}
};
}

util.inherits(CommandRunner, EventEmitter);
};
}

CommandRunner.prototype.setCommands = function(commands) {
if (commands.length === 1 && commands[0].time === -1) {
// built-in command
this.commandFunctions[commands[0].commandName].apply(this, [{
isBuiltIn: true
}].concat(processArguments(commands[0].args)));
} else {
this.commands = commands;
this.stopLoop();
this.clearCustomTimeoutIds();
this.loopMethod(0);
setCommands(commands) {
if (commands.length === 1 && commands[0].time === -1) {
// built-in command
this.commandFunctions[commands[0].commandName].apply(this, [{
isBuiltIn: true
}].concat(processArguments(commands[0].args)));
} else {
this.commands = commands;
this.stopLoop();
this.clearCustomTimeoutIds();
this.loopMethod(0);
}
}
};

CommandRunner.prototype.stopLoop = function() {
if (this.loopTimeoutId !== null) {
clearTimeout(this.loopTimeoutId);
stopLoop() {
if (this.loopTimeoutId) {
clearTimeout(this.loopTimeoutId);
this.loopTimeoutId = null;
}
}
};

CommandRunner.prototype.clearCustomTimeoutIds = function() {
Object.keys(this.customTimeoutIds).forEach(timeoutIdName => {
const timeoutId = this.customTimeoutIds[timeoutIdName];
if (timeoutId !== null) {
clearTimeout(timeoutId);
clearCustomTimeoutIds() {
for (let timeoutIdName in this.customTimeoutIds) {
const timeoutId = this.customTimeoutIds[timeoutIdName];
if (timeoutId) {
clearTimeout(timeoutId);
delete this.customTimeoutIds[timeoutIdName];
}
}
});
}
}

CommandRunner.prototype.loopMethod = function(index) {
if (this.commands.length === 0) {
throw new Error("実行しようとしたcommandsは空でした。: " + this.key);
loopMethod(index) {
if (this.commands.length === 0) {
throw new Error("実行しようとしたcommandsは空でした。: " + this.name);
}
this.clearCustomTimeoutIds();
const currentCommand = this.commands[index];
this.commandFunctions[currentCommand.commandName].apply(this, [{
isBuiltIn: false
}].concat(processArguments(currentCommand.args)));
var nextIndex = index + 1 >= this.commands.length ? 0 : index + 1;
this.loopTimeoutId = setTimeout(() => {
this.loopMethod(nextIndex);
}, currentCommand.time * 1000);
}
this.clearCustomTimeoutIds();
const currentCommand = this.commands[index];
this.commandFunctions[currentCommand.commandName].apply(this, [{
isBuiltIn: false
}].concat(processArguments(currentCommand.args)));
var nextIndex = index + 1 >= this.commands.length ? 0 : index + 1;
this.loopTimeoutId = setTimeout(() => {
this.loopMethod(nextIndex);
}, currentCommand.time * 1000);
};
}

// コマンドの引数にmotionSpecialDataがあった場合、それを実際の値に変更する
function processArguments(args) {
Expand All @@ -111,5 +112,3 @@ function processArguments(args) {
});
}

module.exports = CommandRunner;

24 changes: 24 additions & 0 deletions componentBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import publisher from "./publisher";

export default class ComponentBase {
constructor(models = {}) {
for (let modelName in models) {
this[modelName] = models[modelName];
}
}
publish(subjectName, ...data) {
publisher.publish(this, subjectName, ...data);
}
subscribeModel(subjectName, observeFunction) {
publisher.subscribeModel(subjectName, (author, ...data) => {
observeFunction.apply(this, data);
});
}
subscribe(subjectName, observeFunction) {
publisher.subscribe(subjectName, (author, ...data) => {
if (author !== this) {
observeFunction.apply(this, data);
}
});
}
}
14 changes: 13 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@ export default {
wsPort: 8081
},
dashboardPort: 8082,
scoreboardPort: 8083
scoreboardPort: 8083,
defaultHp: 100,
damage: 10,
defaultColor: "white",
collision: {
meth: 0x01,
xt: 0x20,
xs: 0x20,
yt: 0x20,
ys: 0x20,
dead: 0x02
},
isUseNoble: false
};
33 changes: 31 additions & 2 deletions connector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
class Connector {
constructor() {
import ComponentBase from "./componentBase";

class Connector extends ComponentBase {
constructor(models) {
super(models);

this.rawOrbs = {};

this.originalError = console.error;

console.error = (message) => {
const exec121Error = /Error: Opening (\\\\\.\\)?(.+): Unknown error code (121|1167)/.exec(message);
if (exec121Error) {
const port = exec121Error[2];
if (this.isConnecting(port)) {
this.publish("incrementError121Count");
if (this.appModel.error121Count < 5) {
this.publish("log", `Catched 121 error. Reconnecting... (${models.appModel.error121Count})`, "warning");
this.reconnect(port);
} else {
this.publish("resetError121Count")
this.publish("log", "Catched 121 error. But this is 5th try. Give up.", "warning");
this.giveUp(port);
}
} else {
this.publish("log", "Catched 121 error. But port is invalid.", "error");
}
} else {
this.publish("log", "Catched unknown error: \n" + message.toString(), "error");
}
this.originalError(message);
};
}
connect(port, rawOrb) {
if (this.isConnecting(port)) {
Expand Down
30 changes: 15 additions & 15 deletions controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ export default class Controller extends EventEmitter {
}
setHp(hp) {
this.hp = hp;
if (this.client !== null) {
if (this.client) {
this.client.sendCustomMessage("hp", this.hp);
}
this.emit("hp", this.hp);
}
setIsOni(isOni) {
this.isOni = isOni;
if (this.client !== null) {
if (this.client) {
this.client.sendCustomMessage("oni", this.isOni);
}
this.emit("oni", this.isOni);
}
setLink(orb) {
// client も持っているが、それに左右されずにするため link は別に持つ必要がある
this.linkedOrb = orb !== null ? orb : null;
if (this.client !== null) {
if (orb === null) {
this.linkedOrb = orb;
if (this.client) {
if (!orb) {
this.client.unlink();
} else {
this.client.setLinkedOrb(orb);
Expand All @@ -43,37 +43,37 @@ export default class Controller extends EventEmitter {
}
setClient(client) {
this.client = client;
if (this.client !== null) {
if (this.client) {
this.client.sendCustomMessage("hp", this.hp);
this.client.sendCustomMessage("oni", this.isOni);
this.client.sendCustomMessage("color", this.color);
}
if (this.linkedOrb !== null && this.client !== null) {
// HPなどの Orb -> Client への伝達で、
// client にも linkedOrb を入れておく必要がある。
this.client.setLinkedOrb(this.linkedOrb);
if (this.linkedOrb) {
// HPなどの Orb -> Client への伝達で、
// client にも linkedOrb を入れておく必要がある。
this.client.setLinkedOrb(this.linkedOrb);
}
}
}
setColor(color) {
this.color = color;
updateColor.call(this);
if (this.client !== null) {
if (this.client) {
this.client.sendCustomMessage("color", this.color);
}
}
getStates() {
return {
hp: this.hp,
isOni: this.isOni,
link: this.linkedOrb !== null ? this.linkedOrb.name : null,
key: this.client !== null ? this.client.key : null,
link: this.linkedOrb ? this.linkedOrb.name : null,
key: this.client ? this.client.key : null,
color: this.color
};
}
}

function updateColor() {
if (this.linkedOrb !== null) {
if (this.linkedOrb) {
this.linkedOrb.command("color", [this.color]);
}
}
Loading