Skip to content

Commit

Permalink
Improve Pokemon Showdown library
Browse files Browse the repository at this point in the history
Add option to configure message lines restriction and Pokemon Showdown
login server
  • Loading branch information
AgustinSRG committed Sep 14, 2016
1 parent 645a47b commit c80746d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
9 changes: 3 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ChatBotApp {
loginserv: Default_Login_Server,
serverid: "showdown",
retrydelay: (10 * 1000),
maxlines: 3,
};
}

Expand Down Expand Up @@ -142,12 +143,8 @@ class ChatBotApp {
ShowdownBot = require(Path.resolve(__dirname, 'lib/showdown-sockjs.js')).Bot;
}

this.bot = new ShowdownBot(this.config.bot.server,
this.config.bot.port,
{loginServer: this.config.bot.loginserv,
serverid: this.config.bot.serverid},
{retryDelay: this.config.bot.retrydelay}
);
this.bot = new ShowdownBot(this.config.bot.server, this.config.bot.port, this.config.bot.serverid,
this.config.bot.loginserv, this.config.bot.maxlines, true, this.config.bot.retrydelay);

/* Create the server */
this.server = new Server(this.confDir, this.config.server);
Expand Down
37 changes: 23 additions & 14 deletions src/lib/showdown-sockjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ const Url = require('url');
const SockJS = require('sockjs-client');
const Https = require('https');

const Message_Sent_Delay = 2000; // Delay in order to avoid losing messages because of anti-flood system
const Default_Room = "lobby"; // If the server sends a message without specifying the room

const Default_Login_Server = "play.pokemonshowdown.com";
const Message_Sent_Delay = 2000;
const Default_Server_Id = "showdown";
const Default_Retry_Delay = 10 * 1000;
const Max_Lines = 3;
const Default_Room = "lobby";
const Max_Lines_Default = 3;

