Compare commits
7 commits
2dfec9d105
...
5406c0ce68
Author | SHA1 | Date | |
---|---|---|---|
5406c0ce68 | |||
005a366883 | |||
aebc799966 | |||
7f64065589 | |||
9c408e07b0 | |||
8ccd7c33d4 | |||
ad505b3ea2 |
11 changed files with 104 additions and 30 deletions
|
@ -0,0 +1,8 @@
|
|||
CREATE TABLE `claim` (
|
||||
`Id` varchar(255) NOT NULL,
|
||||
`WhenCreated` datetime NOT NULL,
|
||||
`WhenUpdated` datetime NOT NULL,
|
||||
`ClaimId` varchar(255) NOT NULL,
|
||||
`InventoryId` varchar(255) NOT NULL,
|
||||
PRIMARY KEY (`Id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
@ -0,0 +1,14 @@
|
|||
INSERT INTO claim (
|
||||
Id,
|
||||
WhenCreated,
|
||||
WhenUpdated,
|
||||
ClaimId,
|
||||
InventoryId
|
||||
)
|
||||
SELECT
|
||||
UUID(),
|
||||
NOW(),
|
||||
NOW(),
|
||||
ClaimId,
|
||||
Id
|
||||
FROM inventory;
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE inventory
|
||||
DROP ClaimId;
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "card-drop",
|
||||
"version": "0.1.4",
|
||||
"version": "0.1.5",
|
||||
"main": "./dist/bot.js",
|
||||
"typings": "./dist",
|
||||
"scripts": {
|
||||
|
|
|
@ -2,6 +2,7 @@ import { ButtonInteraction } from "discord.js";
|
|||
import { ButtonEvent } from "../type/buttonEvent";
|
||||
import Inventory from "../database/entities/app/Inventory";
|
||||
import { CoreClient } from "../client/client";
|
||||
import { default as eClaim } from "../database/entities/app/Claim";
|
||||
|
||||
export default class Claim extends ButtonEvent {
|
||||
public override async execute(interaction: ButtonInteraction) {
|
||||
|
@ -9,30 +10,36 @@ export default class Claim extends ButtonEvent {
|
|||
|
||||
const cardNumber = interaction.customId.split(' ')[1];
|
||||
const claimId = interaction.customId.split(' ')[2];
|
||||
const droppedBy = interaction.customId.split(' ')[3];
|
||||
const userId = interaction.user.id;
|
||||
|
||||
const claimed = await Inventory.FetchOneByClaimId(claimId);
|
||||
const claimed = await eClaim.FetchOneByClaimId(claimId);
|
||||
|
||||
if (claimed) {
|
||||
await interaction.reply('This card has already been claimed');
|
||||
return;
|
||||
}
|
||||
|
||||
if (claimId != CoreClient.ClaimId) {
|
||||
await interaction.reply('This card has expired');
|
||||
if (claimId == CoreClient.ClaimId && userId != droppedBy) {
|
||||
await interaction.reply('The latest dropped card can only be claimed by the user who dropped it');
|
||||
return;
|
||||
}
|
||||
|
||||
let inventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
|
||||
|
||||
if (!inventory) {
|
||||
inventory = new Inventory(userId, cardNumber, 1, claimId);
|
||||
inventory = new Inventory(userId, cardNumber, 1);
|
||||
} else {
|
||||
inventory.SetQuantity(inventory.Quantity + 1);
|
||||
}
|
||||
|
||||
await inventory.Save(Inventory, inventory);
|
||||
|
||||
const claim = new eClaim(claimId);
|
||||
claim.SetInventory(inventory);
|
||||
|
||||
await claim.Save(eClaim, claim);
|
||||
|
||||
await interaction.reply('Card claimed');
|
||||
}
|
||||
}
|
|
@ -45,7 +45,7 @@ export default class Reroll extends ButtonEvent {
|
|||
|
||||
row.addComponents(
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`claim ${randomCard.CardNumber} ${claimId}`)
|
||||
.setCustomId(`claim ${randomCard.CardNumber} ${claimId} ${interaction.user.id}`)
|
||||
.setLabel("Claim")
|
||||
.setStyle(ButtonStyle.Primary),
|
||||
new ButtonBuilder()
|
||||
|
|
|
@ -51,7 +51,7 @@ export default class Drop extends Command {
|
|||
|
||||
row.addComponents(
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`claim ${randomCard.CardNumber} ${claimId}`)
|
||||
.setCustomId(`claim ${randomCard.CardNumber} ${claimId} ${interaction.user.id}`)
|
||||
.setLabel("Claim")
|
||||
.setStyle(ButtonStyle.Primary),
|
||||
new ButtonBuilder()
|
||||
|
@ -76,7 +76,6 @@ export default class Drop extends Command {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
CoreClient.ClaimId = claimId;
|
||||
}
|
||||
}
|
31
src/database/entities/app/Claim.ts
Normal file
31
src/database/entities/app/Claim.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
import AppBaseEntity from "../../../contracts/AppBaseEntity";
|
||||
import Inventory from "./Inventory";
|
||||
import AppDataSource from "../../dataSources/appDataSource";
|
||||
|
||||
@Entity()
|
||||
export default class Claim extends AppBaseEntity {
|
||||
constructor(claimId: string) {
|
||||
super();
|
||||
|
||||
this.ClaimId = claimId;
|
||||
}
|
||||
|
||||
@Column()
|
||||
ClaimId: string;
|
||||
|
||||
@ManyToOne(() => Inventory, x => x.Claims)
|
||||
Inventory: Inventory;
|
||||
|
||||
public SetInventory(inventory: Inventory) {
|
||||
this.Inventory = inventory;
|
||||
}
|
||||
|
||||
public static async FetchOneByClaimId(claimId: string): Promise<Claim | null> {
|
||||
const repository = AppDataSource.getRepository(Claim);
|
||||
|
||||
const single = await repository.findOne({ where: { ClaimId: claimId }});
|
||||
|
||||
return single;
|
||||
}
|
||||
}
|
|
@ -1,16 +1,16 @@
|
|||
import { Column, Entity } from "typeorm";
|
||||
import { Column, Entity, OneToMany } from "typeorm";
|
||||
import AppBaseEntity from "../../../contracts/AppBaseEntity";
|
||||
import AppDataSource from "../../dataSources/appDataSource";
|
||||
import Claim from "./Claim";
|
||||
|
||||
@Entity()
|
||||
export default class Inventory extends AppBaseEntity {
|
||||
constructor(userId: string, cardNumber: string, quantity: number, claimId: string) {
|
||||
constructor(userId: string, cardNumber: string, quantity: number) {
|
||||
super();
|
||||
|
||||
this.UserId = userId;
|
||||
this.CardNumber = cardNumber;
|
||||
this.Quantity = quantity;
|
||||
this.ClaimId = claimId;
|
||||
}
|
||||
|
||||
@Column()
|
||||
|
@ -22,13 +22,17 @@ export default class Inventory extends AppBaseEntity {
|
|||
@Column()
|
||||
Quantity: number;
|
||||
|
||||
@Column()
|
||||
ClaimId: string;
|
||||
@OneToMany(() => Claim, x => x.Inventory)
|
||||
Claims: Claim[];
|
||||
|
||||
public SetQuantity(quantity: number) {
|
||||
this.Quantity = quantity;
|
||||
}
|
||||
|
||||
public AddClaim(claim: Claim) {
|
||||
this.Claims.push(claim);
|
||||
}
|
||||
|
||||
public static async FetchOneByCardNumberAndUserId(userId: string, cardNumber: string): Promise<Inventory | null> {
|
||||
const repository = AppDataSource.getRepository(Inventory);
|
||||
|
||||
|
@ -36,12 +40,4 @@ export default class Inventory extends AppBaseEntity {
|
|||
|
||||
return single;
|
||||
}
|
||||
|
||||
public static async FetchOneByClaimId(claimId: string): Promise<Inventory | null> {
|
||||
const repository = AppDataSource.getRepository(Inventory);
|
||||
|
||||
const single = await repository.findOne({ where: { ClaimId: claimId }});
|
||||
|
||||
return single;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
import MigrationHelper from "../../../../helpers/MigrationHelper"
|
||||
|
||||
export class CreateClaim1694609771821 implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
MigrationHelper.Up('1694609771821-CreateClaim', '0.1.5', [
|
||||
'01-CreateClaim',
|
||||
'02-MoveToClaim',
|
||||
'03-AlterInventory',
|
||||
], queryRunner);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
}
|
||||
|
||||
}
|
18
yarn.lock
18
yarn.lock
|
@ -888,19 +888,19 @@
|
|||
"@types/node" "*"
|
||||
|
||||
"@types/node@*", "@types/node@^20.0.0":
|
||||
version "20.5.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.5.1.tgz#178d58ee7e4834152b0e8b4d30cbfab578b9bb30"
|
||||
integrity sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==
|
||||
version "20.7.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.7.1.tgz#06d732ead0bd5ad978ef0ea9cbdeb24dc8717514"
|
||||
integrity sha512-LT+OIXpp2kj4E2S/p91BMe+VgGX2+lfO+XTpfXhh+bCk2LkQtHZSub8ewFBMGP5ClysPjTDFa4sMI8Q3n4T0wg==
|
||||
|
||||
"@types/normalize-package-data@^2.4.1":
|
||||
version "2.4.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
|
||||
integrity sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==
|
||||
version "2.4.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.2.tgz#9b0e3e8533fe5024ad32d6637eb9589988b6fdca"
|
||||
integrity sha512-lqa4UEhhv/2sjjIQgjX8B+RBjj47eo0mzGasklVJ78UKGQY1r0VpB9XHDaZZO9qzEFDdy4MrXLuEaSmPrPSe/A==
|
||||
|
||||
"@types/responselike@^1.0.0":
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29"
|
||||
integrity sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.1.tgz#1dd57e54509b3b95c7958e52709567077019d65d"
|
||||
integrity sha512-TiGnitEDxj2X0j+98Eqk5lv/Cij8oHd32bU4D/Yw6AOq7vvTk0gSD2GPj0G/HkvhMoVsdlhYF4yqqlyPBTM6Sg==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
|
|
Loading…
Reference in a new issue