From 4a9189ab9206f35d2a726c3bcf5e2b00cd1abb63 Mon Sep 17 00:00:00 2001 From: Lukasdotcom Date: Sun, 14 Jul 2024 09:07:48 -0400 Subject: [PATCH] Update player points even after final matchday is done --- store/Euro2024/Euro2024.json | 2 +- store/Euro2024/index.ts | 4 +- test.ts | 205 +++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 test.ts diff --git a/store/Euro2024/Euro2024.json b/store/Euro2024/Euro2024.json index 86b1c170..f5683376 100644 --- a/store/Euro2024/Euro2024.json +++ b/store/Euro2024/Euro2024.json @@ -1,6 +1,6 @@ { "id": "Euro2024", - "version": "0.5.3", + "version": "0.5.4", "min_version": "1.20.0", "description": "This is for the 2024 European Championship. Note that the last clubs player scores aren't always updated in time.", "files": [ diff --git a/store/Euro2024/index.ts b/store/Euro2024/index.ts index e0ea399e..62506dab 100644 --- a/store/Euro2024/index.ts +++ b/store/Euro2024/index.ts @@ -23,7 +23,6 @@ const Main: dataGetter = async function () { const playerList = data.data.value.playerList; const positions: position[] = ["gk", "gk", "def", "mid", "att"]; let transferOpen = true; - let update_points_after_game_end = true; let countdown = 0; const matchdays: { club: ResultClubs; matchday: CurrentMatchDay }[] = await fetch( @@ -73,7 +72,6 @@ const Main: dataGetter = async function () { // If game_data is undefined it means that all the matchdays are over if (!game_data) { game_data = matchdays[matchdays.length - 1].club; - update_points_after_game_end = false; // Will set this to false to not change the points for players after this is done transferOpen = false; } const clubs = []; @@ -202,7 +200,7 @@ const Main: dataGetter = async function () { countdown, players, clubs as clubs[], - { update_points_after_game_end }, + { update_points_after_game_end: true }, ]; }; export default Main; diff --git a/test.ts b/test.ts new file mode 100644 index 00000000..240bfd71 --- /dev/null +++ b/test.ts @@ -0,0 +1,205 @@ +import connect from "./Modules/database"; +import { leagueUsers, players, points, position } from "./types/database"; +async function update_points(times: number[]) { + const connection = await connect(); + for (const matchday of [6]) { + const time = times[matchday - 1]; + const leagueUsers: leagueUsers[] = await connection.query( + "SELECT * FROM leagueUsers WHERE EXISTS (SELECT * FROM leagueSettings WHERE leagueSettings.leagueID=leagueUsers.leagueID AND archived=0 AND league='Euro2024')", + ); + for (const leagueUser of leagueUsers) { + const leagueID = leagueUser.leagueID; + const userID = leagueUser.user; + const formation = [ + 1, + await connection + .query( + "SELECT * FROM historicalSquad WHERE leagueID=? AND user=? AND matchday=? AND position='def'", + [leagueID, userID, matchday], + ) + .then((e) => e.length), + await connection + .query( + "SELECT * FROM historicalSquad WHERE leagueID=? AND user=? AND matchday=? AND position='mid'", + [leagueID, userID, matchday], + ) + .then((e) => e.length), + await connection + .query( + "SELECT * FROM historicalSquad WHERE leagueID=? AND user=? AND matchday=? AND position='att'", + [leagueID, userID, matchday], + ) + .then((e) => e.length), + ]; + // Moves all players off the field + await connection.query( + `UPDATE historicalSquad SET position=(SELECT position FROM players WHERE players.uid=historicalSquad.playeruid AND league=(SELECT league FROM leagueSettings WHERE leagueID=?)) WHERE leagueID=? AND user=? AND matchday=?`, + [leagueID, leagueID, userID, matchday], + ); + const players: { + playeruid: string; + position: position; + points: number; + }[] = await connection.query( + `SELECT + historicalSquad.playeruid as playeruid, + historicalPlayers.position as position, + historicalPlayers.last_match + historicalPlayers.last_match * starred AS points + FROM + historicalSquad + LEFT OUTER JOIN historicalPlayers ON historicalPlayers.uid = historicalSquad.playeruid AND time=? + WHERE + user = ? + AND leagueID = ? + AND matchday = ? + ORDER BY + historicalPlayers.position, + points DESC`, + [time, userID, leagueID, matchday], + ); + const parts = ["gk", "def", "mid", "att"]; + console.log(players) + // Goes through every character and moves them to the correct position + for (const player of players) { + const position = parts.indexOf(player.position); + if (formation[position] > 0) { + await connection.query( + "UPDATE historicalSquad SET position=? WHERE playeruid=? AND leagueID=? AND user=? AND matchday=?", + [player.position, player.playeruid, leagueID, userID, matchday], + ); + formation[position]--; + } else { + await connection.query( + "UPDATE historicalSquad SET position='bench' WHERE playeruid=? AND leagueID=? AND user=? AND matchday=?", + [player.playeruid, leagueID, userID, matchday], + ); + } + } + const unstarred_points = await connection + .query( + "SELECT SUM(last_match) AS SUM FROM historicalPlayers WHERE EXISTS (SELECT * FROM historicalSquad WHERE historicalSquad.playeruid=historicalPlayers.uid AND position!='bench' AND leagueID=? AND user=? AND starred=0 AND matchday=?) AND time=?", + [leagueID, userID, matchday, time], + ) + .then((res) => (res.length > 0 ? res[0].SUM : 0)); + const starred_points = Math.ceil( + 1.5 * + (await connection + .query( + "SELECT SUM(last_match) AS SUM FROM historicalPlayers WHERE EXISTS (SELECT * FROM historicalSquad WHERE historicalSquad.playeruid=historicalPlayers.uid AND position!='bench' AND leagueID=? AND user=? AND starred=1 AND matchday=?) AND time=?", + [leagueID, userID, matchday, time], + ) + .then((res) => (res.length > 0 ? res[0].SUM : 0))), + ); + const total_points = unstarred_points + starred_points; + const points: points[] = await connection.query( + "SELECT * FROM points WHERE leagueID=? AND user=? AND matchday=?", + [leagueID, userID, matchday], + ); + console.log("CHANGE FOR: ", leagueID, userID); + if (points.length < 1) { + continue; + } + const change = total_points - points[0].fantasyPoints; + console.log("of", change); + await connection.query( + "UPDATE leagueUsers SET fantasyPoints=fantasyPoints+?, points=points+? WHERE leagueID=? AND user=?", + [change, change, leagueID, userID], + ); + await connection.query( + "UPDATE points SET fantasyPoints=fantasyPoints+?, points=points+? WHERE leagueID=? AND user=? AND matchday=?", + [change, change, leagueID, userID, matchday], + ); + } + } +} +async function run() { + const connection = await connect(); + // const time1 = 1718755203; + const time2 = 1720310405; + const time3 = 1720702209; + await connection.query( + "CREATE TABLE IF NOT EXISTS temp (matchday int, leagueID int, user int, playeruid varchar(25), position varchar(5), starred bool DEFAULT 0, PRIMARY KEY(matchday, leagueID, user, playeruid))", + ); + await connection.query("INSERT OR IGNORE INTO temp SELECT * FROM historicalSquad;"); + await connection.query("DELETE FROM historicalSquad;"); + await connection.query("INSERT INTO historicalSquad SELECT * FROM temp;"); + await connection.query("DROP TABLE temp;"); + + // const teams1 = ["POR", "GEO"]; + // for (const team of teams1) { + // console.log(team); + // const players: historicalPlayers[] = await connection.query( + // "SELECT * FROM historicalPlayers WHERE time=? AND club=?", + // [time2, team], + // ); + // for (const player of players) { + // await connection.query( + // "UPDATE historicalPlayers SET last_match=?, total_points=?, average_points=? WHERE uid=? AND time=?", + // [ + // player.total_points, + // player.total_points, + // player.total_points, + // player.uid, + // time1, + // ], + // ); + // } + // } + // const teams2 = ["TUR", "POR", "BEL", "ROU"]; + // for (const team of teams2) { + // console.log(team); + // const players: players[] = await connection.query( + // "SELECT * FROM players WHERE club=? AND league='Euro2024'", + // [team], + // ); + // for (const player of players) { + // const player_data_old = await connection.query( + // "SELECT * FROM historicalPlayers WHERE uid=? AND time=?", + // [player.uid, time1], + // ); + // await connection.query( + // "UPDATE historicalPlayers SET last_match=?, total_points=?, average_points=? WHERE uid=? AND time=?", + // [ + // player.total_points - player_data_old[0].total_points, + // player.total_points, + // player.total_points / 2, + // player.uid, + // time2, + // ], + // ); + // } + // } + const teams3 = ["ENG", "NED"]; + for (const team of teams3) { + console.log(team); + const players: players[] = await connection.query( + "SELECT * FROM players WHERE club=? AND league='Euro2024'", + [team], + ); + for (const player of players) { + const player_data_old = await connection.query( + "SELECT * FROM historicalPlayers WHERE uid=? AND time=?", + [player.uid, time2], + ); + await connection.query( + "UPDATE historicalPlayers SET last_match=?, total_points=?, average_points=? WHERE uid=? AND time=?", + [ + player.total_points - player_data_old[0].total_points, + player.total_points, + player.average_points, + player.uid, + time3, + ], + ); + await connection.query("UPDATE players SET last_match=? WHERE uid=?", [ + player.total_points - player_data_old[0].total_points, + player.uid, + ]); + } + } + // Now for updating points + update_points([0, 0, 0, 0, time2, time3]); + connection.end(); +} + +run();