Skip to content

Commit

Permalink
Make support.element case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
frizz925 committed Aug 13, 2017
1 parent ecc65d6 commit e9b2ddd
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 37 deletions.
5 changes: 5 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const gutil = require("gulp-util");
const babel = require("gulp-babel");
const rename = require("gulp-rename");
const nodemon = require("gulp-nodemon");
const shell = require("gulp-shell");
const livereload = require("gulp-livereload");
const FileCache = require("gulp-file-cache");
const resolve = require("path").resolve;
Expand Down Expand Up @@ -80,6 +81,10 @@ gulp.task("serve", ["build:server"], function() {
});
});

gulp.task("serve2", shell.task([
"concurrently \"gulp serve\" \"python controller/controller.py\""
]));

gulp.task("config", function() {
return gulp.src("./{server,controller,extension}/*.sample.{json,ini}")
.pipe(rename(function(path) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"gulp-livereload": "^3.8.1",
"gulp-nodemon": "^2.2.1",
"gulp-rename": "^1.2.2",
"gulp-shell": "^0.6.3",
"gutil": "^1.6.4",
"webpack": "^2.6.1",
"webpack-merge": "^4.1.0"
Expand Down
1 change: 1 addition & 0 deletions server/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ BotTimeoutInMins = 60
[Chatbot]
URL = http://localhost:49546
Token = <JWT token goes here>
UserId = <LINE user id goes here>

[Viramate]
QuickSkill = false
Expand Down
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const readScenario = (scenarioName) => {
return scenario;
};

new Server(readConfig(), (scenarioName) => {
new Server(readConfig(), __dirname, () => {
return new Promise((resolve, reject) => {
try {
const config = readConfig();
const scenario = readScenario(scenarioName || config.Server.Scenario);
const scenario = readScenario(config.Server.Scenario);
resolve({config, scenario});
} catch (err) {
reject(err);
Expand Down
44 changes: 34 additions & 10 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ import RaidQueue from "./server/extensions/RaidQueue";
import Chatbot from "./server/extensions/Chatbot";

export default class Server {
constructor(initConfig, configHandler, extensions) {
this.initConfig = initConfig;
this.configHandler = configHandler.bind(this);
constructor(initConfig, rootDir, configHandler, extensions) {
this.config = initConfig;
this.rootDir = rootDir;
this.configHandler = () => {
return new Promise((resolve, reject) => {
configHandler.apply(this).then((result) => {
this.refreshConfig(result.config);
resolve(result);
}, reject);
});
};
this.port = process.env.PORT || Number(initConfig.Server.ListenerPort);
this.refreshConfig(initConfig);

Expand All @@ -32,6 +40,7 @@ export default class Server {
};
this.sockets = {};

this.running = false;
this.app = Express();
this.server = http.Server(this.app);
this.io = SocketIO(this.server);
Expand Down Expand Up @@ -68,11 +77,19 @@ export default class Server {
}

refreshConfig(config) {
this.config = config;
this.controllerPort = Number(config.Server.ControllerPort);
this.timeout = Number(config.Server.ActionTimeoutInMs);
}

onConnect(socket) {
if (!this.running) this.running = true;

const errorHandler = (err) => {
console.error(err);
socket.disconnect();
};

console.log(`Client '${socket.id}' connected!`);
this.configHandler().then(({config, scenario}) => {
this.refreshConfig(config);
Expand All @@ -94,10 +111,8 @@ export default class Server {
};
this.makeRequest("start").then(() => {
worker.start(scenario);
}, (err) => {
console.error(err);
});
}, ::console.error);
}, errorHandler);
}, errorHandler);
}

getAction(socket, id) {
Expand Down Expand Up @@ -125,6 +140,8 @@ export default class Server {
}

onDisconnect(socket) {
if (this.running) this.running = false;

console.log(`Client '${socket.id}' disconnected!`);
this.makeRequest("stop").then(() => {
if (this.sockets[socket.id]) {
Expand All @@ -135,14 +152,20 @@ export default class Server {
});
}

sendAction(socket, actionName, payload, timeout) {
sendAction(realSocket, actionName, payload, timeout) {
timeout = timeout || this.timeout;
return new Promise((resolve, reject) => {
var resolved = false;
const id = shortid.generate();
const json = JSON.stringify(payload);
const expression = `${actionName}(${json})`;
const actions = this.sockets[socket.id].actions;
const socket = this.sockets[realSocket.id];
if (!socket) {
reject(new Error("Socket not found!"));
return;
}

const actions = socket.actions;
const done = () => {
resolved = true;
clearTimeout(actions[id].timer);
Expand Down Expand Up @@ -172,7 +195,7 @@ export default class Server {
data.type = "response";

console.log(`Socket: ${expression}`);
socket.emit("action", data);
realSocket.emit("action", data);
});
}

Expand All @@ -198,6 +221,7 @@ export default class Server {

stop() {
console.log("Stopping...");
if (this.running) this.running = false;
forEach(this.sockets, (socket) => {
socket.socket.disconnect();
});
Expand Down
6 changes: 2 additions & 4 deletions src/server/actions/chatbot.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import axios from "axios";

export default {
"chatbot": function(text) {
const port = this.config.Chatbot.ListenerPort;
return axios.post(`http://localhost:${port}/broadcast`, {
const chatbot = this.server.extensions.chatbot;
return chatbot.pushToUsers({
type: "text",
text
});
Expand Down
1 change: 1 addition & 0 deletions src/server/actions/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default {
});
},
"support.element": function(element, summonIds, party) {
element = element.toLowerCase().trim();
const elementId = elementIds[element];
return this.actions.merge(
["wait", supportScreenSelector, (next, actions) => {
Expand Down
55 changes: 36 additions & 19 deletions src/server/extensions/Chatbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ import axios from "axios";
import io from "socket.io-client";
import BaseExtension from "./BaseExtension";
import forEach from "lodash/forEach";
import commands from "./Chatbot/commands";

export default class Chatbot extends BaseExtension {
constructor() {
super();
this.prefix = "/";
this.commands = {
"th": ::this.treasureHunt
};
this.commands = {};
this.subscribers = new Set();
forEach(commands, (command, name) => {
this.commands[name] = command.bind(this);
});
}

onSetup(server) {
this.server = server;
const config = server.initConfig;

this.url = config.Chatbot.URL;
this.token = config.Chatbot.Token;
this.config = server.config;
this.url = this.config.Chatbot.URL;
this.token = this.config.Chatbot.Token;
this.http = axios.create({
baseURL: this.url + "/api/",
headers: {
Expand All @@ -31,6 +33,8 @@ export default class Chatbot extends BaseExtension {
});
this.socket.on("connect", ::this.onChatConnect);
this.socket.on("events", ::this.onChatEvents);

this.users = this.config.Chatbot.UserId.split(/[,\s]/);
}

onChatConnect() {}
Expand All @@ -50,20 +54,33 @@ export default class Chatbot extends BaseExtension {
const command = this.commands[trigger.substr(prefixLength)];
if (!command) return;
const arg = text.substr(triggerLength).trim();
command(arg, (message) => {
return this.http.post("/reply", {
token: event.replyToken,
message
});
});
command(this.createReplyCallback(event.replyToken), arg, event);
}
}

treasureHunt(code, reply) {
this.server.extensions.raidQueue.push(code);
reply({
type: "text",
text: "Raid added to queue"
});
createReplyCallback(replyToken) {
return {
text: (text) => {
return this.reply(replyToken, {type: "text", text});
}
};
}

broadcast(message) {
return this.http.post("/broadcast", {message});
}

reply(token, message) {
return this.http.post("/reply", {token, message});
}

pushToUsers(message) {
const users = this.users.slice();
const pushMessage = () => {
const user = users.pop();
if (!user) return;
this.http.post("/push", {to: user, message}).then(pushMessage, ::console.error);
};
pushMessage();
}
}
34 changes: 34 additions & 0 deletions src/server/extensions/Chatbot/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export default {
"th": function(reply, code) {
if (!this.server.running) {
return reply.text("Autopilot is not running");
}
if (this.config.Server.Scenario !== "Leech") {
return reply.text("Autopilot is not running in Leech mode");
}
this.server.extensions.raidQueue.push(code);
return reply.text("Raid added to the queue");
},
"subscribe": function(reply, arg, evt) {
if (!evt.source.userId) return;
const userId = evt.source.userId;
if (this.subscribers.has(userId)) {
return reply.text("You're already subscribed to the bot.");
}
this.subscribers.add(userId);
return reply.text("You're now subscribed to the bot.");
},
"unsubscribe": function(reply, arg, evt) {
if (!evt.source.userId) return;
const userId = evt.source.userId;
if (!this.subscribers.has(userId)) {
return reply.text("You're not subscribed to the bot.");
}
this.subscribers.delete(userId);
return reply.text("You're now unsubscribed from the bot.");
},
"user-id": function(reply, arg, evt) {
if (!evt.source.userId) return;
return reply.text("Your user ID is " + evt.source.userId);
}
};
7 changes: 5 additions & 2 deletions src/server/extensions/RaidQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export default class RaidQueue extends BaseExtension {
}

onSetup(server) {
server.app.get("/raid/reset", (req, res) => {
this.queue = [];
res.end("OK");
});
server.app.post("/raid", (req, res) => {
console.log("New raid: " + req.body);
this.push(req.body);
res.end();
res.end("OK");
});
}

Expand Down

0 comments on commit e9b2ddd

Please sign in to comment.