Merge pull request 'color is optional' (#3) from mertoalex/ballsoffline:main into main

Reviewed-on: #3
This commit is contained in:
gnawmon 2024-04-23 16:21:36 +00:00
commit b04461b49d
4 changed files with 23 additions and 5 deletions

View File

@ -8,7 +8,9 @@ To play first create an accounts.json, you can look example file [account.def.js
If you're on linux, you can do:
```bash
echo '{"username":"'$USER'","color":"000000"}' > account.json
echo '{"username":"'$USER'"}' > account.json
# or
echo '{"username":"'$USER'","color":"DEAD00"}' > account.json
```
## Credits

2
bf.go
View File

@ -11,7 +11,7 @@ const (
screenWidth = 1280
screenHeight = 720
ballRadius = 12
debug = false
debug = false
)
var (

View File

@ -15,10 +15,26 @@ func checkValid(something any) bool {
return true
}
func makeDefault(variable *map[string]interface{}, Type string) {
switch Type {
case "color": (*variable)[Type] = "000000"
default: panic(fmt.Sprint("\"", Type, "\" isn't a type!"))
}
}
func checkUserDataValid(userdata map[string]interface{}) {
for _, data := range []string{"username", "color"} {
//required
for _, data := range []string{"username"} {
if !checkValid(userdata[data]) {
panic(fmt.Sprint("\"", data, "\" isn't defined in userdata:\n", userdata))
panic(fmt.Sprint("\"", data, "\" isn't defined in userdata:\n\t", userdata))
}
}
//optional
for _, data := range []string{"color"} {
if !checkValid(userdata[data]) {
fmt.Printf("WARNING: \"%s\" isn't defined in userdata:\n\t%s\n\tgonna use default variable for that\n", data, userdata)
makeDefault(&userdata, data)
}
}
}

View File

@ -9,7 +9,7 @@ func loadJson(file string) map[string]interface{} {
jsonRaw, Error := os.ReadFile(file)
check(Error)
var jsonData map[string]interface{}
check(json.Unmarshal([]byte(jsonRaw), &jsonData))
check(json.Unmarshal([]byte(jsonRaw), &jsonData))
return jsonData
}