📄 rootsystem.java
字号:
package net.sf.jawp.gf.system;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.login.LoginException;
import net.sf.jawp.gf.api.SimpleUIDGenerator;
import net.sf.jawp.gf.api.UIDGenerator;
import net.sf.jawp.gf.api.domain.GameOptions;
import net.sf.jawp.gf.api.domain.GameWorld;
import net.sf.jawp.gf.api.domain.Player;
import net.sf.jawp.gf.api.domain.User;
import net.sf.jawp.gf.domain.GameWorldBase;
import net.sf.jawp.gf.domain.GameWorldRO;
import net.sf.jawp.gf.domain.PlayerDO;
import net.sf.jawp.gf.domain.UserDO;
import net.sf.jawp.gf.persistence.PersistenceController;
import net.sf.jawp.gf.persistence.PersistentSystem;
import net.sf.jawp.util.NumUtils;
/**
* root of storage all stored objects must be associated with this one
*
* @author jarek
* @version $Revision: 1.23 $
* @param <GAME> game type
* @param <GAMEVIEW> read only view of game
* @param <GAMESERVICE> type of gamespecific service
*/
public class RootSystem<GAME extends GameWorldBase<GAMEVIEW>, GAMEVIEW extends GameWorldRO, GAMESERVICE> implements PersistentSystem<RootSystemUnmodifiable<GAMEVIEW> >,
RootSystemUnmodifiable<GAMEVIEW>, SystemContext
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* users by login
*/
private final HashMap<String, UserDO> users;
/**
* users by key
*/
private final HashMap<Long, UserDO> usersByKey;
private final Map<Long, GAME> runningGames;
private final SimpleUIDGenerator uidGenerator;
private final NumUtils numContext;
public RootSystem()
{
super();
this.uidGenerator = new SimpleUIDGenerator(1000);
this.users = new HashMap<String, UserDO>();
this.usersByKey = new HashMap<Long, UserDO>();
this.runningGames = new HashMap<Long, GAME>(1);
this.numContext = new NumUtils((int)(System.currentTimeMillis() % 1024));
}
/**
* returns unmodifiable view of this system {@inheritDoc}
*/
public final RootSystemUnmodifiable<GAMEVIEW> getUnmodifiable()
{
return this;
}
public final User registerUser(final String name, final String encodedPass)
{
if (findUser(name) == null)
{
final UserDO usr = new UserDO(this.uidGenerator.nextUID());
usr.setLogin(name);
usr.setEncodedPass(encodedPass);
addUser(usr);
return new User(usr);
}
else
{
throw new RuntimeException("user already exists");
}
}
private void addUser(final UserDO usr)
{
synchronized (this.users)
{
this.users.put(usr.getLogin(), usr);
}
synchronized ( this.usersByKey)
{
this.usersByKey.put( usr.getKey(), usr);
}
}
/**
* adds game to system
* @param game
*/
private void addGame( final GAME game)
{
synchronized (this.runningGames)
{
this.runningGames.put( game.getKey(), game);
}
}
/**
* finds game for a given key
* @param key
* @return
*/
public final GAME findGame( final long key)
{
synchronized (this.runningGames)
{
return this.runningGames.get( key);
}
}
public final GAMEVIEW findGameView( final long key)
{
return findGame(key).getUnmodifiable();
}
/**
*
* @param login
* @return null if not found
*/
public final UserDO findUser(final String login)
{
synchronized (this.users)
{
return this.users.get(login);
}
}
public final UserDO findUser(final long key)
{
synchronized (this.usersByKey)
{
return this.usersByKey.get(key);
}
}
/**
* @todo: exception handling {@inheritDoc}
*/
public User login(final String login, final String clientMix, final String chapSeed)
throws LoginException
{
final UserDO usr = findUser(login);
if (usr != null)
{
if (usr.login(clientMix, chapSeed))
{
return usr;
}
else
{
throw new LoginException("wrong pass for user:" + login);
}
}
else
{
throw new LoginException("no such user:" + login);
}
}
/**
* retrieves unmodifiable view of game
* {@inheritDoc}
*/
public final GAMEVIEW getGameView(final long key)
{
final GAME game = findGame(key);
return game.getUnmodifiable();
}
/**
* added defensive copy
*/
public final Collection<GameWorld> getGameWorlds()
{
Collection<GAME> games;
synchronized (this.runningGames)
{
games = this.runningGames.values();
}
final Collection<GameWorld> worlds = new ArrayList<GameWorld>(games.size());
for ( final GAME g : games)
{
worlds.add( g.cloneGameWorld());
}
return worlds;
}
/**
* creates and adds game to system
* @param name
* @return
*/
public final GameWorld createGame(final String name, final GameOptions options)
{
final GAME game = internalCreateGame( name, options );
addGame(game);
return game.getUnmodifiable().getGameWorldDescription();
}
public final UIDGenerator getUIDGenerator()
{
return this.uidGenerator;
}
public final Player joinGame(final long gameKey, final long userKey)
{
final GAME game = findGame(gameKey);
final UserDO user = findUser(userKey);
assert user != null;
final PlayerDO player = new PlayerDO( this.uidGenerator.nextUID(), user, game);
game.addPlayer( player, this);
return new Player(player);
}
/**
* @todo: make this one abstract
* @param name
* @return
*/
protected GAME internalCreateGame(final String name, final GameOptions options)
{
throw new UnsupportedOperationException();
}
public NumUtils getNumericalContext()
{
return this.numContext;
}
public GAMESERVICE createGameService( final PersistenceController<RootSystem<GAME, GAMEVIEW, GAMESERVICE>, RootSystemUnmodifiable<GAMEVIEW> > persistence, final GAMEVIEW game, final Player player)
{
throw new UnsupportedOperationException();
}
public Remote createGameServiceRemote( final PersistenceController<RootSystem<GAME, GAMEVIEW, GAMESERVICE>, RootSystemUnmodifiable<GAMEVIEW> > persistence, final GAMEVIEW game, final Player player)
throws RemoteException
{
throw new UnsupportedOperationException();
}
/**
* makes micro-turn for all games
*
*/
public final void doStep(final long timeSlice)
{
Collection<GAME> games;
synchronized (this.runningGames)
{
games = new ArrayList<GAME>(this.runningGames.values());
}
for ( final GAME game : games)
{
game.doStep(timeSlice);
}
}
public final int getNumberOfGames()
{
synchronized (this.runningGames)
{
return this.runningGames.size();
}
}
public final int getNumberOfUsers()
{
synchronized (this.users)
{
return this.users.size();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -