aboutsummaryrefslogtreecommitdiff
path: root/server/lib/Cafp/Game.hs
blob: a5f367a890d80c4ebb8476c35b09cdcb1c2cb3aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
module Cafp.Game
    ( PlayerId
    , Game (..)

    , newGame
    , joinGame
    , leaveGame

    , gameViewForPlayer
    ) where

import           Cafp.Messages
import qualified Data.HashMap.Strict as HMS
import           Data.Text           (Text)
import qualified Data.Text           as T

type PlayerId = Int

data Game = Game
    { gamePlayers      :: !(HMS.HashMap Int Text)
    , gameNextPlayerId :: !Int
    } deriving (Show)

newGame :: Game
newGame = Game HMS.empty 1

joinGame :: Game -> (PlayerId, Game)
joinGame game@Game {..} =
    let pid = gameNextPlayerId
        name = "Player " <> T.pack (show pid)
        players = HMS.insert pid name gamePlayers in
    (pid, game {gameNextPlayerId = pid + 1, gamePlayers = players})

leaveGame :: PlayerId -> Game -> Game
leaveGame pid game = game {gamePlayers = HMS.delete pid $ gamePlayers game}

gameViewForPlayer :: PlayerId -> Game -> GameView
gameViewForPlayer _ = GameView . map snd . HMS.toList . gamePlayers