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