aboutsummaryrefslogtreecommitdiff
path: root/features/friendsGUI/index.js
blob: 51335a9cf022bd1cec709602f12f798df4b1b161 (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
/// <reference types="../../../CTAutocomplete" />
/// <reference lib="es2015" />
import { SoopyRenderEvent } from "../../../guimanager";
import ButtonWithArrowAndDescription from "../../../guimanager/GuiElement/ButtonWithArrowAndDescription";
import SoopyGuiElement from "../../../guimanager/GuiElement/SoopyGuiElement";
import SoopyTextElement from "../../../guimanager/GuiElement/SoopyTextElement";
import BoxWithLoading from "../../../guimanager/GuiElement/BoxWithLoading";
import Feature from "../../featureClass/class";
import GuiPage from "../soopyGui/GuiPage";

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

    onEnable(){
        this.initVariables()

        this.GuiPage = new SettingPage()

        this.registerChat("&9-----------------------------------------------------&r&9${*}&r&9             ${*} &6Friends (Page ${pagenum} of ${maxpages})${friendslist}&r&9-----------------------------------------------------&r", (...args)=>{this.GuiPage.friendListMessageEvent.call(this.GuiPage, ...args)})
        this.registerStep(true, 5,  ()=>{this.GuiPage.regenGuiElements.call(this.GuiPage)})
    }

    initVariables(){
        this.GuiPage = undefined
    }

    onDisable(){
        this.initVariables()
    }
}


class SettingPage extends GuiPage {
    constructor(){
        super(7)
        
        this.name = "Friends"

        this.pages = [this.newPage()]

        this.lastRender = 0

        this.pages[0].addEvent(new SoopyRenderEvent().setHandler(()=>{this.lastRender = Date.now()}))
        
        
        let friendsTitle = new SoopyTextElement().setText("§0Friends").setMaxTextScale(3).setLocation(0.1, 0.05, 0.8, 0.1)
        this.pages[0].addChild(friendsTitle)

        this.friendsArea = new SoopyGuiElement().setLocation(0.1, 0.2, 0.8, 0.8).setScrollable(true)
        this.pages[0].addChild(this.friendsArea)

        this.loadedFriends = {}

        this.pageCount = undefined

        this.heightPerFriend = 0.2
        this.loadingElement = new BoxWithLoading().setLocation(0,0, 1, 0.175)
        this.lastLoadedPage = 0
        this.maxLoadedPage = 0

        this.loadingElement.addEvent(new SoopyRenderEvent().setHandler(()=>{
            if(Date.now()-this.lastLoadedPage > 1000){
                this.loadFriendPage(this.maxLoadedPage+1)
            }
        }))


        this.finaliseLoading()
    }

    friendListMessageEvent(pagenum, maxpages, friendslist, event){
        if(Date.now()-this.lastRender < 1000){
            cancel(event)
            // console.log("Canceling")
        }else{
            return
        }
        
        this.maxLoadedPage = parseInt(pagenum)

        this.pageCount = parseInt(maxpages)

        friendslist = friendslist.replace(/\-\>newLine\<\-/g,"\n").split("\n")
        friendslist.shift()

        friendslist.pop()

        friendslist.forEach((line, i) => {
            let [name, location] = line.split(" is ")

            if(location){
                if(this.loadedFriends[ChatLib.removeFormatting(name)]){
                    this.loadedFriends[ChatLib.removeFormatting(name)].nameWithFormatting = name
                    this.loadedFriends[ChatLib.removeFormatting(name)].currentGame = location.replace("in ", "").replace("currently offline", "&cCurrently offline")
                    this.loadedFriends[ChatLib.removeFormatting(name)].element.setText(name).setDesc("&7"+location.replace("in ", "").replace("currently offline", "&cCurrently offline"))
                }else{
                    this.loadedFriends[ChatLib.removeFormatting(name)] = {
                        nameWithFormatting: name,
                        currentGame: location.replace("in ", "").replace("currently offline", "&cCurrently offline"),
                        element:  new ButtonWithArrowAndDescription().setLocation(0, this.heightPerFriend*Object.keys(this.loadedFriends).length+1, 1, this.heightPerFriend*0.875).setText(name).setDesc("&7"+location.replace("in ", "").replace("currently offline", "&cCurrently offline"))
                    }
                    this.loadedFriends[ChatLib.removeFormatting(name)].element.location.location.y.set(this.heightPerFriend*Object.keys(this.loadedFriends).length-this.heightPerFriend, 500+100*i)
                }
            }
        });

        this.regenGuiElements()
    }

    regenGuiElements(){
        if(Date.now()-this.lastRender < 1000){
            this.friendsArea.children = []

            let scroll = this.friendsArea.location.scroll.y.get()/this.friendsArea.location.getHeightExact()

            let minY = -scroll-this.heightPerFriend*0.875-0.5

            let maxY = -scroll+1.5

            Object.keys(this.loadedFriends).forEach((ign, i)=>{
                if(this.loadedFriends[ign].element.location.location.y.get() > minY && this.loadedFriends[ign].element.location.location.y.get() < maxY){
                    this.friendsArea.addChild(this.loadedFriends[ign].element)
                }
            })
            
            this.loadingElement.location.location.y.set(this.heightPerFriend*Object.keys(this.loadedFriends).length, 500)
            this.friendsArea.addChild(this.loadingElement)
                
            if(this.maxLoadedPage !== this.pageCount){
                this.loadingElement.visable = true
            }else{
                this.loadingElement.visable = false
            }
        }
    }

    loadFriendPage(page){
        ChatLib.command("friends list " + page)
        this.lastLoadedPage = Date.now()
    }

    onOpen(){
        this.loadedFriends = {}
        this.pageCount = undefined
        this.lastLoadedPage = 0
        this.maxLoadedPage = 0

        this.regenGuiElements()

        this.loadFriendPage(1)
    }
}

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