Advertisement
Guest User

zmi stats parser

a guest
Feb 13th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. parseMessage' :: ReadP (String, [UserId], Integer)
  2. parseMessage' = do
  3.   skipSpaces
  4.   client  <- parseClient
  5.   skipSpaces
  6.   runners <- parseRunners
  7.   skipSpaces
  8.   hours   <- parseHours
  9.   pure (client, runners, hours)
  10.  
  11. parseClient :: ReadP String
  12. parseClient = do
  13.   string "client"
  14.   skipSpaces
  15.   char ':'
  16.   skipSpaces
  17.   many1 $ satisfy $ \char -> (not . (== '\n')) char
  18.  
  19. parseRunners :: ReadP [UserId]
  20. parseRunners = do
  21.   string "runners"
  22.   skipSpaces
  23.   char ':'
  24.   many1 parseDiscordId
  25.  
  26. -- If I replace 'skipSpaces' with a function that skips until I find the first '<' it seems to
  27. -- break everything
  28. parseDiscordId :: ReadP UserId
  29. parseDiscordId = do
  30.   skipSpaces
  31.   char '<'
  32.   char '@'
  33.   optional $ char '!'
  34.   userid <- fmap (Snowflake . read) $ many1 $ satisfy isDigit
  35.   char '>'
  36.   skipSpaces
  37.   pure userid
  38.  
  39. parseHours :: ReadP Integer
  40. parseHours = do
  41.   start     <- helper "start"
  42.   optional $ (helper "pause" :: ReadP Integer)
  43.   optional $ (helper "resume" :: ReadP Integer) <++ helper "restart"
  44.   end       <- helper "end"
  45.   -- hack to figure out if it was actually a 1.5 or 2 hour session
  46.   -- TODO: make the hack actually work
  47.   extraHour <- pure 0
  48.   -- figures out whether it was an hour or half hour duration
  49.   pure $ 1 + (1 - (round (abs (((realToFrac end) - (realToFrac start)) / 30)))) + extraHour
  50.     where notColon char = not $ char == ':'
  51.           helper s = do
  52.             string s
  53.             skipMany $ satisfy notColon
  54.             char ':'
  55.             skipMany $ satisfy notColon
  56.             char ':'
  57.             skipSpaces
  58.             time <- fmap read $ count 2 $ satisfy isDigit
  59.             skipSpaces
  60.             return time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement