11package com.lambda.client.gui.hudgui.elements.misc
22
33import 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
56import com.lambda.client.commons.utils.grammar
67import com.lambda.client.event.SafeClientEvent
78import com.lambda.client.gui.hudgui.LabelHud
89import com.lambda.client.manager.managers.NetworkManager
910import com.lambda.client.util.CachedValue
1011import com.lambda.client.util.TickTimer
1112import com.lambda.client.util.TimeUnit
12- import com.lambda.client.util.WebUtils
1313import com.lambda.client.util.text.MessageSendHelper
1414import com.lambda.client.util.threads.defaultScope
1515import kotlinx.coroutines.Dispatchers
1616import kotlinx.coroutines.launch
17+ import java.time.Instant
18+ import java.time.ZonedDateTime
1719
1820internal object Queue2B2T : LabelHud(
1921 name = " 2B2T Queue" ,
@@ -22,22 +24,26 @@ internal object Queue2B2T : LabelHud(
2224) {
2325 private val hasShownWarning = setting(" Has Shown Warning" , false , { false })
2426 private val show by setting(" Show" , Show .BOTH )
27+ private val showUpdatedTime by setting(" Show Updated Time" , true )
2528
2629 private enum class Show {
2730 BOTH , PRIORITY , REGULAR
2831 }
2932
30- private const val apiUrl = " https://2bqueue.info /queue"
33+ private const val apiUrl = " https://api.2b2t.vc /queue"
3134
3235 private val gson = Gson ()
3336 private val dataUpdateTimer = TickTimer (TimeUnit .SECONDS )
37+ private var hasInitialized = false
3438
35- private var queueData = QueueData (0 , 0 , 0 , 0 )
39+ private var queueData = QueueData (0 , 0 , ZonedDateTime .now().toString() )
3640 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()
3844
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()
4147 val minutes = grammar(minuteAmt, " minute" , " minutes" )
4248 val seconds = grammar(secondAmt, " second" , " seconds" )
4349
@@ -53,44 +59,54 @@ internal object Queue2B2T : LabelHud(
5359 sendWarning()
5460 }
5561
56- if (dataUpdateTimer.tick(15L )) {
62+ if (dataUpdateTimer.tick(300L ) // API caches queue data for 5 minutes
63+ || ! hasInitialized) {
64+ hasInitialized = true
5765 updateQueueData()
5866 }
5967
6068 if (NetworkManager .isOffline) {
61- displayText.addLine(" Cannot connect to 2bqueue.info " , primaryColor)
69+ displayText.addLine(" Cannot connect to api.2b2t.vc " , primaryColor)
6270 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+ }
6873
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+ }
7378
79+ if (showRegular) {
80+ displayText.add(" Regular: " , primaryColor)
81+ displayText.add(" ${queueData.regular} " , secondaryColor)
82+ }
83+ if (showUpdatedTime) {
7484 displayText.addLine(" " , primaryColor)
7585 displayText.add(" Last updated $lastUpdate ago" , primaryColor)
7686 }
7787 }
7888
7989 private fun sendWarning () {
8090 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."
8393 )
8494 hasShownWarning.value = true
8595 }
8696
8797 private fun updateQueueData () {
8898 defaultScope.launch(Dispatchers .IO ) {
8999 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+ }
94110 }
95111 }
96112 }
@@ -99,11 +115,8 @@ internal object Queue2B2T : LabelHud(
99115 private val showRegular get() = show == Show .BOTH || show == Show .REGULAR
100116
101117 private class QueueData (
102- @SerializedName(" prio" )
103- val priority : Int ,
118+ val prio : Int ,
104119 val regular : Int ,
105- val total : Int ,
106- @SerializedName(" timems" )
107- val lastUpdated : Long
120+ val time : String
108121 )
109122}
0 commit comments