diff --git a/migrations/20231121082135_initHealth.js b/migrations/20231121082135_initHealth.js new file mode 100644 index 0000000..876d373 --- /dev/null +++ b/migrations/20231121082135_initHealth.js @@ -0,0 +1,19 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function(knex) { + return knex.schema + .createTable('initHealth', table => + table.string('user').notNullable().unique() + ); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function(knex) { + return knex.schema + .dropTable('initHealth'); +}; diff --git a/src/lib/db.ts b/src/lib/db.ts index db1f2da..7616aa7 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -86,4 +86,7 @@ export interface Session { accessToken: string, refreshToken: string, expiresAt: number, +} +export interface InitHealth { + user: string, } \ No newline at end of file diff --git a/src/lib/rpg/items.ts b/src/lib/rpg/items.ts index d2efe1a..4fbd118 100644 --- a/src/lib/rpg/items.ts +++ b/src/lib/rpg/items.ts @@ -1,5 +1,6 @@ import { AutocompleteInteraction } from 'discord.js'; import { CustomItem, ItemInventory, db } from '../db'; +import { MAX_HEALTH } from './pvp'; export type DefaultItem = Omit; // uses negative IDs export type Item = DefaultItem | CustomItem; @@ -117,7 +118,7 @@ export const defaultItems: DefaultItem[] = [ description: 'ow', emoji: '🩸', type: 'plain', - maxStack: 1024, + maxStack: MAX_HEALTH, untradable: false }, { diff --git a/src/lib/rpg/pvp.ts b/src/lib/rpg/pvp.ts new file mode 100644 index 0000000..87e59f6 --- /dev/null +++ b/src/lib/rpg/pvp.ts @@ -0,0 +1,15 @@ +import { InitHealth, db } from '../db'; +import { DefaultItems, getDefaultItem, giveItem } from './items'; + +export const MAX_HEALTH = 100; + +export async function initHealth(user: string) { + const isInitialized = await db('initHealth') + .where('user', user) + .first(); + + if (!isInitialized) { + giveItem(user, getDefaultItem(DefaultItems.BLOOD), MAX_HEALTH); + await db('initHealth').insert({ user }); + } +} \ No newline at end of file