|
|
|
@ -4,60 +4,44 @@ |
|
|
|
|
module Worldcup.Squads (Squad(..), buildSquads) where |
|
|
|
|
|
|
|
|
|
import Data.Aeson |
|
|
|
|
import Data.Aeson.Types |
|
|
|
|
import Data.HashMap.Strict as HM |
|
|
|
|
import Data.Maybe as M |
|
|
|
|
import Prelude as P |
|
|
|
|
import Worldcup.Continents |
|
|
|
|
import Worldcup.Countries |
|
|
|
|
import Worldcup.Events |
|
|
|
|
import Worldcup.EventsTeams |
|
|
|
|
import Data.ByteString.Lazy.Char8 as BL8 |
|
|
|
|
import Prelude as P |
|
|
|
|
import Worldcup.Games |
|
|
|
|
import Worldcup.Rounds |
|
|
|
|
import Worldcup.Teams |
|
|
|
|
import Worldcup.Tourneys |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data Squad = Squad { |
|
|
|
|
squadCountryId :: String, |
|
|
|
|
squadContinentId :: String, |
|
|
|
|
squadCountryId :: Int, |
|
|
|
|
squadGoalsFor :: Int, |
|
|
|
|
squadGoalsAgainst :: Int |
|
|
|
|
} deriving (Show) |
|
|
|
|
|
|
|
|
|
instance ToJSON Squad where |
|
|
|
|
toJSON Squad{..} = |
|
|
|
|
object [ "id" .= squadId, |
|
|
|
|
"ct" .= squadCountry, |
|
|
|
|
"cn" .= squadContinent, |
|
|
|
|
"g" .= squadGoals ] |
|
|
|
|
|
|
|
|
|
buildSquads :: [Squad] |
|
|
|
|
buildSquads = [] |
|
|
|
|
|
|
|
|
|
-- countryFromTeamId :: Int -> Teams -> Countries -> Country |
|
|
|
|
-- countryFromTeamId _id ts = lookupCountry a where |
|
|
|
|
-- a = teamCountryId $ lookupTeam _id ts |
|
|
|
|
-- |
|
|
|
|
-- parseInt :: Value -> Int |
|
|
|
|
-- parseInt Null = 0 |
|
|
|
|
-- parseInt x = read $ BL8.unpack $ encode x :: Int |
|
|
|
|
-- |
|
|
|
|
-- continentFromTeamId :: Int -> Teams -> Countries -> Continents -> Continent |
|
|
|
|
-- continentFromTeamId _id hmT hmC = lookupContinent (countryContinentId b) where |
|
|
|
|
-- a = teamCountryId $ lookupTeam _id hmT |
|
|
|
|
-- b = lookupCountry a hmC |
|
|
|
|
-- |
|
|
|
|
-- goalsFromTeamId :: Int -> [Game] -> Int |
|
|
|
|
-- goalsFromTeamId _id = P.foldl reducer 0 where |
|
|
|
|
-- reducer acc x |
|
|
|
|
-- | team1Id x == _id = acc + parseInt (score1 x) + parseInt (score1et x) + parseInt (score1p x) |
|
|
|
|
-- | team2Id x == _id = acc + parseInt (score2 x) + parseInt (score2et x) + parseInt (score2p x) |
|
|
|
|
-- | otherwise = acc |
|
|
|
|
-- |
|
|
|
|
-- buildFinalResult :: [Int] -> Teams -> Countries -> Continents -> [Game] -> [FinalResult] |
|
|
|
|
-- buildFinalResult ets hmT hmC hmN gs = P.foldl reducer [] ets where |
|
|
|
|
-- reducer acc x = FinalResult x (getCountryName x) (getContinentName x) (goalCount x) : acc |
|
|
|
|
-- getCountryName x = countryName (countryFromTeamId x hmT hmC) |
|
|
|
|
-- getContinentName x = continentName (continentFromTeamId x hmT hmC hmN) |
|
|
|
|
-- goalCount x = goalsFromTeamId x gs |
|
|
|
|
object [ "cId" .= squadCountryId, |
|
|
|
|
"gf" .= squadGoalsFor, |
|
|
|
|
"ga" .= squadGoalsAgainst ] |
|
|
|
|
|
|
|
|
|
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 :: [Game] -> Teams -> [Int] -> [Squad] |
|
|
|
|
buildSquads gs ts = P.foldl reducer [] where |
|
|
|
|
reducer acc x = Squad (getCountry x) (getGoalsFor x) (getGoalsAgainst x) : acc |
|
|
|
|
getCountry x = teamCountryId (lookupTeam x ts) |
|
|
|
|
getGoalsFor x = goalsForTeamId x gs |
|
|
|
|
getGoalsAgainst x = goalsAgainstTeamId x gs |
|
|
|
|