|
| 1 | +const db = require('sqlite'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const teams = require('../data/teams'); |
| 4 | +const results = require('../data/results'); |
| 5 | + |
| 6 | +module.exports = async (argv) => { |
| 7 | + const {dbPath, verbose} = argv; |
| 8 | + if (verbose) { |
| 9 | + console.info(`using db in ${dbPath}`); |
| 10 | + } |
| 11 | + |
| 12 | + await db.open(dbPath, { Promise }); |
| 13 | + |
| 14 | + const startDate = new Date('December 11, 1973').getTime(); |
| 15 | + const startElo = 1300; |
| 16 | + console.log(`seeding table with start elo ${startElo} on date ${startDate}`); |
| 17 | + await Promise.all(teams.map(team => db.run(` |
| 18 | + INSERT INTO rankings (rankingteamid, elo, date) VALUES ( |
| 19 | + (SELECT teamid FROM teams WHERE teams.abbreviation = "${team.abbreviation}"), |
| 20 | + ${startElo}, |
| 21 | + ${startDate} |
| 22 | + ) |
| 23 | + `))); |
| 24 | + const matches = await db.all(` |
| 25 | + select |
| 26 | + competitions.k, |
| 27 | + hometeam, |
| 28 | + awayteam, |
| 29 | + homegoals, |
| 30 | + awaygoals, |
| 31 | + matches.date as date |
| 32 | + from matches |
| 33 | + inner join competitions on competitions.competitionid = matches.matchcompetition |
| 34 | + order by date ASC |
| 35 | + `); |
| 36 | + console.log(`processing ${matches.length} matches`); |
| 37 | + for (const match of matches) { |
| 38 | + const awayranking = await db.get(` |
| 39 | + select |
| 40 | + rankings.elo, |
| 41 | + rankings.date |
| 42 | + from rankings |
| 43 | + inner join ( |
| 44 | + select |
| 45 | + max(date) as date, |
| 46 | + max.rankingteamid |
| 47 | + from rankings as max |
| 48 | + where max.rankingteamid = ${match.awayteam} |
| 49 | + group by max.rankingteamid |
| 50 | + ) as maxranking |
| 51 | + on rankings.rankingteamid = ${match.awayteam} |
| 52 | + and rankings.date = maxranking.date |
| 53 | + `); |
| 54 | + const homeranking = await db.get(` |
| 55 | + select |
| 56 | + rankings.elo, |
| 57 | + rankings.date |
| 58 | + from rankings |
| 59 | + inner join ( |
| 60 | + select |
| 61 | + max(date) as date, |
| 62 | + max.rankingteamid |
| 63 | + from rankings as max |
| 64 | + where max.rankingteamid = ${match.hometeam} |
| 65 | + group by max.rankingteamid |
| 66 | + ) as maxranking |
| 67 | + on rankings.rankingteamid = ${match.hometeam} |
| 68 | + and rankings.date = maxranking.date |
| 69 | + `); |
| 70 | + match.homeranking = homeranking.elo; |
| 71 | + match.awayranking = awayranking.elo; |
| 72 | + let homeElo, awayElo; |
| 73 | + if (match.homegoals < match.awaygoals) { |
| 74 | + homeElo = updateElo({ |
| 75 | + match, |
| 76 | + won: false, |
| 77 | + isHome: true |
| 78 | + }); |
| 79 | + awayElo = updateElo({ |
| 80 | + match, |
| 81 | + won: true, |
| 82 | + isHome: false |
| 83 | + }); |
| 84 | + } else if (match.homegoals === match.awaygoals) { |
| 85 | + homeElo = updateElo({ |
| 86 | + match, |
| 87 | + won: false, |
| 88 | + isHome: true |
| 89 | + }); |
| 90 | + awayElo = updateElo({ |
| 91 | + match, |
| 92 | + won: false, |
| 93 | + isHome: false |
| 94 | + }); |
| 95 | + } else { |
| 96 | + homeElo = updateElo({ |
| 97 | + match, |
| 98 | + won: true, |
| 99 | + isHome: true |
| 100 | + }); |
| 101 | + awayElo = updateElo({ |
| 102 | + match, |
| 103 | + won: false, |
| 104 | + isHome: false |
| 105 | + }); |
| 106 | + } |
| 107 | + await Promise.all([db.run(` |
| 108 | + INSERT INTO rankings (elo, date, rankingteamid) VALUES ( |
| 109 | + ${homeElo}, |
| 110 | + ${match.date}, |
| 111 | + ${match.hometeam} |
| 112 | + ) |
| 113 | + `), |
| 114 | + db.run(` |
| 115 | + INSERT INTO rankings (elo, date, rankingteamid) VALUES ( |
| 116 | + ${awayElo}, |
| 117 | + ${match.date}, |
| 118 | + ${match.awayteam} |
| 119 | + ) |
| 120 | + `) |
| 121 | + ]); |
| 122 | + console.log(`finished processing ${match.hometeam} vs ${match.awayteam} on ${match.date}`); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function updateElo({match, isHome, won}) { |
| 127 | + console.log(`${JSON.stringify(match)} ${isHome} ${won}`); |
| 128 | + let priorElo, opponentPriorElo; |
| 129 | + if (isHome) { |
| 130 | + priorElo = match.homeranking; |
| 131 | + opponentPriorElo = match.awayranking; |
| 132 | + } else { |
| 133 | + priorElo = match.awayranking; |
| 134 | + opponentPriorElo = match.homeranking; |
| 135 | + } |
| 136 | + const k = match.k; |
| 137 | + console.log(`priorElo ${priorElo} opponentPriorElo ${opponentPriorElo} k ${k}`); |
| 138 | + |
| 139 | + const eloDifference = priorElo - opponentPriorElo; |
| 140 | + let homeAdjustedEloDifference; |
| 141 | + if (isHome) { |
| 142 | + homeAdjustedEloDifference = eloDifference + 100; |
| 143 | + } else { |
| 144 | + homeAdjustedEloDifference = eloDifference - 100; |
| 145 | + } |
| 146 | + const x = homeAdjustedEloDifference / 200; |
| 147 | + const sexp = 1 / (1 + Math.pow(10, -x/2)); |
| 148 | + console.log(`eloDifference ${eloDifference} x ${x} sexp ${sexp}`); |
| 149 | + |
| 150 | + let sact; |
| 151 | + if (won) { |
| 152 | + let losingGoals = isHome ? match.awaygoals : match.homegoals; |
| 153 | + if (losingGoals > 5) { |
| 154 | + losingGoals = 5; |
| 155 | + } |
| 156 | + let goalDifferential = Math.abs(match.homegoals - match.awaygoals); |
| 157 | + if (goalDifferential > 6) { |
| 158 | + goalDifferential = 6; |
| 159 | + } |
| 160 | + sact = (1 - (results[losingGoals][goalDifferential] * .01)); |
| 161 | + } else { |
| 162 | + let losingGoals = isHome ? match.homegoals : match.awaygoals; |
| 163 | + if (losingGoals > 5) { |
| 164 | + losingGoals = 5; |
| 165 | + } |
| 166 | + let goalDifferential = Math.abs(match.homegoals - match.awaygoals); |
| 167 | + if (goalDifferential > 6) { |
| 168 | + goalDifferential = 6; |
| 169 | + } |
| 170 | + sact = (results[losingGoals][goalDifferential] * .01); |
| 171 | + } |
| 172 | + console.log(`sact ${sact} returning ${priorElo + (k * (sact - sexp))}`); |
| 173 | + return priorElo + (k * (sact - sexp)); |
| 174 | +} |
0 commit comments