Skip to content

Commit 92f7c60

Browse files
committed
Some code documentation.
refs #1.
1 parent bacb59c commit 92f7c60

File tree

6 files changed

+131
-61
lines changed

6 files changed

+131
-61
lines changed

src/Core/Board.hs

-7
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,6 @@ parseBoardKey rules bk = foldr set (buildBoard orient bsize) list
595595
[(lbl, Piece King Second) | lbl <- labelSetToList (bkSecondKings bk)]
596596
set (label, piece) board = setPiece' label piece board
597597

598-
boardLineCounts :: Board -> BoardLineCounts
599-
boardLineCounts board =
600-
let (nrows,ncols) = bSize board
601-
go row = fromIntegral $ length [col | col <- [0..ncols-1], not (isFree' (Label col row) board)]
602-
counts = map go [0..nrows-1]
603-
in BoardLineCounts $ counts ++ replicate (16 - fromIntegral nrows) 0
604-
605598
-- | Generic implementation of @getGameResult@, which suits most rules.
606599
-- This can not, however, recognize draws.
607600
genericGameResult :: GameRules rules => rules -> Board -> Maybe GameResult

src/Core/Game.hs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{-# LANGUAGE DeriveGeneric #-}
22
{-# LANGUAGE ExistentialQuantification #-}
33

4+
{-
5+
- Game is a record for interaction between two players.
6+
- It is created in New status, and there are no players in the game at that moment.
7+
- Then two players are attached to the game (in any order). Each player can be a
8+
- human user or an AI (NB: having two AI players in one game is not supported
9+
- currently by Supervisor).
10+
- After both players are attached, the game can be switched to Running state.
11+
-}
12+
413
module Core.Game where
514

615
import Control.Monad.State
@@ -9,10 +18,10 @@ import Control.Monad.Except
918
import Core.Types
1019
import Core.Board
1120

12-
-- import Debug.Trace
13-
21+
-- | A monad to track game's state
1422
type GameM a = ExceptT Error (State Game) a
1523

24+
-- | Initialize Game instance
1625
mkGame :: GameRules rules => rules -> Int -> Maybe BoardRep -> Game
1726
mkGame rules id mbBoardRep =
1827
let board = case mbBoardRep of
@@ -30,21 +39,25 @@ mkGame rules id mbBoardRep =
3039
gMsgbox2 = []
3140
}
3241

42+
-- | get currently possible moves in this game
3343
gamePossibleMoves :: GameM [Move]
3444
gamePossibleMoves = do
3545
SomeRules rules <- gets gRules
3646
board <- gets (gsCurrentBoard . gState)
3747
currentSide <- gets (gsSide . gState)
3848
return $ map pmMove $ possibleMoves rules currentSide board
3949

50+
-- | get current state of the game
4051
gameState :: GameM (Side, GameStatus, Board)
4152
gameState = do
4253
st <- gets gState
4354
status <- gets gStatus
4455
return (gsSide st, status, gsCurrentBoard st)
4556

57+
-- | Move result. Contains resulting board and a list of notification messages.
4658
data GMoveRs = GMoveRs Board [Notify]
4759

60+
-- | Perform specified move
4861
doMoveRq :: Side -> Move -> GameM GMoveRs
4962
doMoveRq side move = do
5063
currentSide <- gets (gsSide . gState)
@@ -70,6 +83,7 @@ doMoveRq side move = do
7083
_ -> return ()
7184
return $ GMoveRs board' messages
7285

86+
-- | Perform specified move, parsing it from MoveRep
7387
doMoveRepRq :: Side -> MoveRep -> GameM GMoveRs
7488
doMoveRepRq side mRep = do
7589
SomeRules rules <- gets gRules
@@ -79,8 +93,10 @@ doMoveRepRq side mRep = do
7993
AmbigousMove moves -> throwError $ AmbigousMoveError $ map (moveRep rules side . pmMove) moves
8094
Parsed move -> doMoveRq side move
8195

96+
-- | Undo result
8297
data GUndoRs = GUndoRs Board [Notify]
8398

99+
-- | Execute undo
84100
doUndoRq :: Side -> GameM GUndoRs
85101
doUndoRq side = do
86102
currentSide <- gets (gsSide . gState)

src/Core/Json.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@ instance ToJSON RsPayload where
127127
toJSON (LobbyRs games) = toJSON games
128128
toJSON (NotationRs size list) = object ["size" .= size, "notation" .= list]
129129

130-
instance ToJSON SupervisorRs where
131-
toJSON (SupervisorRs payload messages) = object ["response" .= payload, "messages" .= messages]
130+
instance ToJSON Response where
131+
toJSON (Response payload messages) = object ["response" .= payload, "messages" .= messages]
132132

src/Core/Rest.hs

+12-12
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ restServer = do
7272
Right board -> do
7373
gameId <- liftCheckers $ newGame rules board
7474
liftCheckers $ $info "Created new game #{} with board: {}" (gameId, show board)
75-
json $ SupervisorRs (NewGameRs gameId) []
75+
json $ Response (NewGameRs gameId) []
7676

7777
post "/game/:id/attach/ai/:side" $ do
7878
gameId <- param "id"
@@ -85,24 +85,24 @@ restServer = do
8585
liftCheckers $ $info "Attached AI: {} to game #{}" (show ai, gameId)
8686
liftCheckers $ initAiStorage rules ai
8787
liftCheckers $ attachAi gameId side ai
88-
json $ SupervisorRs AttachAiRs []
88+
json $ Response AttachAiRs []
8989

9090
post "/game/:id/attach/:name/:side" $ do
9191
gameId <- param "id"
9292
name <- param "name"
9393
side <- param "side"
9494
liftCheckers $ registerUser gameId side name
95-
json $ SupervisorRs RegisterUserRs []
95+
json $ Response RegisterUserRs []
9696

9797
post "/game/:id/run" $ do
9898
gameId <- param "id"
9999
liftCheckers $ runGame gameId
100-
json $ SupervisorRs RunGameRs []
100+
json $ Response RunGameRs []
101101

102102
get "/game/:id/state" $ do
103103
gameId <- param "id"
104104
rs <- liftCheckers $ getState gameId
105-
json $ SupervisorRs rs []
105+
json $ Response rs []
106106

107107
get "/game/:id/fen" $ do
108108
gameId <- param "id"
@@ -120,41 +120,41 @@ restServer = do
120120
moveRq <- jsonData
121121
board <- liftCheckers $ doMove gameId name moveRq
122122
messages <- liftCheckers $ getMessages name
123-
json $ SupervisorRs (MoveRs board) messages
123+
json $ Response (MoveRs board) messages
124124

125125
get "/game/:id/moves/:name" $ do
126126
gameId <- param "id"
127127
name <- param "name"
128128
side <- liftCheckers $ getSideByUser gameId name
129129
moves <- liftCheckers $ getPossibleMoves gameId side
130130
messages <- liftCheckers $ getMessages name
131-
json $ SupervisorRs (PossibleMovesRs moves) messages
131+
json $ Response (PossibleMovesRs moves) messages
132132

133133
post "/game/:id/undo/:name" $ do
134134
gameId <- param "id"
135135
name <- param "name"
136136
board <- liftCheckers $ doUndo gameId name
137137
messages <- liftCheckers $ getMessages name
138-
json $ SupervisorRs (UndoRs board) messages
138+
json $ Response (UndoRs board) messages
139139

140140
get "/poll/:name" $ do
141141
name <- param "name"
142142
messages <- liftCheckers $ getMessages name
143-
json $ SupervisorRs (PollRs messages) []
143+
json $ Response (PollRs messages) []
144144

145145
get "/lobby/:rules" $ do
146146
rules <- param "rules"
147147
games <- liftCheckers $ getGames (Just rules)
148-
json $ SupervisorRs (LobbyRs games) []
148+
json $ Response (LobbyRs games) []
149149

150150
get "/lobby" $ do
151151
games <- liftCheckers $ getGames Nothing
152-
json $ SupervisorRs (LobbyRs games) []
152+
json $ Response (LobbyRs games) []
153153

154154
get "/notation/:rules" $ do
155155
rules <- param "rules"
156156
(size, notation) <- liftCheckers $ getNotation rules
157-
json $ SupervisorRs (NotationRs size notation) []
157+
json $ Response (NotationRs size notation) []
158158

159159
runRestServer :: Checkers ()
160160
runRestServer = do

0 commit comments

Comments
 (0)