fix model
This commit is contained in:
+10
-23
@@ -1,7 +1,7 @@
|
||||
import { Kysely, PostgresDialect } from 'kysely'
|
||||
import { InsertObject, Kysely, PostgresDialect } from 'kysely'
|
||||
import { Pool } from 'pg'
|
||||
import migrateToLatest from './migrations'
|
||||
import Database from './model'
|
||||
import Database, { KillTable } from './model'
|
||||
|
||||
const migration = migrateToLatest()
|
||||
// You'd create one of these when you start your app.
|
||||
@@ -17,28 +17,15 @@ const db = new Kysely<Database>({
|
||||
})
|
||||
})
|
||||
|
||||
interface KillRecord {
|
||||
server: string
|
||||
attacker: number
|
||||
victim: number
|
||||
weapon: number
|
||||
map: number
|
||||
distance: number
|
||||
date: Date
|
||||
interface RemoveFromKill {
|
||||
id: unknown
|
||||
unix_time: unknown
|
||||
}
|
||||
export async function CreateKillRecord({
|
||||
server,
|
||||
attacker,
|
||||
victim,
|
||||
weapon,
|
||||
map,
|
||||
distance,
|
||||
date
|
||||
}: KillRecord) {
|
||||
await db
|
||||
.insertInto('kill')
|
||||
.values({ server, attacker, victim, weapon, map, distance })
|
||||
.execute()
|
||||
|
||||
type KillRecord = Omit<KillTable, keyof RemoveFromKill>
|
||||
|
||||
export async function CreateKillRecord(data: KillRecord) {
|
||||
await db.insertInto('kill').values({ ...data })
|
||||
}
|
||||
/*
|
||||
async function demo() {
|
||||
|
||||
@@ -1,54 +1,66 @@
|
||||
import { Kysely, sql } from "kysely";
|
||||
import { Kysely, sql } from 'kysely'
|
||||
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.createTable("entity")
|
||||
.addColumn("id", "integer", (col) => col.unique().primaryKey())
|
||||
.addColumn("name", "varchar")
|
||||
.execute();
|
||||
.createTable('player')
|
||||
.addColumn('id', 'integer', (col) => col.unique().primaryKey())
|
||||
.addColumn('name', 'varchar')
|
||||
.addColumn('optout', 'boolean')
|
||||
.addColumn('hide_TOS', 'boolean')
|
||||
.execute()
|
||||
|
||||
await db.schema
|
||||
.createTable("weapon")
|
||||
.addColumn("id", "varchar", (col) => col.unique().primaryKey())
|
||||
.addColumn("name", "varchar")
|
||||
.addColumn("description", "varchar")
|
||||
.addColumn("image", "varchar")
|
||||
.execute();
|
||||
.createTable('weapon')
|
||||
.addColumn('id', 'varchar', (col) => col.unique().primaryKey())
|
||||
.addColumn('name', 'varchar')
|
||||
.addColumn('description', 'varchar')
|
||||
.addColumn('image', 'varchar')
|
||||
.execute()
|
||||
|
||||
await db.schema
|
||||
.createTable("map")
|
||||
.addColumn("id", "varchar", (col) => col.unique().primaryKey())
|
||||
.addColumn("name", "varchar")
|
||||
.addColumn("description", "varchar")
|
||||
.addColumn("image", "varchar")
|
||||
.execute();
|
||||
.createTable('map')
|
||||
.addColumn('id', 'varchar', (col) => col.unique().primaryKey())
|
||||
.addColumn('name', 'varchar')
|
||||
.addColumn('description', 'varchar')
|
||||
.addColumn('image', 'varchar')
|
||||
.execute()
|
||||
|
||||
await db.schema
|
||||
.createTable("server")
|
||||
.addColumn("name", "varchar", (col) => col.unique().primaryKey())
|
||||
.addColumn("description", "varchar")
|
||||
.addColumn("token", "varchar")
|
||||
.execute();
|
||||
.createTable('server')
|
||||
.addColumn('id', 'integer')
|
||||
.addColumn('name', 'varchar', (col) => col.unique().primaryKey())
|
||||
.addColumn('description', 'varchar')
|
||||
.addColumn('token', 'varchar')
|
||||
.execute()
|
||||
|
||||
await db.schema
|
||||
.createTable("kill")
|
||||
.addColumn("id", "serial", (col) => col.primaryKey())
|
||||
.addColumn("server", "varchar", (col) =>
|
||||
col.references("server.name").notNull()
|
||||
.createTable('kill')
|
||||
.addColumn('id', 'serial', (col) => col.primaryKey())
|
||||
.addColumn('server', 'integer', (col) =>
|
||||
col.references('server.id').notNull().onDelete('cascade')
|
||||
)
|
||||
.addColumn("attacker", "integer", (col) =>
|
||||
col.references("entity.id").notNull()
|
||||
)
|
||||
.addColumn("victim", "integer", (col) =>
|
||||
col.references("entity.id").notNull()
|
||||
)
|
||||
.addColumn("weapon", "varchar", (col) =>
|
||||
col.references("weapon.id").notNull()
|
||||
)
|
||||
.addColumn("map", "varchar", (col) => col.references("map.id").notNull())
|
||||
.addColumn("distance", "integer", (col) => col.notNull())
|
||||
.addColumn("date", "varchar", (col) => col.defaultTo(sql`now()`).notNull())
|
||||
.execute();
|
||||
.addColumn('damage_source', 'varchar')
|
||||
.addColumn('attacker', 'integer')
|
||||
.addColumn('attacker_type', 'varchar')
|
||||
.addColumn('attacker_weapon', 'varchar')
|
||||
.addColumn('attacker_weapon_2', 'varchar')
|
||||
.addColumn('attacker_weapon_3', 'varchar')
|
||||
.addColumn('attacker_ordnance', 'varchar')
|
||||
.addColumn('attacker_ability', 'varchar')
|
||||
.addColumn('attacker_kit_1', 'varchar')
|
||||
.addColumn('attacker_kit_2', 'varchar')
|
||||
.addColumn('victim', 'integer')
|
||||
.addColumn('victim_weapon', 'varchar')
|
||||
.addColumn('victim_weapon_2', 'integer')
|
||||
.addColumn('victim_weapon_3', 'integer')
|
||||
.addColumn('victim_ordnance', 'integer')
|
||||
.addColumn('victim_ability', 'integer')
|
||||
.addColumn('victim_kit_1', 'integer')
|
||||
.addColumn('victim_kit_2', 'integer')
|
||||
.addColumn('map', 'varchar')
|
||||
.addColumn('distance', 'integer')
|
||||
.addColumn('date', 'varchar', (col) => col.defaultTo(sql`now()`).notNull())
|
||||
.execute()
|
||||
|
||||
/*await db.schema
|
||||
.createTable("pet")
|
||||
@@ -68,9 +80,9 @@ export async function up(db: Kysely<any>): Promise<void> {
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await db.schema.dropTable("kill").execute();
|
||||
await db.schema.dropTable("entity").execute();
|
||||
await db.schema.dropTable("weapon").execute();
|
||||
await db.schema.dropTable("map").execute();
|
||||
await db.schema.dropTable("server").execute();
|
||||
await db.schema.dropTable('kill').execute()
|
||||
await db.schema.dropTable('entity').execute()
|
||||
await db.schema.dropTable('weapon').execute()
|
||||
await db.schema.dropTable('map').execute()
|
||||
await db.schema.dropTable('server').execute()
|
||||
}
|
||||
|
||||
+36
-7
@@ -2,18 +2,47 @@ import { ColumnType, Generated } from 'kysely'
|
||||
|
||||
export interface KillTable {
|
||||
id: Generated<number>
|
||||
server: string
|
||||
attacker: number
|
||||
victim: number
|
||||
weapon: string
|
||||
server: number
|
||||
tone_version: string
|
||||
match_id: string
|
||||
game_mode: string
|
||||
map: string
|
||||
unix_time: Generated<Date>
|
||||
game_time: number
|
||||
player_count: number
|
||||
attacker_name: string
|
||||
attacker_id: number
|
||||
attacker_current_weapon: string
|
||||
attacker_current_weapon_mods: string
|
||||
attacker_weapon_1: string
|
||||
attacker_weapon_1_mods: string
|
||||
attacker_weapon_2: string
|
||||
attacker_weapon_2_mods: string
|
||||
attacker_weapon_3: string
|
||||
attacker_weapon_3_mods: string
|
||||
attacker_offhand_weapon_1: string
|
||||
attacker_offhand_weapon_2: string
|
||||
victim_name: string
|
||||
victim_id: number
|
||||
victim_current_weapon: string
|
||||
victim_current_weapon_mods: string
|
||||
victim_weapon_1: string
|
||||
victim_weapon_1_mods: string
|
||||
victim_weapon_2: string
|
||||
victim_weapon_2_mods: string
|
||||
victim_weapon_3: string
|
||||
victim_weapon_3_mods: string
|
||||
victim_offhand_weapon_1: string
|
||||
victim_offhand_weapon_2: string
|
||||
cause_of_death: string
|
||||
distance: number
|
||||
date: ColumnType<Date, string | Date | undefined, never>
|
||||
}
|
||||
|
||||
interface EntityTable {
|
||||
interface PlayerTable {
|
||||
id: number
|
||||
name: string
|
||||
'opt-out': boolean
|
||||
hide_TOS: boolean
|
||||
}
|
||||
interface WeaponTable {
|
||||
id: string
|
||||
@@ -34,7 +63,7 @@ interface ServerTable {
|
||||
}
|
||||
interface Database {
|
||||
kill: KillTable
|
||||
entity: EntityTable
|
||||
player: PlayerTable
|
||||
weapon: WeaponTable
|
||||
maps: MapTable
|
||||
server: ServerTable
|
||||
|
||||
+3
-1
@@ -8,7 +8,9 @@ import server from './server'
|
||||
console.log('dotenv')
|
||||
|
||||
const app = express()
|
||||
const port = 3000
|
||||
const port = 3001
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World!')
|
||||
|
||||
+124
-11
@@ -1,23 +1,136 @@
|
||||
import { Router } from 'express'
|
||||
import { CreateKillRecord } from './db/db'
|
||||
import db, { CreateKillRecord } from './db/db'
|
||||
import { body } from 'express-validator'
|
||||
const router = Router()
|
||||
|
||||
//auth middleware
|
||||
//auth middleware, maybe some timeout ?
|
||||
router.post('/*', (req, res, next) => {
|
||||
next()
|
||||
})
|
||||
router.post(
|
||||
'/severs/:serverId/kills',
|
||||
body('server').isString(),
|
||||
body('attacker').isInt(),
|
||||
body('victim').isInt(),
|
||||
body('weapon').isString(),
|
||||
body('map').isString(),
|
||||
body('distance').isInt(),
|
||||
'/servers/:serverId/kills',
|
||||
body(
|
||||
['attacker_id', 'victim_id'],
|
||||
'attacker and victim must be player UIDs'
|
||||
).isInt(),
|
||||
body(
|
||||
[
|
||||
'tone_version',
|
||||
'match_id',
|
||||
'game_mode',
|
||||
'map',
|
||||
'player_count',
|
||||
'attacker_name',
|
||||
'attacker_current_weapon',
|
||||
'attacker_current_weapon_mods',
|
||||
'attacker_weapon_1',
|
||||
'attacker_weapon_1_mods',
|
||||
'attacker_weapon_2',
|
||||
'attacker_weapon_2_mods',
|
||||
'attacker_weapon_3',
|
||||
'attacker_weapon_3_mods',
|
||||
'attacker_offhand_weapon_1',
|
||||
'attacker_offhand_weapon_2',
|
||||
'victim_name',
|
||||
'victim_current_weapon',
|
||||
'victim_current_weapon_mods',
|
||||
'victim_weapon_1',
|
||||
'victim_weapon_1_mods',
|
||||
'victim_weapon_2',
|
||||
'victim_weapon_2_mods',
|
||||
'victim_weapon_3',
|
||||
'victim_weapon_3_mods',
|
||||
'victim_offhand_weapon_1',
|
||||
'victim_offhand_weapon_2',
|
||||
'cause_of_death'
|
||||
],
|
||||
'values must be composed of a maximum of 50 valid ascii characters'
|
||||
)
|
||||
.isString()
|
||||
.isLength({ max: 50 })
|
||||
.isAscii(),
|
||||
body(
|
||||
['distance', 'game_time'],
|
||||
'distance and game_time must be postitive floats'
|
||||
).isFloat({
|
||||
min: 0
|
||||
}),
|
||||
body(
|
||||
['cause_of_death', 'victim_id'],
|
||||
'cause_of_death and victim_id are mandatory'
|
||||
).notEmpty(),
|
||||
(req, res, next) => {
|
||||
//CreateKillRecord({ server, attacker, victim, weapon, map, distance })
|
||||
res.send(400)
|
||||
const server = 1 // set server ID here
|
||||
const {
|
||||
tone_version,
|
||||
match_id,
|
||||
game_mode,
|
||||
map,
|
||||
game_time,
|
||||
player_count,
|
||||
attacker_name,
|
||||
attacker_id,
|
||||
attacker_current_weapon,
|
||||
attacker_current_weapon_mods,
|
||||
attacker_weapon_1,
|
||||
attacker_weapon_1_mods,
|
||||
attacker_weapon_2,
|
||||
attacker_weapon_2_mods,
|
||||
attacker_weapon_3,
|
||||
attacker_weapon_3_mods,
|
||||
attacker_offhand_weapon_1,
|
||||
attacker_offhand_weapon_2,
|
||||
victim_name,
|
||||
victim_id,
|
||||
victim_current_weapon,
|
||||
victim_current_weapon_mods,
|
||||
victim_weapon_1,
|
||||
victim_weapon_1_mods,
|
||||
victim_weapon_2,
|
||||
victim_weapon_2_mods,
|
||||
victim_weapon_3,
|
||||
victim_weapon_3_mods,
|
||||
victim_offhand_weapon_1,
|
||||
victim_offhand_weapon_2,
|
||||
cause_of_death,
|
||||
distance
|
||||
} = req.body
|
||||
CreateKillRecord({
|
||||
tone_version,
|
||||
server,
|
||||
match_id,
|
||||
game_mode,
|
||||
map,
|
||||
game_time,
|
||||
player_count,
|
||||
attacker_name,
|
||||
attacker_id,
|
||||
attacker_current_weapon,
|
||||
attacker_current_weapon_mods,
|
||||
attacker_weapon_1,
|
||||
attacker_weapon_1_mods,
|
||||
attacker_weapon_2,
|
||||
attacker_weapon_2_mods,
|
||||
attacker_weapon_3,
|
||||
attacker_weapon_3_mods,
|
||||
attacker_offhand_weapon_1,
|
||||
attacker_offhand_weapon_2,
|
||||
victim_name,
|
||||
victim_id,
|
||||
victim_current_weapon,
|
||||
victim_current_weapon_mods,
|
||||
victim_weapon_1,
|
||||
victim_weapon_1_mods,
|
||||
victim_weapon_2,
|
||||
victim_weapon_2_mods,
|
||||
victim_weapon_3,
|
||||
victim_weapon_3_mods,
|
||||
victim_offhand_weapon_1,
|
||||
victim_offhand_weapon_2,
|
||||
cause_of_death,
|
||||
distance
|
||||
})
|
||||
res.send(200)
|
||||
}
|
||||
)
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user