org.starhope.appius.game
Class ZoneSpawner

java.lang.Object
  extended by org.starhope.appius.game.ZoneSpawner
All Implemented Interfaces:
Comparable<AcceptsMetronomeTicks>, AcceptsMetronomeTicks, HasName

public class ZoneSpawner
extends Object
implements AcceptsMetronomeTicks, Comparable<AcceptsMetronomeTicks>

Thread to manage automated zone spawn/retirement

Author:
brpocock@star-hope.org

Field Summary
private static Lock lock
          a lock for zone spawning
static String myServer
          the server to which this Zone Spawner belongs
private  long nextCheck
          Time to next check zones for spawning
private static Set<Zone> retiredZones
          Zones which have been Retired may still be in play (there are probably users in them), but new users cannot see them or join them.
private static int USER_ROOM_MAX_USERS
          Max users allowed into one room of an user's house at a time
private static double ZONE_FULL_RATIO
          Get the percentage of "full" at which we consider a zone to be full enough to warrant spawning new ones.
(package private) static double ZONE_LIGHT_RATIO
          Get the percentage of "full" at which we consider a zone to be light enough that it's almost empty
private static int ZONE_MAX_USERS
          The max.
private static int ZONE_SPAWN_SECONDS
          Time (in seconds) between spawning new zones
 
Constructor Summary
ZoneSpawner(String server)
          create a ZoneSpawner object for a given server
 
Method Summary
static void checkZonesForSpawn()
          See if enough zones are full to warrant spawning a new one; or, if zones are empty and can be deallocated.
 int compareTo(AcceptsMetronomeTicks o)
           
static void configUpdated()
          When the configuration is updated, update some “constants” used by the Zone Spawn system from the values in the configuration file (or in-core configuration overrides)
 boolean equals(Object obj)
           
(package private) static AbstractZone findLightZone()
          Deprecated. Use TheZones.findLightZone() instead
(package private)  boolean freeInDB(String zoneName)
          mark a zone as being free in the database
private  String genZoneName()
          Generate the name for a new zone based upon this one's name
 String getName()
          Return a user-visible, unique name for this class.
private static int getNumberOfZonesNeeded(ConcurrentLinkedQueue<AbstractZone> zones)
          This is a breakout from checkZonesForSpawn() to determine the number of zones that need to be spawned or removed ignoring hidden zones
static int getUserRoomMaxUsers()
           
private static double getZoneFullRatio()
           
static double getZoneLightRatio()
           
static int getZoneMaxUsers()
           
static int getZoneSpawnSeconds()
          Get the number of seconds between checking for zone spawning
 int hashCode()
           
private static void retireUnneededZones(ConcurrentLinkedQueue<AbstractZone> zones, int numZonesToRemove)
          retire any zones which are unneeded, due to population drop
private static void spawnNeededZones(int needZones)
          spawn the number of zones needed (in parallel)
static String spawnNewZone()
           
private static void spawnZone_addRooms(Zone zone)
          While spawning a new zone, add rooms to it.
static Zone spawnZone()
          spawn a single new zone, and return that Zone
static Zone spawnZone(String zoneName)
          Create a new, empty zone, and attach the default properties to it.
static Zone spawnZone(String zoneName, String image)
          Create a new, empty zone, and attach the default properties to it.
 void tick(long currentTime, long deltaTime)
          This method is called periodically from the metronome thread.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lock

private static Lock lock
a lock for zone spawning


myServer

public static String myServer
the server to which this Zone Spawner belongs


retiredZones

private static final Set<Zone> retiredZones
Zones which have been Retired may still be in play (there are probably users in them), but new users cannot see them or join them. They have been retired pending destruction, once all current players have signed off.


USER_ROOM_MAX_USERS

private static int USER_ROOM_MAX_USERS
Max users allowed into one room of an user's house at a time


ZONE_FULL_RATIO

private static double ZONE_FULL_RATIO
Get the percentage of "full" at which we consider a zone to be full enough to warrant spawning new ones.


ZONE_LIGHT_RATIO

static double ZONE_LIGHT_RATIO
Get the percentage of "full" at which we consider a zone to be light enough that it's almost empty


ZONE_MAX_USERS

private static int ZONE_MAX_USERS
The max. users per Zone (And per every room in every real zone)


ZONE_SPAWN_SECONDS

private static int ZONE_SPAWN_SECONDS
Time (in seconds) between spawning new zones


nextCheck

private long nextCheck
Time to next check zones for spawning

Constructor Detail

ZoneSpawner

public ZoneSpawner(String server)
create a ZoneSpawner object for a given server

Parameters:
server - the hostname of the server
Method Detail

checkZonesForSpawn

public static void checkZonesForSpawn()
See if enough zones are full to warrant spawning a new one; or, if zones are empty and can be deallocated.


configUpdated

public static void configUpdated()
When the configuration is updated, update some “constants” used by the Zone Spawn system from the values in the configuration file (or in-core configuration overrides)


