Add cron job to add coins to a user every 30 minutes (#219)
# Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Added a timer helper to run timers on a cron job - Added a currency timer function to give a user 5 currency every 30 minutes #204 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: #219 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
parent
5a8ec932b4
commit
836a1d341f
10 changed files with 138 additions and 10 deletions
81
src/helpers/TimerHelper.ts
Normal file
81
src/helpers/TimerHelper.ts
Normal file
|
@ -0,0 +1,81 @@
|
|||
import { CronJob } from "cron";
|
||||
import { v4 } from "uuid";
|
||||
import { Primitive } from "../type/primitive";
|
||||
|
||||
interface Timer {
|
||||
id: string;
|
||||
job: CronJob;
|
||||
context: Map<string, Primitive>;
|
||||
onTick: ((context: Map<string, Primitive>) => void) | ((context: Map<string, Primitive>) => Promise<void>);
|
||||
runOnStart: boolean;
|
||||
}
|
||||
|
||||
export default class TimerHelper {
|
||||
private _timers: Timer[];
|
||||
|
||||
constructor() {
|
||||
this._timers = [];
|
||||
}
|
||||
|
||||
public AddTimer(
|
||||
cronTime: string,
|
||||
timeZone: string,
|
||||
onTick: ((context: Map<string, Primitive>) => void) | ((context: Map<string, Primitive>) => Promise<void>),
|
||||
runOnStart: boolean = false): string {
|
||||
const context = new Map<string, Primitive>();
|
||||
|
||||
const job = new CronJob(
|
||||
cronTime,
|
||||
() => {
|
||||
onTick(context);
|
||||
},
|
||||
null,
|
||||
false,
|
||||
timeZone,
|
||||
);
|
||||
|
||||
const id = v4();
|
||||
|
||||
this._timers.push({
|
||||
id,
|
||||
job,
|
||||
context,
|
||||
onTick,
|
||||
runOnStart,
|
||||
});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public StartAllTimers() {
|
||||
this._timers.forEach(timer => this.StartJob(timer));
|
||||
}
|
||||
|
||||
public StopAllTimers() {
|
||||
this._timers.forEach(timer => timer.job.stop());
|
||||
}
|
||||
|
||||
public StartTimer(id: string) {
|
||||
const timer = this._timers.find(x => x.id == id);
|
||||
|
||||
if (!timer) return;
|
||||
|
||||
this.StartJob(timer);
|
||||
}
|
||||
|
||||
public StopTimer(id: string) {
|
||||
const timer = this._timers.find(x => x.id == id);
|
||||
|
||||
if (!timer) return;
|
||||
|
||||
timer.job.stop();
|
||||
}
|
||||
|
||||
private StartJob(timer: Timer) {
|
||||
timer.job.start();
|
||||
|
||||
if (timer.runOnStart) {
|
||||
timer.onTick(timer.context);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue