Commit 0a806a01 authored by nanahira's avatar nanahira

add post score nonce

parent 76ff2157
Pipeline #39768 failed with stages
in 2 minutes and 4 seconds
This diff is collapsed.
...@@ -42,11 +42,18 @@ import { AccountService } from './account/account.service'; ...@@ -42,11 +42,18 @@ import { AccountService } from './account/account.service';
import { CodeResponseDto } from './dto/CodeResponse.dto'; import { CodeResponseDto } from './dto/CodeResponse.dto';
import { PayExpDto } from './dto/PayExp.dto'; import { PayExpDto } from './dto/PayExp.dto';
import { UserHistoricalRecord } from './entities/mycard/UserHistoricalRecord'; import { UserHistoricalRecord } from './entities/mycard/UserHistoricalRecord';
import { Aragami, CacheKey, CacheTTL } from 'aragami';
const attrOffset = 1010; const attrOffset = 1010;
const raceOffset = 1020; const raceOffset = 1020;
const typeOffset = 1050; const typeOffset = 1050;
@CacheTTL(3600000)
class ProcessedPostNonce {
@CacheKey()
nonce: string;
}
const ygoproConstants = { const ygoproConstants = {
TYPES: { TYPES: {
TYPE_MONSTER: 1, TYPE_MONSTER: 1,
...@@ -140,6 +147,16 @@ export class AppService { ...@@ -140,6 +147,16 @@ export class AppService {
} }
} }
private aragami = new Aragami(
config.redisUrl
? {
redis: {
uri: config.redisUrl,
},
}
: {},
);
private async monthlyJob() { private async monthlyJob() {
this.log.log( this.log.log(
`The scheduleJob run on first day of every month: ${moment().format( `The scheduleJob run on first day of every month: ${moment().format(
...@@ -296,11 +313,11 @@ export class AppService { ...@@ -296,11 +313,11 @@ export class AppService {
const from_time = (inputFrom_time const from_time = (inputFrom_time
? moment(inputFrom_time) ? moment(inputFrom_time)
: moment().startOf('day') : moment().startOf('day')
).toDate() ).toDate();
const to_time = (inputTo_time const to_time = (inputTo_time
? moment(inputTo_time) ? moment(inputTo_time)
: moment().startOf('day').add(1, 'day') : moment().startOf('day').add(1, 'day')
).toDate() ).toDate();
try { try {
const [ const [
{ entertainTotal }, { entertainTotal },
...@@ -549,6 +566,25 @@ export class AppService { ...@@ -549,6 +566,25 @@ export class AppService {
} }
async postScore(body: any): Promise<string> { async postScore(body: any): Promise<string> {
const nonce = body.nonce;
if (!nonce) {
return this.postScore(body);
}
const exist = () => this.aragami.has(ProcessedPostNonce, nonce);
if (await exist()) return;
return this.aragami.lock(`postScoreLock:${nonce}`, async () => {
if (await exist()) return;
const result = await this.postScoreProcess(body);
if (!result) {
const nonceObj = new ProcessedPostNonce();
nonceObj.nonce = nonce;
await this.aragami.set(nonceObj);
}
return result;
});
}
async postScoreProcess(body: any): Promise<string> {
let usernameA: string = body.usernameA; let usernameA: string = body.usernameA;
let usernameB: string = body.usernameB; let usernameB: string = body.usernameB;
const userscoreA = parseInt(body.userscoreA) || 0; const userscoreA = parseInt(body.userscoreA) || 0;
......
...@@ -9,6 +9,7 @@ export interface Config { ...@@ -9,6 +9,7 @@ export interface Config {
analyzerHost: string; analyzerHost: string;
enableSchedule: boolean; enableSchedule: boolean;
accountsUrl: string; accountsUrl: string;
redisUrl: string;
} }
export const athleticCheckConfig = { export const athleticCheckConfig = {
...@@ -34,4 +35,5 @@ export const config: Config = { ...@@ -34,4 +35,5 @@ export const config: Config = {
enableSchedule: !process.env.NO_SCHEDULE, enableSchedule: !process.env.NO_SCHEDULE,
accountsUrl: accountsUrl:
process.env.ACCOUNTS_URL || 'https://sapi.moecube.com:444/accounts', process.env.ACCOUNTS_URL || 'https://sapi.moecube.com:444/accounts',
redisUrl: process.env.REDIS_URL,
}; };
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