⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 realmdo.java

📁 Java mulitplayer strategy game. Adaptation of KDE Galaxy Conquest. (rules are changed - but still th
💻 JAVA
字号:
package net.sf.jawp.game.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;


import net.sf.jawp.api.domain.BattleReportVO;
import net.sf.jawp.api.domain.Fleet;
import net.sf.jawp.api.domain.Realm;
import net.sf.jawp.api.domain.stub.FleetVO;
import net.sf.jawp.gf.api.domain.PlayerStatus;
import net.sf.jawp.gf.domain.PlayerDO;

/**
 * a single player realm
 * @author jarek
 *
 */
public final class RealmDO extends Realm
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;

	/**
	 * owner of empire/realm
	 */
	private final PlayerDO owner;
	
	
	
	/**
	 * this may be changed for some players
	 */
	private long productivityFactor;
	
	/**
	 * current value of buerocracy - it affects production
	 */
	private long buerocracy;
	
	/**
	 * total value (planets + fleets)
	 */
	private long netWorth;
	
	private final HashMap<Long, FleetDO> fleets;
	
	private final HashMap<Long, PlanetDO> planets;
	
	//statistics
	private int totalShips; 
	
	private int totalProductivity;
	
	private final ArrayList<BattleReportVO> battleResults = new ArrayList<BattleReportVO>(10);
	
	private PlayerStatus status;
	
	public RealmDO ( final long key, final PlayerDO player, final String name )
	{
		super( key, name);
		this.owner = player;
		this.fleets = new HashMap<Long, FleetDO>();
		this.planets = new HashMap<Long, PlanetDO>();
	}
	
	public PlayerDO getOwner()
	{
		return this.owner;
	}

	public void removeFleet(final FleetDO fleet)
	{
		synchronized (this.fleets)
		{
			this.fleets.remove( fleet.getKey() );
		}
		
	}

	public void registerFleet(final FleetDO fleet)
	{
		synchronized (this.fleets)
		{
			this.fleets.put( fleet.getKey(), fleet );
		}
	}

	public Collection<Fleet> getFleets()
	{
		final Collection<Fleet> result = new ArrayList<Fleet>();
		synchronized (this.fleets)
		{
			//result.addAll( this.fleets.values());
			for ( Fleet f : this.fleets.values())
			{
				result.add( new FleetVO( f));
			}
			
		}
		return result;
	}

	public void registerPlanet( final PlanetDO planet)
	{
		//System.out.println(getName()+ " - adding planet:"+planet.getName());
		synchronized ( this.planets)
		{
			this.planets.put( planet.getKey(), planet);
		}
	}
	
	public void removePlanet( final PlanetDO planet)
	{
		//System.out.println(getName()+ " - removing planet:"+planet.getName());
		synchronized (this.planets)
		{
			
			this.planets.remove( planet.getKey());
		}
	}
	
	@Override
	public long getBuerocracy()
	{
		return this.buerocracy;
	}

	@Override
	public long getNetWorth()
	{
		return this.netWorth;
	}

	public int getTotalShips()
	{
		return totalShips;
	}

	public void setTotalShips(final int totalFleet)
	{
		this.totalShips = totalFleet;
	}

	public int getTotalProductivity()
	{
		return totalProductivity;
	}

	public void setTotalProductivity(final int totalProductivity)
	{
		this.totalProductivity = totalProductivity;
	}
	
	public Collection<BattleReportVO> retrieveBattleRaports()
	{
		synchronized (this.battleResults)
		{
			final ArrayList<BattleReportVO> result = new ArrayList<BattleReportVO>( this.battleResults.size());
			result.addAll( this.battleResults);
			this.battleResults.clear();
			return result;
		}
	}
	
	public void recalcStatistics()
	{
		int prod = 0;
		int fleetsCNT = 0;
		synchronized ( this.fleets )
		{
			for ( final FleetDO f : this.fleets.values()  )
			{
				fleetsCNT += f.getSize();
			}
		}
		
		synchronized ( this.planets)
		{
			for ( final PlanetDO  p : this.planets.values())
			{
				prod += p.getProductivity(); 
			}
		}
		setTotalShips( fleetsCNT);
		setTotalProductivity( prod);
		if ( fleetsCNT == 0 && prod == 0 )
		{
			//posible defeat
			markDefeated();
		}
	}
	
	private void markDefeated()
	{
		if ( getOwner() != null)
		{
			//lets forget about neutral one
			getOwner().markDefeated();
		}
		setStatus(PlayerStatus.DEFEATED);
	}

	public int getOwnedFleetsNumber()
	{
		synchronized ( this.fleets )
		{
			return this.fleets.size();
		}
	}

	public int getOwnedPlanetsNumber()
	{
		synchronized ( this.planets )
		{
			return this.planets.size();
		}
	}
	
	public void addReport( final BattleReportVO report)
	{
		synchronized (this.battleResults)
		{
			this.battleResults.add(report);
		}
	}

	public PlayerStatus getStatus()
	{
		return status;
	}

	public void setStatus(final PlayerStatus status)
	{
		this.status = status;
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -