make RpgEvent work on shards

This commit is contained in:
HF 2022-09-15 00:58:26 +02:00
parent 29cc314b14
commit 4ca3b9d56c
5 changed files with 69 additions and 26 deletions

View File

@ -14,11 +14,11 @@ import {
clearOldEvent, clearOldEvent,
CANVAS_ID, CANVAS_ID,
} from '../data/redis/Event'; } from '../data/redis/Event';
import { setCoolDownFactor } from './draw';
import Void from './Void'; import Void from './Void';
import { protectCanvasArea } from './Image'; import { protectCanvasArea } from './Image';
import { setPixelByOffset } from './setPixel'; import { setPixelByOffset } from './setPixel';
import { TILE_SIZE, EVENT_USER_NAME } from './constants'; import { TILE_SIZE, EVENT_USER_NAME } from './constants';
import socketEvents from '../socket/socketEvents';
import chatProvider from './ChatProvider'; import chatProvider from './ChatProvider';
import canvases from './canvases'; import canvases from './canvases';
@ -77,15 +77,16 @@ class RpgEvent {
chatTimeout; // number chatTimeout; // number
constructor() { constructor() {
this.eventState = -1;
this.eventCenterC = null;
this.void = null;
this.chatTimeout = 0;
this.runEventLoop = this.runEventLoop.bind(this); this.runEventLoop = this.runEventLoop.bind(this);
} }
setSuccess(success, save = true) { setSuccess(success) {
this.success = success; this.success = success;
setSuccess(success);
RpgEvent.setCoolDownFactorFromSuccess(success);
}
static setCoolDownFactorFromSuccess(success) {
let fac = 1; let fac = 1;
switch (success) { switch (success) {
case 1: case 1:
@ -97,24 +98,31 @@ class RpgEvent {
default: default:
// nothing // nothing
} }
if (save) { socketEvents.setCoolDownFactor(fac);
setSuccess(success);
}
setCoolDownFactor(fac);
} }
async initialize() { async initialize() {
this.eventState = -1;
this.eventCenterC = null;
this.void = null;
this.chatTimeout = 0;
const success = await getSuccess(); const success = await getSuccess();
this.setSuccess(success, false); this.success = success;
let eventTimestamp = await nextEvent(); RpgEvent.setCoolDownFactorFromSuccess(success);
if (!eventTimestamp) { if (socketEvents.amIImportant()) {
eventTimestamp = await RpgEvent.setNextEvent(); let eventTimestamp = await nextEvent();
if (!eventTimestamp) {
eventTimestamp = await RpgEvent.setNextEvent();
await this.calcEventCenter();
const [x, y, w, h] = this.eventArea;
await protectCanvasArea(CANVAS_ID, x, y, w, h, true);
}
this.eventTimestamp = eventTimestamp;
await this.calcEventCenter(); await this.calcEventCenter();
const [x, y, w, h] = this.eventArea; logger.info('initialized Event');
await protectCanvasArea(CANVAS_ID, x, y, w, h, true); } else {
logger.info('Loaded Event cooldown factor, but i am not running it');
} }
this.eventTimestamp = eventTimestamp;
await this.calcEventCenter();
this.runEventLoop(); this.runEventLoop();
} }
@ -181,6 +189,25 @@ class RpgEvent {
} }
async runEventLoop() { async runEventLoop() {
/*
* if we aren't the main shard, we just wait and regularly check,
* re-intilializing if we become it
*/
if (!socketEvents.amIImportant()) {
this.iAmNotImportant = true;
if (this.void) {
this.void.cancel();
this.void = null;
}
setTimeout(this.runEventLoop, 180000);
return;
}
if (this.iAmNotImportant) {
this.iAmNotImportant = false;
this.initialize();
return;
}
const { const {
eventState, eventState,
} = this; } = this;
@ -338,7 +365,7 @@ class RpgEvent {
// 32min after last Event // 32min after last Event
// clear old event area // clear old event area
// reset success state // reset success state
logger.info('Restoring old event area'); logger.info('Restoring old Event area');
await clearOldEvent(); await clearOldEvent();
if (this.success === 1) { if (this.success === 1) {
RpgEvent.broadcastChatMessage( RpgEvent.broadcastChatMessage(

View File

@ -1,5 +1,5 @@
/* /*
* draw pixel on canvas * draw pixel on canvas by user
*/ */
import { import {
@ -8,6 +8,7 @@ import {
import logger, { pixelLogger } from './logger'; import logger, { pixelLogger } from './logger';
import RedisCanvas from '../data/redis/RedisCanvas'; import RedisCanvas from '../data/redis/RedisCanvas';
import allowPlace from '../data/redis/cooldown'; import allowPlace from '../data/redis/cooldown';
import socketEvents from '../socket/socketEvents';
import { import {
setPixelByOffset, setPixelByOffset,
setPixelByCoords, setPixelByCoords,
@ -18,9 +19,9 @@ import canvases from './canvases';
import { THREE_CANVAS_HEIGHT, THREE_TILE_SIZE, TILE_SIZE } from './constants'; import { THREE_CANVAS_HEIGHT, THREE_TILE_SIZE, TILE_SIZE } from './constants';
let coolDownFactor = 1; let coolDownFactor = 1;
export function setCoolDownFactor(fac) { socketEvents.on('setCoolDownFactor', (newFac) => {
coolDownFactor = fac; coolDownFactor = newFac;
} });
/* /*
* IPs who are currently requesting pixels * IPs who are currently requesting pixels

View File

@ -119,9 +119,7 @@ sequelize.sync({ alter: { drop: false } })
logger.info('I am the main shard'); logger.info('I am the main shard');
} }
rankings.initialize(); rankings.initialize();
if (HOURLY_EVENT && !SHARD_NAME) { if (HOURLY_EVENT) {
// TODO make it wok in a cluster
logger.info('Initializing RpgEvent');
rpgEvent.initialize(); rpgEvent.initialize();
} }
}); });

View File

@ -215,6 +215,14 @@ class MessageBroker extends SocketEvents {
super.emit('chunkUpdate', canvasId, [i, j]); super.emit('chunkUpdate', canvasId, [i, j]);
} }
setCoolDownFactor(fac) {
if (this.amIImportant()) {
this.emit('setCoolDownFactor', fac);
} else {
super.emit('setCoolDownFactor', fac);
}
}
broadcastChunkUpdate( broadcastChunkUpdate(
canvasId, canvasId,
chunk, chunk,

View File

@ -103,6 +103,15 @@ class SocketEvents extends EventEmitter {
this.emit('recvChatMessage', user, message, channelId); this.emit('recvChatMessage', user, message, channelId);
} }
/*
* set cooldownfactor
* (used by RpgEvent)
* @param fac factor by which cooldown changes globally
*/
setCoolDownFactor(fac) {
this.emit('setCoolDownFactor', fac);
}
/* /*
* broadcast chat message to all users in channel * broadcast chat message to all users in channel
* @param name chatname * @param name chatname