zajucd / zajucdBetterTeleport

// ==UserScript==
// @name         zajucdBetterTeleport
// @namespace    zajucd
// @version      2024-10-06
// @description  优化zajucdBOT的传送
// @author       zajucd
// @match        *://*/*BondageClub*
// @license      MIT
// @copyright    2024, zajucd (https://openuserjs.org/users/zajucd)
// @updateURL    https://openuserjs.org/meta/zajucd/zajucdBetterTeleport.meta.js
// @downloadURL  https://openuserjs.org/install/zajucd/zajucdBetterTeleport.user.js
// ==/UserScript==
setTimeout
  (function () {
      'use strict';
      let ChatRoomMessageAdditionDict = {};
      ServerSocket.on("ChatRoomMessage", function (data) {
        ChatRoomMessageAdd(data);
      });
      if (typeof ChatRoomMessageAdditionDict === 'undefined') {
        ChatRoomMessageAdditionDict = {}
      }

      function ChatRoomMessageAdd(data) {

        // Make sure the message is valid (needs a Sender and Content)
        if ((data != null) && (typeof data === "object") && (data.Content != null) && (typeof data.Content === "string") && (data.Content != "") && (data.Sender != null) && (typeof data.Sender === "number")) {

          // Make sure the sender is in the room
          var SenderCharacter = null;
          for (var C = 0; C < ChatRoomCharacter.length; C++)
            if (ChatRoomCharacter[C].MemberNumber == data.Sender) {
              SenderCharacter = ChatRoomCharacter[C]
              break;
            }

          // If we found the sender
          if (SenderCharacter != null) {

            // Replace < and > characters to prevent HTML injections
            var msg = data.Content;
            while (msg.indexOf("<") > -1) msg = msg.replace("<", "&lt;");
            while (msg.indexOf(">") > -1) msg = msg.replace(">", "&gt;");

            // This part is to append code react to certain message
            for (var key in ChatRoomMessageAdditionDict) {
              ChatRoomMessageAdditionDict[key](SenderCharacter, msg, data)
            }
          }
        }
      }
      ChatRoomMessageAdditionDict["BetterTeleportC"] = function (SenderCharacter, msg, data) {
        ChatRoomMessageBetterTeleportC(SenderCharacter, msg, data)
      };

      function ChatRoomMessageBetterTeleportC(SenderCharacter, msg, data) {
        if (data.Type === "Hidden" && msg.includes("zajucdtpcheck")) {
          setTimeout(function () {
            ServerSend("ChatRoomChat", {
              Content: "zajucdtpok",
              Type: "Hidden",
              Target: SenderCharacter.MemberNumber
            });
          }, 200);
        }
        else if (data.Type === "Hidden" && msg.includes("zajucdtpreq")) {
          if (data.Dictionary.MemberNumber === Player.MemberNumber) {
            ChatRoomMapViewMovement = null;
            Player.MapData.Pos = data.Dictionary.Pos;
            ServerSend("ChatRoomCharacterMapDataUpdate", data.Dictionary.Pos);
          }
        }
      }

      function ServerDisconnect(data, close = false) {
        if (data == "ErrorRateLimited") {
          alert("传送优化插件可能与其他插件冲突,建议暂时关闭其他插件.")
        }
        if (!ServerIsConnected) return;
        console.warn("Server connection lost");
        const ShouldRelog = Player.Name != "";
        let msg = ShouldRelog ? "Disconnected" : "ErrorDisconnectedFromServer";
        if (data) {
          console.warn(data);
          msg = data;
        }
        ServerSetConnected(false, msg);
        if (close) {
          ServerSocket.disconnect();
          // If the error was duplicated login, we want to reconnect
          if (data === "ErrorDuplicatedLogin") {
            ServerInit();
          }
        }

        if (ShouldRelog) {
          if (CurrentScreen != "Relog") {

            // Exits out of the chat room or a sub screen of the chatroom, so we'll be able to get in again when we log back
            if (ServerPlayerIsInChatRoom()) {
              ChatRoomHideElements();
              CurrentScreen = "ChatSearch";
              CurrentModule = "Online";
              CurrentCharacter = null;
            }

            // Keeps the relog data
            RelogData = {
              Screen: CurrentScreen,
              Module: CurrentModule,
              Character: CurrentCharacter
            };
            CurrentCharacter = null;
            CommonSetScreen("Character", "Relog");

          }
        }

        // Raise a notification to alert the user
        if (!document.hasFocus()) {
          NotificationRaise(NotificationEventType.DISCONNECT);
        }
      }
    },
    2000
  );