Commit 6967209c authored by nanahira's avatar nanahira

still not finished

parent 07769f24
...@@ -10,6 +10,7 @@ import {DuelLog} from "./entities/DuelLog"; ...@@ -10,6 +10,7 @@ import {DuelLog} from "./entities/DuelLog";
import {Deck} from "./DeckEncoder"; import {Deck} from "./DeckEncoder";
import {DuelLogPlayer} from "./entities/DuelLogPlayer"; import {DuelLogPlayer} from "./entities/DuelLogPlayer";
import {User} from "./entities/User"; import {User} from "./entities/User";
import {VipKey} from "./entities/VipKey";
interface BasePlayerInfo { interface BasePlayerInfo {
name: string; name: string;
...@@ -304,7 +305,7 @@ export class DataManager { ...@@ -304,7 +305,7 @@ export class DataManager {
async getUser(key: string) { async getUser(key: string) {
const repo = this.db.getRepository(User); const repo = this.db.getRepository(User);
try { try {
const user = await repo.findOne(key); const user = await repo.findOne(key, {relations: ["dialogues"]});
return user; return user;
} catch (e) { } catch (e) {
this.log.warn(`Failed to fetch user: ${e.toString()}`); this.log.warn(`Failed to fetch user: ${e.toString()}`);
...@@ -365,4 +366,50 @@ export class DataManager { ...@@ -365,4 +366,50 @@ export class DataManager {
}); });
} }
async getVipKeys(keyType: number) {
const repo = this.db.getRepository(VipKey);
const queryCondition = {
type: keyType,
isUsed: 0
}
try {
const keys = await repo.find(queryCondition);
return keys.map(k => k.toJSON());
} catch (e) {
this.log.warn(`Failed to fetch keys of keyType ${keyType}: ${e.toString()}`);
return [];
}
}
async useVipKey(userKey: string, vipKeyText: string) {
let user = await this.getOrCreateUser(userKey);
let result = 0;
await this.db.transaction(async (mdb) => {
try {
const vipKey = await mdb.findOne(VipKey, {key: vipKeyText, isUsed: 0});
if(!vipKey) {
return;
}
const keyType = vipKey.type;
const previousDate = user.vipExpireDate;
if(previousDate && moment().isBefore(previousDate)) {
user.vipExpireDate = moment(previousDate).add(keyType, "d").toDate();
} else {
user.vipExpireDate = moment().add(keyType, "d").toDate();
}
user = await mdb.save(user);
vipKey.isUsed = 1;
vipKey.usedBy = user;
await mdb.save(vipKey);
result = previousDate ? 2 : 1;
} catch (e) {
this.log.warn(`Failed to use VIP key for user ${userKey} ${vipKeyText}: ${e.toString()}`);
result = 0;
return;
}
});
return result;
}
} }
import {Column, Entity, Index, OneToMany, PrimaryColumn} from "typeorm"; import {Column, Entity, Index, OneToMany, PrimaryColumn} from "typeorm";
import {UserDialog} from "./UserDialog"; import {UserDialog} from "./UserDialog";
import {VipKey} from "./VipKey";
@Entity() @Entity()
export class User { export class User {
...@@ -11,7 +12,7 @@ export class User { ...@@ -11,7 +12,7 @@ export class User {
@Index() @Index()
@Column("datetime", {nullable: true}) @Column("datetime", {nullable: true})
vipExpireDate: string; vipExpireDate: Date;
@Column("text", {nullable: true}) @Column("text", {nullable: true})
victory: string; victory: string;
...@@ -21,4 +22,7 @@ export class User { ...@@ -21,4 +22,7 @@ export class User {
@OneToMany(() => UserDialog, dialog => dialog.user) @OneToMany(() => UserDialog, dialog => dialog.user)
dialogues: UserDialog[]; dialogues: UserDialog[];
@OneToMany(() => VipKey, vipKey => vipKey.usedBy)
usedKeys: VipKey[];
} }
import {Column, Entity, Index, PrimaryGeneratedColumn} from "typeorm"; import {Column, Entity, Index, ManyToOne, PrimaryGeneratedColumn} from "typeorm";
import {User} from "./User";
@Entity() @Entity()
export class VipKey { export class VipKey {
...@@ -12,6 +13,12 @@ export class VipKey { ...@@ -12,6 +13,12 @@ export class VipKey {
@Column("int", {unsigned: true}) @Column("int", {unsigned: true})
type: number; type: number;
@Column("tinyint", {unsigned: true})
isUsed: number;
@ManyToOne(() => User, user => user.usedKeys)
usedBy: User;
toJSON() { toJSON() {
return {key: this.key, type: this.type}; return {key: this.key, type: this.type};
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment