initial framework added
[fp1415-soccerfun.git] / src / Game / matchLog.icl
diff --git a/src/Game/matchLog.icl b/src/Game/matchLog.icl
new file mode 100644 (file)
index 0000000..744f25b
--- /dev/null
@@ -0,0 +1,125 @@
+implementation module matchLog\r
+\r
+import StdEnvExt, fileIO\r
+import matchControl\r
+\r
+logFile                                                :: String\r
+logFile                                                = "SoccerFun.log"\r
+\r
+getWhatToLog                           :: !*env -> (!WhatToLog,!*env) | FileSystem env\r
+getWhatToLog env\r
+       = case readFile fileStr env of\r
+               (Just line,env)         = (fromString line,env)\r
+               (nothing,  env)         = (defaultLogging, env)\r
+where\r
+       fileStr                                 = "logMatch.conf"\r
+\r
+instance toString WhatToLog where \r
+       toString l = "{footballerActions=" <+++ l.footballerActions +++ \r
+                                ",fbPositions="       <+++ l.fbPositions       +++ \r
+                                ",refEvents="         <+++ l.refEvents         +++ \r
+                                ",ballPosition="      <+++ l.ballPosition      +++ \r
+                                "}"\r
+instance fromString WhatToLog where\r
+       fromString str\r
+               # (okActions,  actions,  str)                   = isBool (literal "{footballerActions=" str)\r
+               # (okPositions,positions,str)                   = isBool (literal ",fbPositions="       str)\r
+               # (okRefEvents,events,   str)                   = isBool (literal ",refEvents="         str)\r
+               # (okPositions,pos,      str)                   = isBool (literal ",ballPosition="      str)\r
+               | okActions && okPositions && okRefEvents && okPositions && str=="}"\r
+                       = { footballerActions                           = actions\r
+                         , fbPositions                                         = positions\r
+                         , refEvents                                           = events\r
+                         , ballPosition                                        = pos\r
+                         }\r
+               | otherwise\r
+                       = defaultLogging\r
+       where\r
+               literal literal str\r
+               | stringStarts str literal                              = str%(size literal,size str-1)\r
+               | otherwise                                                             = str\r
+               isBool str\r
+               | stringStarts str "True"                               = (True,True, str%(4,size str-1))\r
+               | stringStarts str "False"                              = (True,False,str%(5,size str-1))\r
+               | otherwise                                                             = (False,undef,str)\r
+\r
+defaultLogging         = { footballerActions           = False\r
+                                         , fbPositions                         = False\r
+                                         , refEvents                           = False\r
+                                         , ballPosition                        = False\r
+                                         }\r
+\r
+\r
+/*     logMatch logging match refereeActions succeededActions env\r
+               writes the log fields indicated in logging from the match and actions to disk.\r
+*/\r
+logMatch :: !WhatToLog !Match ![RefereeAction] !(AssocList FootballerID FootballerAction) !*env -> *env | FileSystem env\r
+logMatch logging match_to_log=:{Match | team1,team2,playingTime,theBall} refereeActions succeededActions env\r
+       | logging.footballerActions || logging.fbPositions || logging.refEvents || logging.ballPosition\r
+               # env                           = writeFile True logFile ("BEGIN STEP "<+++playingTime<+++"\n") env\r
+               # env                           = if (logging.footballerActions) (writeFile True logFile (logActions    allPlayers succeededActions) env) env\r
+               # env                           = if (logging.fbPositions)       (writeFile True logFile (logPositions  allPlayers)                  env) env\r
+               # env                           = if (logging.refEvents)         (writeFile True logFile (logRefereeActions refereeActions)          env) env\r
+               # env                           = if (logging.ballPosition)      (writeFile True logFile ("BallPos: " <+++ ball.ballPos +++ "\n")    env) env\r
+               # env                           = writeFile True logFile ("END STEP " <+++ playingTime <+++ "\n") env\r
+               = env\r
+       | otherwise\r
+               = env\r
+where\r
+       allPlayers                              = team1 ++ team2\r
+       ball                                    = getFootball theBall allPlayers \r
+\r
+       logActions :: ![Footballer] !(AssocList FootballerID FootballerAction) -> String\r
+       logActions _ []         = ""\r
+       logActions allPlayers [(playerID=:{clubName,playerNr},action) : mfals]\r
+                                               = "FootballerWithAction\n\tTeam: "     <+++ \r
+                                                 clubName                             <+++ \r
+                                                 "\n\tNumber: "                       <+++ \r
+                                                 playerNr                             <+++ \r
+                                                 "\n\tName: "                         <+++ \r
+                                                 nameOf self                          <+++ \r
+                                                 "\n\tAction: "                       <+++ \r
+                                                 improveString (toString action)      <+++ \r
+                                                 "\n"                                 <+++ logActions allPlayers mfals\r
+       where\r
+               self                    = find1 (identify_player playerID) allPlayers\r
+               \r
+       improveString           = (replaceInString "}\"" "}") o (replaceInString "\"{" "{") o (replaceInString "\\\"" "\"")\r
+\r
+       logPositions :: ![Footballer] -> String\r
+       logPositions fbs        = "POSITIONS:\n" <+++ foldl printFootballer "" fbs\r
+       where\r
+               printFootballer str {playerID={clubName,playerNr},name,pos}\r
+                                               = str <+++ "\t(" <+++ clubName <+++ ",(" <+++ playerNr <+++"," <+++ name <+++")):\t" <+++ pos <+++ "\n"\r
+\r
+       logRefereeActions :: ![RefereeAction] -> String\r
+       logRefereeActions []                            = ""\r
+       logRefereeActions refls                         = "REFEREE_ACTIONS:" <+++ logRefereeActions` refls <+++ "\n"\r
+       where\r
+               logRefereeActions` :: ![RefereeAction] -> String\r
+               logRefereeActions` []                   = ""\r
+               logRefereeActions` [ref:refls]  = "\n\t" <+++ logRefereeAction ref <+++ logRefereeActions` refls\r
+               \r
+               logRefereeAction :: !RefereeAction -> String\r
+               logRefereeAction (ReprimandPlayer   tfp r)      = "(ReprimandPlayer " <+++ tfp.playerNr <+++ " " <+++ r <+++ ")"\r
+               logRefereeAction (Hands                         tfp)    = "(Hands "           <+++ tfp.playerNr <+++ ")"\r
+               logRefereeAction (TackleDetected        tfp)    = "(TackleDetected "  <+++ tfp.playerNr <+++ ")"\r
+               logRefereeAction (DangerousPlay         tfp)    = "(DangerousPlay "   <+++ tfp.playerNr <+++ ")"\r
+               logRefereeAction  GameOver                                      = "GameOver"\r
+               logRefereeAction (GameCancelled     mt)         = "(GameCancelled "   <+++ mt <+++ ")"\r
+               logRefereeAction  PauseGame                                     = "PauseGame"\r
+               logRefereeAction (AddTime                       t)              = "(AddTime "         <+++ t <+++ ")"\r
+               logRefereeAction  EndHalf                                       = "EndHalf"\r
+               logRefereeAction (Goal                          t)              = "(Goal "            <+++ t <+++ ")"\r
+               logRefereeAction (Offside                       tfp)    = "(Offside "         <+++ tfp.playerNr <+++ ")"\r
+               logRefereeAction (DirectFreeKick        t p)    = "(DirectFreeKick "  <+++ t <+++ " " <+++ p <+++ ")"\r
+               logRefereeAction (GoalKick              t)              = "(GoalKick "        <+++ t <+++ ")"\r
+               logRefereeAction (Corner                t e)    = "(Corner "          <+++ t <+++ " " <+++ e <+++ ")"\r
+               logRefereeAction (ThrowIn               t p)    = "(ThrowIn "         <+++ t <+++ " " <+++ p <+++ ")"\r
+               logRefereeAction (Penalty                       t)              = "(Penalty "         <+++ t <+++ ")"\r
+               logRefereeAction (CenterKick            t)              = "(CenterKick "      <+++ t <+++ ")"\r
+               logRefereeAction (Advantage                     t)              = "(Advantage "       <+++ t <+++ ")"\r
+               logRefereeAction (OwnBallIllegally  tfp)        = "(OwnBallIllegally" <+++ tfp.playerNr <+++ ")"\r
+               logRefereeAction (DisplacePlayers       ds)             = "DisplacePlayers"\r
+               logRefereeAction  ContinueGame                          = "ContinueGame"\r
+               logRefereeAction (TellMessage           txt)    = "(TellMessage "     <+++ txt <+++ ")"\r