Skip to content

Commit

Permalink
Fixes bugs with first boot connections not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasdotcom committed Jun 15, 2022
1 parent 1de8129 commit 1c68941
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bundesliga",
"version": "0.1.2",
"version": "0.1.3",
"private": true,
"scripts": {
"dev": "npm install; node scripts/entrypoint.mjs & next dev",
Expand Down
64 changes: 45 additions & 19 deletions scripts/entrypoint.mjs
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
import { createConnection } from 'mysql';
import {updateData} from './update.mjs'
const connection = createConnection({
host : process.env.MYSQL_HOST,
user : "root",
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DATABASE
})
// Used to tell the program what version of the database to use
const currentVersion = "0.1.1"
let date = new Date
var day = date.getDay()
connection.query("CREATE TABLE IF NOT EXISTS players (uid varchar(25) PRIMARY KEY, name 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, locked bool, `exists` bool)")
// Creates a table that contains some key value pairs for data that is needed for some things
connection.query("CREATE TABLE IF NOT EXISTS data (value1 varchar(25) PRIMARY KEY, value2 varchar(255))")
// Used to store the leagues
connection.query("CREATE TABLE IF NOT EXISTS leagues (leagueName varchar(255), leagueID int, player varchar(255), points int, money int, formation varchar(255))")
// Used to store the Historical Points
connection.query("CREATE TABLE IF NOT EXISTS points (leagueID int, player varchar(255), points int, matchday int)")
// Used to store transfers
connection.query("CREATE TABLE IF NOT EXISTS transfers (leagueID int, seller varchar(255), buyer varchar(255), playeruid varchar(25), value int)")
// Used to store invite links
connection.query("CREATE TABLE IF NOT EXISTS invite (inviteID varchar(25) PRIMARY KEY, leagueID int)")
// Used to store player squads
connection.query("CREATE TABLE IF NOT EXISTS squad (leagueID int, player varchar(255), playeruid varchar(25), position varchar(5))")

async function startUp() {
// Waits unitl the sql server is ready
let error = true
while (error) {
error = await new Promise((res) => {
setTimeout(() => {
const connection = createConnection({
host : process.env.MYSQL_HOST,
user : "root",
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DATABASE
})
connection.query("SHOW TABLES", function(error) {
res(error)
})
}, 500)
})
}
const connection = createConnection({
host : process.env.MYSQL_HOST,
user : "root",
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DATABASE
})
// Used to store the players data
connection.query("CREATE TABLE IF NOT EXISTS players (uid varchar(25) PRIMARY KEY, name 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, locked bool, `exists` bool)")
// Creates a table that contains some key value pairs for data that is needed for some things
connection.query("CREATE TABLE IF NOT EXISTS data (value1 varchar(25) PRIMARY KEY, value2 varchar(255))")
// Used to store the leagues
connection.query("CREATE TABLE IF NOT EXISTS leagues (leagueName varchar(255), leagueID int, player varchar(255), points int, money int, formation varchar(255))")
// Used to store the Historical Points
connection.query("CREATE TABLE IF NOT EXISTS points (leagueID int, player varchar(255), points int, matchday int)")
// Used to store transfers
connection.query("CREATE TABLE IF NOT EXISTS transfers (leagueID int, seller varchar(255), buyer varchar(255), playeruid varchar(25), value int)")
// Used to store invite links
connection.query("CREATE TABLE IF NOT EXISTS invite (inviteID varchar(25) PRIMARY KEY, leagueID int)")
// Used to store player squads
connection.query("CREATE TABLE IF NOT EXISTS squad (leagueID int, player varchar(255), playeruid varchar(25), position varchar(5))")
// Checks the version of the database is out of date
await new Promise((res) => {
connection.query("SELECT value2 FROM data WHERE value1='version'", function(error, result, field) {
Expand All @@ -46,6 +65,12 @@ async function startUp() {
}
startUp()
async function update() {
const connection = createConnection({
host : process.env.MYSQL_HOST,
user : "root",
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DATABASE
})
let newDate = new Date
// Checks if a new day is happening
if (day != newDate.getDay()) {
Expand Down Expand Up @@ -76,4 +101,5 @@ async function update() {
})
}
connection.query("UPDATE data SET value2='0' WHERE value1='update'")
connection.end()
}
7 changes: 5 additions & 2 deletions scripts/update.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export async function updateData() {
// Puts in the data if the transfermarket is open
const oldTransfer = await new Promise((res) => {
connection.query("SELECT * FROM data WHERE value1='transferOpen'", function(error, result, field) {
if (result.length == 0) res(true)
res(parseInt(result[0].value2) > 0)
if (result.length == 0) {
res(true)
} else {
res(parseInt(result[0].value2) > 0)
}
})
})
const newTransfer = (await data).opening_hour.countdown > 3600 ? (await data).opening_hour.countdown - 3600 : 0
Expand Down

0 comments on commit 1c68941

Please sign in to comment.