eslint format

This commit is contained in:
2023-09-07 00:30:19 +02:00
parent c92492a587
commit 11008c3a01
2 changed files with 289 additions and 242 deletions
+65 -58
View File
@@ -1,22 +1,22 @@
import { Router, Request, Response } from "express"; /* eslint-disable @typescript-eslint/naming-convention */
import { validateErrors } from "../common"; import { Router, type Response } from 'express'
import { import {
checkOrCreateLoadout, checkOrCreateLoadout,
checkOrCreateWeapon, checkOrCreateTitan,
checkUpdateOrCreatePlayer, checkOrCreateWeapon
} from "../utils"; } from '../utils'
import { KillData, validateBody, RequestBody } from "../types"; import { type KillData, validateBody, type RequestBody } from '../types'
import db from "../db"; import db from '../db'
import typia from "typia"; import typia from 'typia'
const router = Router(); const router = Router()
router.post( router.post(
"/kill", '/',
validateBody(typia.createValidate<KillData>()), validateBody(typia.createValidate<KillData>()),
(req: RequestBody<KillData>, res: Response) => { (req: RequestBody<KillData>, res: Response) => {
void (async () => { void (async () => {
const host_id = res.locals.host_id; const host_id = res.locals.host_id
const startTime = Date.now() const startTime = Date.now()
const { const {
game_time, game_time,
@@ -24,72 +24,79 @@ router.post(
match_id, match_id,
attacker: attackerData, attacker: attackerData,
victim: victimData, victim: victimData,
cause_of_death, cause_of_death
} = req.body; } = req.body
const attacker_id = Number(attackerData.id); const attacker_id = Number(attackerData.id)
const victim_id = Number(victimData.id); const victim_id = Number(victimData.id)
if (isNaN(attacker_id) || isNaN(victim_id)) { if (isNaN(attacker_id) || isNaN(victim_id)) {
res res
.status(400) .status(400)
.send({ errors: [{ msg: "attacker_id or victim_id is NaN" }] }); .send({ errors: [{ msg: 'attacker_id or victim_id is NaN' }] })
return; return
} }
const match = await db const match = await db
.selectFrom("ToneAPI_v3.match") .selectFrom('ToneAPI_v3.match')
.select(["ToneAPI_v3.match.ongoing", "ToneAPI_v3.match.match_id"]) .select(['ToneAPI_v3.match.ongoing', 'ToneAPI_v3.match.match_id'])
.where("ToneAPI_v3.match.host_id", "=", host_id) .where('ToneAPI_v3.match.host_id', '=', host_id)
.where("ToneAPI_v3.match.match_id", "=", match_id) .where('ToneAPI_v3.match.match_id', '=', match_id)
.executeTakeFirst(); .executeTakeFirst()
if (!match) { if (!match) {
res.status(403).send({ res.status(403).send({
errors: [ errors: [
{ {
msg: "Match does not exists", msg: 'Match does not exists',
param: "match_id", param: 'match_id',
location: "body", location: 'body'
}, }
], ]
}); })
return; return
} }
if (!match.ongoing) { if (!match.ongoing) {
res.status(409).send({ res.status(409).send({
errors: [ errors: [
{ {
msg: "Match is already closed", msg: 'Match is already closed',
param: "match_id", param: 'match_id',
location: "body", location: 'body'
},
],
});
return;
} }
let attacker_loadout: number | undefined; ]
let victim_loadout: number | undefined; })
return
}
let attacker_loadout: number | undefined
let victim_loadout: number | undefined
// await checkUpdateOrCreatePlayer(
// { id: attacker_id, name: attackerData.name })
// await checkUpdateOrCreatePlayer(
// { id: victim_id, name: victimData.name }
// )
if (attackerData.loadout.titan) {
await checkOrCreateTitan(attackerData.loadout.titan)
}
if (victimData.loadout.titan) {
await checkOrCreateTitan(victimData.loadout.titan)
}
await checkOrCreateWeapon(cause_of_death)
await checkOrCreateWeapon(attackerData.current_weapon.id)
await checkOrCreateWeapon(victimData.current_weapon.id)
await checkUpdateOrCreatePlayer(
{ id: attacker_id, name: attackerData.name });
await checkUpdateOrCreatePlayer(
{ id: victim_id, name: victimData.name }
);
await checkOrCreateWeapon(cause_of_death);
await checkOrCreateWeapon(attackerData.current_weapon.id);
await checkOrCreateWeapon(victimData.current_weapon.id);
await checkOrCreateLoadout(attackerData.loadout).then( await checkOrCreateLoadout(attackerData.loadout).then(
(e) => (attacker_loadout = e) (e) => (attacker_loadout = e)
); )
await checkOrCreateLoadout(victimData.loadout).then( await checkOrCreateLoadout(victimData.loadout).then(
(e) => (victim_loadout = e) (e) => (victim_loadout = e)
); )
if (attacker_loadout === undefined) { if (attacker_loadout === undefined) {
throw new Error("attacker_loadout is undefined"); throw new Error('attacker_loadout is undefined')
} }
if (victim_loadout === undefined) { if (victim_loadout === undefined) {
throw new Error("victim_loadout is undefined"); throw new Error('victim_loadout is undefined')
} }
const insertResult = await db const insertResult = await db
.insertInto("ToneAPI_v3.kill") .insertInto('ToneAPI_v3.kill')
.values({ .values({
attacker_id, attacker_id,
victim_id, victim_id,
@@ -104,15 +111,15 @@ router.post(
game_time, game_time,
cause_of_death, cause_of_death,
attacker_held_weapon: attackerData.current_weapon.id, attacker_held_weapon: attackerData.current_weapon.id,
victim_held_weapon: victimData.current_weapon.id, victim_held_weapon: victimData.current_weapon.id
}) })
.returning("kill_id") .returning('kill_id')
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow()
res.status(201).send({ id: insertResult.kill_id }); res.status(201).send({ id: insertResult.kill_id })
console.log(Date.now()-startTime) console.log((Date.now() - startTime) + ' ms')
})(); })()
} }
); )
export default router export default router
+155 -115
View File
@@ -1,185 +1,225 @@
import { afterAll, beforeAll, describe, expect, test } from '@jest/globals' import { afterAll, beforeAll, describe, expect, test } from '@jest/globals'
import serverMain from '../src/serverMain' import serverMain from '../src/serverMain'
import db, {dbReady} from "../src/db" import db, { dbReady } from '../src/db'
import * as dotenv from 'dotenv' import * as dotenv from 'dotenv'
import {type KillData} from '../src/types' import { type MatchCloseData, type KillData } from '../src/types'
dotenv.config() dotenv.config()
let listenServer let listenServer
const headers = { const headers = {
"Content-Type": "application/json", 'Content-Type': 'application/json',
'Authorization': `Bearer ${Buffer.from(process.env.SERVERAUTH_TOKEN + '').toString('base64')}` Authorization: `Bearer ${Buffer.from(process.env.SERVERAUTH_TOKEN + '').toString('base64')}`
} }
const testMatch = { air_accel:false, server_name:"servertest"+Math.floor(Math.random()*100).toString(), game_map:"testMap", gamemode:"test" } const testMatch = { air_accel: false, server_name: 'servertest' + Math.floor(Math.random() * 100).toString(), game_map: 'testMap', gamemode: 'test' }
const testKill = { const testKill: KillData = {
"game_time": 22.616668701171876, game_time: 22.61666870117186,
"player_count": 1, player_count: 1,
"match_id": 1, match_id: 1,
"victim": { victim: {
"velocity": 0.0, velocity: 0.0,
"name": "Legonzaur", name: 'Legonzaur',
"loadout": { loadout: {
"titan":"testTitan", titan: 'testTitan',
"passive1":"testPassive1", passive1: 'testPassive1',
"passive2":"testPassive2", passive2: 'testPassive2',
"ordnance": { ordnance: {
"id": "mp_weapon_satchel", id: 'mp_weapon_satchel',
"mods": 0 mods: 0
}, },
"secondary": { secondary: {
"id": "mp_weapon_wingman", id: 'mp_weapon_wingman',
"mods": 140 mods: 140
}, },
"primary": { primary: {
"id": "mp_weapon_sniper", id: 'mp_weapon_sniper',
"mods": 1168 mods: 1168
}, },
"tactical": { tactical: {
"id": "mp_ability_grapple", id: 'mp_ability_grapple',
"mods": 8 mods: 8
}, },
"anti_titan": { anti_titan: {
"id": "mp_weapon_defender", id: 'mp_weapon_defender',
"mods": 134 mods: 134
} }
}, },
"current_weapon": { current_weapon: {
"id": "mp_weapon_sniper", id: 'mp_weapon_sniper',
"mods": 1168 mods: 1168
}, },
"state": "OnGround", state: 'OnGround',
"titan": "null", id: '1005930844007',
"id": 1005930844007, cloaked: false
"cloaked": false
}, },
"attacker": { attacker: {
"velocity": 0.0, velocity: 0.0,
"name": "Legonzaur", name: 'Legonzaur',
"loadout": { loadout: {
"titan":"testTitan", titan: 'testTitan',
"passive1":"testPassive1", passive1: 'testPassive1',
"passive2":"testPassive2", passive2: 'testPassive2',
"ordnance": { ordnance: {
"id": "mp_weapon_satchel", id: 'mp_weapon_satchel',
"mods": 0 mods: 0
}, },
"secondary": { secondary: {
"id": "mp_weapon_wingman", id: 'mp_weapon_wingman',
"mods": 140 mods: 140
}, },
"primary": { primary: {
"id": "mp_weapon_sniper", id: 'mp_weapon_sniper',
"mods": 1168 mods: 1168
}, },
"tactical": { tactical: {
"id": "mp_ability_grapple", id: 'mp_ability_grapple',
"mods": 8 mods: 8
}, },
"anti_titan": { anti_titan: {
"id": "mp_weapon_defender", id: 'mp_weapon_defender',
"mods": 134 mods: 134
} }
}, },
"current_weapon": { current_weapon: {
"id": "mp_weapon_sniper", id: 'mp_weapon_sniper',
"mods": 1168 mods: 1168
}, },
"state": "OnGround", state: 'OnGround',
"titan": "null", id: '1005930844007',
"id": 1005930844007, cloaked: false
"cloaked": false
}, },
"distance": 0.0, distance: 0.0,
"cause_of_death": "mp_weapon_satchel" cause_of_death: 'mp_weapon_satchel'
} as KillData }
const testMatchStats: MatchCloseData = {
1005930844007: {
stats: {
distance: {
air: 122.2,
ground: 50.4,
wall: 20.1
},
time: {
air: 1220.2,
ground: 500.4,
wall: 200.1
},
data: {
username: 'Legonzaur'
}
},
weapons: {}
}
}
beforeAll(async () => { beforeAll(async () => {
listenServer = await serverMain; listenServer = await serverMain
await dbReady() await dbReady()
}) })
describe('server', () => { describe('auth', () => {
test('bad auth prefetch', async () => { test('bad auth prefetch', async () => {
const response = await fetch(`http://127.0.0.1:3001/`, { const response = await fetch('http://127.0.0.1:3001/', {
method: "POST", // *GET, POST, PUT, DELETE, etc. method: 'POST', // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit credentials: 'same-origin', // include, *same-origin, omit
headers: { headers: {
"Content-Type": "application/json", 'Content-Type': 'application/json',
'Authorization': `Bearere ${Buffer.from('' + process.env.SERVERAUTH_TOKEN).toString('base64')}` Authorization: `Bearere ${Buffer.from('' + process.env.SERVERAUTH_TOKEN).toString('base64')}`
} }
}); })
expect(response.status).toBe(400) expect(response.status).toBe(400)
const response2 = await fetch(`http://127.0.0.1:3001/`, { const response2 = await fetch('http://127.0.0.1:3001/', {
method: "POST", // *GET, POST, PUT, DELETE, etc. method: 'POST', // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit credentials: 'same-origin', // include, *same-origin, omit
headers: { headers: {
"Content-Type": "application/json", 'Content-Type': 'application/json',
'Authorization': `Bearer ${Buffer.from('badtoken').toString('base64')}` Authorization: `Bearer ${Buffer.from('badtoken').toString('base64')}`
} }
}); })
expect(response2.status).toBe(401) expect(response2.status).toBe(401)
}) })
test('bad kill token', async () => { test('bad kill token', async () => {
const response2 = await fetch(`http://127.0.0.1:3001/kill`, { const response2 = await fetch('http://127.0.0.1:3001/kill', {
method: "POST", // *GET, POST, PUT, DELETE, etc. method: 'POST', // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit credentials: 'same-origin', // include, *same-origin, omit
headers: { headers: {
"Content-Type": "application/json", 'Content-Type': 'application/json',
'Authorization': `Bearer ${Buffer.from('badtoken').toString('base64')}` Authorization: `Bearer ${Buffer.from('badtoken').toString('base64')}`
} }
}); })
expect(response2.status).toBe(401) expect(response2.status).toBe(401)
}) })
test('good auth prefetch', async () => { test('good auth prefetch', async () => {
const response = await fetch(`http://127.0.0.1:3001/`, { const response = await fetch('http://127.0.0.1:3001/', {
method: "POST", // *GET, POST, PUT, DELETE, etc. method: 'POST', // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit credentials: 'same-origin', // include, *same-origin, omit
headers headers
}); })
expect(response.status).toBe(200) expect(response.status).toBe(200)
}) })
})
describe('stats', () => {
let matchId: string
test('register a match', async () => { test('register a match', async () => {
const response = await fetch(`http://127.0.0.1:3001/match`, { const response = await fetch('http://127.0.0.1:3001/match', {
method: "POST", method: 'POST',
headers, headers,
body: JSON.stringify(testMatch), body: JSON.stringify(testMatch)
}); })
const json = await response.json() const json = await response.json()
expect(json).toHaveProperty("match") matchId = json.match
testKill.match_id = Number(matchId)
expect(json).toHaveProperty('match')
expect(json.match).not.toBeNaN() expect(json.match).not.toBeNaN()
expect(response.status).toBe(201) expect(response.status).toBe(201)
}) })
test('register a kill', async () => { test('register a kill', async () => {
const response = await fetch(`http://127.0.0.1:3001/kill`, { const response = await fetch('http://127.0.0.1:3001/kill', {
method: "POST", method: 'POST',
headers, headers,
body: JSON.stringify(testKill), body: JSON.stringify(testKill)
}); })
console.log(await response.text()) console.log(await response.text())
expect(response.status).toBe(201) expect(response.status).toBe(201)
}) })
test('register a kill with missing data', async () => { // test('register a kill with missing data', async () => {
const data = testKill // const data = testKill
const response = await fetch(`http://127.0.0.1:3001/kill`, { // const response = await fetch('http://127.0.0.1:3001/kill', {
method: "POST", // *GET, POST, PUT, DELETE, etc. // method: 'POST', // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit // credentials: 'same-origin', // include, *same-origin, omit
headers: { // headers: {
"Content-Type": "application/json", // 'Content-Type': 'application/json',
'Authorization': `Bearer ${Buffer.from(process.env.SERVERAUTH_TOKEN + '').toString('base64')}` // Authorization: `Bearer ${Buffer.from(process.env.SERVERAUTH_TOKEN + '').toString('base64')}`
}, // },
body: JSON.stringify(data), // body: JSON.stringify(data)
}); // })
// expect(response.status).toBe(400)
// })
test('close a match', async () => {
const response = await fetch(`http://127.0.0.1:3001/match/${matchId}/close`, {
method: 'POST',
headers,
body: JSON.stringify(testMatchStats)
})
expect(response.status).toBe(201) expect(response.status).toBe(201)
}) })
test('register a kill after a match is closed', async () => {
const response = await fetch('http://127.0.0.1:3001/kill', {
method: 'POST',
headers,
body: JSON.stringify(testKill)
})
expect(response.status).toBe(409)
})
}) })
afterAll((done) => { afterAll((done) => {