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, "
"); 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; } };