findLightZone

@Deprecated
static AbstractZone findLightZone()
Deprecated. Use TheZones.findLightZone() instead

Returns a zone rated as being “light” traffic. If there are no light traffic zones, attempt to spawn one. If that fails, then resort to finding the least-busy zone of all zones visible.

Returns:
a zone with relatively light traffic.

getNumberOfZonesNeeded

private static int getNumberOfZonesNeeded(ConcurrentLinkedQueue<AbstractZone> zones)
This is a breakout from checkZonesForSpawn() to determine the number of zones that need to be spawned or removed ignoring hidden zones

Parameters:
zones - The set of all Zones in the multiverse
Returns:
the (signed) number of zones to be created (positive) or removed (negative)

getUserRoomMaxUsers

public static int getUserRoomMaxUsers()
Returns:
get the max number of users allowed in an user's house/rooms

getZoneFullRatio

private static double getZoneFullRatio()
Returns:
the ratio of its capacity at which we declare that a zone is “full,” and do not allow it to accept more users. Each lightly-filled or empty zone counteracts a full zone when deciding whether to spawn additional zones.

getZoneLightRatio

public static double getZoneLightRatio()
Returns:
the ratio of its capacity at which we declare that a zone is “lightly used,” and assume that it can absorb more users. Each lightly-filled or empty zone counteracts a full zone when deciding whether to spawn additional zones.

getZoneMaxUsers

public static int getZoneMaxUsers()
Returns:
get the maximum number of users allowed in a zone

getZoneSpawnSeconds

public static int getZoneSpawnSeconds()
Get the number of seconds between checking for zone spawning

Returns:
The interval between checks for spawning zones

retireUnneededZones

private static void retireUnneededZones(ConcurrentLinkedQueue<AbstractZone> zones,
                                        int numZonesToRemove)
retire any zones which are unneeded, due to population drop

Parameters:
zones - the set of zones to be considered for retirement
numZonesToRemove - the number of zones to be removed

spawnNeededZones

private static void spawnNeededZones(int needZones)
spawn the number of zones needed (in parallel)

Parameters:
needZones - the number of new zones to be spawned

spawnNewZone

public static String spawnNewZone()
Returns:
spawn a single new zone, and return its name

spawnZone

public static Zone spawnZone()
spawn a single new zone, and return that Zone

Returns:
the newly-spawned Zone (or null, if zone spawn failed)

spawnZone

public static Zone spawnZone(String zoneName)
Create a new, empty zone, and attach the default properties to it. The zone's image will be “hearts.” FIXME

Parameters:
zoneName - the name of the new zone
Returns:
the zone spawned

spawnZone

public static Zone spawnZone(String zoneName,
                             String image)
Create a new, empty zone, and attach the default properties to it.

Parameters:
zoneName - the name of the new zone
image - the background nugget icon
Returns:
the new zone spawned (or null, if zone spawn failed)

spawnZone_addRooms

private static void spawnZone_addRooms(Zone zone)
While spawning a new zone, add rooms to it. Set the room variables from the defaults in the database. If the room has a variable named "lobby," mark it as a lobby. (XXX: scan for lobbies in real time, not just at zone creation.)

Parameters:
zone - the Zone to which rooms should be added

compareTo

public int compareTo(AcceptsMetronomeTicks o)
Specified by:
compareTo in interface Comparable<AcceptsMetronomeTicks>
See Also:
Comparable.compareTo(java.lang.Object)

equals

public boolean equals(Object obj)
Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object)

freeInDB

boolean freeInDB(String zoneName)
mark a zone as being free in the database

XXX: contains SQL

Parameters:
zoneName - the new zone's name
Returns:
true, if successful.

genZoneName

private String genZoneName()
Generate the name for a new zone based upon this one's name

Returns:
a new zone name

getName

public String getName()
Description copied from interface: HasName
Return a user-visible, unique name for this class. This name may not necessarily be globally unique, but must be unique across all objects of this class. For example, having both a User and a Zone named “Lightning” is acceptable, but there may not be two users with the same name.

Specified by:
getName in interface HasName
Returns:
a user-visible string name for this instance
See Also:
HasName.getName()

hashCode

public int hashCode()
Overrides:
hashCode in class Object
See Also:
Object.hashCode()

tick

public void tick(long currentTime,
                 long deltaTime)
          throws UserDeadException
Description copied from interface: AcceptsMetronomeTicks
This method is called periodically from the metronome thread. To save computation, it receives both the current time since epoch in milliseconds at the start of the global tick propagation, and the delta time since the previous metronome tick.

Specified by:
tick in interface AcceptsMetronomeTicks
Parameters:
currentTime - Time since epoch at the start of the global metronome propagation, as per System.currentTimeMillis()
deltaTime - Delta-time in milliseconds since the prior global metronome tick
Throws:
UserDeadException - if a user has died during this tick
See Also:
AcceptsMetronomeTicks.tick(long, long)