eslint format

This commit is contained in:
2023-09-06 14:08:17 +02:00
parent 9c58af1e13
commit 0ad5946246
2 changed files with 121 additions and 126 deletions
+28 -28
View File
@@ -1,43 +1,43 @@
// Typed routes from https://urosstok.com/blog/typed-routes-in-express // Typed routes from https://urosstok.com/blog/typed-routes-in-express
import * as dotenv from "dotenv"; import * as dotenv from 'dotenv'
import express from "express"; import express from 'express'
import cors from "cors"; import cors from 'cors'
import { dbReady } from "./db"; import { dbReady } from './db'
import server from "./generated/server"; import server from './generated/server'
dotenv.config(); dotenv.config()
const app = express(); const app = express()
const port = 3001; const port = 3001
app.use(express.json()); app.use(express.json())
app.use(cors()); app.use(cors())
app.get("/", (req, res) => { app.get('/', (req, res) => {
res.send("Tone server API online"); res.send('Tone server API online')
}); })
app.use("/", (req, res, next) => { app.use('/', (req, res, next) => {
try { try {
next(); next()
} catch (error) { } catch (error) {
res.status(500).send({ res.status(500).send({
errors: [ errors: [
{ {
msg: "Internal error", msg: 'Internal error',
data: error, data: error
}, }
], ]
}); })
} }
}); })
app.use("/", server); app.use('/', server)
export default new Promise((resolve, reject) => { export default new Promise((resolve, reject) => {
void dbReady().then((e) => { void dbReady().then((e) => {
const listenServer = app.listen(port, "0.0.0.0", () => { const listenServer = app.listen(port, '0.0.0.0', () => {
console.log(`Tone server api listening on port ${port}`); console.log(`Tone server api listening on port ${port}`)
resolve(listenServer); resolve(listenServer)
}); })
}); })
}); })
+93 -98
View File
@@ -1,163 +1,158 @@
import { Kysely, Transaction } from "kysely"; import { type Transaction } from 'kysely'
import db from "./db"; import db from './db'
import Database from "./db/model"; import type Database from './db/model'
import { LoadoutKillData } from "./types"; import { type LoadoutKillData } from './types'
import { Data } from 'ws';
export async function createOrRunInTransaction (callback: (trx: Transaction<Database>) => Promise<void>, trx?: Transaction<Database>) {
if (trx) {
export async function createOrRunInTransaction(callback: (trx: Transaction<Database>) => Promise<void>, trx?:Transaction<Database>){
if(trx){
await callback(trx) await callback(trx)
}else{ } else {
await db await db
.transaction() .transaction()
.setIsolationLevel("serializable") .setIsolationLevel('serializable')
.execute(callback); .execute(callback)
} }
} }
export async function checkUpdateOrCreatePlayer(data: { export async function checkUpdateOrCreatePlayer (data: {
id: number; id: number
name: string; name: string
}) { }) {
await db await db
.transaction() .transaction()
.setIsolationLevel("serializable") .setIsolationLevel('serializable')
.execute(async (trx) => { .execute(async (trx) => {
const player = await trx const player = await trx
.selectFrom("ToneAPI_v3.player") .selectFrom('ToneAPI_v3.player')
.select(["player_name"]) .select(['player_name'])
.where("player_id", "=", data.id) .where('player_id', '=', data.id)
.executeTakeFirst(); .executeTakeFirst()
if (!player) { if (!player) {
await trx await trx
.insertInto("ToneAPI_v3.player") .insertInto('ToneAPI_v3.player')
.values({ .values({
player_id: data.id, player_id: data.id,
player_name: data.name, player_name: data.name
}) })
.execute(); .execute()
} else if (player.player_name != data.name) { } else if (player.player_name !== data.name) {
await trx await trx
.updateTable("ToneAPI_v3.player") .updateTable('ToneAPI_v3.player')
.set({ player_name: data.name }) .set({ player_name: data.name })
.where("ToneAPI_v3.player.player_id", "=", data.id) .where('ToneAPI_v3.player.player_id', '=', data.id)
.execute(); .execute()
} }
}); })
} }
export async function checkOrCreateWeapon(weapon_id: string, trx?:Transaction<Database>) { export async function checkOrCreateWeapon (weaponID: string, trx?: Transaction<Database>) {
const runner = async (trx:Transaction<Database>) => { const runner = async (trx: Transaction<Database>) => {
const weapon = await trx const weapon = await trx
.selectFrom("ToneAPI_v3.weapon") .selectFrom('ToneAPI_v3.weapon')
.select("ToneAPI_v3.weapon.weapon_id") .select('ToneAPI_v3.weapon.weapon_id')
.where("weapon_id", "=", weapon_id) .where('weapon_id', '=', weaponID)
.executeTakeFirst(); .executeTakeFirst()
if (!weapon) { if (!weapon) {
await trx await trx
.insertInto("ToneAPI_v3.weapon") .insertInto('ToneAPI_v3.weapon')
.values({ .values({
weapon_id, weapon_id: weaponID
}) })
.execute(); .execute()
} }
} }
await createOrRunInTransaction(runner, trx) await createOrRunInTransaction(runner, trx)
} }
export async function checkOrCreateWeaponMods(weapon: { export async function checkOrCreateWeaponMods (weapon: {
id: string; id: string
mods: number; mods: number
}, trx?:Transaction<Database>) { }, trx?: Transaction<Database>) {
const runner = async (trx:Transaction<Database>) => { const runner = async (trx: Transaction<Database>) => {
await checkOrCreateWeapon(weapon.id, trx); await checkOrCreateWeapon(weapon.id, trx)
const weaponMods = await trx const weaponMods = await trx
.selectFrom("ToneAPI_v3.mods_on_weapon") .selectFrom('ToneAPI_v3.mods_on_weapon')
.select("ToneAPI_v3.mods_on_weapon.mod_id") .select('ToneAPI_v3.mods_on_weapon.mod_id')
.where("mod_id", "=", weapon.mods) .where('mod_id', '=', weapon.mods)
.where("weapon_id", "=", weapon.id) .where('weapon_id', '=', weapon.id)
.executeTakeFirst(); .executeTakeFirst()
if (!weaponMods) { if (!weaponMods) {
await trx await trx
.insertInto("ToneAPI_v3.mods_on_weapon") .insertInto('ToneAPI_v3.mods_on_weapon')
.values({ .values({
mod_id: weapon.mods, mod_id: weapon.mods,
weapon_id: weapon.id, weapon_id: weapon.id,
autogenerated: true, autogenerated: true
}) })
.execute(); .execute()
} }
} }
await createOrRunInTransaction(runner, trx) await createOrRunInTransaction(runner, trx)
} }
export async function checkOrCreateTitan(titan_id: string | null, trx?:Transaction<Database>) { export async function checkOrCreateTitan (titanID: string | null, trx?: Transaction<Database>) {
if (titan_id == null) { if (titanID == null) {
return; return
} }
const runner = async (trx:Transaction<Database>) => { const runner = async (trx: Transaction<Database>) => {
const titan = await trx const titan = await trx
.selectFrom("ToneAPI_v3.titan_chassis") .selectFrom('ToneAPI_v3.titan_chassis')
.select("ToneAPI_v3.titan_chassis.titan_id") .select('ToneAPI_v3.titan_chassis.titan_id')
.where("titan_id", "=", titan_id) .where('titan_id', '=', titanID)
.executeTakeFirst(); .executeTakeFirst()
if (!titan) { if (!titan) {
await trx await trx
.insertInto("ToneAPI_v3.titan_chassis") .insertInto('ToneAPI_v3.titan_chassis')
.values({ .values({
titan_id, titan_id: titanID
}) })
.execute(); .execute()
} }
} }
await createOrRunInTransaction(runner, trx) await createOrRunInTransaction(runner, trx)
} }
export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) { export async function checkOrCreateLoadout (loadoutData: LoadoutKillData) {
const loadout = await db const loadout = await db
.transaction() .transaction()
.setIsolationLevel("serializable") .setIsolationLevel('serializable')
.execute(async (trx) => { .execute(async (trx) => {
let loadout = await trx const loadout = await trx
.selectFrom("ToneAPI_v3.loadout") .selectFrom('ToneAPI_v3.loadout')
.select("ToneAPI_v3.loadout.loadout_id") .select('ToneAPI_v3.loadout.loadout_id')
.where("primary_weapon", loadoutData.primary?.id !== undefined ? "=" : "is", loadoutData.primary?.id === undefined ? null : loadoutData.primary?.id) .where('primary_weapon', loadoutData.primary?.id !== undefined ? '=' : 'is', loadoutData.primary?.id ?? null)
.where("primary_mod_id", loadoutData.primary?.mods !== undefined ? "=" : "is", loadoutData.primary?.mods === undefined ? null : loadoutData.primary?.mods) .where('primary_mod_id', loadoutData.primary?.mods !== undefined ? '=' : 'is', loadoutData.primary?.mods ?? null)
.where("secondary_weapon", loadoutData.secondary?.id !== undefined ? "=" : "is", loadoutData.secondary?.id === undefined ? null : loadoutData.secondary?.id ) .where('secondary_weapon', loadoutData.secondary?.id !== undefined ? '=' : 'is', loadoutData.secondary?.id ?? null)
.where("secondary_mod_id", loadoutData.secondary?.mods !== undefined ? "=" : "is", loadoutData.secondary?.mods === undefined ? null : loadoutData.secondary?.mods) .where('secondary_mod_id', loadoutData.secondary?.mods !== undefined ? '=' : 'is', loadoutData.secondary?.mods ?? null)
.where("anti_titan_weapon", loadoutData.anti_titan?.id !== undefined ? "=" : "is", loadoutData.anti_titan?.id === undefined ? null : loadoutData.anti_titan?.id ) .where('anti_titan_weapon', loadoutData.anti_titan?.id !== undefined ? '=' : 'is', loadoutData.anti_titan?.id ?? null)
.where("anti_titan_mod_id", loadoutData.anti_titan?.mods !== undefined ? "=" : "is", loadoutData.anti_titan?.mods === undefined ? null : loadoutData.anti_titan?.mods) .where('anti_titan_mod_id', loadoutData.anti_titan?.mods !== undefined ? '=' : 'is', loadoutData.anti_titan?.mods ?? null)
.where("ordnance", loadoutData.ordnance?.id !== undefined ? "=" : "is", loadoutData.ordnance?.id === undefined ? null : loadoutData.ordnance?.id) .where('ordnance', loadoutData.ordnance?.id !== undefined ? '=' : 'is', loadoutData.ordnance?.id ?? null)
.where("tactical", loadoutData.tactical?.id !== undefined ? "=" : "is", loadoutData.tactical?.id === undefined ? null : loadoutData.tactical?.id) .where('tactical', loadoutData.tactical?.id !== undefined ? '=' : 'is', loadoutData.tactical?.id ?? null)
.where("pilot_passive_1", loadoutData.passive1 !== undefined ? "=" : "is", loadoutData.passive1 === undefined ? null : loadoutData.passive1) .where('pilot_passive_1', loadoutData.passive1 !== undefined ? '=' : 'is', loadoutData.passive1 ?? null)
.where("pilot_passive_2", loadoutData.passive2 !== undefined ? "=" : "is", loadoutData.passive2 === undefined ? null : loadoutData.passive2) .where('pilot_passive_2', loadoutData.passive2 !== undefined ? '=' : 'is', loadoutData.passive2 ?? null)
.where("titan_id", loadoutData.titan !== undefined ? "=" : "is", loadoutData.titan === undefined ? null : loadoutData.titan) .where('titan_id', loadoutData.titan !== undefined ? '=' : 'is', loadoutData.titan ?? null)
.executeTakeFirst(); .executeTakeFirst()
if (!loadout) { if (!loadout) {
if (loadoutData.primary !== undefined) { if (loadoutData.primary !== undefined) {
await checkOrCreateWeaponMods(loadoutData.primary, trx); await checkOrCreateWeaponMods(loadoutData.primary, trx)
} }
if (loadoutData.secondary !== undefined) { if (loadoutData.secondary !== undefined) {
await checkOrCreateWeaponMods(loadoutData.secondary, trx); await checkOrCreateWeaponMods(loadoutData.secondary, trx)
} }
if (loadoutData.anti_titan !== undefined) { if (loadoutData.anti_titan !== undefined) {
await checkOrCreateWeaponMods(loadoutData.anti_titan, trx); await checkOrCreateWeaponMods(loadoutData.anti_titan, trx)
} }
if (loadoutData.ordnance !== undefined) { if (loadoutData.ordnance !== undefined) {
await checkOrCreateWeapon(loadoutData.ordnance.id, trx); await checkOrCreateWeapon(loadoutData.ordnance.id, trx)
} }
if (loadoutData.titan !== undefined) { if (loadoutData.titan !== undefined) {
await checkOrCreateTitan(loadoutData.titan, trx); await checkOrCreateTitan(loadoutData.titan, trx)
} }
const result = await trx const result = await trx
.insertInto("ToneAPI_v3.loadout") .insertInto('ToneAPI_v3.loadout')
.values({ .values({
primary_weapon: loadoutData.primary?.id ?? null, primary_weapon: loadoutData.primary?.id ?? null,
primary_mod_id: loadoutData.primary?.mods ?? null, primary_mod_id: loadoutData.primary?.mods ?? null,
@@ -169,16 +164,16 @@ export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
tactical: loadoutData.tactical?.id ?? null, tactical: loadoutData.tactical?.id ?? null,
pilot_passive_1: loadoutData.passive1, pilot_passive_1: loadoutData.passive1,
pilot_passive_2: loadoutData.passive2, pilot_passive_2: loadoutData.passive2,
titan_id: loadoutData.titan, titan_id: loadoutData.titan
}) })
.returning("ToneAPI_v3.loadout.loadout_id") .returning('ToneAPI_v3.loadout.loadout_id')
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow()
return result; return result
} }
return loadout return loadout
}); })
if (!loadout) { if (!loadout) {
throw Error("loadout is undefined"); throw Error('loadout is undefined')
} }
return loadout.loadout_id; return loadout.loadout_id
} }