Skip to content

Commit bfb1462

Browse files
committed
construct match from score and shooter stream
1 parent be5e617 commit bfb1462

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/Practiscore/CLI.hs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import Options.Applicative
1616
showHelpOnError,
1717
strOption,
1818
)
19+
import Practiscore.USPSA.CLI (streamRawReport)
20+
import Practiscore.Parser.Shooter (toUspsaMemberId)
21+
import Conduit qualified
22+
import Conduit ((.|), MonadThrow, MonadUnliftIO)
23+
import Practiscore.Parser.Report qualified
24+
import Control.Exception (throwIO)
25+
import Practiscore.USPSA.Match (getShooterMatch, encodeMatch)
1926

2027
data CLI = CLI
2128
{ uspsaMemberId :: Text,
@@ -50,10 +57,19 @@ data CliParseErrors
5057

5158
instance Exception CliParseErrors
5259

53-
parseCLI :: IO ()
60+
parseCLI :: (MonadUnliftIO m, MonadThrow m) => m ()
5461
parseCLI = do
55-
_cli <- showHelpOnErrorOnExecParser (info (helper <*> cli) fullDesc)
56-
pure ()
62+
cli <- liftIO $ showHelpOnErrorOnExecParser (info (helper <*> cli) fullDesc)
63+
64+
Conduit.runConduitRes $ do
65+
let stream = streamRawReport cli.reportPath
66+
shooters <- lift $ Practiscore.Parser.Report.toShooters stream
67+
scores <- lift $ Practiscore.Parser.Report.toScores stream
68+
case toUspsaMemberId cli.uspsaMemberId of
69+
Nothing -> liftIO $ throwIO (InvalidUspsaMemberId $ cli.uspsaMemberId <> " is not valid")
70+
Just uspsaMemberId ->
71+
Conduit.yield (toStrict $ encodeMatch $ getShooterMatch uspsaMemberId shooters scores)
72+
.| Conduit.sinkFile cli.output
5773

5874
showHelpOnErrorOnExecParser :: ParserInfo a -> IO a
5975
showHelpOnErrorOnExecParser = customExecParser (prefs showHelpOnError)

src/Practiscore/Parser/Report.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ toShooters reportFieldsStream = do
9191
(currentHeader, [(header, line)])
9292
shooterStepParser _ currentHeader = (currentHeader, [])
9393

94-
toScores :: (MonadUnliftIO m) => ConduitT () ReportFields (ResourceT m) () -> m ()
94+
toScores :: (MonadUnliftIO m) => ConduitT () ReportFields (ResourceT m) () -> m [Score]
9595
toScores reportFieldsStream =
9696
Conduit.runConduitRes $
9797
reportFieldsStream
9898
.| scoreParser
9999
.| scoreWithFieldNames
100100
.| decodeScore
101-
.| Conduit.mapM_C (print @String <<< show)
101+
.| Conduit.sinkList
102102
where
103103
scoreParser = Conduit.concatMapAccumC scoreStepParser Nothing
104104

src/Practiscore/USPSA/Match.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Practiscore.USPSA.Match
44
( Match (..),
5-
getShooterFromReport,
5+
getShooterMatch,
66
encodeMatch,
77
)
88
where
@@ -15,8 +15,6 @@ import Data.Csv
1515
namedRecord,
1616
(.=),
1717
)
18-
import Practiscore.Parser.Report (Report)
19-
import Practiscore.Parser.Report qualified
2018
import Practiscore.Parser.Score qualified
2119
import Practiscore.Parser.Shooter qualified
2220
import Practiscore.USPSA (UspsaMemberId (..))
@@ -79,19 +77,20 @@ instance ToNamedRecord Match where
7977
"stage_place" .= export.score.stagePlace
8078
]
8179

82-
getShooterFromReport ::
80+
getShooterMatch ::
8381
UspsaMemberId ->
84-
Report ->
82+
[Practiscore.Parser.Shooter.Shooter] ->
83+
[Practiscore.Parser.Score.Score] ->
8584
[Match]
86-
getShooterFromReport memberId report = do
87-
case find (\shooter -> shooter.uspsa == Just memberId) report.shooters of
85+
getShooterMatch memberId shooters scores =
86+
case find (\shooter -> shooter.uspsa == Just memberId) shooters of
8887
Nothing -> []
89-
Just shooter ->
88+
Just shooter ->
9089
[ Match
9190
{ shooter,
9291
score
9392
}
94-
| score <- (filter (\score -> score.comp == shooter.comp) report.scores)
93+
| score <- filter (\score -> score.comp == shooter.comp) scores
9594
]
9695

9796
encodeMatch :: [Match] -> LByteString

0 commit comments

Comments
 (0)