/**
* Represents a Pokemon Showdown Bot
Expand All @@ -38,20 +40,24 @@ class Bot {
/**
* @param {String} server - The Pokemon Showdown sever to connect
* @param {Number} port
* @param {Object} loginOptions - Login Options are: loginServer {String}, serverId {String}
* @param {Object} errOptions - Error Options are: retryDelay {Number}, ckeckInterval {Number}
* @param {String} loginServer - Default: play.pokemonshowdown.com
* @param {Number} maxLinesSend - Pokemon Showdown server lines per message restriction
* @param {Boolean} connectionRetry - true for retrying the connectio on disconnect
* @param {Number} connectionRetryDelay - miliseconds to wait before retrying the connection
*/
constructor(server, port, loginOptions, errOptions) {
constructor(server, port, serverId, loginServer, maxLinesSend, connectionRetry, connectionRetryDelay) {
this.server = server;
this.port = port;

this.loginOptions = loginOptions || null;
this.loginOptions = {};
this.loginOptions.serverId = serverId;
this.loginOptions.loginServer = loginServer;
this.loginUrl = {
loginServer: (loginOptions ? (loginOptions.loginServer || Default_Login_Server) : Default_Login_Server),
serverId: (loginOptions ? (loginOptions.serverId || "showdown") : "showdown"),
loginServer: (this.loginOptions.loginServer || Default_Login_Server),
serverId: (this.loginOptions.serverId || Default_Server_Id),
};

this.errOptions = errOptions || null;
this.maxLinesSend = maxLinesSend || Max_Lines_Default;

this.socket = null;
this.connecting = false;
Expand All @@ -68,14 +74,17 @@ class Bot {

this.connectionRetryTimer = null;

if (errOptions) {
this.errOptions = {};
this.errOptions.retryDelay = connectionRetryDelay;

if (connectionRetry) {
this.on('connectFailed', function () {
if (this.closed || this.connecting || this.status.connected) return;
this.retryConnect(errOptions.retryDelay || Default_Retry_Delay);
this.retryConnect(this.errOptions.retryDelay || Default_Retry_Delay);
}.bind(this));
this.on('disconnect', function () {
if (this.closed || this.connecting || this.status.connected) return;
this.retryConnect(errOptions.retryDelay || Default_Retry_Delay);
this.retryConnect(this.errOptions.retryDelay || Default_Retry_Delay);
}.bind(this));
}
}
Expand Down Expand Up @@ -387,7 +396,7 @@ class Bot {
send(data) {
if (!this.socket) return null;
let id = this.getSendId();
let manager = new SendManager(data, Max_Lines,
let manager = new SendManager(data, this.maxLinesSend,
function (msg) {
this.socket.send(msg);
this.events.emit('send', msg);
Expand Down
39 changes: 24 additions & 15 deletions src/lib/showdown-ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ const Url = require('url');
const WebSocketClient = require('websocket').client;
const Https = require('https');

const Message_Sent_Delay = 2000; // Delay in order to avoid losing messages because of anti-flood system
const Default_Room = "lobby"; // If the server sends a message without specifying the room
const Max_Idle_Interval = 2 * 60 * 1000; // To avoid websocket bugs causes a false active connection

const Default_Login_Server = "play.pokemonshowdown.com";
const Max_Idle_Interval = 2 * 60 * 1000;
const Message_Sent_Delay = 2000;
const Default_Server_Id = "showdown";
const Default_Retry_Delay = 10 * 1000;
const Max_Lines = 3;
const Default_Room = "lobby";
const Max_Lines_Default = 3;

/**
* Represents a Pokemon Showdown Bot
Expand All @@ -39,20 +41,24 @@ class Bot {
/**
* @param {String} server - The Pokemon Showdown sever to connect
* @param {Number} port
* @param {Object} loginOptions - Login Options are: loginServer {String}, serverId {String}
* @param {Object} errOptions - Error Options are: retryDelay {Number}, ckeckInterval {Number}
* @param {String} loginServer - Default: play.pokemonshowdown.com
* @param {Number} maxLinesSend - Pokemon Showdown server lines per message restriction
* @param {Boolean} connectionRetry - true for retrying the connectio on disconnect
* @param {Number} connectionRetryDelay - miliseconds to wait before retrying the connection
*/
constructor(server, port, loginOptions, errOptions) {
constructor(server, port, serverId, loginServer, maxLinesSend, connectionRetry, connectionRetryDelay) {
this.server = server;
this.port = port;

this.loginOptions = loginOptions || null;
this.loginOptions = {};
this.loginOptions.serverId = serverId;
this.loginOptions.loginServer = loginServer;
this.loginUrl = {
loginServer: (loginOptions ? (loginOptions.loginServer || Default_Login_Server) : Default_Login_Server),
serverId: (loginOptions ? (loginOptions.serverId || "showdown") : "showdown"),
loginServer: (this.loginOptions.loginServer || Default_Login_Server),
serverId: (this.loginOptions.serverId || Default_Server_Id),
};

this.errOptions = errOptions || null;
this.maxLinesSend = maxLinesSend || Max_Lines_Default;

this.connection = null;
this.connecting = false;
Expand All @@ -70,6 +76,9 @@ class Bot {
this.connectionCheckTimer = null;
this.connectionRetryTimer = null;

this.errOptions = {};
this.errOptions.retryDelay = connectionRetryDelay;

let webSocket = this.webSocket = new WebSocketClient();
webSocket.on('connectFailed', function (err) {
webSocket.abort();
Expand All @@ -87,14 +96,14 @@ class Bot {
this.events.emit('connect', connection);
}.bind(this));

if (errOptions) {
if (connectionRetry) {
this.on('connectFailed', function () {
if (this.closed || this.connecting || this.status.connected) return;
this.retryConnect(errOptions.retryDelay || Default_Retry_Delay);
this.retryConnect(this.errOptions.retryDelay || Default_Retry_Delay);
}.bind(this));
this.on('disconnect', function () {
if (this.closed || this.connecting || this.status.connected) return;
this.retryConnect(errOptions.retryDelay || Default_Retry_Delay);
this.retryConnect(this.errOptions.retryDelay || Default_Retry_Delay);
}.bind(this));
}
}
Expand Down Expand Up @@ -422,7 +431,7 @@ class Bot {
send(data) {
if (!this.connection) return null;
let id = this.getSendId();
let manager = new SendManager(data, Max_Lines,
let manager = new SendManager(data, this.maxLinesSend,
function (msg) {
this.connection.send(msg);
this.events.emit('send', msg);
Expand Down
9 changes: 9 additions & 0 deletions src/server/handlers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ App.server.setHandler('admin', (context, parts) => {
/* Save administration options */
let newPort = parseInt(context.post.port);
let newHttpsPort = parseInt(context.post.sslport);
let maxlines = parseInt(context.post.maxlines);
let loginserv = (context.post.loginserv || "").trim();

/* Check */
try {
check(!isNaN(newPort), "Invalid port.");
check(!isNaN(maxlines) && maxlines > 0, "Invalid lines restriction");
} catch (err) {
error = err.message;
}
Expand All @@ -78,6 +81,8 @@ App.server.setHandler('admin', (context, parts) => {
}
App.config.server.url = context.post.appurl || "";
App.config.apptitle = context.post.apptitle || "";
App.config.bot.loginserv = loginserv || "play.pokemonshowdown.com";
App.config.bot.maxlines = maxlines;
App.config.debug = !!context.post.debugmode;
App.config.useproxy = !!context.post.useproxy;
App.config.blockautodownload = !!context.post.blockautodownload;
Expand Down Expand Up @@ -131,6 +136,10 @@ App.server.setHandler('admin', (context, parts) => {
html += '<option value="sockjs"' + (App.config.websocketLibrary !== 'websocket' ? 'selected="selected"' : '') + '>SockJS</option>';
html += '<option value="websocket"' + (App.config.websocketLibrary === 'websocket' ? 'selected="selected"' : '') + '>Websocket</option>';
html += '</select></td></tr>';
html += '<tr><td><strong>Pokemon Showdown Login Server</strong>: </td><td><input type="text" name="loginserv" value="' +
(App.config.bot.loginserv || 'play.pokemonshowdown.com') + '" /></td></tr>';
html += '<tr><td><strong>Pokemon Showdown Lines Restriction</strong>: </td><td><input type="text" name="maxlines" value="' +
(App.config.bot.maxlines || '3') + '" /></td></tr>';
html += '</table>';
html += '<p><label><input type="checkbox" name="debugmode" value="true" ' +
(App.config.debug ? 'checked="checked"' : '') + ' /></label>&nbsp;Enable debug mode.</p>';
Expand Down

0 comments on commit c80746d

Please sign in to comment.