diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-10-27 19:26:52 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-10-27 19:26:52 -0400 |
commit | 38f488528ba96e8445f29c9fe24131930f03a697 (patch) | |
tree | fd87eb69a0f785739e51077aa3b94921fa9be8ee /src/lib/models/Level.ts | |
parent | 43dadce8b744a43b86cc3febba1046d714cdafcb (diff) | |
download | tanzanite-38f488528ba96e8445f29c9fe24131930f03a697.tar.gz tanzanite-38f488528ba96e8445f29c9fe24131930f03a697.tar.bz2 tanzanite-38f488528ba96e8445f29c9fe24131930f03a697.zip |
use declaration merging for models and clean them up
Diffstat (limited to 'src/lib/models/Level.ts')
-rw-r--r-- | src/lib/models/Level.ts | 70 |
1 files changed, 21 insertions, 49 deletions
diff --git a/src/lib/models/Level.ts b/src/lib/models/Level.ts index 0960d4f..7f418fd 100644 --- a/src/lib/models/Level.ts +++ b/src/lib/models/Level.ts @@ -1,7 +1,6 @@ import { Snowflake } from 'discord.js'; import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { NEVER_USED } from './__helpers'; export interface LevelModel { user: Snowflake; @@ -15,40 +14,20 @@ export interface LevelModelCreationAttributes { xp?: number; } -export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { - /** - * The user's id. - */ - public get user(): Snowflake { - throw new Error(NEVER_USED); - } - public set user(_: Snowflake) { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface Level { + /** The user's id. */ + user: Snowflake; - /** - * The guild where the user is gaining xp. - */ - public get guild(): Snowflake { - throw new Error(NEVER_USED); - } - public set guild(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The guild where the user is gaining xp. */ + guild: Snowflake; - /** - * The user's xp. - */ - public get xp(): number { - throw new Error(NEVER_USED); - } - public set xp(_: number) { - throw new Error(NEVER_USED); - } + /**The user's xp.*/ + xp: number; +} - /** - * The user's level. - */ +export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> implements LevelModel { + /** The user's level. */ public get level(): number { return Level.convertXpToLevel(this.xp); } @@ -56,24 +35,15 @@ export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { public static initModel(sequelize: Sequelize): void { Level.init( { - user: { - type: DataTypes.STRING, - allowNull: false - }, - guild: { - type: DataTypes.STRING, - allowNull: false - }, - xp: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: 0 - } + user: { type: DataTypes.STRING, allowNull: false }, + guild: { type: DataTypes.STRING, allowNull: false }, + xp: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 } }, - { sequelize: sequelize } + { sequelize } ); } - static convertXpToLevel(xp: number): number { + + public static convertXpToLevel(xp: number): number { let i = 1; let lvl: number; // eslint-disable-next-line no-constant-condition @@ -88,14 +58,16 @@ export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { } return lvl - 1; // I have to do this don't question it ok } - static convertLevelToXp(level: number): number { + + public static convertLevelToXp(level: number): number { let xp = 0; for (let i = 0; i < level; i++) { xp += 100 * i + 75; } return xp; } - static genRandomizedXp(): number { + + public static genRandomizedXp(): number { return Math.floor(Math.random() * (40 - 15 + 1)) + 15; } } |