All pastes #730830 Raw Edit

Someone

public java v1 · immutable
·published 2007-10-09 15:35 UTC
rendered paste body
package src.muu.Universe;import java.util.HashMap;import java.util.Iterator;import java.util.concurrent.ConcurrentLinkedQueue;import src.muu.Universe.Commander.*;import src.muu.Universe.Data.*;import src.muu.Universe.DataTypes.*;public class UniverseEngine extends Thread {	private ConcurrentLinkedQueue<CommandEvent> delayQueue = new ConcurrentLinkedQueue<CommandEvent>(); // Queue of Actions to complete	private ConcurrentLinkedQueue<CommandEvent> commandQueue = new ConcurrentLinkedQueue<CommandEvent>(); // Queue of Actions to complete	private HashMap<String,MuuObject> objects = new HashMap<String,MuuObject>(); // Master list of all Objects	private HashMap<String,MuuRegion> reigons = new HashMap<String,MuuRegion>(); // Master list of all Regions	private Metronome metronome; // Game Ticker	private boolean active = false; // Server Engine Active		public class Metronome extends Thread {		int tickLength=1000;		long tick;		boolean active=false;		public Metronome(int tickLength) {			this.tickLength = tickLength;			this.active=true;			this.start();		}		public void run() {			this.setName("Metronome");			while(active) {				try {					long startTime = System.currentTimeMillis();					tick++;					if(tick%86400==0) {//On the daily tick					} else if(tick%3600==0) { // Hourly Tick						// Backup					} else if(tick%1800==0) { // 30 Minute Tick										} else if(tick%900==0) { // 15 Minute Tick										} else if(tick%600==0) { // 10 Minute Tick											} else if(tick%60==0) { // Minute Tick						CommandEvent event = new CommandEvent(null,"test",3);						processCommand(event);					} else { // Second Tick						// Process delay Queue						Iterator<CommandEvent> delayIterator = delayQueue.iterator();						while(delayIterator.hasNext()) {							CommandEvent delayedEvent = delayIterator.next();							if(delayedEvent.getDelay()>0){delayedEvent.decrementDelay();							} else { // Send to actionQueue								executeCommand(delayedEvent);								delayIterator.remove();							}						}					}					long waitTime = tickLength-(System.currentTimeMillis()-startTime);					synchronized(this) {						System.out.println("Tick-"+tick);						if (waitTime>0) {this.wait(waitTime);} else {continue;}					}				} catch (InterruptedException e) {}			}		}		public void shutdown() {active=false;}	}		public void processCommand(CommandEvent event) {		delayQueue.add(event);	}			private synchronized void executeCommand(CommandEvent event) {		commandQueue.add(event);		commandQueue.notifyAll();	}		public void initialize() {		CommandSession.setEngine(this); // All CommandSessions know me.		metronome = new Metronome(1000);		// Load it all up				// Set active and run		active=true;		this.start();	}		public void run() {		this.setName("Universe Engine");		while(active) {			synchronized(commandQueue) {				while(!commandQueue.isEmpty()) { // Handle Commands					CommandEvent event = commandQueue.poll();					System.out.println(event.getMessage());				} // Then Wait				try {commandQueue.wait();} catch (InterruptedException e) {}				System.out.println("EngineTick!");			}		}	}		public void shutdown() {		metronome.shutdown();	}}