Skip to content

Commit

Permalink
feat(be): add repeat quest every day
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Oct 20, 2023
1 parent b3edfea commit 57bd31a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/quest/quest.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ import { SubscribersModule } from '../subscribers/subscribers.module';
],
providers: [QuestService, QuestGraphql],
controllers: [QuestController],
exports: [],
exports: [QuestGraphql],
})
export class QuestModule {}
2 changes: 1 addition & 1 deletion src/quest/quest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class QuestService {
unlock.push(after < now);
}

if (condition.after) {
if (condition.before) {
const before = new Date(condition.before);
unlock.push(now < before);
}
Expand Down
40 changes: 40 additions & 0 deletions src/repeat-quests/repeat-quests.graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ export class RepeatQuestsGraphql {
private graphqlSvc: GraphqlService
) {}

async getRepeatableQuests() {
const result = await this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
'',
`query repeatable_quests {
quests(where: {type: {_in: ["Daily"]}, status: {_eq: "Published"}}) {
id
condition
status
}
}
`,
'repeatable_quests',
{}
);

return result.data.quests;
}

async queryRepeatQuest(variables: any) {
const result = await this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
Expand All @@ -27,4 +46,25 @@ export class RepeatQuestsGraphql {

return result.data.repeat_quests[0];
}

insertRepeatQuests(variables: any) {
const headers = {
'x-hasura-admin-secret': this.configSvc.get<string>(
'graphql.adminSecret'
),
};

return this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
'',
`mutation insert_repeat_quests($objects: [repeat_quests_insert_input!] = {quest_id: 10}) {
insert_repeat_quests(objects: $objects) {
affected_rows
}
}`,
'insert_repeat_quests',
variables,
headers
);
}
}
3 changes: 2 additions & 1 deletion src/repeat-quests/repeat-quests.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';

import { GraphqlModule } from '../graphql/graphql.module';
import { RepeatQuestsGraphql } from './repeat-quests.graphql';
import { RepeatQuestTaskService } from './repeat-quests.task';

@Module({
imports: [GraphqlModule],
providers: [RepeatQuestsGraphql],
providers: [RepeatQuestsGraphql, RepeatQuestTaskService],
exports: [RepeatQuestsGraphql],
})
export class RepeatQuestModule {}
54 changes: 54 additions & 0 deletions src/repeat-quests/repeat-quests.task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { RepeatQuestsGraphql } from './repeat-quests.graphql';

@Injectable()
export class RepeatQuestTaskService {
private readonly logger = new Logger(RepeatQuestTaskService.name);
constructor(private repeatQuestsGraphql: RepeatQuestsGraphql) {}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async createRepeatQuests() {
// get all repeatable quest
const quests = await this.repeatQuestsGraphql.getRepeatableQuests();
const insertObjects = [];
quests.forEach((quest) => {
if (this.inDurationCondition(quest.condition)) {
// create repeat quest
insertObjects.push({
quest_id: quest.id,
});
}

if (quest.condition.level) {
insertObjects.push({
quest_id: quest.id,
});
}
});

const result = await this.repeatQuestsGraphql.insertRepeatQuests({
objects: insertObjects,
});

this.logger.debug('Insert repeat quests result:');
this.logger.debug(JSON.stringify(result));
}

inDurationCondition(condition: any) {
const inDuration = [];

const now = new Date();
if (condition.after) {
const after = new Date(condition.after);
inDuration.push(after < now);
}

if (condition.before) {
const before = new Date(condition.before);
inDuration.push(now < before);
}

return inDuration.includes(false) || inDuration.length === 0 ? false : true;
}
}

0 comments on commit 57bd31a

Please sign in to comment.