aboutsummaryrefslogtreecommitdiff
path: root/socketConnection.js
blob: e582a4dfc86c954245798a089b2c8b7aa0dbae8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import WebsiteCommunicator from "./../soopyApis/websiteCommunicator"
import socketData from "../soopyApis/socketData"
import logger from "./logger"
import metadata from "./metadata"
const Cosmetics = require("./features/cosmetics/index.js")

class SoopyV2Server extends WebsiteCommunicator {
    constructor(){
        super(socketData.serverNameToId.soopyv2)

        this.spammedMessages = []

        this.errorsToReport = []

        this.lbdatathing = undefined
        this.lbdatathingupdated = 0

        this.reportErrorsSetting = undefined

        this.onPlayerStatsLoaded = undefined

        this.userCosmeticPermissions = undefined
    }

    onData(data){
        if(data.type === "updateCosmeticPermissions"){
            this.userCosmeticPermissions = data.permissions
            Cosmetics.class.updateUserCosmeticPermissionSettings.call(Cosmetics.class)
        }
        if(data.type === "updateCosmetics"){
            Cosmetics.class.setUserCosmeticsInformation.call(Cosmetics.class, data.uuid, data.cosmetics)
        }
        if(data.type === "spammedmessage"){
            this.spammedMessages.push(...data.messages)
        }
        if(data.type === "playerStatsQuick"){
            if(this.onPlayerStatsLoaded) this.onPlayerStatsLoaded(data.data)
        }
        if(data.type === "updateLbDataThing"){
            this.lbdatathing = data.data
            this.lbdatathingupdated = data.lastUpdated
        }
    }

    onConnect(){
        if(this.reportErrorsSetting && !this.reportErrorsSetting.getValue()) return
        new Thread(() => {
            while(!this.reportErrorsSetting){
                Thread.sleep(1000)
            }

            if(!this.reportErrorsSetting.getValue()) return
            
            this.errorsToReport.forEach(data => {
                this.sendData({
                    type: "error",
                    data: data
                })
            })
            this.errorsToReport = []
        }).start()
    }

    updateCosmeticsData(data){
        this.sendData({
            type: "cosmeticSettings",
            data: data
        })
    }

    sendMessageToServer(message, lobbyId){
        this.sendData({
            type: "chatMessage",
            message: message,
            lobbyId: lobbyId
        })
    }

    reportError(error, description){
        // ChatLib.chat(JSON.stringify(error))
        if(this.reportErrorsSetting && !this.reportErrorsSetting.getValue()) return
        let data = {
            lineNumber: error.lineNumber,
            fileName: error.fileName.replace(/file:.+?ChatTriggers/g, "file:"), //The replace is to not leak irl names thru windows acct name
            message: error.message,
            description: description,
            stack: error.stack.replace(/file:.+?ChatTriggers/g, "file:"),
            modVersion: metadata.version,
            modVersionId: metadata.versionId,
        }

        if(this.connected && this.reportErrorsSetting){
            this.sendData({
                type: "error",
                data: data
            })
        }else{
            this.errorsToReport.push(data)
        }
    }

    requestPlayerStats(uuid, username){
        this.sendData({
            type: "loadStatsQuick",
            uuid: uuid,
            username: username
        })
    }
}

if(!global.soopyV2Server){
    global.soopyV2Server = new SoopyV2Server()
    
    register("gameUnload", ()=>{
        global.soopyV2Server = undefined
    })
}

export default global.soopyV2Server