aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Utils/ScoresFile.hs
blob: 936a5dc22acdbd42570f516057428665a9b07aca (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Utils.ScoresFile ( readScoresFile
                        , writeScoresFile) where

import Types.Scores
import Types.Classes
import Data.Classes
import Data.Time.Clock.POSIX (getPOSIXTime)
import Control.Applicative ((<$>))
import System.IO (readFile, writeFile)
import Data.List (sort)
import System.Directory (getDirectoryContents, getCurrentDirectory)
import System.FilePath (takeExtension)
import Control.Monad (liftM)
import Data.List.Split (splitOn)
import Data.Maybe (fromJust)
import System.FilePath ((</>))

scoresToCSV :: ScoresList -> String
scoresToCSV = unlines . foldr step []
  where
    step (theClass, (Score x y)) theLines =
                          (show theClass ++ "," ++ show x ++ "," ++ show y) : theLines

-- TODO: back these up

-- no malformed CSV handling here yet!
-- this function currently doesn't work
scoresFromCSV     :: String -> ScoresList
scoresFromCSV csv = foldr step [] (lines csv)
  where
     step line scores = (theClass, Score (read scoreString) (read timeString)) : scores
      where
        classString:scoreString:timeString:[] = splitOn "," line
        theClass = fromJust $ lookupSariulClass ((read . (:[]) . head) classString) ((read . (:[]) . last) classString)

-- read from scores-XX.csv where XX is largest timestamp
readScoresFile :: IO (Maybe ScoresList)
readScoresFile = do
    curDir <- getCurrentDirectory
    let dataDir = curDir </> "data"
    filenames <- liftM (reverse . sort . filter isCSV) $ getDirectoryContents dataDir
    case filenames of
        [] -> return Nothing
        _  -> Just . scoresFromCSV <$> readFile (dataDir </> head filenames)
  where isCSV path = takeExtension path == ".csv"

-- writes to score-XX.csv where XX is unix timestamp: a simple-minded logging
writeScoresFile :: ScoresList -> IO ()
writeScoresFile scores = do
    curDir <- getCurrentDirectory
    let dataDir = curDir </> "data"
    timestamp <- round <$> getPOSIXTime
    let filename = dataDir </> ("scores-" ++ show timestamp ++ ".csv")
    writeFile filename (scoresToCSV scores)