76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"hudly/hypixel"
|
||
|
"hudly/mcfetch"
|
||
|
"log"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var key = "9634ea92-80f0-482f-aebd-b082c6ed6f19"
|
||
|
var uuid = "5328930e-d411-49cb-90ad-4e5c7b27dd86"
|
||
|
|
||
|
func demo() {
|
||
|
// Ensure a username is provided as a command-line argument
|
||
|
if len(os.Args) < 2 {
|
||
|
log.Fatal("Please provide a Minecraft username as a command-line argument.")
|
||
|
}
|
||
|
|
||
|
// Get the username from the command-line arguments
|
||
|
username := os.Args[1]
|
||
|
|
||
|
thing := hypixel.NewAPIKey(key)
|
||
|
api := hypixel.NewAPI(*thing)
|
||
|
thing.UsesLeft = 11
|
||
|
|
||
|
// Create a MemoryCache instance
|
||
|
memCache := &mcfetch.MemoryCache{}
|
||
|
memCache.Init()
|
||
|
|
||
|
// Create a channel to receive the result
|
||
|
resultChan := make(chan map[string]interface{})
|
||
|
errorChan := make(chan error)
|
||
|
|
||
|
// Create an AsyncPlayerFetcher for asynchronous data fetching with MemoryCache
|
||
|
asyncFetcher := mcfetch.NewAsyncPlayerFetcher(
|
||
|
username, // Minecraft username or UUID
|
||
|
memCache, // Pass the memory cache instance
|
||
|
2, // Number of retries
|
||
|
2*time.Second, // Retry delay
|
||
|
5*time.Second, // Request timeout
|
||
|
)
|
||
|
|
||
|
// Start asynchronous data fetching
|
||
|
asyncFetcher.FetchPlayerData(resultChan, errorChan)
|
||
|
|
||
|
// Non-blocking code execution (do something else while waiting)
|
||
|
fmt.Println("Fetching data asynchronously...")
|
||
|
|
||
|
var userID string
|
||
|
// Block until we receive data or an error
|
||
|
select {
|
||
|
case data := <-resultChan:
|
||
|
fmt.Printf("Player data: %+v\n", data)
|
||
|
|
||
|
// Check if "uuid" exists and is not nil
|
||
|
if uuid, ok := data["id"].(string); ok {
|
||
|
userID = uuid
|
||
|
} else {
|
||
|
fmt.Println(fmt.Sprintf("%+v", data))
|
||
|
log.Fatal("UUID not found or invalid for player")
|
||
|
}
|
||
|
|
||
|
case err := <-errorChan:
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Use the Hypixel API to get additional player data
|
||
|
res, err := api.GetPlayerResponse(userID)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
fmt.Println(fmt.Sprintf("%+v", res))
|
||
|
}
|