Skip to content

Commit

Permalink
feat utils GetMinecraftInfosFromNames
Browse files Browse the repository at this point in the history
  • Loading branch information
a3510377 committed May 5, 2024
1 parent 06b6737 commit 86a4c3f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 77 deletions.
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
data/
*.exe
108 changes: 33 additions & 75 deletions server/utils/minecraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io"
"net/http"
"sync"
)

type OnlineUUIDStruct struct {
Expand Down Expand Up @@ -33,7 +34,7 @@ func GetMinecraftInfoFromName(name string) (*OnlineUUIDStruct, error) {
return &onlineUUID, nil
}

func getMinecraftInfosFrom10Names(names []string) (*[]OnlineUUIDStruct, error) {
func GetMinecraftInfosFrom10Names(names []string) (*[]OnlineUUIDStruct, error) {
size := len(names)
if size > 10 {
return nil, errors.New("Too many names")
Expand Down Expand Up @@ -67,82 +68,39 @@ func getMinecraftInfosFrom10Names(names []string) (*[]OnlineUUIDStruct, error) {
return &result, nil
}

func GetMinecraftInfosFromNames(names ...string) ([]*OnlineUUIDStruct, error) {
func GetMinecraftInfosFromNames(names ...string) *[]OnlineUUIDStruct {
results := []OnlineUUIDStruct{}
max := len(names)
for i := 0; i < max; i += 10 {
end := i + 10
if end > max {
end = max
}

mu, wg := sync.Mutex{}, &sync.WaitGroup{}
for i := 0; i <= max; i += 10 {
end := min(i+10, max)
sliceNames := names[i:end]
result, err := getMinecraftInfosFrom10Names(sliceNames)
if err != nil {
continue
}

results = append(results, *result...)
go func(names []string) {
defer wg.Done()

result, err := GetMinecraftInfosFrom10Names(names)
if err == nil {
mu.Lock()
results = append(results, *result...)
mu.Unlock()
return
}

for _, name := range names {
go func(name string) {
defer wg.Done()
info, _ := GetMinecraftInfoFromName(name)
if info != nil {
mu.Lock()
results = append(results, *info)
mu.Unlock()
}
}(name)
wg.Add(1)
}
}(sliceNames)
wg.Add(1)
}
// if err != nil {
// getOnes(sliceNames...)
// return
// }
// defer resp.Body.Close()

// body, err := io.ReadAll(resp.Body)
// if err != nil {
// getOnes(sliceNames...)
// return
// }

// onlineUUID := []OnlineUUIDStruct{}
// if err := json.Unmarshal(body, &onlineUUID); err != nil {
// getOnes(sliceNames...)
// return
// }
return nil, nil

// go func(start, end int) {
// defer wg.Done()

// sliceNames := names[start:end]
// nameString, err := json.Marshal(sliceNames)
// if err != nil {
// getOnes(sliceNames...)
// return
// }

// resp, err := http.Post("https://api.mojang.com/profiles/minecraft", "application/json", bytes.NewBuffer(nameString))
// if err != nil {
// getOnes(sliceNames...)
// return
// }
// defer resp.Body.Close()

// body, err := io.ReadAll(resp.Body)
// if err != nil {
// getOnes(sliceNames...)
// return
// }

// onlineUUID := []OnlineUUIDStruct{}
// if err := json.Unmarshal(body, &onlineUUID); err != nil {
// getOnes(sliceNames...)
// return
// }

// // Check if the length is equal
// if len(onlineUUID) != len(sliceNames) {
// getOnes(sliceNames...)
// return
// }

// mu.Lock()
// defer mu.Unlock()
// for _, u := range onlineUUID {
// result[u.Name] = u.ID
// }
// }(i, end)

wg.Wait()
return &results
}
30 changes: 28 additions & 2 deletions server/utils/minecraft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGetMinecraftInfosFrom10Names(t *testing.T) {
"Zuri", "Makena", "Kai", "Efe",
}

infos, err := getMinecraftInfosFrom10Names(names)
infos, err := GetMinecraftInfosFrom10Names(names)
if err != nil {
t.Error(err)
} else {
Expand All @@ -57,8 +57,34 @@ func TestGetMinecraftInfosFrom10Names(t *testing.T) {

// test 2.
names = []string{"-test", "+test"}
infos, err = getMinecraftInfosFrom10Names(names)
infos, err = GetMinecraftInfosFrom10Names(names)
if err != nil {
t.Error(err)
}
}

func TestGetMinecraftInfosFromNames(t *testing.T) {
names := []string{
"Steve", "Alex", "Noor", "Sunny", "Ari",
"Zuri", "Makena", "Kai", "Efe", "Test", "Sleep",
}

infos := GetMinecraftInfosFromNames(names...)
players := mapset.NewSet(
OnlineUUIDStruct{ID: "ec561538f3fd461daff5086b22154bce", Name: "Alex"},
OnlineUUIDStruct{ID: "938e960d50ab489b9b2aaf3751942989", Name: "Ari"},
OnlineUUIDStruct{ID: "20bf454f34e34010a378613546e3d0f9", Name: "efe"},
OnlineUUIDStruct{ID: "cf9858b6ed4946538e47f0e4214539f7", Name: "Kai"},
OnlineUUIDStruct{ID: "6c4bc87ce82944efa1ad63d45e2b9545", Name: "Makena"},
OnlineUUIDStruct{ID: "2d9f2227592b481d8433d13b69473ccc", Name: "noor"},
OnlineUUIDStruct{ID: "8667ba71b85a4004af54457a9734eed7", Name: "Steve"},
OnlineUUIDStruct{ID: "bafbe1cb77b348099fa3c89604bda644", Name: "Sunny"},
OnlineUUIDStruct{ID: "f5e039b93b8a45109ee8e7552e098c55", Name: "Zuri"},
OnlineUUIDStruct{ID: "d8d5a9237b2043d8883b1150148d6955", Name: "Test"},
OnlineUUIDStruct{ID: "cfea6145d3e74d04849a9020b3812792", Name: "Sleep"},
)

if diff := players.Difference(mapset.NewSet(*infos...)); diff.Cardinality() != 0 {
t.Errorf("Expected %v, got nil", diff)
}
}

0 comments on commit 86a4c3f

Please sign in to comment.