kill insertion working

This commit is contained in:
2023-08-12 13:51:18 +02:00
parent 338a283a07
commit 59c53a8417
9 changed files with 330 additions and 94 deletions
+1 -1
Submodule src/db updated: c985feb726...dc5325c23a
+34 -22
View File
@@ -1,31 +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();
const app = express();
const port = 3001;
import * as dotenv from 'dotenv'
import express from 'express'
import cors from 'cors'
import { dbReady } from './db/db'
import server from './generated/server'
dotenv.config()
app.use(express.json());
const app = express()
const port = 3001
app.use(cors());
app.get("/", (req, res) => {
res.send("Tone server API online");
});
app.use(express.json())
app.use(cors())
app.get('/', (req, res) => {
res.send('Tone server API online')
})
app.use('/', server)
app.use("/", (req, res, next) => {
try {
next();
} catch (error) {
res.status(500).send({
errors: [
{
msg: "Internal error",
data: error,
},
],
});
}
});
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);
});
});
});
+87 -21
View File
@@ -1,10 +1,15 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Router, Request, Response } from "express";
import { header } from "express-validator";
import db, { /* CreateKillRecord, */ CheckServerToken } from "../db/db";
import { header, param } from "express-validator";
import {
checkOrCreateLoadout,
checkOrCreateWeapon,
checkUpdateOrCreatePlayer,
} from "../utils";
import db, { /* createKillRecord, */ checkServerToken } from "../db";
import { validateErrors } from "../common";
import { KillData, MatchData, validateBody } from "../server/types";
import typia from "typia";
import { KillData, MatchData, validateBody } from "../types";
import typia, { validate } from "typia";
const router = Router();
@@ -23,7 +28,7 @@ router.post(
if (!req.headers.authorization) {
return res.sendStatus(401);
}
const query = await CheckServerToken(
const query = await checkServerToken(
req.headers.authorization.split(" ")[1]
);
if (!query?.host_id) {
@@ -85,7 +90,14 @@ router.post(
(req: RequestBody<KillData>, res: Response) => {
void (async () => {
const host_id = res.locals.host_id;
const { match_id } = req.body;
const {
game_time,
distance,
match_id,
attacker: attackerData,
victim: victimData,
cause_of_death,
} = req.body;
const match = await db
.selectFrom("ToneAPI_v3.match")
.select(["ToneAPI_v3.match.host_id", "ToneAPI_v3.match.match_id"])
@@ -93,20 +105,60 @@ router.post(
.where("ToneAPI_v3.match.match_id", "=", match_id)
.executeTakeFirst();
if (!match) {
res
.status(403)
.send({
errors: [
{
msg: "Match does not exists",
param: "match_id",
location: "body",
},
],
});
res.status(403).send({
errors: [
{
msg: "Match does not exists",
param: "match_id",
location: "body",
},
],
});
return;
}
req.body;
let attacker_loadout: number | undefined;
let victim_loadout: number | undefined;
await Promise.all([
checkUpdateOrCreatePlayer(attackerData),
checkUpdateOrCreatePlayer(victimData),
checkOrCreateWeapon(cause_of_death),
checkOrCreateWeapon(attackerData.current_weapon.id),
checkOrCreateWeapon(victimData.current_weapon.id),
checkOrCreateLoadout(attackerData.loadout).then(
(e) => (attacker_loadout = e)
),
checkOrCreateLoadout(victimData.loadout).then(
(e) => (victim_loadout = e)
),
]);
if (attacker_loadout === undefined) {
throw new Error("attacker_lodaout is undefined");
}
if (victim_loadout === undefined) {
throw new Error("victim_lodaout is undefined");
}
const insertResult = await db
.insertInto("ToneAPI_v3.kill")
.values({
attacker_id: attackerData.id,
victim_id: victimData.id,
match_id,
attacker_loadout_id: attacker_loadout,
victim_loadout_id: victim_loadout,
attacker_speed: attackerData.velocity,
victim_speed: victimData.velocity,
attacker_movementstate: attackerData.state,
victim_movementstate: victimData.state,
distance,
game_time,
cause_of_death,
attacker_held_weapon: attackerData.current_weapon.id,
victim_held_weapon: victimData.current_weapon.id,
})
.returning("kill_id")
.executeTakeFirstOrThrow();
res.status(201).send({ id: insertResult.kill_id });
})();
}
);
@@ -130,11 +182,25 @@ router.post(
.values({ host_id, server_name })
.execute();
}
await db.updateTable("ToneAPI_v3.match")
.set({ ongoing: false })
.where("ToneAPI_v3.match.server_name", "=", server_name).execute();
const match = await db
.insertInto("ToneAPI_v3.match")
.values({ air_accel, server_name, game_map, gamemode, host_id })
.executeTakeFirst();
res.status(201).send({ match: match.insertId });
.values({
air_accel,
server_name,
game_map,
gamemode,
host_id,
ongoing: true,
})
.returning("ToneAPI_v3.match.match_id")
.executeTakeFirstOrThrow();
res.status(201).send({ match: match?.match_id });
})();
}
);
+14 -10
View File
@@ -2,24 +2,28 @@ import { RequestHandler, Request } from "express";
import typia from "typia";
type WeaponKillData = {
name: string;
id: string;
mods: number;
};
export type LoadoutKillData = {
ordnance: WeaponKillData | null;
secondary: WeaponKillData | null;
primary: WeaponKillData | null;
tactical: WeaponKillData | null;
anti_titan: WeaponKillData | null;
passive1:string | null;
passive2:string | null;
titan: string | null;
};
type PlayerKillData = {
velocity: number;
name: string;
loadout: {
ordnance: WeaponKillData;
secondary: WeaponKillData;
primary: WeaponKillData;
tactical: WeaponKillData;
anti_titan: WeaponKillData;
};
loadout: LoadoutKillData;
current_weapon: WeaponKillData;
state: string;
titan: string | null;
id: number | bigint;
id: number;
cloaked: boolean;
};
+138
View File
@@ -0,0 +1,138 @@
import db from "./db";
import { LoadoutKillData } from "./types";
export async function checkUpdateOrCreatePlayer(data: {
id: number;
name: string;
}) {
const player = await db
.selectFrom("ToneAPI_v3.player")
.select(["player_name"])
.where("player_id", "=", data.id)
.executeTakeFirst();
if (!player) {
await db
.insertInto("ToneAPI_v3.player")
.values({
player_id: data.id,
player_name: data.name,
})
.execute();
} else if (player.player_name != data.name) {
await db
.updateTable("ToneAPI_v3.player")
.set({ player_name: data.name })
.where("ToneAPI_v3.player.player_id", "=", data.id)
.execute();
}
}
export async function checkOrCreateWeapon(weapon_id: string) {
const weapon = await db
.selectFrom("ToneAPI_v3.weapon")
.select("ToneAPI_v3.weapon.weapon_id")
.where("weapon_id", "=", weapon_id)
.executeTakeFirst();
if (!weapon) {
await db
.insertInto("ToneAPI_v3.weapon")
.values({
weapon_id,
})
.execute();
}
}
export async function checkOrCreateWeaponMods(weapon_mods: {
id: string;
mods: number;
}) {
await checkOrCreateWeapon(weapon_mods.id);
const weaponMods = await db
.selectFrom("ToneAPI_v3.mods_on_weapon")
.select("ToneAPI_v3.mods_on_weapon.mod_id")
.where("mod_id", "=", weapon_mods.mods)
.where("weapon_id", "=", weapon_mods.id)
.executeTakeFirst();
if (!weaponMods) {
await db
.insertInto("ToneAPI_v3.mods_on_weapon")
.values({
mod_id: weapon_mods.mods,
weapon_id: weapon_mods.id,
autogenerated: true,
})
.execute();
}
}
export async function checkOrCreateTitan(titan_id: string | null) {
if (titan_id == null) {
return;
}
const titan = await db
.selectFrom("ToneAPI_v3.titan_chassis")
.select("ToneAPI_v3.titan_chassis.titan_id")
.where("titan_id", "=", titan_id)
.executeTakeFirst();
if (!titan) {
await db
.insertInto("ToneAPI_v3.titan_chassis")
.values({
titan_id,
})
.execute();
}
}
export async function checkOrCreateLoadout(loadoutData: LoadoutKillData) {
let loadout = await db
.selectFrom("ToneAPI_v3.loadout")
.select("ToneAPI_v3.loadout.loadout_id")
.where("primary_weapon", "=", loadoutData.primary?.id ?? null)
.where("primary_mod_id", "=", loadoutData.primary?.mods ?? null)
.where("secondary_weapon", "=", loadoutData.secondary?.id ?? null)
.where("secondary_mod_id", "=", loadoutData.secondary?.mods ?? null)
.where("anti_titan_weapon", "=", loadoutData.anti_titan?.id ?? null)
.where("anti_titan_mod_id", "=", loadoutData.anti_titan?.mods ?? null)
.where("ToneAPI_v3.loadout.ordnance", "=", loadoutData.ordnance?.id ?? null)
.where("ToneAPI_v3.loadout.tactical", "=", loadoutData.tactical?.id ?? null)
.where("ToneAPI_v3.loadout.pilot_passive_1", "=", loadoutData.passive1)
.where("ToneAPI_v3.loadout.pilot_passive_2", "=", loadoutData.passive2)
.where("ToneAPI_v3.loadout.titan_id", "=", loadoutData.titan)
.executeTakeFirst();
if (!loadout) {
const promises = new Array<Promise<void>>();
if (loadoutData.primary !== null) {
promises.push(checkOrCreateWeaponMods(loadoutData.primary));
}
if (loadoutData.secondary !== null) {
promises.push(checkOrCreateWeaponMods(loadoutData.secondary));
}
if (loadoutData.anti_titan !== null) {
promises.push(checkOrCreateWeaponMods(loadoutData.anti_titan));
}
if (loadoutData.ordnance !== null) {
promises.push(checkOrCreateWeapon(loadoutData.ordnance.id));
}
await Promise.all([...promises, checkOrCreateTitan(loadoutData.titan)]);
const result = await db
.insertInto("ToneAPI_v3.loadout")
.values({
primary_weapon: loadoutData.primary?.id ?? null,
primary_mod_id: loadoutData.primary?.mods ?? null,
secondary_weapon: loadoutData.secondary?.id ?? null,
secondary_mod_id: loadoutData.secondary?.mods ?? null,
anti_titan_weapon: loadoutData.anti_titan?.id ?? null,
anti_titan_mod_id: loadoutData.anti_titan?.mods ?? null,
ordnance: loadoutData.ordnance?.id ?? null,
tactical: loadoutData.tactical?.id ?? null,
pilot_passive_1: loadoutData.passive1,
pilot_passive_2: loadoutData.passive2,
titan_id: loadoutData.titan,
}).returning('ToneAPI_v3.loadout.loadout_id')
.executeTakeFirstOrThrow();
return result.loadout_id;
}
return loadout.loadout_id
}