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.ByteString.Lazy.Char8 as BL8
import Prelude as P
import Worldcup.Countries
import Worldcup.Games
import Worldcup.Teams
data Squad = Squad {
squadCountryId :: Int,
squadGoalsFor :: Int,
squadGoalsAgainst :: Int
squadTeamId :: Int,
squadCountryId :: Int,
squadGoalsFor :: Int,
squadGoalsAgainst :: Int,
squadCountryPopulation :: Int
} deriving (Show)
instance ToJSON Squad where
toJSON Squad{..} =
object [ "cId" .= squadCountryId,
object [ "tId" .= squadTeamId,
"cId" .= squadCountryId,
"gf" .= squadGoalsFor,
"ga" .= squadGoalsAgainst ]
"ga" .= squadGoalsAgainst,
"p" .= squadCountryPopulation ]
parseInt :: Value -> Int
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)
| 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
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)

@ -21,7 +21,7 @@ data Tourney = Tourney {
tourneySquads :: [Squad] } deriving (Show)
instance ToJSON Tourney where
toJSON Tourney{..} = object [ "games" .= tourneyGames, "squad" .= tourneySquads ]
toJSON Tourney{..} = object [ "games" .= tourneyGames, "teams" .= tourneySquads ]
roundIsInEvent :: Int -> Int -> Rounds -> Bool
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
| otherwise = acc
buildTourneys :: Rounds -> [EventTeam] -> Teams -> [Game] -> [Event] -> Tourneys
buildTourneys rs ets ts gs = foldl reducer HM.empty where
buildTourneys :: Countries -> Rounds -> [EventTeam] -> Teams -> [Game] -> [Event] -> Tourneys
buildTourneys cs 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
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)

@ -102,7 +102,7 @@ const Diagram = {
return goalsAgainst[teamId];
},
build: (data, eventIndex, metaMatrix, chordMatrix) => {
build: (data, eventIndex, matrix) => {
const svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
@ -112,14 +112,15 @@ const Diagram = {
const chords = d3.chord()
.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.getGoalsFor.bind(null, data, eventIndex),
// Diagram.getGoalsAgainst.bind(null, data, eventIndex),
Diagram.getPopulation.bind(null, data, eventIndex),
Diagram.swapGroups.bind(null, data, eventIndex));
// Diagram.getPopulation.bind(null, data, eventIndex),
// Diagram.swapGroups.bind(null, data, eventIndex));
const arc = d3.arc()
.innerRadius(innerRadius)
@ -196,9 +197,9 @@ const Diagram = {
.text(function(d) {
// 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 country = data.countries[data.teams[teamId]];
return data.countries[data.teams[teamId]].n + ' ' + country.p;
const team = data.tourneys[eventIndex].teams[d.index];
const country = data.countries[team.cId];
return data.countries[team.cId] + ' ' + team.p;
});
},
};

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

@ -5,30 +5,14 @@ const Matrices = {
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).
buildChordMatrix: (json, eventKey) => {
buildMatrix: (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);
const i1 = teams.findIndex(v => v.tId === g.t1);
const i2 = teams.findIndex(v => v.tId === g.t2);
matrix[i1][i2] = g.s1 + g.se1 + g.sp1;
matrix[i2][i1] = g.s2 + g.se2 + g.sp2;

@ -4,9 +4,7 @@
import Data.Aeson as AE
import Data.ByteString.Lazy as BL
import Data.ByteString.Lazy.Char8 as BL8
import Data.HashMap.Strict as HM
import Prelude as P
import Worldcup.Continents
import Worldcup.Countries
import Worldcup.Events
import Worldcup.EventsTeams
@ -43,7 +41,7 @@ main = do
let countries = parseCountries (AE.eitherDecode dataCountries)
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 encoded = encode worldcup

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