store event success in redis

This commit is contained in:
HF 2020-11-05 23:14:23 +01:00
parent 648c767849
commit 516131c7a2
2 changed files with 26 additions and 4 deletions

View File

@ -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;
}

View File

@ -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
*/