Skip to content

Commit

Permalink
added colour if player doesn't pick one themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
brensch committed Mar 19, 2022
1 parent 78c9374 commit 7b2e765
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 21 deletions.
23 changes: 20 additions & 3 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package battleword
import (
"bytes"
"context"
"crypto/sha1"
"crypto/tls"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -75,13 +76,13 @@ type PlayerGameState struct {
GuessDurationsNS []int64 `json:"guess_durations_ns,omitempty"`
}

func ValidDefinition(definition PlayerDefinition) (bool, error) {
func ValidColour(colour string) (bool, error) {

if !strings.HasPrefix(definition.Colour, "#") {
if !strings.HasPrefix(colour, "#") {
return false, fmt.Errorf("colour must start with a #")
}

parsedColour, err := strconv.ParseUint(strings.Replace(definition.Colour, "#", "", 1), 16, 64)
parsedColour, err := strconv.ParseUint(strings.Replace(colour, "#", "", 1), 16, 64)
if err != nil {
return false, err
}
Expand All @@ -94,6 +95,17 @@ func ValidDefinition(definition PlayerDefinition) (bool, error) {
return true, nil
}

func ColourFromString(input string) string {

hash := sha1.New()
hash.Write([]byte(input))
sum := hash.Sum(nil)

// The first 3 bytes of the sum will give us a valid hex colour
return fmt.Sprintf("#%X", sum[:3])

}

func InitPlayer(mu *sync.Mutex, log logrus.FieldLogger, uri string) (*Player, error) {

// we want the GetDefinition call to be the thing that wakes an api up if people are hosting
Expand Down Expand Up @@ -125,6 +137,11 @@ func InitPlayer(mu *sync.Mutex, log logrus.FieldLogger, uri string) (*Player, er
return nil, fmt.Errorf("failed to retrieve definition from player: %+v", err)
}

validColour, _ := ValidColour(definition.Colour)
if !validColour {
definition.Colour = ColourFromString(definition.Description)
}

client := &http.Client{
Transport: &http.Transport{
// odds are someone will be hosting this jankily.
Expand Down
79 changes: 61 additions & 18 deletions player_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,82 @@
package battleword

import (
"encoding/json"
"fmt"
"testing"
)

func TestValidDefinition(t *testing.T) {
defString := []byte(`
{
"name": "Sendooooo",
"description": "yoloest",
"concurrent_connection_limit": 3,
"colour": "#fcba03"
var (
validColours = []string{
"#60283c",
"#60283C",
"#4b2860",
}
`)
invalidColours = []string{
"60283c",
"#60283C1",
"##4b2860",
}
)

var def PlayerDefinition
err := json.Unmarshal(defString, &def)
if err != nil {
t.Log(err)
t.FailNow()
func TestValidColour(t *testing.T) {

for _, validColour := range validColours {

valid, err := ValidColour(validColour)
if err != nil {
t.Log(err)
t.FailNow()
}

if !valid {
t.Log("should be valid")
t.FailNow()
}
}

for _, invalidColour := range invalidColours {

valid, _ := ValidColour(invalidColour)
if valid {
t.Log("should not be valid")
t.FailNow()
}
}

fmt.Println(def.Colour)
}

func TestColourFromString(t *testing.T) {

colour := ColourFromString("yolo swagginss")
fmt.Println(colour)

valid, err := ValidDefinition(def)
valid, err := ValidColour(colour)
if err != nil {
t.Log(err)
t.FailNow()
}

if !valid {
t.Log("not valid")
t.FailNow()
t.Log("did not get valid colour from colourfromstring")
t.Fail()
}
}

func FuzzColourFromString(f *testing.F) {
f.Add("yeet")
f.Fuzz(func(t *testing.T, s string) {
colour := ColourFromString(s)
fmt.Println(colour)

valid, err := ValidColour(colour)
if err != nil {
t.Log(err)
t.FailNow()
}

if !valid {
t.Log("did not get valid colour from colourfromstring")
t.Fail()
}
})
}

0 comments on commit 7b2e765

Please sign in to comment.