RanvierMUD / progressive-respawn

2 stars 5 forks source link

Many areas share one lastRespawnTick #2

Open galaxian85 opened 5 years ago

galaxian85 commented 5 years ago

I have 3 areas (limbo, mapped and myarea) in my ranvier starter kit. The manifest settings are:

title: "Limbo"
behaviors:
  progressive-respawn:
    interval: 10
title: "Map test"
behaviors:
  progressive-respawn:
    interval: 15
title: "My Area"
behaviors:
  progressive-respawn:
    interval: 20

And I noticed only limbo will trigger the event correctly. I insert some code try to find out why.

      return function (config) {
        // setup respawnTick to only happen every [interval] seconds
        const respawnInterval = config.interval || 30;
        const sinceLastTick = Date.now() - lastRespawnTick;
        console.log(this.name + " " + sinceLastTick);
        if (sinceLastTick >= respawnInterval * 1000) {
          console.log(this.name + " is respawning"  + "\n");
          lastRespawnTick = Date.now();
          for (const [id, room] of this.rooms) {
            room.emit('respawnTick', state);
          }
        }
      };

And here is the console log.

limbo 10044
mapped 10044
myarea 10044
limbo is respawning

limbo 109
mapped 110
myarea 110

As you can see, limbo trigger the event and update the lastRespawnTick variable same time. Means other areas will never respawn and makes this bundle unusable.

So I change the logic. No count in millisecond anymore, it should be counted in ticks = Config.get(entityTickFrequency) Since entityTickFrequency is hardcoded in main process, so I just use magic number 100 & 10 here.

    updateTick: state => {
      let serverStartTime = Math.trunc(Date.now() / 100);
      return function (config) {
        const respawnInterval = config.interval || 30;
        const upTime = Math.trunc(Date.now() / 100 - serverStartTime);
        if (upTime % (respawnInterval * 10) == 0) {
          console.log(this.name + " is respawning" + "\n");
          for (const [id, room] of this.rooms) {
            room.emit('respawnTick', state);
          }
        }
      };
    },

First time using github and English is not my native language(JavaScript neither). Hope this post will not offend anybody.

At last, thanks you guys making this awesome software, have a nice day!

galaxian85 commented 5 years ago

I misunderstanding the meaning of Config.get('entityTickFrequency', 100) in the main process.

The 100 there is default setting, not hard-coded. so here is the new code :)

const { Config } = require('ranvier');
...
..
.
    updateTick: state => {
      const tickFrequency = Config.get('entityTickFrequency', 100);
      const serverStartTime = Math.trunc(Date.now() / tickFrequency);
      return function (config) {
        const respawnInterval = config.interval || 30;
        const upTime = Math.trunc(Date.now() / tickFrequency - serverStartTime);
        if (upTime % (respawnInterval * (1000 / tickFrequency)) == 0) {
          for (const [id, room] of this.rooms) {
            room.emit('respawnTick', state);
          }
        }
      };
    },
clagiordano commented 4 years ago

@galaxian85 thanks for your fix, I had the same issue, the area with lower respan interval resets counter for other areas then just the area with lowest respawn interval was respawned, with this fix every area respawn correctly.

Thanks.

I hope @shawncplus will consider your fix and approve its merge.