Adding population to team meta.

master
Ben Burlingham 9 years ago
parent c331c9eade
commit c5844ad6a6
  1. 22
      Worldcup/Squads.hs
  2. 8
      Worldcup/Tourneys.hs
  3. 17
      js/diagram.js
  4. 5
      js/main.js
  5. 22
      js/matrices.js
  6. 4
      worldcup.hs
  7. 2
      worldcup.json

@ -6,20 +6,25 @@ module Worldcup.Squads (Squad(..), buildSquads) where
import Data.Aeson import Data.Aeson
import Data.ByteString.Lazy.Char8 as BL8 import Data.ByteString.Lazy.Char8 as BL8
import Prelude as P import Prelude as P
import Worldcup.Countries
import Worldcup.Games import Worldcup.Games
import Worldcup.Teams import Worldcup.Teams
data Squad = Squad { data Squad = Squad {
squadCountryId :: Int, squadTeamId :: Int,
squadGoalsFor :: Int, squadCountryId :: Int,
squadGoalsAgainst :: Int squadGoalsFor :: Int,
squadGoalsAgainst :: Int,
squadCountryPopulation :: Int
} deriving (Show) } deriving (Show)
instance ToJSON Squad where instance ToJSON Squad where
toJSON Squad{..} = toJSON Squad{..} =
object [ "cId" .= squadCountryId, object [ "tId" .= squadTeamId,
"cId" .= squadCountryId,
"gf" .= squadGoalsFor, "gf" .= squadGoalsFor,
"ga" .= squadGoalsAgainst ] "ga" .= squadGoalsAgainst,
"p" .= squadCountryPopulation ]
parseInt :: Value -> Int parseInt :: Value -> Int
parseInt Null = 0 parseInt Null = 0
@ -39,9 +44,10 @@ goalsAgainstTeamId _id = P.foldl reducer 0 where
| gameTeam2Id x == _id = acc + parseInt (gameScore1 x) + parseInt (gameScore1et x) + parseInt (gameScore1p x) | gameTeam2Id x == _id = acc + parseInt (gameScore1 x) + parseInt (gameScore1et x) + parseInt (gameScore1p x)
| otherwise = acc | otherwise = acc
buildSquads :: [Game] -> Teams -> [Int] -> [Squad] buildSquads :: Countries -> [Game] -> Teams -> [Int] -> [Squad]
buildSquads gs ts = P.foldl reducer [] where buildSquads cs gs ts = P.foldl reducer [] where
reducer acc x = Squad (getCountry x) (getGoalsFor x) (getGoalsAgainst x) : acc reducer acc x = Squad x (getCountry x) (getGoalsFor x) (getGoalsAgainst x) (getPopulation x) : acc
getCountry x = teamCountryId (lookupTeam x ts) getCountry x = teamCountryId (lookupTeam x ts)
getGoalsFor x = goalsForTeamId x gs getGoalsFor x = goalsForTeamId x gs
getGoalsAgainst x = goalsAgainstTeamId x gs getGoalsAgainst x = goalsAgainstTeamId x gs
getPopulation x = countryPop (lookupCountry (teamCountryId $ lookupTeam x ts) cs)

