Skip to content

Commit

Permalink
Merge pull request #281 from Lukasdotcom/historical-attendance
Browse files Browse the repository at this point in the history
Adds historical attendance storage
  • Loading branch information
Lukasdotcom authored Nov 10, 2022
2 parents 60df300 + 94633ea commit 398ddc5
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 39 deletions.
1 change: 1 addition & 0 deletions Modules/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export interface historicalPlayers {
pictureUrl: string;
value: number;
position: position;
forecast: forecast;
total_points: number;
average_points: number;
last_match: number;
Expand Down
1 change: 0 additions & 1 deletion components/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function InternalPlayer({ data, children, starred, extraText, condensed }) {
alt=""
onError={() => {
// If the picture does not exist a fallback picture is used
console.log(1);
setPictureUrl(fallbackImg);
}}
src={pictureUrl}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "fantasy-manager",
"version": "1.9.0",
"version": "1.9.1",
"private": true,
"scripts": {
"dev": "export NODE_ENV=development APP_ENV=development; npm install; npm run build:ts; node scripts/entrypoint.js & next dev",
"dev": "export NODE_ENV=development APP_ENV=development; npm run build:ts; node scripts/entrypoint.js & next dev",
"build": "npm ci; npm run build:ts; next build;",
"start": "export NODE_ENV=production APP_ENV=production; npm run start2",
"test": "npm run cypress & npm run start:test",
Expand Down
3 changes: 1 addition & 2 deletions pages/api/player/[leagueType]/[uid].ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { checkUpdate } from "../../../../scripts/checkUpdate";
// Used to return a dictionary on the data for a player
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<result>
) {
if (req.method == "GET") {
const connection = await connect();
Expand Down Expand Up @@ -40,7 +40,6 @@ export default async function handler(
);
returnValue.push({
...answer[0],
forecast: "a",
updateRunning: true,
game,
});
Expand Down
39 changes: 26 additions & 13 deletions pages/player/[league]/[uid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import Link from "../../../components/Link";
import Dialog from "../../../components/Dialog";
import { GetServerSideProps } from "next";
import Head from "next/head.js";
import connect, { historicalPlayers, players } from "../../../Modules/database";
import connect, {
forecast,
historicalPlayers,
players,
} from "../../../Modules/database";
import Image from "next/image";
import { useEffect, useState } from "react";
import {
Expand Down Expand Up @@ -92,6 +96,7 @@ interface Data {
opponent: string;
position: string;
exists: boolean;
forecast: forecast;
loading: false;
}
export default function Home({
Expand Down Expand Up @@ -127,6 +132,7 @@ export default function Home({
club: player.club,
position: player.position,
exists: player.exists,
forecast: player.forecast,
loading: false,
},
});
Expand Down Expand Up @@ -158,6 +164,7 @@ export default function Home({
opponent: data.game ? data.game.opponent : "",
position: data.position,
exists: data.exists,
forecast: data.forecast,
loading: false,
};
return newRows;
Expand Down Expand Up @@ -269,7 +276,10 @@ export default function Home({
<h2>Historical Data Table</h2>
<p>
All the ones with a purple background mean the player was not in the
league during these matchdays.
league during these matchdays. Red background means that they were
missing and yellow that attendence was unknown(This data is what was
known just before the game started). Note: That that historical forecast
and historical opponents only exist since version 1.9.1.
</p>
<Paper sx={{ width: "100%", overflow: "hidden" }}>
<TableContainer>
Expand All @@ -286,7 +296,7 @@ export default function Home({
.sort((e) => e.time)
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row) => {
if (row.loading)
if (row.loading) {
return (
<TableRow
hover
Expand All @@ -300,17 +310,21 @@ export default function Home({
</TableCell>
</TableRow>
);
}
// Gets the background color based on the status of the player
let background;
if (row.forecast == "u") {
background = dark ? "rgb(50, 50, 0)" : "rgb(255, 255, 220)";
} else if (row.forecast == "m") {
background = dark ? "rgb(50, 0, 0)" : "rgb(255, 200, 200)";
}
// Checks if the player exists
if (!row.exists) {
background = dark ? "rgb(50, 0, 50)" : "rgb(255, 235, 255)";
}
return (
<TableRow
style={
!row.exists
? {
background: dark
? "rgb(50, 0, 50)"
: "rgb(255, 235, 255)",
}
: {}
}
style={{ background }}
hover
role="checkbox"
tabIndex={-1}
Expand Down Expand Up @@ -421,7 +435,6 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
});
return result;
});
console.log(player);
connection.end();
return {
props: {
Expand Down
21 changes: 19 additions & 2 deletions scripts/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (process.env.APP_ENV !== "test") {
}
const analyticsDomain = "https://fantasy.lschaefer.xyz";
// Used to tell the program what version of the database to use
const currentVersion = "1.9.0";
const currentVersion = "1.9.1";
const date = new Date();
let day = date.getDay();

Expand Down Expand Up @@ -72,7 +72,7 @@ async function startUp() {
),
// Used to store historical player data
connection.query(
"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))"
"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))"
),
// Used to store historical transfer data
connection.query(
Expand Down Expand Up @@ -365,6 +365,23 @@ async function startUp() {
await connection.query("DROP TABLE leagueSettingsTemp");
oldVersion = "1.9.0";
}
if (oldVersion === "1.9.0") {
console.log("Updating database to version 1.9.1");
// Checks if the forecast column actually exists because it was removed a long time ago but not actually dropped
const forecastExists = await connection
.query(
"SELECT COUNT(*) AS CNT FROM pragma_table_info('historicalPlayers') WHERE name='forecast'"
)
.then((e) => e[0].CNT === 1);
if (!forecastExists) {
await connection.query(
"ALTER TABLE historicalPlayers ADD forecast varchar(1)"
);
}
// Sets all the forecasts for the historical players to attending because they were unknown.
await connection.query("UPDATE historicalPlayers SET forecast='a'");
oldVersion = "1.9.1";
}
// HERE IS WHERE THE CODE GOES TO UPDATE THE DATABASE FROM ONE VERSION TO THE NEXT
// Makes sure that the database is up to date
if (oldVersion !== currentVersion) {
Expand Down
54 changes: 37 additions & 17 deletions scripts/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export async function updateData(
let index = 0;
let club = "";
let clubDone = false;
let gameStarted = false;
const getClub = (club: string): clubs => {
const result = clubs.filter((e) => e.club == club);
if (result.length == 0) {
Expand Down Expand Up @@ -178,9 +179,11 @@ export async function updateData(
.then((res) =>
res.length > 0 ? clubData.opponent !== res[0].opponent : false
);
// Checks if the club has not changed during the transfer period
gameStarted = clubData.gameStart <= currentTime;
// Checks if the club has not changed during the matchday
if (!clubDone) {
if (clubData.gameStart > currentTime) {
// Checks if the game is supposed to have already started
if (!gameStarted) {
await connection.query(
"INSERT INTO clubs (club, gameStart, opponent, league) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE gameStart=?, opponent=?",
[
Expand Down Expand Up @@ -232,20 +235,37 @@ export async function updateData(
);
// Makes sure that the club is not done for the matchday
} else if (!clubDone) {
await connection.query(
"UPDATE players SET value=?, forecast=?, total_points=?, average_points=?, last_match=?, locked=?, `exists`=1 WHERE uid=?",
[
val.value,
val.forecast,
val.total_points,
val.average_points,
playerExists[0].last_match +
val.total_points -
playerExists[0].total_points,
val.locked,
val.uid,
]
);
// Only updates the forecast if the game has not already started
if (gameStarted) {
await connection.query(
"UPDATE players SET value=?, total_points=?, average_points=?, last_match=?, locked=?, `exists`=1 WHERE uid=?",
[
val.value,
val.total_points,
val.average_points,
playerExists[0].last_match +
val.total_points -
playerExists[0].total_points,
val.locked,
val.uid,
]
);
} else {
await connection.query(
"UPDATE players SET value=?, forecast=?, total_points=?, average_points=?, last_match=?, locked=?, `exists`=1 WHERE uid=?",
[
val.value,
val.forecast,
val.total_points,
val.average_points,
playerExists[0].last_match +
val.total_points -
playerExists[0].total_points,
val.locked,
val.uid,
]
);
}
} else {
await connection.query("UPDATE players SET `exists`=1 WHERE uid=?", [
val.uid,
Expand Down Expand Up @@ -375,7 +395,7 @@ async function endMatchday(league: string) {
console.log(`Archiving player data for ${league}`);
// Copies all the player data to the historical player data
await connection.query(
"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=?",
"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=?",
[time, league]
);
console.log(`Archiving matchday data for ${league}`);
Expand Down

0 comments on commit 398ddc5

Please sign in to comment.