aboutsummaryrefslogtreecommitdiff
path: root/src/features/statHistoryGui/index.js
blob: 0390772ab99dda861f424108349822bd1a0a46ea (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/// <reference types="../../../../CTAutocomplete" />
/// <reference lib="es2015" />
import SoopyTextElement from "../../../guimanager/GuiElement/SoopyTextElement";
import Feature from "../../featureClass/class";
import GuiPage from "../soopyGui/GuiPage";
import BoxWithLoading from "../../../guimanager/GuiElement/BoxWithLoading";
import BoxWithTextAndDescription from "../../../guimanager/GuiElement/BoxWithTextAndDescription";
import SoopyGuiElement from "../../../guimanager/GuiElement/SoopyGuiElement";
import TextBox from "../../../guimanager/GuiElement/TextBox";
import SoopyKeyPressEvent from "../../../guimanager/EventListener/SoopyKeyPressEvent";
import { firstLetterWordCapital } from "../../utils/stringUtils";
import SoopyMouseClickEvent from "../../../guimanager/EventListener/SoopyMouseClickEvent";
import ButtonWithArrow from "../../../guimanager/GuiElement/ButtonWithArrow";
import { fetch } from "../../utils/networkUtils";
import SoopyImageElement from "../../../guimanager/GuiElement/SoopyImageElement";
import renderLibs from "../../../guimanager/renderLibs";
import SoopyBoxElement from "../../../guimanager/GuiElement/SoopyBoxElement";
import Dropdown from "../../../guimanager/GuiElement/Dropdown";
import SoopyContentChangeEvent from "../../../guimanager/EventListener/SoopyContentChangeEvent";

class StatHistoryGUI extends Feature {
    constructor() {
        super()
    }

    onEnable() {
        this.initVariables()

        this.GuiPage = new StatGraphPage()

    }

    initVariables() {
        this.GuiPage = undefined
    }

    onDisable() {
        this.initVariables()
    }
}


class StatGraphPage extends GuiPage {
    constructor() {
        super(7)

        this.name = "Stat Graphs"

        this.pages = [this.newPage()]

        this.pages[0].addChild(new SoopyTextElement().setText("§0Stat Graphs").setMaxTextScale(3).setLocation(0.1, 0.05, 0.6, 0.1))
        let button = new ButtonWithArrow().setText("§0Open in browser").setLocation(0.7, 0.05, 0.2, 0.1)
        this.pages[0].addChild(button)

        button.addEvent(new SoopyMouseClickEvent().setHandler(() => {
            java.awt.Desktop.getDesktop().browse(
                new java.net.URI("https://soopy.dev/stathistory")
            );
        }))

        this.nameInput = new TextBox().setPlaceholder("Click to search").setLocation(0.1, 0.225, 0.8, 0.1)
        this.pages[0].addChild(this.nameInput)

        this.nameInput.addEvent(new SoopyKeyPressEvent().setHandler((key, keyId) => {
            if (this.nameInput.text.selected && keyId === 28) {
                this.playerLoad = this.nameInput.text.text
                this.nameInput.setText("")
                this.updateData(this.playerLoad)
            }
        }))

        this.statArea = new SoopyGuiElement().setLocation(0.05, 0.325, 0.9, 0.675).setScrollable(true)
        this.pages[0].addChild(this.statArea)

        this.loadingElm = new BoxWithLoading().setLocation(0.35, 0.4, 0.3, 0.2)
        this.errorElm = new BoxWithTextAndDescription().setLocation(0.2, 0.3, 0.6, 0.4).setText("Error!").setDesc("An error occured while loading the data!")
        this.statArea.addChild(this.loadingElm)

        this.playerLoad = undefined

        this.finaliseLoading()
    }

    async updateData(player, profIn) {
        this.closeSidebarPage()

        this.playerLoad = player

        this.statArea._scrollAmount = 0
        this.statArea.location.scroll.y.set(0, 100)

        this.statArea.clearChildren()
        this.statArea.addChild(this.loadingElm)

        let playerData = await fetch("http://soopy.dev/api/v2/player/" + player).json()

        if (player !== this.playerLoad) return

        if (!playerData.success) {
            this.statArea.clearChildren()
            this.statArea.addChild(this.errorElm)
            this.errorElm.setText("§0" + playerData.error.name)
            this.errorElm.setDesc("§0" + playerData.error.description)
            return
        }
        this.statArea.clearChildren()
        let nameElm = new SoopyTextElement().setText(playerData.data.stats.nameWithPrefix.replace(/§f/g, "§7")).setMaxTextScale(2).setLocation(0.1, 0.05, 0.5, 0.1)
        this.statArea.addChild(nameElm)
        this.statArea.addChild(this.loadingElm)

        let skyblockData = await fetch("http://soopy.dev/api/v2/player_skyblock/" + playerData.data.uuid).json()
        if (player !== this.playerLoad) return

        if (!skyblockData.success) {
            this.statArea.clearChildren()
            this.statArea.addChild(this.errorElm)
            this.errorElm.setText("§0" + skyblockData.error.name)
            this.errorElm.setDesc("§0" + skyblockData.error.description)
            return
        }

        let selectedProf = profIn || skyblockData.data.stats.bestProfileId
        let profOptions = {}
        Object.keys(skyblockData.data.profiles).forEach(p => {
            profOptions[p] = skyblockData.data.profiles[p].stats.cute_name
        })

        let profileSelect = new Dropdown().setOptions(profOptions).setSelectedOption(selectedProf).setLocation(0.6, 0.05, 0.3, 0.1).addEvent(new SoopyContentChangeEvent().setHandler(newval => {
            this.updateData(player, newval)
        }))
        this.statArea.addChild(profileSelect)

        fetch("http://soopy.dev/statgraphgenerations/" + playerData.data.uuid + "/" + selectedProf).json().then(graphData => {
            if (player !== this.playerLoad) return

            new Thread(() => {
                let y = 0.2
                Object.keys(graphData).forEach(type => {
                    let typeElm = new SoopyTextElement().setText("§0" + firstLetterWordCapital(type.replace("total", ""))).setMaxTextScale(2).setLocation(0.1, y, 0.8, 0.1)
                    this.statArea.addChild(typeElm)
                    y += 0.15
                    let graph = Object.keys(graphData[type])[0]
                    renderLibs.getImage(graphData[type][graph], true) //load image synchronously into cache so it knows the height
                    let graphElm = new SoopyImageElement()
                    this.statArea.addChild(graphElm)
                    graphElm.setImage(graphData[type][graph])

                    graphElm.setLocation(0.1, y, 0.8, 0.25)
                    graphElm.loadHeightFromImage()
                    y += graphElm.location.size.y.get() + 0.05

                    if (Object.keys(graphData[type]).length > 1) {
                        graphElm.setLore(["Click to show more graphs"])
                        graphElm.addEvent(new SoopyMouseClickEvent().setHandler(() => {
                            let sideBarElm = new SoopyGuiElement().setLocation(0, 0, 1, 1)
                            sideBarElm.scrollable = true
                            this.openSidebarPage(sideBarElm)
                            let y2 = 0.05
                            new Thread(() => {
                                Object.keys(graphData[type]).forEach((graph, i) => {
                                    if (i === 0) return
                                    renderLibs.getImage(graphData[type][graph], true) //load image synchronously into cache so it knows the height
                                    let graphElm = new SoopyImageElement()
                                    sideBarElm.addChild(graphElm)
                                    graphElm.setImage(graphData[type][graph])

                                    graphElm.setLocation(0.1, y2, 0.8, 0.25)
                                    graphElm.loadHeightFromImage()
                                    y2 += graphElm.location.size.y.get() + 0.05
                                })
                            }).start()

                        }))
                    }

                })
            }).start()
        })
    }

    onOpen() {
        this.playerLoad = Player.getName()
        this.updateData(Player.getName())
    }
}

module.exports = {
    class: new StatHistoryGUI()
}