-
Notifications
You must be signed in to change notification settings - Fork 0
/
funthello.hs
302 lines (245 loc) · 10.1 KB
/
funthello.hs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
-- "THE BEER-WARE LICENSE" (Revision 42):
-- Albatrouss and <[email protected]> wrote this software. As long as you retain
-- this notice you can do whatever you want with this stuff. If we meet some
-- day, and you think this stuff is worth it, you can buy me a beer in return.
-- -- Albatrouss and Skruppy
module Main where
import AI as AI
import Conf as C
import Conf.Args as CA
import Conf.Gui as CG
import Control.Concurrent
import Control.Concurrent.MVar
import Control.Monad
import Data.Array
import Data.Maybe
import Data.String.Utils
import Data.Version (showVersion)
import GameGUI as GG
import Graphics.UI.Gtk
import Graphics.UI.Gtk.Windows.MessageDialog
import Net
import Network.Socket as NS
import PP as PP
import Paths_funthello (version)
import Sm as S
import System.Console.ANSI
import System.Environment
import System.Exit
import System.IO
import Util
-- The name speaks for it self. Here you are looking at the beautiful main-l↺↺p.
(↺) :: Handle -> StepResult -> IO (Either String (GameData, Maybe Int, Board))
(↺) hdl (SmEnd gameData winner board) = return $ Right (gameData, winner, board)
(↺) hdl (SmError msg) = return $ Left msg
(↺) hdl (SmOk s o) = do
i <- converse hdl o
let (s', io) = smStep s i
io
(↺) hdl s'
play :: Cfg -> NS.Socket -> IO (Either String (GameData, Maybe Int, Board))
play cfg socket = do
-- Convert socket to unbuffered handle
hdl <- socketToHandle socket ReadWriteMode
hSetBuffering hdl NoBuffering
-- Main loop
res <- (↺) hdl $ smCreate cfg
-- So close!
hClose hdl
return res
finalizeCfg :: IntermediateCfg -> IO (Either String (String, Maybe Int, NS.Socket))
finalizeCfg IntermediateCfg { C.host = Nothing } = return $ Left "Missing hostname"
finalizeCfg IntermediateCfg { C.port = Nothing } = return $ Left "Missing port"
finalizeCfg IntermediateCfg { C.gameId = Nothing } = return $ Left "Missing game ID"
finalizeCfg IntermediateCfg
{ C.host = Just host'
, C.port = Just port'
, C.gameId = Just gameId'
, C.player = player'
}
= do
res <- Net.connect host' port'
case res of
Right socket -> return $ Right (gameId', player', socket)
Left msg -> return $ Left msg
printGameData :: String -> GameData -> IO ()
printGameData gameId gameData = do
putStrLn $ "Server Version: "++(show $ serverMajor gameData)++"."++(show $ serverMinor gameData)
putStrLn $ " Game Type: "++(gameType gameData)
putStrLn $ " Game Name: "++(gameName gameData)
putStrLn $ " Game Id: "++(gameId)++" (Watch: http://sysprak.priv.lab.nm.ifi.lmu.de/sysprak/"++(gameType gameData)++"/"++(gameId)++")"
putStrLn $ ""
putStrLn $ "Players:"
mapM_ printPlayer $ assocs $ players gameData
where
printPlayer (i, (PlayerItem playerName' isReady' itsMe')) = do
putStr $ " "++(show i)++": "
setSGR $
if itsMe' then [SetColor Foreground Vivid Green, SetConsoleIntensity BoldIntensity]
else if isReady' then [SetColor Foreground Dull Green]
else [SetColor Foreground Vivid Yellow]
putStr playerName'
setSGR [Reset]
putStrLn $
if itsMe' then " (You)"
else if isReady' then " (Ready)"
else " (Play: http://sysprak.priv.lab.nm.ifi.lmu.de/sysprak/"++(gameType gameData)++"/"++(gameId)++"#"++(show i)++")"
{-== GUI MODE ==-}
guiPreAi :: Gui -> String -> GameData -> Array (Int, Int) String -> Int -> IO ()
guiPreAi gui gameId gameData board time = do
putStrLn $ "Now I can think "++(show time)++"ms about the best move on the following board:"
prettyPrint board
updateBoard gui board
guiAi :: Gui -> String -> GameData -> Array (Int,Int) String -> Int -> (String, IO () )
guiAi gui gameId gameData board time =
(move, do
putStrLn $ "I decided to do: "++move
prettyPrint nextBoard
updateBoard gui nextBoard
)
where
(move, nextBoard) = AI.getNextMove board gameData
startMainGui :: (String, Maybe Int, NS.Socket) -> IO Bool
startMainGui (gameId', player', socket) = do
gui <- guiNew
forkIO $ do
let cfg = S.Cfg {
S.gameId = gameId'
, S.player = player'
, S.gameDataComplete = updateGameData gui
, S.preAi = guiPreAi gui
, S.ai = guiAi gui
}
-- Do the game.
res <- play cfg socket
-- Check the result of the game.
case res of
-- The game ended without protocol issues.
Right (gameData, winner, board) -> do
-- Print the final board
prettyPrint board
updateBoard gui board
case winner of
-- The game ended with a WINNER (and a looser)
Just winnerNr -> do
let winnerRecord = (players gameData) ! winnerNr
if itsMe winnerRecord
-- We won
then do
let msg = "You won"
putStrLn msg
showGuiMsg gui MessageInfo msg
-- We lost
else do
let msg = (playerName winnerRecord) ++ " won"
putStrLn msg
showGuiMsg gui MessageError msg
-- The game ended in a DRAW
Nothing -> do
let msg = "The game ended in a draw"
putStrLn msg
showGuiMsg gui MessageInfo msg
-- ERROR in game. We got some protocol issues.
Left msg -> do
putStrLn msg
showGuiMsg gui MessageError msg
runGui gui
-- The GUI always exits successfully (to big to fail :)
return True
guiMode :: IntermediateCfg -> IO Bool
guiMode cfg = do
-- Init GTK once before any other GTK call
initGUI
-- Try to get a config from the user via GUI
cfg' <- CG.getCfg cfg finalizeCfg
-- If we got a valid config, start the main GUI, otherwise return successfully
maybe (return True) startMainGui cfg'
{-== CONSOLE MODE ==-}
consoleGameDataComplete :: String -> GameData -> IO ()
consoleGameDataComplete gameId gameData = do
printGameData gameId gameData
consolePreAi :: String -> GameData -> Array (Int,Int) String -> Int -> IO ()
consolePreAi gameId gameData board time = do
putStrLn $ "Now I can think "++(show time)++"ms about the best move on the following board:"
prettyPrint board
consoleAi :: String -> GameData -> Array (Int,Int) String -> Int -> (String ,IO ())
consoleAi gameId gameData board time =
(move, do
putStrLn $ "I decided to do: "++move
prettyPrint nextBoard
)
where
(move, nextBoard) = AI.getNextMove board gameData
consoleMode :: IntermediateCfg -> IO Bool
consoleMode cfg = do
res <- finalizeCfg cfg
case res of
Right (gameId', player', socket) -> do
let cfg' = S.Cfg {
S.gameId = gameId'
, S.player = player'
, S.gameDataComplete = consoleGameDataComplete
, S.preAi = consolePreAi
, S.ai = consoleAi
}
-- Do the game.
res <- play cfg' socket
-- Check the result of the game.
case res of
-- The game ended without protocol issues.
Right (gameData, winner, board) -> do
-- Print the final board
prettyPrint board
case winner of
-- The game ended with a WINNER (and a looser)
Just winnerNr -> do
let winnerRecord = (players gameData) ! winnerNr
if itsMe winnerRecord
-- We won
then putStrLn "You won"
-- We lost
else putStrLn $ (playerName winnerRecord) ++ " won"
-- The game ended in a DRAW
Nothing -> do
putStrLn "The game ended in a draw"
-- Exit successfully
return True
-- Error in game
Left msg -> do
putStrLn msg
return False
-- Configuration error
Left msg -> do
putStrLn msg
return False
main :: IO ()
main = do
-- Config from command line
argsCfg <- CA.getCfg
-- http://patorjk.com/software/taag/#p=display&f=Big&t=funthello
setSGR [SetConsoleIntensity BoldIntensity]
putStrLn " ______ _ _ _ _"
putStrLn " | ___| | | | | | | |"
putStrLn " | |_ _ _ _ __ | |_| |__ ___| | | ___"
putStrLn " | _| | | | '_ \\| __| '_ \\ / _ \\ | |/ _ \\"
putStrLn " | | | |_| | | | | |_| | | | __/ | | (_) |"
putStrLn " \\_| \\__,_|_| |_|\\__|_| |_|\\___|_|_|\\___/"
setSGR [Reset]
putStrLn ""
setSGR [SetColor Foreground Dull Green]
putStr " Loading version "
putStr $ showVersion version
putStr " "
setSGR [SetBlinkSpeed SlowBlink]
putStr "_"
putStrLn ""
setSGR [Reset]
putStrLn ""
let cfg = mergeCfg [argsCfg, defaultCfg]
progName <- getProgName
ret <- if endswith "-gui" progName
then guiMode cfg
else consoleMode cfg
if ret
then exitSuccess
else exitFailure