-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update player points even after final matchday is done
- Loading branch information
1 parent
89916cb
commit 4a9189a
Showing
3 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |