Add cron job to add coins to a user every 30 minutes #219

Merged
Vylpes merged 7 commits from feature/204-coins-cron into develop 2024-05-17 20:57:01 +01:00
7 changed files with 51 additions and 20 deletions
Showing only changes of commit c1ca37283c - Show all commits

View file

@ -59,11 +59,3 @@ if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO
} }
client.start(); client.start();
const timerHelper = new TimerHelper();
const id = timerHelper.AddTimer('* * * * * *', 'Europe/London', GiveCurrency);
console.log(`Timer Created: ${id}`);
timerHelper.StartAllTimers();

View file

@ -14,6 +14,8 @@ import Webhooks from "../webhooks";
import CardMetadataFunction from "../Functions/CardMetadataFunction"; import CardMetadataFunction from "../Functions/CardMetadataFunction";
import { SeriesMetadata } from "../contracts/SeriesMetadata"; import { SeriesMetadata } from "../contracts/SeriesMetadata";
import AppLogger from "./appLogger"; import AppLogger from "./appLogger";
import TimerHelper from "../helpers/TimerHelper";
import GiveCurrency from "../timers/GiveCurrency";
export class CoreClient extends Client { export class CoreClient extends Client {
private static _commandItems: ICommandItem[]; private static _commandItems: ICommandItem[];
@ -23,6 +25,7 @@ export class CoreClient extends Client {
private _events: Events; private _events: Events;
private _util: Util; private _util: Util;
private _webhooks: Webhooks; private _webhooks: Webhooks;
private _timerHelper: TimerHelper;
public static ClaimId: string; public static ClaimId: string;
public static Environment: Environment; public static Environment: Environment;
@ -59,6 +62,7 @@ export class CoreClient extends Client {
this._events = new Events(); this._events = new Events();
this._util = new Util(); this._util = new Util();
this._webhooks = new Webhooks(); this._webhooks = new Webhooks();
this._timerHelper = new TimerHelper();
AppLogger.LogInfo("Client", `Environment: ${CoreClient.Environment}`); AppLogger.LogInfo("Client", `Environment: ${CoreClient.Environment}`);
@ -72,7 +76,12 @@ export class CoreClient extends Client {
} }
await AppDataSource.initialize() await AppDataSource.initialize()
.then(() => AppLogger.LogInfo("Client", "App Data Source Initialised")) .then(() => {
AppLogger.LogInfo("Client", "App Data Source Initialised");
const timerId = this._timerHelper.AddTimer("*/30 * * * * *", "Europe/London", GiveCurrency, false);
this._timerHelper.StartTimer(timerId);
})
.catch(err => { .catch(err => {
AppLogger.LogError("Client", "App Data Source Initialisation Failed"); AppLogger.LogError("Client", "App Data Source Initialisation Failed");
AppLogger.LogError("Client", err); AppLogger.LogError("Client", err);

View file

@ -27,6 +27,12 @@ export default class AppBaseEntity {
await repository.save(entity); await repository.save(entity);
} }
public static async SaveAll<T extends AppBaseEntity>(target: EntityTarget<T>, entities: DeepPartial<T>[]): Promise<void> {
const repository = AppDataSource.getRepository<T>(target);
await repository.save(entities);
}
public static async Remove<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> { public static async Remove<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
const repository = AppDataSource.getRepository<T>(target); const repository = AppDataSource.getRepository<T>(target);

View file

@ -13,8 +13,8 @@ export default class User extends AppBaseEntity {
@Column() @Column()
Currency: number; Currency: number;
public UpdateCurrency(currency: number) { public AddCurrency(amount: number) {
this.Currency = currency; this.Currency += amount;
} }
public RemoveCurrency(amount: number): boolean { public RemoveCurrency(amount: number): boolean {

View file

@ -5,6 +5,8 @@ interface Timer {
id: string; id: string;
job: CronJob; job: CronJob;
context: Map<string, any>; context: Map<string, any>;
onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>);
runOnStart: boolean;
} }
export default class TimerHelper { export default class TimerHelper {
@ -17,7 +19,8 @@ export default class TimerHelper {
public AddTimer( public AddTimer(
cronTime: string, cronTime: string,
timeZone: string, timeZone: string,
onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>)): string { onTick: ((context: Map<string, any>) => void) | ((context: Map<string, any>) => Promise<void>),
runOnStart: boolean = false): string {
const context = new Map<string, any>(); const context = new Map<string, any>();
const job = new CronJob( const job = new CronJob(
@ -36,13 +39,15 @@ export default class TimerHelper {
id, id,
job, job,
context, context,
onTick,
runOnStart,
}); });
return id; return id;
} }
public StartAllTimers() { public StartAllTimers() {
this._timers.forEach(timer => timer.job.start()); this._timers.forEach(timer => this.StartJob(timer));
} }
public StopAllTimers() { public StopAllTimers() {
@ -54,7 +59,7 @@ export default class TimerHelper {
if (!timer) return; if (!timer) return;
timer.job.start(); this.StartJob(timer);
} }
public StopTimer(id: string) { public StopTimer(id: string) {
@ -64,4 +69,12 @@ export default class TimerHelper {
timer.job.stop(); timer.job.stop();
} }
private StartJob(timer: Timer) {
timer.job.start();
if (timer.runOnStart) {
timer.onTick(timer.context);
}
}
} }

View file

@ -52,4 +52,8 @@ export default class Registry {
CoreClient.RegisterButtonEvent("series", new SeriesEvent()); CoreClient.RegisterButtonEvent("series", new SeriesEvent());
CoreClient.RegisterButtonEvent("trade", new TradeButtonEvent()); CoreClient.RegisterButtonEvent("trade", new TradeButtonEvent());
} }
public static RegisterTimers() {
Vylpes marked this conversation as resolved Outdated

Need to do something with this function

Need to do something with this function
}
} }

View file

@ -1,9 +1,16 @@
export default function GiveCurrency(context: Map<string, any>) { import AppLogger from "../client/appLogger";
let calledTimes = context.get('times') as number; import User from "../database/entities/app/User";
if (!calledTimes) calledTimes = 1; export default async function GiveCurrency(context: Map<string, any>) {
AppLogger.LogInfo("Timers/GiveCurrency", "Giving currency to every known user");
console.log(`This has been called ${calledTimes} times!`); const users = await User.FetchAll(User);
context.set('times', calledTimes + 1); for (const user of users) {
user.AddCurrency(5);
}
User.SaveAll(User, users);
AppLogger.LogInfo("Timers/GiveCurrency", `Successfully gave +5 currency to ${users.length} users`);
} }