Skip to content

Commit

Permalink
Update player points even after final matchday is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasdotcom committed Jul 14, 2024
1 parent 89916cb commit 4a9189a
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 4 deletions.
2 changes: 1 addition & 1 deletion store/Euro2024/Euro2024.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
4 changes: 1 addition & 3 deletions store/Euro2024/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
205 changes: 205 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 4a9189a

Please sign in to comment.