You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
2.0 KiB
53 lines
2.0 KiB
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
module Worldcup.Squads (Squad(..), buildSquads) where
|
|
|
|
import Data.Aeson
|
|
import Data.ByteString.Lazy.Char8 as BL8
|
|
import Prelude as P
|
|
import Worldcup.Countries
|
|
import Worldcup.Games
|
|
import Worldcup.Teams
|
|
|
|
data Squad = Squad {
|
|
squadTeamId :: Int,
|
|
squadCountryId :: Int,
|
|
squadGoalsFor :: Int,
|
|
squadGoalsAgainst :: Int,
|
|
squadCountryPopulation :: Int
|
|
} deriving (Show)
|
|
|
|
instance ToJSON Squad where
|
|
toJSON Squad{..} =
|
|
object [ "tId" .= squadTeamId,
|
|
"cId" .= squadCountryId,
|
|
"gf" .= squadGoalsFor,
|
|
"ga" .= squadGoalsAgainst,
|
|
"p" .= squadCountryPopulation ]
|
|
|
|
parseInt :: Value -> Int
|
|
parseInt Null = 0
|
|
parseInt x = read $ BL8.unpack $ encode x :: Int
|
|
|
|
goalsForTeamId :: Int -> [Game] -> Int
|
|
goalsForTeamId _id = P.foldl reducer 0 where
|
|
reducer acc x
|
|
| gameTeam1Id x == _id = acc + parseInt (gameScore1 x) + parseInt (gameScore1et x) + parseInt (gameScore1p x)
|
|
| gameTeam2Id x == _id = acc + parseInt (gameScore2 x) + parseInt (gameScore2et x) + parseInt (gameScore2p x)
|
|
| otherwise = acc
|
|
|
|
goalsAgainstTeamId :: Int -> [Game] -> Int
|
|
goalsAgainstTeamId _id = P.foldl reducer 0 where
|
|
reducer acc x
|
|
| gameTeam1Id x == _id = acc + parseInt (gameScore2 x) + parseInt (gameScore2et x) + parseInt (gameScore2p x)
|
|
| gameTeam2Id x == _id = acc + parseInt (gameScore1 x) + parseInt (gameScore1et x) + parseInt (gameScore1p x)
|
|
| otherwise = acc
|
|
|
|
buildSquads :: Countries -> [Game] -> Teams -> [Int] -> [Squad]
|
|
buildSquads cs gs ts = P.foldl reducer [] where
|
|
reducer acc x = Squad x (getCountry x) (getGoalsFor x) (getGoalsAgainst x) (getPopulation x) : acc
|
|
getCountry x = teamCountryId (lookupTeam x ts)
|
|
getGoalsFor x = goalsForTeamId x gs
|
|
getGoalsAgainst x = goalsAgainstTeamId x gs
|
|
getPopulation x = countryPop (lookupCountry (teamCountryId $ lookupTeam x ts) cs)
|
|
|