initial framework added
[fp1415-soccerfun.git] / src / Game / matchLog.icl
1 implementation module matchLog
2
3 import StdEnvExt, fileIO
4 import matchControl
5
6 logFile :: String
7 logFile = "SoccerFun.log"
8
9 getWhatToLog :: !*env -> (!WhatToLog,!*env) | FileSystem env
10 getWhatToLog env
11 = case readFile fileStr env of
12 (Just line,env) = (fromString line,env)
13 (nothing, env) = (defaultLogging, env)
14 where
15 fileStr = "logMatch.conf"
16
17 instance toString WhatToLog where
18 toString l = "{footballerActions=" <+++ l.footballerActions +++
19 ",fbPositions=" <+++ l.fbPositions +++
20 ",refEvents=" <+++ l.refEvents +++
21 ",ballPosition=" <+++ l.ballPosition +++
22 "}"
23 instance fromString WhatToLog where
24 fromString str
25 # (okActions, actions, str) = isBool (literal "{footballerActions=" str)
26 # (okPositions,positions,str) = isBool (literal ",fbPositions=" str)
27 # (okRefEvents,events, str) = isBool (literal ",refEvents=" str)
28 # (okPositions,pos, str) = isBool (literal ",ballPosition=" str)
29 | okActions && okPositions && okRefEvents && okPositions && str=="}"
30 = { footballerActions = actions
31 , fbPositions = positions
32 , refEvents = events
33 , ballPosition = pos
34 }
35 | otherwise
36 = defaultLogging
37 where
38 literal literal str
39 | stringStarts str literal = str%(size literal,size str-1)
40 | otherwise = str
41 isBool str
42 | stringStarts str "True" = (True,True, str%(4,size str-1))
43 | stringStarts str "False" = (True,False,str%(5,size str-1))
44 | otherwise = (False,undef,str)
45
46 defaultLogging = { footballerActions = False
47 , fbPositions = False
48 , refEvents = False
49 , ballPosition = False
50 }
51
52
53 /* logMatch logging match refereeActions succeededActions env
54 writes the log fields indicated in logging from the match and actions to disk.
55 */
56 logMatch :: !WhatToLog !Match ![RefereeAction] !(AssocList FootballerID FootballerAction) !*env -> *env | FileSystem env
57 logMatch logging match_to_log=:{Match | team1,team2,playingTime,theBall} refereeActions succeededActions env
58 | logging.footballerActions || logging.fbPositions || logging.refEvents || logging.ballPosition
59 # env = writeFile True logFile ("BEGIN STEP "<+++playingTime<+++"\n") env
60 # env = if (logging.footballerActions) (writeFile True logFile (logActions allPlayers succeededActions) env) env
61 # env = if (logging.fbPositions) (writeFile True logFile (logPositions allPlayers) env) env
62 # env = if (logging.refEvents) (writeFile True logFile (logRefereeActions refereeActions) env) env
63 # env = if (logging.ballPosition) (writeFile True logFile ("BallPos: " <+++ ball.ballPos +++ "\n") env) env
64 # env = writeFile True logFile ("END STEP " <+++ playingTime <+++ "\n") env
65 = env
66 | otherwise
67 = env
68 where
69 allPlayers = team1 ++ team2
70 ball = getFootball theBall allPlayers
71
72 logActions :: ![Footballer] !(AssocList FootballerID FootballerAction) -> String
73 logActions _ [] = ""
74 logActions allPlayers [(playerID=:{clubName,playerNr},action) : mfals]
75 = "FootballerWithAction\n\tTeam: " <+++
76 clubName <+++
77 "\n\tNumber: " <+++
78 playerNr <+++
79 "\n\tName: " <+++
80 nameOf self <+++
81 "\n\tAction: " <+++
82 improveString (toString action) <+++
83 "\n" <+++ logActions allPlayers mfals
84 where
85 self = find1 (identify_player playerID) allPlayers
86
87 improveString = (replaceInString "}\"" "}") o (replaceInString "\"{" "{") o (replaceInString "\\\"" "\"")
88
89 logPositions :: ![Footballer] -> String
90 logPositions fbs = "POSITIONS:\n" <+++ foldl printFootballer "" fbs
91 where
92 printFootballer str {playerID={clubName,playerNr},name,pos}
93 = str <+++ "\t(" <+++ clubName <+++ ",(" <+++ playerNr <+++"," <+++ name <+++")):\t" <+++ pos <+++ "\n"
94
95 logRefereeActions :: ![RefereeAction] -> String
96 logRefereeActions [] = ""
97 logRefereeActions refls = "REFEREE_ACTIONS:" <+++ logRefereeActions` refls <+++ "\n"
98 where
99 logRefereeActions` :: ![RefereeAction] -> String
100 logRefereeActions` [] = ""
101 logRefereeActions` [ref:refls] = "\n\t" <+++ logRefereeAction ref <+++ logRefereeActions` refls
102
103 logRefereeAction :: !RefereeAction -> String
104 logRefereeAction (ReprimandPlayer tfp r) = "(ReprimandPlayer " <+++ tfp.playerNr <+++ " " <+++ r <+++ ")"
105 logRefereeAction (Hands tfp) = "(Hands " <+++ tfp.playerNr <+++ ")"
106 logRefereeAction (TackleDetected tfp) = "(TackleDetected " <+++ tfp.playerNr <+++ ")"
107 logRefereeAction (DangerousPlay tfp) = "(DangerousPlay " <+++ tfp.playerNr <+++ ")"
108 logRefereeAction GameOver = "GameOver"
109 logRefereeAction (GameCancelled mt) = "(GameCancelled " <+++ mt <+++ ")"
110 logRefereeAction PauseGame = "PauseGame"
111 logRefereeAction (AddTime t) = "(AddTime " <+++ t <+++ ")"
112 logRefereeAction EndHalf = "EndHalf"
113 logRefereeAction (Goal t) = "(Goal " <+++ t <+++ ")"
114 logRefereeAction (Offside tfp) = "(Offside " <+++ tfp.playerNr <+++ ")"
115 logRefereeAction (DirectFreeKick t p) = "(DirectFreeKick " <+++ t <+++ " " <+++ p <+++ ")"
116 logRefereeAction (GoalKick t) = "(GoalKick " <+++ t <+++ ")"
117 logRefereeAction (Corner t e) = "(Corner " <+++ t <+++ " " <+++ e <+++ ")"
118 logRefereeAction (ThrowIn t p) = "(ThrowIn " <+++ t <+++ " " <+++ p <+++ ")"
119 logRefereeAction (Penalty t) = "(Penalty " <+++ t <+++ ")"
120 logRefereeAction (CenterKick t) = "(CenterKick " <+++ t <+++ ")"
121 logRefereeAction (Advantage t) = "(Advantage " <+++ t <+++ ")"
122 logRefereeAction (OwnBallIllegally tfp) = "(OwnBallIllegally" <+++ tfp.playerNr <+++ ")"
123 logRefereeAction (DisplacePlayers ds) = "DisplacePlayers"
124 logRefereeAction ContinueGame = "ContinueGame"
125 logRefereeAction (TellMessage txt) = "(TellMessage " <+++ txt <+++ ")"