mirror of
https://github.com/2006-Scape/2006Scape.git
synced 2026-07-06 16:49:07 +00:00
Moved main loop into FixedRate scheduler. Added sync lock on runnable.
This commit is contained in:
@@ -3,6 +3,11 @@ package com.rebotted;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import org.apache.mina.common.IoAcceptor;
|
import org.apache.mina.common.IoAcceptor;
|
||||||
import org.apache.mina.transport.socket.nio.SocketAcceptor;
|
import org.apache.mina.transport.socket.nio.SocketAcceptor;
|
||||||
@@ -73,6 +78,8 @@ public class GameEngine {
|
|||||||
public static FightCaves fightCaves = new FightCaves();
|
public static FightCaves fightCaves = new FightCaves();
|
||||||
private static PestControl pestControl = new PestControl();
|
private static PestControl pestControl = new PestControl();
|
||||||
public static Trawler trawler = new Trawler();
|
public static Trawler trawler = new Trawler();
|
||||||
|
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||||
|
private final static Lock lock = new ReentrantLock();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Port and Cycle rate.
|
* Port and Cycle rate.
|
||||||
@@ -147,51 +154,76 @@ public class GameEngine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Main Server Tick
|
* Main Server Tick
|
||||||
|
*
|
||||||
|
* This scheduler will tick once every 600ms. If the previous tick takes
|
||||||
|
* 300ms to execute, this scheduler will wait 300ms only before the next
|
||||||
|
* tick.
|
||||||
|
*
|
||||||
|
* Each tick, it will attempt to get a lock on the main server thread.
|
||||||
|
* Without locking this runnable, multiple ticks could occur at once.
|
||||||
*/
|
*/
|
||||||
try {
|
scheduler.scheduleAtFixedRate(new Runnable() {
|
||||||
while (!GameEngine.shutdownServer) {
|
public void run() {
|
||||||
Thread.sleep(600);
|
if (lock.tryLock()) {
|
||||||
itemHandler.process();
|
synchronized (lock) {
|
||||||
playerHandler.process();
|
/**
|
||||||
npcHandler.process();
|
* Main Server Tick
|
||||||
shopHandler.process();
|
*/
|
||||||
objectManager.process();
|
try {
|
||||||
CastleWars.process();
|
if (GameEngine.shutdownServer) {
|
||||||
FightPits.process();
|
scheduler.shutdown();
|
||||||
pestControl.process();
|
}
|
||||||
CycleEventHandler.getSingleton().process();
|
itemHandler.process();
|
||||||
PlayersOnlineWebsite.addUpdatePlayersOnlineTask();
|
playerHandler.process();
|
||||||
RegisteredAccsWebsite.addUpdateRegisteredUsersTask();
|
npcHandler.process();
|
||||||
DiscordActivity.updateActivity();
|
shopHandler.process();
|
||||||
if (System.currentTimeMillis() - lastMassSave > 300000) {
|
objectManager.process();
|
||||||
for (Player p : PlayerHandler.players) {
|
CastleWars.process();
|
||||||
if (p == null) {
|
FightPits.process();
|
||||||
continue;
|
pestControl.process();
|
||||||
|
CycleEventHandler.getSingleton().process();
|
||||||
|
PlayersOnlineWebsite.addUpdatePlayersOnlineTask();
|
||||||
|
RegisteredAccsWebsite
|
||||||
|
.addUpdateRegisteredUsersTask();
|
||||||
|
DiscordActivity.updateActivity();
|
||||||
|
if (System.currentTimeMillis()
|
||||||
|
- lastMassSave > 300000) {
|
||||||
|
for (Player p : PlayerHandler.players) {
|
||||||
|
if (p == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
PlayerSave.saveGame((Client) p);
|
||||||
|
System.out.println("Saved game for " + p.playerName + ".");
|
||||||
|
lastMassSave = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
System.out.println("A fatal exception has been thrown!");
|
||||||
|
for (Player p : PlayerHandler.players) {
|
||||||
|
if (p == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (p.inTrade) {
|
||||||
|
((Client) p).getTrading().declineTrade();
|
||||||
|
}
|
||||||
|
if (p.duelStatus == 6) {
|
||||||
|
((Client) p).getDueling().claimStakedItems();
|
||||||
|
}
|
||||||
|
PlayerSave.saveGame((Client) p);
|
||||||
|
System.out.println("Saved game for " + p.playerName + ".");
|
||||||
|
scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PlayerSave.saveGame((Client) p);
|
}
|
||||||
System.out.println("Saved game for " + p.playerName
|
} else {
|
||||||
+ ".");
|
System.out.println("Can't Keep up! Did the system time change or is the server overloaded?");
|
||||||
lastMassSave = System.currentTimeMillis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
System.out.println("A fatal exception has been thrown!");
|
|
||||||
for (Player p : PlayerHandler.players) {
|
|
||||||
if (p == null) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if (p.inTrade) {
|
|
||||||
((Client)p).getTrading().declineTrade();
|
|
||||||
}
|
|
||||||
if(p.duelStatus == 6) {
|
|
||||||
((Client)p).getDueling().claimStakedItems();
|
|
||||||
}
|
|
||||||
PlayerSave.saveGame((Client) p);
|
|
||||||
System.out.println("Saved game for " + p.playerName + ".");
|
|
||||||
}
|
}
|
||||||
}
|
}, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
acceptor = null;
|
acceptor = null;
|
||||||
connectionHandler = null;
|
connectionHandler = null;
|
||||||
sac = null;
|
sac = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user