Data structure adjustments.

master
Ben Burlingham 9 years ago
parent b352040a9a
commit c331c9eade
  1. 4
      Worldcup/Countries.hs
  2. 19
      Worldcup/Rounds.hs
  3. 78
      Worldcup/Squads.hs
  4. 17
      Worldcup/Tourneys.hs
  5. 16
      worldcup.hs
  6. 2
      worldcup.json

@ -15,10 +15,10 @@ data Country = Country {
countryId :: Int,
countryContinentId :: Int,
countryPop :: Int,
countryName :: String } deriving (Show)
countryName :: Value } deriving (Show)
instance ToJSON Country where
toJSON Country{..} = object [ "cId" .= countryContinentId, "n" .= countryName, "p" .= countryPop ]
toJSON Country{..} = countryName
instance FromJSON Country where
parseJSON = withObject "country" $ \o -> do

@ -14,7 +14,17 @@ type Rounds = HashMap Int Round
data Round = Round {
roundId :: Int,
roundEventId :: Int,
roundName :: String } deriving (Show)
roundName :: Value } deriving (Show)
instance ToJSON Round where
toJSON Round{..} = roundName
instance FromJSON Round where
parseJSON = withObject "round" $ \o -> do
roundId <- o .: "id"
roundEventId <- o .: "event_id"
roundName <- o .: "title"
return Round{..}
parseRounds :: Either String [Value] -> Rounds
parseRounds (Left x) = error x
@ -25,10 +35,3 @@ parseRounds (Right xs) = P.foldl reduce HM.empty xs where
lookupRound :: Int -> Rounds -> Round
lookupRound _id hm = M.fromMaybe (Round 999 999 "DNE") (HM.lookup _id hm)
instance FromJSON Round where
parseJSON = withObject "round" $ \o -> do
roundId <- o .: "id"
roundEventId <- o .: "event_id"
roundName <- o .: "title"
return Round{..}

@ -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

@ -6,19 +6,22 @@ module Worldcup.Tourneys (Tourneys, Tourney(..), buildTourneys) where
import Data.Aeson
import Data.HashMap.Strict as HM
import Prelude as P
import Worldcup.Countries
import Worldcup.Events
import Worldcup.EventsTeams
import Worldcup.Games
import Worldcup.Rounds
import Worldcup.Squads
import Worldcup.Teams
type Tourneys = HashMap String Tourney
data Tourney = Tourney {
tourneyGames :: [Game],
tourneyTeams :: [Int] } deriving (Show)
tourneyGames :: [Game],
tourneySquads :: [Squad] } deriving (Show)
instance ToJSON Tourney where
toJSON Tourney{..} = object [ "games" .= tourneyGames, "teams" .= tourneyTeams ]
toJSON Tourney{..} = object [ "games" .= tourneyGames, "squad" .= tourneySquads ]
roundIsInEvent :: Int -> Int -> Rounds -> Bool
roundIsInEvent eid rid rs = roundEventId getRound == eid where
@ -36,9 +39,9 @@ gamesFromEventId eid rs = foldl reducer [] where
| roundIsInEvent eid (gameRoundId x) rs = x : acc
| otherwise = acc
buildTourneys :: Rounds -> [EventTeam] -> [Game] -> [Event] -> Tourneys
buildTourneys rs ets gs = foldl reducer HM.empty where
reducer acc x = HM.insert (getEvent x) (Tourney (getGames x) (getTeams x)) acc
buildTourneys :: Rounds -> [EventTeam] -> Teams -> [Game] -> [Event] -> Tourneys
buildTourneys rs ets ts gs = foldl reducer HM.empty where
reducer acc x = HM.insert (getEvent x) (Tourney (getGames x) (getSquads x)) acc
getGames x = gamesFromEventId (eventId x) rs gs
getTeams x = teamIdsFromEventId (eventId x) ets
getSquads x = buildSquads gs ts (teamIdsFromEventId (eventId x) ets)
getEvent x = drop 6 (eventName x)

@ -16,17 +16,15 @@ import Worldcup.Teams
import Worldcup.Tourneys
data WorldcupData = WorldcupData {
worldcupTeams :: Teams,
worldcupCountries :: Countries,
worldcupContinents :: Continents,
worldcupTourneys :: Tourneys
worldcupCountries :: Countries,
worldcupRounds :: Rounds,
worldcupTourneys :: Tourneys
} deriving (Show)
instance ToJSON WorldcupData where
toJSON WorldcupData{..} = object [
"teams" .= worldcupTeams,
"countries" .= worldcupCountries,
"continents" .= worldcupContinents,
"rounds" .= worldcupRounds,
"tourneys" .= worldcupTourneys ]
main :: IO ()
@ -34,7 +32,6 @@ main = do
dataTeams <- BL.readFile "./data/teams.json"
dataEventsTeams <- BL.readFile "./data/events_teams.json"
dataCountries <- BL.readFile "./data/countries.json"
dataContinents <- BL.readFile "./data/continents.json"
dataGames <- BL.readFile "./data/games.json"
dataEvents <- BL.readFile "./data/events.json"
dataRounds <- BL.readFile "./data/rounds.json"
@ -44,11 +41,10 @@ main = do
let eventsteams = parseEventsTeams (AE.eitherDecode dataEventsTeams)
let games = parseGames (AE.eitherDecode dataGames)
let countries = parseCountries (AE.eitherDecode dataCountries)
let continents = parseContinents (AE.eitherDecode dataContinents)
let rounds = parseRounds (AE.eitherDecode dataRounds)
let tourneys = buildTourneys rounds eventsteams games events
let worldcup = WorldcupData teams countries continents tourneys
let tourneys = buildTourneys rounds eventsteams teams games events
let worldcup = WorldcupData countries rounds tourneys
let encoded = encode worldcup
BL8.putStrLn encoded

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save