Skip to content

Commit

Permalink
Keep track of the score
Browse files Browse the repository at this point in the history
  • Loading branch information
magopian committed Aug 6, 2019
1 parent 49ca518 commit 84d0867
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/Main.elm
Expand Up @@ -16,6 +16,7 @@ type alias Model =
, rightPaddleMovement : PaddleMovement
, leftPaddleMovement : PaddleMovement
, gameStatus : GameStatus
, score : Score
}


Expand Down Expand Up @@ -71,6 +72,12 @@ type PlayerAction
| LeftPaddleDown


type alias Score =
{ rightPlayerScore : Int
, leftPlayerScore : Int
}


type alias Flags =
()

Expand All @@ -83,6 +90,10 @@ init _ =
, rightPaddleMovement = NotMoving
, leftPaddleMovement = NotMoving
, gameStatus = NoWinner
, score =
{ rightPlayerScore = 0
, leftPlayerScore = 0
}
}
, Cmd.none
)
Expand Down Expand Up @@ -160,10 +171,10 @@ update msg model =
updatedLeftPaddle =
updatePaddle model.leftPaddleMovement model.leftPaddle

( gameStatus, cmd ) =
( gameStatus, score, cmd ) =
case maybeWinner updatedBall of
Nothing ->
( NoWinner, Cmd.none )
( NoWinner, model.score, Cmd.none )

Just player ->
let
Expand All @@ -174,14 +185,19 @@ update msg model =
delayCmd =
Process.sleep 500
|> Task.perform alwaysSleepDone

updatedScore =
updateScores model.score player
|> Debug.log "score"
in
( Winner player, delayCmd )
( Winner player, updatedScore, delayCmd )
in
( { model
| ball = updatedBall
, rightPaddle = updatedRightPaddle
, leftPaddle = updatedLeftPaddle
, gameStatus = gameStatus
, score = score
}
, cmd
)
Expand Down Expand Up @@ -306,6 +322,16 @@ maybeWinner ball =
Nothing


updateScores : Score -> Player -> Score
updateScores score winner =
case winner of
RightPlayer ->
{ score | rightPlayerScore = score.rightPlayerScore + 1 }

LeftPlayer ->
{ score | leftPlayerScore = score.leftPlayerScore + 1 }


view : Model -> Svg.Svg Msg
view { ball, rightPaddle, leftPaddle } =
svg
Expand Down

0 comments on commit 84d0867

Please sign in to comment.