@ -21,7 +21,7 @@ data Tourney = Tourney {
tourneySquads :: [Squad] } deriving (Show) tourneySquads :: [Squad] } deriving (Show)
instance ToJSON Tourney where instance ToJSON Tourney where
toJSON Tourney{..} = object [ "games" .= tourneyGames, "squad" .= tourneySquads ] toJSON Tourney{..} = object [ "games" .= tourneyGames, "teams" .= 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
@ -39,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] -> Teams -> [Game] -> [Event] -> Tourneys buildTourneys :: Countries -> Rounds -> [EventTeam] -> Teams -> [Game] -> [Event] -> Tourneys
buildTourneys rs ets ts gs = foldl reducer HM.empty where buildTourneys cs rs ets ts gs = foldl reducer HM.empty where
reducer acc x = HM.insert (getEvent x) (Tourney (getGames x) (getSquads 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
getSquads x = buildSquads gs ts (teamIdsFromEventId (eventId x) ets) getSquads x = buildSquads cs gs ts (teamIdsFromEventId (eventId x) ets)
getEvent x = drop 6 (eventName x) getEvent x = drop 6 (eventName x)

@ -102,7 +102,7 @@ const Diagram = {
return goalsAgainst[teamId]; return goalsAgainst[teamId];
}, },
build: (data, eventIndex, metaMatrix, chordMatrix) => { build: (data, eventIndex, matrix) => {
const svg = d3.select("svg"), const svg = d3.select("svg"),
width = +svg.attr("width"), width = +svg.attr("width"),
height = +svg.attr("height"), height = +svg.attr("height"),
@ -112,14 +112,15 @@ const Diagram = {
const chords = d3.chord() const chords = d3.chord()
.padAngle(0.05) .padAngle(0.05)
.call(null, chordMatrix); .call(null, matrix);
const sortedChords = Sorter.sort(chords, 0, chords.groups.length - 1, const sortedChords = chords;
// const sortedChords = Sorter.sort(chords, 0, chords.groups.length - 1,
// Diagram.getCountryName.bind(null, data, eventIndex), // Diagram.getCountryName.bind(null, data, eventIndex),
// Diagram.getGoalsFor.bind(null, data, eventIndex), // Diagram.getGoalsFor.bind(null, data, eventIndex),
// Diagram.getGoalsAgainst.bind(null, data, eventIndex), // Diagram.getGoalsAgainst.bind(null, data, eventIndex),
Diagram.getPopulation.bind(null, data, eventIndex), // Diagram.getPopulation.bind(null, data, eventIndex),
Diagram.swapGroups.bind(null, data, eventIndex)); // Diagram.swapGroups.bind(null, data, eventIndex));
const arc = d3.arc() const arc = d3.arc()
.innerRadius(innerRadius) .innerRadius(innerRadius)
@ -196,9 +197,9 @@ const Diagram = {
.text(function(d) { .text(function(d) {
// STRANGE EXTENDED TIME CHILE-BRAZIL - FIX BY HAND? IS BECAUE se1 IS SCORE __GOING INTO__ EXTENDED TIME // STRANGE EXTENDED TIME CHILE-BRAZIL - FIX BY HAND? IS BECAUE se1 IS SCORE __GOING INTO__ EXTENDED TIME
const teamId = data.tourneys[eventIndex].teams[d.index]; const team = data.tourneys[eventIndex].teams[d.index];
const country = data.countries[data.teams[teamId]]; const country = data.countries[team.cId];
return data.countries[data.teams[teamId]].n + ' ' + country.p; return data.countries[team.cId] + ' ' + team.p;
}); });
}, },
}; };

@ -11,10 +11,9 @@ const fetch = (url) => new Promise((resolve, reject) => {
const main = { const main = {
generateDiagram: (eventKey) => { generateDiagram: (eventKey) => {
const metaMatrix = Matrices.buildMetaMatrix(main.json, eventKey); const matrix = Matrices.buildMatrix(main.json, eventKey);
const chordMatrix = Matrices.buildChordMatrix(main.json, eventKey);
Diagram.clear(); Diagram.clear();
Diagram.build(main.json, eventKey, metaMatrix, chordMatrix); Diagram.build(main.json, eventKey, matrix);
}, },
generateUI: () => { generateUI: () => {

@ -5,30 +5,14 @@ const Matrices = {
return empty.map(() => empty.map(() => null)); return empty.map(() => empty.map(() => null));
}, },
// Identical structure of chord matrix but with { game, team }.
buildMetaMatrix: (json, eventKey) => {
const teams = json.tourneys[eventKey].teams;
const matrix = Matrices.createEmptyMatrix(teams.length);
json.tourneys[eventKey].games.forEach(g => {
const i1 = teams.indexOf(g.t1);
const i2 = teams.indexOf(g.t2);
matrix[i1][i2] = { game: g, team: g.t1 };
matrix[i2][i1] = { game: g, team: g.t2 };
}, []);
return matrix;
},
// Scalar data points (sum of goals scored). // Scalar data points (sum of goals scored).
buildChordMatrix: (json, eventKey) => { buildMatrix: (json, eventKey) => {
const teams = json.tourneys[eventKey].teams; const teams = json.tourneys[eventKey].teams;
const matrix = Matrices.createEmptyMatrix(teams.length); const matrix = Matrices.createEmptyMatrix(teams.length);
json.tourneys[eventKey].games.forEach(g => { json.tourneys[eventKey].games.forEach(g => {
const i1 = teams.indexOf(g.t1); const i1 = teams.findIndex(v => v.tId === g.t1);
const i2 = teams.indexOf(g.t2); const i2 = teams.findIndex(v => v.tId === g.t2);
matrix[i1][i2] = g.s1 + g.se1 + g.sp1; matrix[i1][i2] = g.s1 + g.se1 + g.sp1;
matrix[i2][i1] = g.s2 + g.se2 + g.sp2; matrix[i2][i1] = g.s2 + g.se2 + g.sp2;

@ -4,9 +4,7 @@
import Data.Aeson as AE import Data.Aeson as AE
import Data.ByteString.Lazy as BL import Data.ByteString.Lazy as BL
import Data.ByteString.Lazy.Char8 as BL8 import Data.ByteString.Lazy.Char8 as BL8
import Data.HashMap.Strict as HM
import Prelude as P import Prelude as P
import Worldcup.Continents
import Worldcup.Countries import Worldcup.Countries
import Worldcup.Events import Worldcup.Events
import Worldcup.EventsTeams import Worldcup.EventsTeams
@ -43,7 +41,7 @@ main = do
let countries = parseCountries (AE.eitherDecode dataCountries) let countries = parseCountries (AE.eitherDecode dataCountries)
let rounds = parseRounds (AE.eitherDecode dataRounds) let rounds = parseRounds (AE.eitherDecode dataRounds)
let tourneys = buildTourneys rounds eventsteams teams games events let tourneys = buildTourneys countries rounds eventsteams teams games events
let worldcup = WorldcupData countries rounds tourneys let worldcup = WorldcupData countries rounds tourneys
let encoded = encode worldcup let encoded = encode worldcup

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