Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(util): way better EventQueue
- Loading branch information
Showing
5 changed files
with
134 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/net/programmer/igoodie/twitchspawn/util/EventQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package net.programmer.igoodie.twitchspawn.util; | ||
|
||
import net.programmer.igoodie.twitchspawn.TwitchSpawn; | ||
import net.programmer.igoodie.twitchspawn.tslanguage.EventArguments; | ||
import net.programmer.igoodie.twitchspawn.tslanguage.event.TSLEvent; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
public class EventQueue { | ||
|
||
private Timer timer; | ||
private volatile boolean timerTicking; | ||
private volatile Queue<TimerTask> tasks; | ||
private long cooldown; // milliseconds | ||
private long frozenUntil; // ms timestamp | ||
private int succeededEvents; | ||
private int discardedEvents; | ||
|
||
public EventQueue(long cooldownDuration) { | ||
this.timer = new Timer(); | ||
this.tasks = new LinkedList<>(); | ||
this.cooldown = cooldownDuration; | ||
this.frozenUntil = -1; | ||
} | ||
|
||
public void queue(Runnable task) { | ||
tasks.add(new TimerTask() { | ||
@Override | ||
public void run() { | ||
long now = System.currentTimeMillis(); | ||
|
||
task.run(); | ||
|
||
frozenUntil = now + cooldown; | ||
} | ||
}); | ||
updateTimer(); | ||
} | ||
|
||
public void queue(TSLEvent eventNode, EventArguments args, CooldownBucket cooldownBucket) { | ||
tasks.add(new TimerTask() { | ||
@Override | ||
public void run() { | ||
TwitchSpawn.SERVER.execute(() -> { | ||
long now = System.currentTimeMillis(); | ||
boolean performed = eventNode.process(args); | ||
|
||
if (!performed) { | ||
discardedEvents++; | ||
return; | ||
} | ||
|
||
if (cooldownBucket != null) | ||
cooldownBucket.consume(args.actorNickname); | ||
|
||
frozenUntil = now + cooldown; | ||
succeededEvents++; | ||
}); | ||
} | ||
}); | ||
updateTimer(); | ||
} | ||
|
||
private synchronized void updateTimer() { | ||
if (!timerTicking) { | ||
timer.scheduleAtFixedRate(new TimerTask() { | ||
@Override | ||
public void run() { | ||
if (hasUnhandledEvent()) { | ||
proceed(); | ||
|
||
} else { | ||
this.cancel(); // XXX: Is reliable? | ||
timerTicking = false; | ||
} | ||
} | ||
}, 10, 10); // per 10ms | ||
timerTicking = true; | ||
} | ||
} | ||
|
||
public synchronized int succeededEventCount() { | ||
return succeededEvents; | ||
} | ||
|
||
public synchronized int discardedEventCount() { | ||
return discardedEvents; | ||
} | ||
|
||
public synchronized int unhandledEventCount() { | ||
return tasks.size(); | ||
} | ||
|
||
public synchronized boolean hasUnhandledEvent() { | ||
return !tasks.isEmpty(); | ||
} | ||
|
||
public boolean proceed() { | ||
long now = System.currentTimeMillis(); | ||
if (now < frozenUntil) return false; | ||
if (tasks.isEmpty()) return false; | ||
|
||
TimerTask task = tasks.remove(); | ||
task.run(); | ||
return true; | ||
} | ||
|
||
public void reset() { | ||
timer.cancel(); | ||
timer.purge(); | ||
timerTicking = false; | ||
tasks.clear(); | ||
this.frozenUntil = -1; | ||
succeededEvents = 0; | ||
discardedEvents = 0; | ||
} | ||
|
||
} |
67 changes: 0 additions & 67 deletions
67
src/main/java/net/programmer/igoodie/twitchspawn/util/TimeTaskQueue.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters