From 516131c7a2fa793e62f37e4f6975b0c3ca028a24 Mon Sep 17 00:00:00 2001 From: HF Date: Thu, 5 Nov 2020 23:14:23 +0100 Subject: [PATCH] store event success in redis --- src/core/event.js | 14 ++++++++++---- src/data/models/Event.js | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/core/event.js b/src/core/event.js index ef02b66..7e19d43 100644 --- a/src/core/event.js +++ b/src/core/event.js @@ -9,6 +9,8 @@ import logger from './logger'; import { nextEvent, setNextEvent, + setSuccess, + getSuccess, getEventArea, clearOldEvent, CANVAS_ID, @@ -69,6 +71,9 @@ class Event { eventCenter: Array; eventCenterC: Array; eventArea: Array; + // 0 if waiting + // 1 if won + // 2 if lost success: boolean; void: Object; chatTimeout: number; @@ -78,10 +83,6 @@ class Event { this.eventState = -1; this.eventCenterC = null; this.void = null; - // 0 if waiting - // 1 if won - // 2 if lost - this.success = 0; this.chatTimeout = 0; this.runEventLoop = this.runEventLoop.bind(this); if (HOURLY_EVENT) { @@ -90,6 +91,7 @@ class Event { } async initEventLoop() { + this.success = await getSuccess(); let eventTimestamp = await nextEvent(); if (!eventTimestamp) { eventTimestamp = await Event.setNextEvent(); @@ -265,6 +267,7 @@ class Event { 'Threat couldn\'t be contained, abandon area', ); this.success = 2; + setSuccess(2); this.void = null; const [x, y, w, h] = this.eventArea; await protectCanvasArea(CANVAS_ID, x, y, w, h, true); @@ -294,6 +297,7 @@ class Event { 'Threat successfully defeated. Good work!', ); this.success = 1; + setSuccess(1); } this.void.cancel(); this.void = null; @@ -313,6 +317,7 @@ class Event { 'Void seems to leave again.', ); this.success = 0; + setSuccess(0); } this.eventState = 13; } @@ -330,6 +335,7 @@ class Event { 'Celebration time over, get back to work.', ); this.success = 0; + setSuccess(0); } this.eventState = 14; } diff --git a/src/data/models/Event.js b/src/data/models/Event.js index b407bd4..c4d9699 100644 --- a/src/data/models/Event.js +++ b/src/data/models/Event.js @@ -12,12 +12,28 @@ import redis from '../redis'; import logger from '../../core/logger'; import RedisCanvas from './RedisCanvas'; +const EVENT_SUCCESS_KEY = 'evt:succ'; const EVENT_TIMESTAMP_KEY = 'evt:time'; const EVENT_POSITION_KEY = 'evt:pos'; const EVENT_BACKUP_PREFIX = 'evt:bck'; // Note: Events always happen on canvas 0 export const CANVAS_ID = '0'; + +/* + * set success status of event + * 0 = waiting + * 1 = won + * 2 = lost + */ +export function setSuccess(success) { + return redis.setAsync(EVENT_SUCCESS_KEY, success); +} +export async function getSuccess() { + const success = await redis.getAsync(EVENT_SUCCESS_KEY); + return (success) ? parseInt(success) : 0; +} + /* * @return time till next event in seconds */