Commit 6967209c authored by nanahira's avatar nanahira

still not finished

parent 07769f24
......@@ -10,6 +10,7 @@ import {DuelLog} from "./entities/DuelLog";
import {Deck} from "./DeckEncoder";
import {DuelLogPlayer} from "./entities/DuelLogPlayer";
import {User} from "./entities/User";
import {VipKey} from "./entities/VipKey";
interface BasePlayerInfo {
name: string;
......@@ -304,7 +305,7 @@ export class DataManager {
async getUser(key: string) {
const repo = this.db.getRepository(User);
try {
const user = await repo.findOne(key);
const user = await repo.findOne(key, {relations: ["dialogues"]});
return user;
} catch (e) {
this.log.warn(`Failed to fetch user: ${e.toString()}`);
......@@ -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 {UserDialog} from "./UserDialog";
import {VipKey} from "./VipKey";
@Entity()
export class User {
......@@ -11,7 +12,7 @@ export class User {
@Index()
@Column("datetime", {nullable: true})
vipExpireDate: string;
vipExpireDate: Date;
@Column("text", {nullable: true})
victory: string;
......@@ -21,4 +22,7 @@ export class User {
@OneToMany(() => UserDialog, dialog => dialog.user)
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()
export class VipKey {
......@@ -12,6 +13,12 @@ export class VipKey {
@Column("int", {unsigned: true})
type: number;
@Column("tinyint", {unsigned: true})
isUsed: number;
@ManyToOne(() => User, user => user.usedKeys)
usedBy: User;
toJSON() {
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