websocketlogin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. module.exports = class WebsocketLogin {
  2. constructor(socket) {
  3. this.socket = socket;
  4. this.awaitUsername = false;
  5. this.awaitPassword = false;
  6. this.username = "";
  7. this.password = "";
  8. this.loggedIn = false;
  9. this.player = null;
  10. this.failCount = 0;
  11. this.inputBuffer = "";
  12. this.isReconnecting = false;
  13. this.reconnectLogin = null;
  14. }
  15. attach() {
  16. libs.WebsocketUtil.sendString(this.socket, config.welcomeBanner);
  17. libs.WebsocketUtil.sendString(this.socket, "Please log in to your account.");
  18. libs.WebsocketUtil.sendString(this.socket, "Username: ");
  19. this.awaitUsername = true;
  20. }
  21. receive(data) {
  22. var input = data.trim().split(" ");
  23. if(!this.loggedIn) {
  24. this.handleInput(input);
  25. } else {
  26. this.receivePlayer(input);
  27. }
  28. }
  29. sendMessage(message){
  30. var hintColor = (!this.player.hasOwnProperty("hintColor") || this.player.hintColor == null) ? libs.HtmlColor.FgMagenta : libs.HtmlColor[this.player.hintColor];
  31. message = libs.HtmlColor.Reset + message;
  32. message = message.replace(/\{hint\}/g, hintColor);
  33. message = message.replace(/\{\/hint\}/g, libs.HtmlColor.Reset);
  34. message = message.replace(/\{mobile\}/g, libs.HtmlColor.FgYellow);
  35. message = message.replace(/\{\/mobile\}/g, libs.HtmlColor.Reset);
  36. message = message.replace(/\{item\}/g, libs.HtmlColor.FgGreen);
  37. message = message.replace(/\{\/item\}/g, libs.HtmlColor.Reset);
  38. message = message.replace(/\{error\}/g, libs.HtmlColor.FgRed);
  39. message = message.replace(/\{\/error\}/g, libs.HtmlColor.Reset);
  40. message = message.replace(/\{global\}/g, libs.HtmlColor.FgCyan);
  41. message = message.replace(/\{\/global\}/g, libs.HtmlColor.Reset);
  42. message = message.replace(/\{red\}/g, libs.HtmlColor.FgRed);
  43. message = message.replace(/\{\/red\}/g, libs.HtmlColor.Reset);
  44. message = message.replace(/\{green\}/g, libs.HtmlColor.FgGreen);
  45. message = message.replace(/\{\/green\}/g, libs.HtmlColor.Reset);
  46. message = message.replace(/\{yellow\}/g, libs.HtmlColor.FgYellow);
  47. message = message.replace(/\{\/yellow\}/g, libs.HtmlColor.Reset);
  48. message = message.replace(/\{blue\}/g, libs.HtmlColor.FgBlue);
  49. message = message.replace(/\{\/blue\}/g, libs.HtmlColor.Reset);
  50. message = message.replace(/\{magenta\}/g, libs.HtmlColor.FgMagenta);
  51. message = message.replace(/\{\/magenta\}/g, libs.HtmlColor.Reset);
  52. message = message.replace(/\{cyan\}/g, libs.HtmlColor.FgCyan);
  53. message = message.replace(/\{\/cyan\}/g, libs.HtmlColor.Reset);
  54. message = message.replace(/\{.*?\}/g, "");
  55. message = message.replace(/\n/g, "<br />");
  56. libs.WebsocketUtil.sendString(this.socket, message);
  57. }
  58. disconnect() {
  59. if(this.isReconnecting) {
  60. this.player.login = this.reconnectLogin;
  61. this.isReconnecting = false;
  62. this.socket = null;
  63. this.loggedIn = false;
  64. //this.socket = this.reconnectSocket;
  65. //this.reconnectSocket = null;
  66. libs.Output.toMobile(this.player.id, "Reconnecting to existing session...");
  67. world.mud_handleInput(this.player.roomId, this.player.id, ["look"]);
  68. return;
  69. }
  70. if(this.loggedIn) {
  71. world.mud_playerQuit(this.player);
  72. this.socket = null;
  73. this.loggedIn = false;
  74. }
  75. }
  76. handleInput(input) {
  77. if(this.awaitUsername) {
  78. this.username = input.join(" ");
  79. libs.WebsocketUtil.sendInstruction(this.socket, libs.WebsocketUtil.ProtectInput);
  80. libs.WebsocketUtil.sendString(this.socket, "Password: ");
  81. this.awaitUsername = false;
  82. this.awaitPassword = true;
  83. } else if (this.awaitPassword) {
  84. this.password = input.join(" ");
  85. this.awaitPassword = false;
  86. libs.WebsocketUtil.sendInstruction(this.socket, libs.WebsocketUtil.OpenInput);
  87. var userData = world.mud_getUserData(this.username);
  88. if(userData != null && world.mud_isValidLogin(userData, this.password)) {
  89. libs.WebsocketUtil.sendString(this.socket, "Logging you in...");
  90. world.mud_playerLoad(this, userData);
  91. this.loggedIn = true;
  92. } else {
  93. this.loginFailed();
  94. }
  95. }
  96. }
  97. loginFailed() {
  98. this.failCount++;
  99. if(this.failCount >= 3) {
  100. this.logout();
  101. return;
  102. }
  103. libs.WebsocketUtil.sendString(this.socket, "Your login information was not correct.");
  104. libs.WebsocketUtil.sendString(this.socket, "Username: ");
  105. this.awaitUsername = true;
  106. }
  107. receivePlayer(input) {
  108. if(this.player == null) {
  109. return;
  110. }
  111. world.mud_handleInput(this.player.roomId, this.player.id, input);
  112. }
  113. setConnectionTimeout(time) {
  114. console.log("gotta handle connection timeout");
  115. //this.socket.setTimeout(time);
  116. }
  117. logout() {
  118. this.socket.close();
  119. }
  120. reconnect(newLogin) {
  121. this.socket.close();
  122. this.isReconnecting = true;
  123. this.reconnectLogin = newLogin;
  124. }
  125. };