1
1
package com.lambda.client.gui.hudgui.elements.misc
2
2
3
3
import com.google.gson.Gson
4
- import com.google.gson.annotations.SerializedName
4
+ import com.lambda.client.LambdaMod
5
+ import com.lambda.client.commons.utils.ConnectionUtils
5
6
import com.lambda.client.commons.utils.grammar
6
7
import com.lambda.client.event.SafeClientEvent
7
8
import com.lambda.client.gui.hudgui.LabelHud
8
9
import com.lambda.client.manager.managers.NetworkManager
9
10
import com.lambda.client.util.CachedValue
10
11
import com.lambda.client.util.TickTimer
11
12
import com.lambda.client.util.TimeUnit
12
- import com.lambda.client.util.WebUtils
13
13
import com.lambda.client.util.text.MessageSendHelper
14
14
import com.lambda.client.util.threads.defaultScope
15
15
import kotlinx.coroutines.Dispatchers
16
16
import kotlinx.coroutines.launch
17
+ import java.time.Instant
18
+ import java.time.ZonedDateTime
17
19
18
20
internal object Queue2B2T : LabelHud(
19
21
name = " 2B2T Queue" ,
@@ -22,22 +24,26 @@ internal object Queue2B2T : LabelHud(
22
24
) {
23
25
private val hasShownWarning = setting(" Has Shown Warning" , false , { false })
24
26
private val show by setting(" Show" , Show .BOTH )
27
+ private val showUpdatedTime by setting(" Show Updated Time" , true )
25
28
26
29
private enum class Show {
27
30
BOTH , PRIORITY , REGULAR
28
31
}
29
32
30
- private const val apiUrl = " https://2bqueue.info /queue"
33
+ private const val apiUrl = " https://api.2b2t.vc /queue"
31
34
32
35
private val gson = Gson ()
33
36
private val dataUpdateTimer = TickTimer (TimeUnit .SECONDS )
37
+ private var hasInitialized = false
34
38
35
- private var queueData = QueueData (0 , 0 , 0 , 0 )
39
+ private var queueData = QueueData (0 , 0 , ZonedDateTime .now().toString() )
36
40
private val lastUpdate by CachedValue (1L , TimeUnit .SECONDS ) {
37
- val difference = System .currentTimeMillis() - queueData.lastUpdated
41
+ val dateRaw = queueData.time
42
+ val parsedDate = ZonedDateTime .parse(dateRaw)
43
+ val difference = Instant .now().epochSecond - parsedDate.toEpochSecond()
38
44
39
- val minuteAmt = (difference / 60000L % 60L ).toInt()
40
- val secondAmt = (difference / 1000L % 60L ).toInt()
45
+ val minuteAmt = (difference / 60L % 60L ).toInt()
46
+ val secondAmt = (difference % 60L ).toInt()
41
47
val minutes = grammar(minuteAmt, " minute" , " minutes" )
42
48
val seconds = grammar(secondAmt, " second" , " seconds" )
43
49
@@ -53,44 +59,54 @@ internal object Queue2B2T : LabelHud(
53
59
sendWarning()
54
60
}
55
61
56
- if (dataUpdateTimer.tick(15L )) {
62
+ if (dataUpdateTimer.tick(300L ) // API caches queue data for 5 minutes
63
+ || ! hasInitialized) {
64
+ hasInitialized = true
57
65
updateQueueData()
58
66
}
59
67
60
68
if (NetworkManager .isOffline) {
61
- displayText.addLine(" Cannot connect to 2bqueue.info " , primaryColor)
69
+ displayText.addLine(" Cannot connect to api.2b2t.vc " , primaryColor)
62
70
displayText.add(" Make sure your internet is working!" , primaryColor)
63
- } else {
64
- if (showPriority) {
65
- displayText.add(" Priority: " , primaryColor)
66
- displayText.add(" ${queueData.priority} " , secondaryColor)
67
- }
71
+ return
72
+ }
68
73
69
- if (showRegular ) {
70
- displayText.add(" Regular : " , primaryColor)
71
- displayText.add(" ${queueData.regular } " , secondaryColor)
72
- }
74
+ if (showPriority ) {
75
+ displayText.add(" Priority : " , primaryColor)
76
+ displayText.add(" ${queueData.prio } " , secondaryColor)
77
+ }
73
78
79
+ if (showRegular) {
80
+ displayText.add(" Regular: " , primaryColor)
81
+ displayText.add(" ${queueData.regular} " , secondaryColor)
82
+ }
83
+ if (showUpdatedTime) {
74
84
displayText.addLine(" " , primaryColor)
75
85
displayText.add(" Last updated $lastUpdate ago" , primaryColor)
76
86
}
77
87
}
78
88
79
89
private fun sendWarning () {
80
90
MessageSendHelper .sendWarningMessage(
81
- " This module uses an external API, 2bqueue.info , which is operated by tycrek#0001 ." +
82
- " If you do not trust this external API / have not verified the safety yourself, disable this HUD component."
91
+ " This module uses an external API, api.2b2t.vc , which is operated by rfresh#2222 ." +
92
+ " If you do not trust this external API / have not verified the safety yourself, disable this HUD component."
83
93
)
84
94
hasShownWarning.value = true
85
95
}
86
96
87
97
private fun updateQueueData () {
88
98
defaultScope.launch(Dispatchers .IO ) {
89
99
runCatching {
90
- val json = WebUtils .getUrlContents(apiUrl)
91
- gson.fromJson(json, QueueData ::class .java)
92
- }.getOrNull()?.let {
93
- queueData = it
100
+ ConnectionUtils .requestRawJsonFrom(apiUrl) {
101
+ LambdaMod .LOG .error(" Failed querying queue data" , it)
102
+ }?.let {
103
+ gson.fromJson(it, QueueData ::class .java)?.let { data ->
104
+ queueData = data
105
+ return @runCatching
106
+ }
107
+
108
+ LambdaMod .LOG .error(" No queue data received. Is 2b2t down?" )
109
+ }
94
110
}
95
111
}
96
112
}
@@ -99,11 +115,8 @@ internal object Queue2B2T : LabelHud(
99
115
private val showRegular get() = show == Show .BOTH || show == Show .REGULAR
100
116
101
117
private class QueueData (
102
- @SerializedName(" prio" )
103
- val priority : Int ,
118
+ val prio : Int ,
104
119
val regular : Int ,
105
- val total : Int ,
106
- @SerializedName(" timems" )
107
- val lastUpdated : Long
120
+ val time : String
108
121
)
109
122
}
0 commit comments