123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- module.exports = class WebsocketLogin {
- constructor(socket) {
- this.socket = socket;
- this.awaitUsername = false;
- this.awaitPassword = false;
- this.username = "";
- this.password = "";
- this.loggedIn = false;
- this.player = null;
- this.failCount = 0;
- this.inputBuffer = "";
- this.isReconnecting = false;
- this.reconnectLogin = null;
- }
- attach() {
- libs.WebsocketUtil.sendString(this.socket, config.welcomeBanner);
- libs.WebsocketUtil.sendString(this.socket, "Please log in to your account.");
- libs.WebsocketUtil.sendString(this.socket, "Username: ");
- this.awaitUsername = true;
- }
- receive(data) {
- var input = data.trim().split(" ");
- if(!this.loggedIn) {
- this.handleInput(input);
- } else {
- this.receivePlayer(input);
- }
- }
- sendMessage(message){
- var hintColor = (!this.player.hasOwnProperty("hintColor") || this.player.hintColor == null) ? libs.HtmlColor.FgMagenta : libs.HtmlColor[this.player.hintColor];
- message = libs.HtmlColor.Reset + message;
- message = message.replace(/\{hint\}/g, hintColor);
- message = message.replace(/\{\/hint\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{mobile\}/g, libs.HtmlColor.FgYellow);
- message = message.replace(/\{\/mobile\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{item\}/g, libs.HtmlColor.FgGreen);
- message = message.replace(/\{\/item\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{error\}/g, libs.HtmlColor.FgRed);
- message = message.replace(/\{\/error\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{global\}/g, libs.HtmlColor.FgCyan);
- message = message.replace(/\{\/global\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{red\}/g, libs.HtmlColor.FgRed);
- message = message.replace(/\{\/red\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{green\}/g, libs.HtmlColor.FgGreen);
- message = message.replace(/\{\/green\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{yellow\}/g, libs.HtmlColor.FgYellow);
- message = message.replace(/\{\/yellow\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{blue\}/g, libs.HtmlColor.FgBlue);
- message = message.replace(/\{\/blue\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{magenta\}/g, libs.HtmlColor.FgMagenta);
- message = message.replace(/\{\/magenta\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{cyan\}/g, libs.HtmlColor.FgCyan);
- message = message.replace(/\{\/cyan\}/g, libs.HtmlColor.Reset);
- message = message.replace(/\{.*?\}/g, "");
- message = message.replace(/\n/g, "<br />");
- libs.WebsocketUtil.sendString(this.socket, message);
- }
- disconnect() {
- if(this.isReconnecting) {
- this.player.login = this.reconnectLogin;
- this.isReconnecting = false;
- this.socket = null;
- this.loggedIn = false;
- //this.socket = this.reconnectSocket;
- //this.reconnectSocket = null;
- libs.Output.toMobile(this.player.id, "Reconnecting to existing session...");
- world.mud_handleInput(this.player.roomId, this.player.id, ["look"]);
- return;
- }
- if(this.loggedIn) {
- world.mud_playerQuit(this.player);
- this.socket = null;
- this.loggedIn = false;
- }
- }
- handleInput(input) {
- if(this.awaitUsername) {
- this.username = input.join(" ");
- libs.WebsocketUtil.sendInstruction(this.socket, libs.WebsocketUtil.ProtectInput);
- libs.WebsocketUtil.sendString(this.socket, "Password: ");
- this.awaitUsername = false;
- this.awaitPassword = true;
- } else if (this.awaitPassword) {
- this.password = input.join(" ");
- this.awaitPassword = false;
- libs.WebsocketUtil.sendInstruction(this.socket, libs.WebsocketUtil.OpenInput);
- var userData = world.mud_getUserData(this.username);
- if(userData != null && world.mud_isValidLogin(userData, this.password)) {
- libs.WebsocketUtil.sendString(this.socket, "Logging you in...");
- world.mud_playerLoad(this, userData);
- this.loggedIn = true;
- } else {
- this.loginFailed();
- }
- }
- }
- loginFailed() {
- this.failCount++;
- if(this.failCount >= 3) {
- this.logout();
- return;
- }
- libs.WebsocketUtil.sendString(this.socket, "Your login information was not correct.");
- libs.WebsocketUtil.sendString(this.socket, "Username: ");
- this.awaitUsername = true;
- }
- receivePlayer(input) {
- if(this.player == null) {
- return;
- }
- world.mud_handleInput(this.player.roomId, this.player.id, input);
- }
- setConnectionTimeout(time) {
- console.log("gotta handle connection timeout");
- //this.socket.setTimeout(time);
- }
- logout() {
- this.socket.close();
- }
- reconnect(newLogin) {
- this.socket.close();
- this.isReconnecting = true;
- this.reconnectLogin = newLogin;
- }
- };
-
|