saviola777 / hhm-plugins

Plugins for the Haxball Headless Manager (HHM).
MIT License
3 stars 5 forks source link

Can not create cron jobs "on the fly" #3

Closed morko closed 5 years ago

morko commented 5 years ago

Was trying to implement a MOTD plugin with help of the sav/cron plugin. Noticed that setting a new cron job with e.g. room.onCron3Seconds does not work inside a command handler.

Happened in the testing environment of hhm-plugins.

Here is the affected plugin (stripped):

let room = HBInit();
room.pluginSpec = {
  name: `bug/example`,
  author: `salamini`,
  version: `1.0.0`,
  config: {
    periodicalMessage: `Periodical Message.`,
    messageInterval: 5
  },
  dependencies: [`sav/cron`]
};

if (room.pluginSpec.config.messageInterval > 0) {
  displayMessageOnceIn(
    room.pluginSpec.config.messageInterval,
    room.pluginSpec.config.periodicalMessage
  );
}
function displayMessageOnceIn(interval, message) {
  room[`onCron${interval}Seconds`] = () => room.sendChat(message);
}

room.onConfigSet = ({paramName, newValue, oldValue}) => {
  if (paramName === `messageInterval`) {
    const periodicalMessage = room.pluginSpec.config.periodicalMessage;
    delete room[`onCron${oldValue}Seconds`];
    displayMessageOnceIn(newValue, periodicalMessage);
  }
}

room.onCommand1_motd = (player, [interval]) => {
  room.setConfig('messageInterval', interval);
}

Expected to be able to change the cron job with !motd [interval] in the room but it did not work.