From 0ad5946246d2ae8f9742e42c5533e055d7591e2c Mon Sep 17 00:00:00 2001 From: Legonzaur Date: Wed, 6 Sep 2023 14:08:17 +0200 Subject: [PATCH] eslint format --- src/serverMain.ts | 56 +++++++------- src/utils.ts | 191 ++++++++++++++++++++++------------------------ 2 files changed, 121 insertions(+), 126 deletions(-) diff --git a/src/serverMain.ts b/src/serverMain.ts index 295e4ef..d3bd444 100644 --- a/src/serverMain.ts +++ b/src/serverMain.ts @@ -1,43 +1,43 @@ // Typed routes from https://urosstok.com/blog/typed-routes-in-express -import * as dotenv from "dotenv"; -import express from "express"; -import cors from "cors"; -import { dbReady } from "./db"; -import server from "./generated/server"; -dotenv.config(); +import * as dotenv from 'dotenv' +import express from 'express' +import cors from 'cors' +import { dbReady } from './db' +import server from './generated/server' +dotenv.config() -const app = express(); -const port = 3001; +const app = express() +const port = 3001 -app.use(express.json()); +app.use(express.json()) -app.use(cors()); -app.get("/", (req, res) => { - res.send("Tone server API online"); -}); +app.use(cors()) +app.get('/', (req, res) => { + res.send('Tone server API online') +}) -app.use("/", (req, res, next) => { +app.use('/', (req, res, next) => { try { - next(); + next() } catch (error) { res.status(500).send({ errors: [ { - msg: "Internal error", - data: error, - }, - ], - }); + msg: 'Internal error', + data: error + } + ] + }) } -}); -app.use("/", server); +}) +app.use('/', server) export default new Promise((resolve, reject) => { void dbReady().then((e) => { - const listenServer = app.listen(port, "0.0.0.0", () => { - console.log(`Tone server api listening on port ${port}`); - resolve(listenServer); - }); - }); -}); + const listenServer = app.listen(port, '0.0.0.0', () => { + console.log(`Tone server api listening on port ${port}`) + resolve(listenServer) + }) + }) +}) diff --git a/src/utils.ts b/src/utils.ts index 30d68d3..38e30be 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,163 +1,158 @@ -import { Kysely, Transaction } from "kysely"; -import db from "./db"; -import Database from "./db/model"; -import { LoadoutKillData } from "./types"; -import { Data } from 'ws'; +import { type Transaction } from 'kysely' +import db from './db' +import type Database from './db/model' +import { type LoadoutKillData } from './types' - - -export async function createOrRunInTransaction(callback: (trx: Transaction) => Promise, trx?:Transaction){ - if(trx){ +export async function createOrRunInTransaction (callback: (trx: Transaction) => Promise, trx?: Transaction) { + if (trx) { await callback(trx) - }else{ + } else { await db - .transaction() - .setIsolationLevel("serializable") - .execute(callback); + .transaction() + .setIsolationLevel('serializable') + .execute(callback) } } -export async function checkUpdateOrCreatePlayer(data: { - id: number; - name: string; +export async function checkUpdateOrCreatePlayer (data: { + id: number + name: string }) { await db .transaction() - .setIsolationLevel("serializable") + .setIsolationLevel('serializable') .execute(async (trx) => { const player = await trx - .selectFrom("ToneAPI_v3.player") - .select(["player_name"]) - .where("player_id", "=", data.id) - .executeTakeFirst(); + .selectFrom('ToneAPI_v3.player') + .select(['player_name']) + .where('player_id', '=', data.id) + .executeTakeFirst() if (!player) { await trx - .insertInto("ToneAPI_v3.player") + .insertInto('ToneAPI_v3.player') .values({ player_id: data.id, - player_name: data.name, + player_name: data.name }) - .execute(); - } else if (player.player_name != data.name) { + .execute() + } else if (player.player_name !== data.name) { await trx - .updateTable("ToneAPI_v3.player") + .updateTable('ToneAPI_v3.player') .set({ player_name: data.name }) - .where("ToneAPI_v3.player.player_id", "=", data.id) - .execute(); + .where('ToneAPI_v3.player.player_id', '=', data.id) + .execute() } - }); + }) } -export async function checkOrCreateWeapon(weapon_id: string, trx?:Transaction) { - const runner = async (trx:Transaction) => { +export async function checkOrCreateWeapon (weaponID: string, trx?: Transaction) { + const runner = async (trx: Transaction) => { const weapon = await trx - .selectFrom("ToneAPI_v3.weapon") - .select("ToneAPI_v3.weapon.weapon_id") - .where("weapon_id", "=", weapon_id) - .executeTakeFirst(); + .selectFrom('ToneAPI_v3.weapon') + .select('ToneAPI_v3.weapon.weapon_id') + .where('weapon_id', '=', weaponID) + .executeTakeFirst() if (!weapon) { await trx - .insertInto("ToneAPI_v3.weapon") + .insertInto('ToneAPI_v3.weapon') .values({ - weapon_id, + weapon_id: weaponID }) - .execute(); + .execute() } } - await createOrRunInTransaction(runner, trx) + await createOrRunInTransaction(runner, trx) } -export async function checkOrCreateWeaponMods(weapon: { - id: string; - mods: number; -}, trx?:Transaction) { - const runner = async (trx:Transaction) => { - await checkOrCreateWeapon(weapon.id, trx); +export async function checkOrCreateWeaponMods (weapon: { + id: string + mods: number +}, trx?: Transaction) { + const runner = async (trx: Transaction) => { + await checkOrCreateWeapon(weapon.id, trx) const weaponMods = await trx - .selectFrom("ToneAPI_v3.mods_on_weapon") - .select("ToneAPI_v3.mods_on_weapon.mod_id") - .where("mod_id", "=", weapon.mods) - .where("weapon_id", "=", weapon.id) - .executeTakeFirst(); + .selectFrom('ToneAPI_v3.mods_on_weapon') + .select('ToneAPI_v3.mods_on_weapon.mod_id') + .where('mod_id', '=', weapon.mods) + .where('weapon_id', '=', weapon.id) + .executeTakeFirst() if (!weaponMods) { await trx - .insertInto("ToneAPI_v3.mods_on_weapon") + .insertInto('ToneAPI_v3.mods_on_weapon') .values({ mod_id: weapon.mods, 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) { - if (titan_id == null) { - return; +export async function checkOrCreateTitan (titanID: string | null, trx?: Transaction) { + if (titanID == null) { + return } - const runner = async (trx:Transaction) => { + const runner = async (trx: Transaction) => { const titan = await trx - .selectFrom("ToneAPI_v3.titan_chassis") - .select("ToneAPI_v3.titan_chassis.titan_id") - .where("titan_id", "=", titan_id) - .executeTakeFirst(); + .selectFrom('ToneAPI_v3.titan_chassis') + .select('ToneAPI_v3.titan_chassis.titan_id') + .where('titan_id', '=', titanID) + .executeTakeFirst() if (!titan) { await trx - .insertInto("ToneAPI_v3.titan_chassis") + .insertInto('ToneAPI_v3.titan_chassis') .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 .transaction() - .setIsolationLevel("serializable") + .setIsolationLevel('serializable') .execute(async (trx) => { - let loadout = await trx - .selectFrom("ToneAPI_v3.loadout") - .select("ToneAPI_v3.loadout.loadout_id") - .where("primary_weapon", loadoutData.primary?.id !== undefined ? "=" : "is", loadoutData.primary?.id === undefined ? null : loadoutData.primary?.id) - .where("primary_mod_id", loadoutData.primary?.mods !== undefined ? "=" : "is", loadoutData.primary?.mods === undefined ? null : loadoutData.primary?.mods) - .where("secondary_weapon", loadoutData.secondary?.id !== undefined ? "=" : "is", loadoutData.secondary?.id === undefined ? null : loadoutData.secondary?.id ) - .where("secondary_mod_id", loadoutData.secondary?.mods !== undefined ? "=" : "is", loadoutData.secondary?.mods === undefined ? null : loadoutData.secondary?.mods) - .where("anti_titan_weapon", loadoutData.anti_titan?.id !== undefined ? "=" : "is", loadoutData.anti_titan?.id === undefined ? null : loadoutData.anti_titan?.id ) - .where("anti_titan_mod_id", loadoutData.anti_titan?.mods !== undefined ? "=" : "is", loadoutData.anti_titan?.mods === undefined ? null : loadoutData.anti_titan?.mods) - .where("ordnance", loadoutData.ordnance?.id !== undefined ? "=" : "is", loadoutData.ordnance?.id === undefined ? null : loadoutData.ordnance?.id) - .where("tactical", loadoutData.tactical?.id !== undefined ? "=" : "is", loadoutData.tactical?.id === undefined ? null : loadoutData.tactical?.id) - .where("pilot_passive_1", loadoutData.passive1 !== undefined ? "=" : "is", loadoutData.passive1 === undefined ? null : loadoutData.passive1) - .where("pilot_passive_2", loadoutData.passive2 !== undefined ? "=" : "is", loadoutData.passive2 === undefined ? null : loadoutData.passive2) - .where("titan_id", loadoutData.titan !== undefined ? "=" : "is", loadoutData.titan === undefined ? null : loadoutData.titan) - .executeTakeFirst(); + const loadout = await trx + .selectFrom('ToneAPI_v3.loadout') + .select('ToneAPI_v3.loadout.loadout_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 ?? null) + .where('secondary_weapon', loadoutData.secondary?.id !== undefined ? '=' : 'is', loadoutData.secondary?.id ?? null) + .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 ?? null) + .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 ?? null) + .where('tactical', loadoutData.tactical?.id !== undefined ? '=' : 'is', loadoutData.tactical?.id ?? null) + .where('pilot_passive_1', loadoutData.passive1 !== undefined ? '=' : 'is', loadoutData.passive1 ?? null) + .where('pilot_passive_2', loadoutData.passive2 !== undefined ? '=' : 'is', loadoutData.passive2 ?? null) + .where('titan_id', loadoutData.titan !== undefined ? '=' : 'is', loadoutData.titan ?? null) + .executeTakeFirst() if (!loadout) { if (loadoutData.primary !== undefined) { - await checkOrCreateWeaponMods(loadoutData.primary, trx); + await checkOrCreateWeaponMods(loadoutData.primary, trx) } if (loadoutData.secondary !== undefined) { - await checkOrCreateWeaponMods(loadoutData.secondary, trx); + await checkOrCreateWeaponMods(loadoutData.secondary, trx) } if (loadoutData.anti_titan !== undefined) { - await checkOrCreateWeaponMods(loadoutData.anti_titan, trx); + await checkOrCreateWeaponMods(loadoutData.anti_titan, trx) } if (loadoutData.ordnance !== undefined) { - await checkOrCreateWeapon(loadoutData.ordnance.id, trx); + await checkOrCreateWeapon(loadoutData.ordnance.id, trx) } if (loadoutData.titan !== undefined) { - await checkOrCreateTitan(loadoutData.titan, trx); + await checkOrCreateTitan(loadoutData.titan, trx) } const result = await trx - .insertInto("ToneAPI_v3.loadout") + .insertInto('ToneAPI_v3.loadout') .values({ primary_weapon: loadoutData.primary?.id ?? null, primary_mod_id: loadoutData.primary?.mods ?? null, @@ -169,16 +164,16 @@ export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) { tactical: loadoutData.tactical?.id ?? null, pilot_passive_1: loadoutData.passive1, pilot_passive_2: loadoutData.passive2, - titan_id: loadoutData.titan, + titan_id: loadoutData.titan }) - .returning("ToneAPI_v3.loadout.loadout_id") - .executeTakeFirstOrThrow(); - return result; + .returning('ToneAPI_v3.loadout.loadout_id') + .executeTakeFirstOrThrow() + return result } return loadout - }); + }) if (!loadout) { - throw Error("loadout is undefined"); + throw Error('loadout is undefined') } - return loadout.loadout_id; + return loadout.loadout_id }