Skip to content

Commit 398ddc5

Browse files
authored
Merge pull request #281 from Lukasdotcom/historical-attendance
Adds historical attendance storage
2 parents 60df300 + 94633ea commit 398ddc5

File tree

8 files changed

+88
-39
lines changed

8 files changed

+88
-39
lines changed

Modules/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export interface historicalPlayers {
235235
pictureUrl: string;
236236
value: number;
237237
position: position;
238+
forecast: forecast;
238239
total_points: number;
239240
average_points: number;
240241
last_match: number;

components/Player.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ function InternalPlayer({ data, children, starred, extraText, condensed }) {
9191
alt=""
9292
onError={() => {
9393
// If the picture does not exist a fallback picture is used
94-
console.log(1);
9594
setPictureUrl(fallbackImg);
9695
}}
9796
src={pictureUrl}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "fantasy-manager",
3-
"version": "1.9.0",
3+
"version": "1.9.1",
44
"private": true,
55
"scripts": {
6-
"dev": "export NODE_ENV=development APP_ENV=development; npm install; npm run build:ts; node scripts/entrypoint.js & next dev",
6+
"dev": "export NODE_ENV=development APP_ENV=development; npm run build:ts; node scripts/entrypoint.js & next dev",
77
"build": "npm ci; npm run build:ts; next build;",
88
"start": "export NODE_ENV=production APP_ENV=production; npm run start2",
99
"test": "npm run cypress & npm run start:test",

pages/api/player/[leagueType]/[uid].ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { checkUpdate } from "../../../../scripts/checkUpdate";
99
// Used to return a dictionary on the data for a player
1010
export default async function handler(
1111
req: NextApiRequest,
12-
res: NextApiResponse
12+
res: NextApiResponse<result>
1313
) {
1414
if (req.method == "GET") {
1515
const connection = await connect();
@@ -40,7 +40,6 @@ export default async function handler(
4040
);
4141
returnValue.push({
4242
...answer[0],
43-
forecast: "a",
4443
updateRunning: true,
4544
game,
4645
});

pages/player/[league]/[uid].tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import Link from "../../../components/Link";
33
import Dialog from "../../../components/Dialog";
44
import { GetServerSideProps } from "next";
55
import Head from "next/head.js";
6-
import connect, { historicalPlayers, players } from "../../../Modules/database";
6+
import connect, {
7+
forecast,
8+
historicalPlayers,
9+
players,
10+
} from "../../../Modules/database";
711
import Image from "next/image";
812
import { useEffect, useState } from "react";
913
import {
@@ -92,6 +96,7 @@ interface Data {
9296
opponent: string;
9397
position: string;
9498
exists: boolean;
99+
forecast: forecast;
95100
loading: false;
96101
}
97102
export default function Home({
@@ -127,6 +132,7 @@ export default function Home({
127132
club: player.club,
128133
position: player.position,
129134
exists: player.exists,
135+
forecast: player.forecast,
130136
loading: false,
131137
},
132138
});
@@ -158,6 +164,7 @@ export default function Home({
158164
opponent: data.game ? data.game.opponent : "",
159165
position: data.position,
160166
exists: data.exists,
167+
forecast: data.forecast,
161168
loading: false,
162169
};
163170
return newRows;
@@ -269,7 +276,10 @@ export default function Home({
269276
<h2>Historical Data Table</h2>
270277
<p>
271278
All the ones with a purple background mean the player was not in the
272-
league during these matchdays.
279+
league during these matchdays. Red background means that they were
280+
missing and yellow that attendence was unknown(This data is what was
281+
known just before the game started). Note: That that historical forecast
282+
and historical opponents only exist since version 1.9.1.
273283
</p>
274284
<Paper sx={{ width: "100%", overflow: "hidden" }}>
275285
<TableContainer>
@@ -286,7 +296,7 @@ export default function Home({
286296
.sort((e) => e.time)
287297
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
288298
.map((row) => {
289-
if (row.loading)
299+
if (row.loading) {
290300
return (
291301
<TableRow
292302
hover
@@ -300,17 +310,21 @@ export default function Home({
300310
</TableCell>
301311
</TableRow>
302312
);
313+
}
314+
// Gets the background color based on the status of the player
315+
let background;
316+
if (row.forecast == "u") {
317+
background = dark ? "rgb(50, 50, 0)" : "rgb(255, 255, 220)";
318+
} else if (row.forecast == "m") {
319+
background = dark ? "rgb(50, 0, 0)" : "rgb(255, 200, 200)";
320+
}
321+
// Checks if the player exists
322+
if (!row.exists) {
323+
background = dark ? "rgb(50, 0, 50)" : "rgb(255, 235, 255)";
324+
}
303325
return (
304326
<TableRow
305-
style={
306-
!row.exists
307-
? {
308-
background: dark
309-
? "rgb(50, 0, 50)"
310-
: "rgb(255, 235, 255)",
311-
}
312-
: {}
313-
}
327+
style={{ background }}
314328
hover
315329
role="checkbox"
316330
tabIndex={-1}
@@ -421,7 +435,6 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
421435
});
422436
return result;
423437
});
424-
console.log(player);
425438
connection.end();
426439
return {
427440
props: {

scripts/entrypoint.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (process.env.APP_ENV !== "test") {
1111
}
1212
const analyticsDomain = "https://fantasy.lschaefer.xyz";
1313
// Used to tell the program what version of the database to use
14-
const currentVersion = "1.9.0";
14+
const currentVersion = "1.9.1";
1515
const date = new Date();
1616
let day = date.getDay();
1717

@@ -72,7 +72,7 @@ async function startUp() {
7272
),
7373
// Used to store historical player data
7474
connection.query(
75-
"CREATE TABLE IF NOT EXISTS historicalPlayers (time int, uid varchar(25), name varchar(255), nameAscii varchar(255), club varchar(3), pictureUrl varchar(255), value int, position varchar(3), total_points int, average_points int, last_match int, `exists` bool, league varchar(25))"
75+
"CREATE TABLE IF NOT EXISTS historicalPlayers (time int, uid varchar(25), name varchar(255), nameAscii varchar(255), club varchar(3), pictureUrl varchar(255), value int, position varchar(3), forecast varchar(1), total_points int, average_points int, last_match int, `exists` bool, league varchar(25))"
7676
),
7777
// Used to store historical transfer data
7878
connection.query(
@@ -365,6 +365,23 @@ async function startUp() {
365365
await connection.query("DROP TABLE leagueSettingsTemp");
366366
oldVersion = "1.9.0";
367367
}
368+
if (oldVersion === "1.9.0") {
369+
console.log("Updating database to version 1.9.1");
370+
// Checks if the forecast column actually exists because it was removed a long time ago but not actually dropped
371+
const forecastExists = await connection
372+
.query(
373+
"SELECT COUNT(*) AS CNT FROM pragma_table_info('historicalPlayers') WHERE name='forecast'"
374+
)
375+
.then((e) => e[0].CNT === 1);
376+
if (!forecastExists) {
377+
await connection.query(
378+
"ALTER TABLE historicalPlayers ADD forecast varchar(1)"
379+
);
380+
}
381+
// Sets all the forecasts for the historical players to attending because they were unknown.
382+
await connection.query("UPDATE historicalPlayers SET forecast='a'");
383+
oldVersion = "1.9.1";
384+
}
368385
// HERE IS WHERE THE CODE GOES TO UPDATE THE DATABASE FROM ONE VERSION TO THE NEXT
369386
// Makes sure that the database is up to date
370387
if (oldVersion !== currentVersion) {

scripts/update.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export async function updateData(
9292
let index = 0;
9393
let club = "";
9494
let clubDone = false;
95+
let gameStarted = false;
9596
const getClub = (club: string): clubs => {
9697
const result = clubs.filter((e) => e.club == club);
9798
if (result.length == 0) {
@@ -178,9 +179,11 @@ export async function updateData(
178179
.then((res) =>
179180
res.length > 0 ? clubData.opponent !== res[0].opponent : false
180181
);
181-
// Checks if the club has not changed during the transfer period
182+
gameStarted = clubData.gameStart <= currentTime;
183+
// Checks if the club has not changed during the matchday
182184
if (!clubDone) {
183-
if (clubData.gameStart > currentTime) {
185+
// Checks if the game is supposed to have already started
186+
if (!gameStarted) {
184187
await connection.query(
185188
"INSERT INTO clubs (club, gameStart, opponent, league) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE gameStart=?, opponent=?",
186189
[
@@ -232,20 +235,37 @@ export async function updateData(
232235
);
233236
// Makes sure that the club is not done for the matchday
234237
} else if (!clubDone) {
235-
await connection.query(
236-
"UPDATE players SET value=?, forecast=?, total_points=?, average_points=?, last_match=?, locked=?, `exists`=1 WHERE uid=?",
237-
[
238-
val.value,
239-
val.forecast,
240-
val.total_points,
241-
val.average_points,
242-
playerExists[0].last_match +
243-
val.total_points -
244-
playerExists[0].total_points,
245-
val.locked,
246-
val.uid,
247-
]
248-
);
238+
// Only updates the forecast if the game has not already started
239+
if (gameStarted) {
240+
await connection.query(
241+
"UPDATE players SET value=?, total_points=?, average_points=?, last_match=?, locked=?, `exists`=1 WHERE uid=?",
242+
[
243+
val.value,
244+
val.total_points,
245+
val.average_points,
246+
playerExists[0].last_match +
247+
val.total_points -
248+
playerExists[0].total_points,
249+
val.locked,
250+
val.uid,
251+
]
252+
);
253+
} else {
254+
await connection.query(
255+
"UPDATE players SET value=?, forecast=?, total_points=?, average_points=?, last_match=?, locked=?, `exists`=1 WHERE uid=?",
256+
[
257+
val.value,
258+
val.forecast,
259+
val.total_points,
260+
val.average_points,
261+
playerExists[0].last_match +
262+
val.total_points -
263+
playerExists[0].total_points,
264+
val.locked,
265+
val.uid,
266+
]
267+
);
268+
}
249269
} else {
250270
await connection.query("UPDATE players SET `exists`=1 WHERE uid=?", [
251271
val.uid,
@@ -375,7 +395,7 @@ async function endMatchday(league: string) {
375395
console.log(`Archiving player data for ${league}`);
376396
// Copies all the player data to the historical player data
377397
await connection.query(
378-
"INSERT INTO historicalPlayers (time, uid, name, nameAscii, club, pictureUrl, value, position, total_points, average_points, last_match, `exists`, league) SELECT ? as time, uid, name, nameAscii, club, pictureUrl, value, position, total_points, average_points, last_match, `exists`, league FROM players WHERE league=?",
398+
"INSERT INTO historicalPlayers (time, uid, name, nameAscii, club, pictureUrl, value, position, forecast, total_points, average_points, last_match, `exists`, league) SELECT ? as time, uid, name, nameAscii, club, pictureUrl, value, position, forecast, total_points, average_points, last_match, `exists`, league FROM players WHERE league=?",
379399
[time, league]
380400
);
381401
console.log(`Archiving matchday data for ${league}`);

0 commit comments

Comments
 (0)