Skip to content

Commit

Permalink
time fixed, code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkleen committed Jun 28, 2024
1 parent a0986e3 commit ec9b487
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
58 changes: 33 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"strconv"
"time"

"github.com/drunkleen/blum-bot/requests"
Expand Down Expand Up @@ -140,35 +141,43 @@ func mainLoop(queryList []string) {

if claimRefEnable {
friendsBalance, err := requests.CheckBalanceFriend(token)
if err != nil {
log.Printf(red("[Referrals Balance] Failed to get friend's balance: %v\n"), err)
}

printText += fmt.Sprintf("[" + bold(cyan("Referrals")) + "] ")
printText += fmt.Sprintf(yellow("amount: %v"), friendsBalance.AmountForClaim)
printText += fmt.Sprintf(yellow(" | Claimable: %v"), friendsBalance.CanClaim)
if friendsBalance.AmountForClaim != "0" || friendsBalance.CanClaim {

var claimTime string
if friendsBalance.CanClaimAt != "" {
claimTime, err = utils.ConvertStrTimestamp(friendsBalance.CanClaimAt)
if err != nil {
log.Printf(red("[Referrals Balance] Failed to convert time: %v\n"), err)
log.Printf(red("[Referrals Balance] Failed to get friend's balance: %v\n"), err)
}
printText += fmt.Sprintf(yellow(" | %v\n"), claimTime)
} else {
printText += fmt.Sprintf("\n")
}

if friendsBalance.CanClaim {
ok, err := requests.ClaimBalanceFriend(token)
if err != nil {
log.Printf(red("[Referrals] Failed to claim friend's balance: %v\n"), err)
printText += fmt.Sprintf("[" + bold(cyan("Referrals")) + "] ")
printText += fmt.Sprintf(yellow("amount: %v"), friendsBalance.AmountForClaim)
printText += fmt.Sprintf(yellow(" | Claimable: %v"), friendsBalance.CanClaim)

var claimTime int64
if friendsBalance.CanClaimAt != "" {
claimTime, err = strconv.ParseInt(friendsBalance.CanClaimAt, 10, 64)
if err != nil {
log.Printf(red("[Referrals] Failed to parse claim time: %v\n"), err)
}
remainingClaimTime, err := utils.TimeLeft(claimTime)
if err != nil {
log.Printf(red("[Referrals] Failed to calculate remaining claim time: %v\n"), err)
}
printText += fmt.Sprintf(yellow(" | %v remaining\n"), remainingClaimTime)
} else {
printText += fmt.Sprintf("\n")
}
if ok {
printText += fmt.Sprintf(bold(cyan("[Referrals]")) + " successfully claimed!\n")

if friendsBalance.CanClaim {
ok, err := requests.ClaimBalanceFriend(token)
if err != nil {
log.Printf(red("[Referrals] Failed to claim friend's balance: %v\n"), err)
}
if ok {
printText += fmt.Sprintf(bold(cyan("[Referrals]")) + " successfully claimed!\n")
}
}
}

}
}

printText += fmt.Sprintf("[%v] %v tickets\n", bold(cyan("Play Passes")), balanceInfo.PlayPasses)
Expand All @@ -195,14 +204,13 @@ func mainLoop(queryList []string) {
} else {
time.Sleep(5 * time.Second)
}
timeNow, _ := utils.ConvertStrTimestamp(fmt.Sprintf("%d", time.Now().Unix()))
h, m, _ := time.Now().Clock()
utils.ClearScreen()
utils.PrintLogo()
fmt.Printf(
"-------- Time: %v --------\n%v\n------------ Up Time: %v ------------",
timeNow, printText, yellow(utils.FormatUpTime(time.Since(startTime))),
"-------- Time: %d:%d --------\n%v\n------------ Up Time: %v ------------",
h, m, printText, yellow(utils.FormatUpTime(time.Since(startTime))),
)
fmt.Println(time.Now().Unix())
printText = ""
} // end infinite loop

Expand Down
20 changes: 0 additions & 20 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -122,25 +121,6 @@ func FormatUpTime(d time.Duration) string {
return fmt.Sprintf("%dh%dm%ds", hours, minutes, seconds)
}

func ConvertStrTimestamp(timestampStr string) (string, error) {
// Convert the string to an integer
epochTime, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
return "", fmt.Errorf("error converting timestamp: %w", err)
}

// Convert milliseconds to seconds and nanoseconds
seconds := epochTime / 1000
nanoseconds := (epochTime % 1000) * 1e6

t := time.Unix(seconds, nanoseconds)
const layout = "Monday, 3:04 PM"

humanReadableTime := t.Format(layout)

return humanReadableTime, nil
}

func TimeLeft(futureTimestamp int64) (string, error) {

seconds := futureTimestamp / 1000
Expand Down

0 comments on commit ec9b487

Please sign in to comment.