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, countryId :: Int,
countryContinentId :: Int, countryContinentId :: Int,
countryPop :: Int, countryPop :: Int,
countryName :: String } deriving (Show) countryName :: Value } deriving (Show)
instance ToJSON Country where instance ToJSON Country where
toJSON Country{..} = object [ "cId" .= countryContinentId, "n" .= countryName, "p" .= countryPop ] toJSON Country{..} = countryName
instance FromJSON Country where instance FromJSON Country where
parseJSON = withObject "country" $ \o -> do parseJSON = withObject "country" $ \o -> do

@ -14,7 +14,17 @@ type Rounds = HashMap Int Round
data Round = Round { data Round = Round {
roundId :: Int, roundId :: Int,
roundEventId :: 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 :: Either String [Value] -> Rounds
parseRounds (Left x) = error x parseRounds (Left x) = error x
@ -25,10 +35,3 @@ parseRounds (Right xs) = P.foldl reduce HM.empty xs where
lookupRound :: Int -> Rounds -> Round lookupRound :: Int -> Rounds -> Round
lookupRound _id hm = M.fromMaybe (Round 999 999 "DNE") (HM.lookup _id hm) 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 module Worldcup.Squads (Squad(..), buildSquads) where
import Data.Aeson import Data.Aeson
import Data.Aeson.Types import Data.ByteString.Lazy.Char8 as BL8
import Data.HashMap.Strict as HM import Prelude as P
import Data.Maybe as M
import Prelude as P
import Worldcup.Continents
import Worldcup.Countries
import Worldcup.Events
import Worldcup.EventsTeams
import Worldcup.Games import Worldcup.Games
import Worldcup.Rounds
import Worldcup.Teams import Worldcup.Teams
import Worldcup.Tourneys
data Squad = Squad { data Squad = Squad {
squadCountryId :: String, squadCountryId :: Int,
squadContinentId :: String,
squadGoalsFor :: Int, squadGoalsFor :: Int,
squadGoalsAgainst :: Int squadGoalsAgainst :: Int
} deriving (Show) } deriving (Show)
instance ToJSON Squad where instance ToJSON Squad where
toJSON Squad{..} = toJSON Squad{..} =
object [ "id" .= squadId, object [ "cId" .= squadCountryId,
"ct" .= squadCountry, "gf" .= squadGoalsFor,
"cn" .= squadContinent, "ga" .= squadGoalsAgainst ]
"g" .= squadGoals ]
parseInt :: Value -> Int
buildSquads :: [Squad] parseInt Null = 0
buildSquads = [] parseInt x = read $ BL8.unpack $ encode x :: Int
-- countryFromTeamId :: Int -> Teams -> Countries -> Country goalsForTeamId :: Int -> [Game] -> Int
-- countryFromTeamId _id ts = lookupCountry a where goalsForTeamId _id = P.foldl reducer 0 where
-- a = teamCountryId $ lookupTeam _id ts reducer acc x
-- | gameTeam1Id x == _id = acc + parseInt (gameScore1 x) + parseInt (gameScore1et x) + parseInt (gameScore1p x)
-- parseInt :: Value -> Int | gameTeam2Id x == _id = acc + parseInt (gameScore2 x) + parseInt (gameScore2et x) + parseInt (gameScore2p x)
-- parseInt Null = 0 | otherwise = acc
-- parseInt x = read $ BL8.unpack $ encode x :: Int
-- goalsAgainstTeamId :: Int -> [Game] -> Int
-- continentFromTeamId :: Int -> Teams -> Countries -> Continents -> Continent goalsAgainstTeamId _id = P.foldl reducer 0 where
-- continentFromTeamId _id hmT hmC = lookupContinent (countryContinentId b) where reducer acc x
-- a = teamCountryId $ lookupTeam _id hmT | gameTeam1Id x == _id = acc + parseInt (gameScore2 x) + parseInt (gameScore2et x) + parseInt (gameScore2p x)
-- b = lookupCountry a hmC | gameTeam2Id x == _id = acc + parseInt (gameScore1 x) + parseInt (gameScore1et x) + parseInt (gameScore1p x)
-- | otherwise = acc
-- goalsFromTeamId :: Int -> [Game] -> Int
-- goalsFromTeamId _id = P.foldl reducer 0 where buildSquads :: [Game] -> Teams -> [Int] -> [Squad]
-- reducer acc x buildSquads gs ts = P.foldl reducer [] where
-- | team1Id x == _id = acc + parseInt (score1 x) + parseInt (score1et x) + parseInt (score1p x) reducer acc x = Squad (getCountry x) (getGoalsFor x) (getGoalsAgainst x) : acc
-- | team2Id x == _id = acc + parseInt (score2 x) + parseInt (score2et x) + parseInt (score2p x) getCountry x = teamCountryId (lookupTeam x ts)
-- | otherwise = acc getGoalsFor x = goalsForTeamId x gs
-- getGoalsAgainst x = goalsAgainstTeamId x gs
-- 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

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

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

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