fix(demo): skip sending user data if nicked (for online messages)
This commit is contained in:
parent
350b000e23
commit
a3ef08d877
43
app/main.go
43
app/main.go
@ -42,8 +42,7 @@ func clearTerminal() {
|
||||
cmd.Run()
|
||||
}
|
||||
|
||||
// calculateRatio is a helper function to calculate ratios (e.g., WLR, KDR, etc.)
|
||||
func calculateRatio(numerator, denominator int) float64 {
|
||||
func calcRatio(numerator, denominator int) float64 {
|
||||
if denominator == 0 {
|
||||
return float64(numerator)
|
||||
}
|
||||
@ -104,23 +103,29 @@ func (app *DemoApp) onFileEmit(line string) {
|
||||
if strings.HasPrefix(submsg, OnlinePrefix) { // Online Message
|
||||
newsubmsg := strings.TrimPrefix(submsg, OnlinePrefix)
|
||||
players := strings.Split(newsubmsg, ",")
|
||||
var online []mcfetch.CacheResult
|
||||
for _, player := range players {
|
||||
playerName := strings.TrimSpace(player)
|
||||
plr, err := app.FetchMCPlayer(playerName)
|
||||
res_name := plr.Name
|
||||
res_uuid := plr.UUID
|
||||
if err != nil {
|
||||
log.Fatalf("Error fetching UUID: %v", err)
|
||||
return
|
||||
fmt.Println(fmt.Sprintf("Error fetching UUID: %v", err))
|
||||
continue
|
||||
}
|
||||
fmt.Printf("UUID of player %s: %s\n", res_name, res_uuid)
|
||||
|
||||
res_player := mcfetch.CacheResult{
|
||||
UUID: plr.UUID,
|
||||
Name: plr.Name,
|
||||
}
|
||||
online = append(online, res_player)
|
||||
//names, err := GetNameFromUUID(playerUUID)
|
||||
//if err != nil {
|
||||
// log.Fatalf("Error fetching names from UUID: %v", err)
|
||||
//}
|
||||
//fmt.Printf("Name history for UUID %s: %v\n", playerUUID, names)
|
||||
}
|
||||
app.sendPartyList(online)
|
||||
|
||||
} else if strings.HasPrefix(submsg, PartyListSeparatorLinePrefix) { // Party List
|
||||
last, _ := app.LogBuf.GetSecondToLast()
|
||||
@ -236,30 +241,25 @@ func (app *DemoApp) tailFile(path string, lineCh chan<- string) {
|
||||
|
||||
func (app *DemoApp) sendPartyList(ppl []mcfetch.CacheResult) {
|
||||
for _, user := range ppl {
|
||||
// Fetch player stats
|
||||
res, err := app.API.GetPlayerResponse(user.UUID)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get player data: %v", err)
|
||||
}
|
||||
|
||||
// Calculate derived stats
|
||||
res.Player.Stats.Bedwars.WLR = calculateRatio(res.Player.Stats.Bedwars.Wins, res.Player.Stats.Bedwars.Losses)
|
||||
res.Player.Stats.Bedwars.KDR = calculateRatio(res.Player.Stats.Bedwars.Kills, res.Player.Stats.Bedwars.Deaths)
|
||||
res.Player.Stats.Bedwars.FKDR = calculateRatio(res.Player.Stats.Bedwars.FinalKills, res.Player.Stats.Bedwars.FinalDeaths)
|
||||
res.Player.Stats.Bedwars.BBLR = calculateRatio(res.Player.Stats.Bedwars.BedsBroken, res.Player.Stats.Bedwars.BedsLost)
|
||||
res.Player.Stats.Bedwars.WLR = calcRatio(res.Player.Stats.Bedwars.Wins, res.Player.Stats.Bedwars.Losses)
|
||||
res.Player.Stats.Bedwars.KDR = calcRatio(res.Player.Stats.Bedwars.Kills, res.Player.Stats.Bedwars.Deaths)
|
||||
res.Player.Stats.Bedwars.FKDR = calcRatio(res.Player.Stats.Bedwars.FinalKills, res.Player.Stats.Bedwars.FinalDeaths)
|
||||
res.Player.Stats.Bedwars.BBLR = calcRatio(res.Player.Stats.Bedwars.BedsBroken, res.Player.Stats.Bedwars.BedsLost)
|
||||
|
||||
// Convert player struct to JSON
|
||||
playerJSON, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal player data: %v", err)
|
||||
}
|
||||
|
||||
// Append the player JSON to the stuff list
|
||||
var playerMap map[string]interface{}
|
||||
json.Unmarshal(playerJSON, &playerMap)
|
||||
app.PartyBuilder = append(app.PartyBuilder, playerMap)
|
||||
|
||||
// Send the list of JSON objects to the client
|
||||
message, err := json.Marshal(app.PartyBuilder)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal stuff: %v", err)
|
||||
@ -271,6 +271,9 @@ func (app *DemoApp) sendPartyList(ppl []mcfetch.CacheResult) {
|
||||
fmt.Println("Sent stuff:", app.PartyBuilder)
|
||||
println("Sending Done!")
|
||||
}
|
||||
|
||||
// Clear buffer so it only sends the current party list, not previous party lists
|
||||
app.PartyBuilder = []map[string]interface{}{}
|
||||
}
|
||||
|
||||
func (app *DemoApp) Start() {
|
||||
@ -284,7 +287,7 @@ func (app *DemoApp) Start() {
|
||||
}
|
||||
|
||||
var err error
|
||||
app.Client, err = netclient.NewClient("0.0.0.0", uuid.New().String())
|
||||
app.Client, err = netclient.NewClient("chat.itzilly.com", uuid.New().String())
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create client: %v", err)
|
||||
return
|
||||
@ -301,13 +304,6 @@ func (app *DemoApp) Start() {
|
||||
fmt.Printf("[DEV] Joined Branches\n")
|
||||
|
||||
app.Client.ListenForData()
|
||||
// Handle received data in a separate goroutine
|
||||
go func() {
|
||||
for data := range app.Client.DataChannel {
|
||||
fmt.Printf("Received data: %s\n", data)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case data, ok := <-app.Client.DataChannel:
|
||||
@ -388,7 +384,6 @@ func (app *DemoApp) HandleData(data string) {
|
||||
players = append(players, &wrapper.Player)
|
||||
}
|
||||
|
||||
// Pass the extracted players to DisplayPlayers
|
||||
app.DisplayPlayers(players)
|
||||
}
|
||||
|
||||
@ -399,7 +394,7 @@ func (app *DemoApp) DisplayPlayers(players []*hypixel.Player) {
|
||||
fmt.Println("|----------------------|------------|------------|")
|
||||
|
||||
for _, player := range players {
|
||||
fmt.Printf("| %-20s | %-10d | %-10.3f |\n",
|
||||
fmt.Printf("| %-20s | %-10d | %10.3f |\n",
|
||||
player.DisplayName,
|
||||
player.Achievements.BedwarsLevel,
|
||||
player.Stats.Bedwars.FKDR)
|
||||
|
Loading…
x
Reference in New Issue
Block a user