Skip to content

Commit

Permalink
make the ID opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Apr 5, 2021
1 parent 068a4bb commit 5af16b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/Player.elm
@@ -1,13 +1,22 @@
module Player exposing (Player, decoder, encode, incrementMatchesPlayed, init, setRating)
module Player exposing (Player, PlayerId, decoder, encode, incrementMatchesPlayed, init, playerIdFromIntForTestOnly, setRating)

import Elo
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode exposing (Value)
import Murmur3


type PlayerId
= PlayerId Int


playerIdFromIntForTestOnly : Int -> PlayerId
playerIdFromIntForTestOnly =
PlayerId


type alias Player =
{ id : Int
{ id : PlayerId
, name : String
, rating : Int
, matches : Int
Expand All @@ -16,7 +25,7 @@ type alias Player =

init : String -> Player
init name =
{ id = Murmur3.hashString 0 name
{ id = PlayerId (Murmur3.hashString 0 name)
, name = name
, rating = Elo.initialRating
, matches = 0
Expand All @@ -41,6 +50,7 @@ decoder =
, Decode.field "name" Decode.string
|> Decode.map (Murmur3.hashString 0)
]
|> Decode.map PlayerId
)
(Decode.field "name" Decode.string)
(Decode.field "rating" Decode.int)
Expand All @@ -49,8 +59,12 @@ decoder =

encode : Player -> Value
encode { id, name, rating, matches } =
let
(PlayerId idInt) =
id
in
Encode.object
[ ( "id", Encode.int id )
[ ( "id", Encode.int idInt )
, ( "name", Encode.string name )
, ( "rating", Encode.int rating )
, ( "matches", Encode.int matches )
Expand Down
7 changes: 5 additions & 2 deletions tests/PlayerTest.elm
Expand Up @@ -21,7 +21,10 @@ roundTripDecoderTest =
playerFuzzer : Fuzzer Player
playerFuzzer =
Fuzz.map4 Player
(Fuzz.map (Murmur3.hashString 0) nameFuzzer)
(nameFuzzer
|> Fuzz.map (Murmur3.hashString 0)
|> Fuzz.map Player.playerIdFromIntForTestOnly
)
nameFuzzer
(Fuzz.intRange 1000 3000)
(Fuzz.intRange 0 50)
Expand Down Expand Up @@ -51,6 +54,6 @@ decoderTest =
]
|> Decode.decodeValue Player.decoder
|> Result.map .id
|> Expect.equal (Ok 123038886)
|> Expect.equal (Ok (Player.playerIdFromIntForTestOnly 123038886))
]
]

0 comments on commit 5af16b2

Please sign in to comment.