Skip to content

Commit 88933f0

Browse files
committed
[#2] Predict date ranges
1 parent 7a87092 commit 88933f0

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

commands/predict.js

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,65 @@ module.exports = async (argv) => {
1313

1414
// pop the command name
1515
_.shift();
16-
let hometeam, awayteam;
17-
if (_[1] === 'at') {
18-
hometeam = _[2];
19-
awayteam = _[0];
20-
} else if (_[1] === 'vs') {
21-
hometeam = _[0];
22-
awayteam = _[2];
16+
if (!_[1]) {
17+
console.log(`Predicting from now until ${_[0]}`);
18+
await predictMatchesByDate(_[0]);
2319
} else {
24-
throw new Error(`Unrecognized conjunction ${_[1]}; please use "at" or "vs"`);
20+
let hometeam, awayteam;
21+
if (_[1] === 'at') {
22+
hometeam = _[2];
23+
awayteam = _[0];
24+
} else if (_[1] === 'vs') {
25+
hometeam = _[0];
26+
awayteam = _[2];
27+
} else {
28+
throw new Error(`Unrecognized conjunction ${_[1]}; please use "at" or "vs"`);
29+
}
30+
31+
await predictMatchByName(hometeam, awayteam);
2532
}
33+
}
34+
35+
async function predictMatchesByDate(date) {
36+
const results = await db.all(`
37+
WITH currentelo AS (
38+
SELECT *
39+
FROM rankings
40+
INNER JOIN (
41+
SELECT max(date) as maxDate, rankingteamid as teamid
42+
FROM rankings
43+
GROUP BY teamid
44+
) as dates
45+
ON dates.teamid = rankings.rankingteamid
46+
AND dates.maxDate = rankings.date
47+
)
48+
SELECT
49+
awayteam.teamname as awayName,
50+
away.elo as awayElo,
51+
hometeam.teamname as homeName,
52+
home.elo as homeElo
53+
FROM matches
54+
INNER JOIN currentelo as away
55+
ON away.rankingteamid = matches.awayteam
56+
INNER JOIN currentelo as home
57+
on home.rankingteamid = matches.hometeam
58+
INNER JOIN teams AS awayteam
59+
ON awayteam.teamid = matches.awayteam
60+
INNER JOIN teams AS hometeam
61+
ON hometeam.teamid = matches.hometeam
62+
WHERE matches.date <= ${(new Date(date)).getTime()}
63+
AND matches.date >= ${(new Date()).getTime()}
64+
`);
65+
66+
results.forEach(result => predictMatch({
67+
homeElo: result.homeElo,
68+
homeName: result.homeName,
69+
awayElo: result.awayElo,
70+
awayName: result.awayName
71+
}));
72+
}
2673

74+
async function predictMatchByName(hometeam, awayteam) {
2775
const homeTeamStanding = await db.get(`
2876
SELECT teamname, elo
2977
FROM rankings
@@ -50,9 +98,18 @@ module.exports = async (argv) => {
5098
ORDER BY elo DESC
5199
`);
52100

53-
if ((homeTeamStanding.elo + 100) > awayTeamStanding.elo) {
54-
console.log(`I predict ${homeTeamStanding.teamname} will win.`);
101+
predictMatch({
102+
homeName: homeTeamStanding.teamname,
103+
homeElo: homeTeamStanding.elo,
104+
awayName: awayTeamStanding.teamname,
105+
awayElo: awayTeamStanding.elo
106+
});
107+
}
108+
109+
function predictMatch({homeElo, awayElo, homeName, awayName}) {
110+
if ((homeElo + 100) > awayElo) {
111+
console.log(`I predict ${homeName} will win.`);
55112
} else {
56-
console.log(`I predict ${awayTeamStanding.teamname} will win.`)
113+
console.log(`I predict ${awayName} will win.`)
57114
}
58115
}

0 commit comments

Comments
 (0)