-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type OnlineUUIDStruct struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func GetMinecraftInfoFromName(name string) (*OnlineUUIDStruct, error) { | ||
resp, err := http.Get("https://api.mojang.com/users/profiles/minecraft/" + name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
onlineUUID := OnlineUUIDStruct{} | ||
if err = json.Unmarshal(body, &onlineUUID); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &onlineUUID, nil | ||
} | ||
|
||
func getMinecraftInfosFrom10Names(names []string) (*[]OnlineUUIDStruct, error) { | ||
size := len(names) | ||
if size > 10 { | ||
return nil, errors.New("Too many names") | ||
} | ||
|
||
if size == 0 { | ||
return &[]OnlineUUIDStruct{}, nil | ||
} | ||
|
||
nameString, err := json.Marshal(names) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := http.Post("https://api.mojang.com/profiles/minecraft", "application/json", bytes.NewBuffer(nameString)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := []OnlineUUIDStruct{} | ||
if err = json.Unmarshal(body, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func GetMinecraftInfosFromNames(names ...string) ([]*OnlineUUIDStruct, error) { | ||
results := []OnlineUUIDStruct{} | ||
max := len(names) | ||
for i := 0; i < max; i += 10 { | ||
end := i + 10 | ||
if end > max { | ||
end = max | ||
} | ||
|
||
sliceNames := names[i:end] | ||
result, err := getMinecraftInfosFrom10Names(sliceNames) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
results = append(results, *result...) | ||
} | ||
// 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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
) | ||
|
||
func TestGetMinecraftInfoFromName(t *testing.T) { | ||
for _, name := range []string{"Steve", "StevE"} { | ||
info, err := GetMinecraftInfoFromName(name) | ||
if err != nil { | ||
t.Error(err) | ||
continue | ||
} | ||
|
||
if info == nil { | ||
t.Errorf("Expected info, got nil") | ||
continue | ||
} | ||
|
||
if info.Name != "Steve" { | ||
t.Errorf("Expected name Steve, got %s", info.Name) | ||
} else if info.ID != "8667ba71b85a4004af54457a9734eed7" { | ||
t.Errorf("Expected 8667ba71b85a4004af54457a9734eed7, got %s", info.ID) | ||
} | ||
} | ||
} | ||
|
||
func TestGetMinecraftInfosFrom10Names(t *testing.T) { | ||
names := []string{ | ||
"Steve", "Alex", "Noor", "Sunny", "Ari", | ||
"Zuri", "Makena", "Kai", "Efe", | ||
} | ||
|
||
infos, err := getMinecraftInfosFrom10Names(names) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
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"}, | ||
) | ||
fmt.Println(players.Difference(mapset.NewSet(*infos...))) | ||
|
||
// waitMatchData := mapset.NewSet(*infos...) | ||
// waitMatchData.Difference() | ||
// for _, player := range players { | ||
// if waitMatchData.Contains(player) { | ||
// waitMatchData.Remove(player) | ||
// } else { | ||
// t.Errorf("Expected %v, got nil", player) | ||
// } | ||
// } | ||
// if waitMatchData.Cardinality() != 0 { | ||
// t.Errorf("Expected 0, got %d", waitMatchData.Cardinality()) | ||
// } | ||
} |