Developing TicToe HTML5/Implementing Server Entities: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Hendrik Brummermann
imported>Hendrik Brummermann
No edit summary
Line 218: Line 218:


Again the imports and constructors have been left out.
Again the imports and constructors have been left out.

== Factory ==

The last step is to make our entities known to Marauroa. Therefore we create a new ObjectFactory implementation called net.sf.arianne.tictoe.server.core.TicToeObjectFactory. While this does the same thing as the object factory on the client side, the syntax is a bit different:

<source lang="java">
package net.sf.arianne.tictoe.server.core;

import marauroa.common.game.RPClass;
import marauroa.common.game.RPObject;
import marauroa.server.game.rp.RPObjectFactory;
import net.sf.arianne.tictoe.server.entity.Gameboard;
import net.sf.arianne.tictoe.server.entity.Player;
import net.sf.arianne.tictoe.server.entity.Token;

/**
* Creates concrete objects of entities.
*/
public class TicToeObjectFactory extends RPObjectFactory {
private static RPObjectFactory singleton;

/**
* returns the factory instance (this method is called
* by Marauroa using reflection).
*
* @return RPObjectFactory
*/
public static RPObjectFactory getFactory() {
if (singleton == null) {
singleton = new TicToeObjectFactory();
}
return singleton;
}

@Override
public RPObject transform(final RPObject object) {
final RPClass clazz = object.getRPClass();
if (clazz == null) {
return super.transform(object);
}

final String name = clazz.getName();
if (name.equals("player")) {
return new Player(object);
} else if (name.equals("gameboard")) {
return new Gameboard(object);
} else if (name.equals("token")) {
return new Token(object);
}

return super.transform(object);
}
}
</source>

This is a very simple implementation of the transform method, but as we only have 3 entities, this is okay for now. Complex programs usually replace the if/else-if construct with a registration mechanism.

This new class has to be added to server.ini:
<source lang="ini">
factory_implementation=net.sf.arianne.tictoe.server.core.TicToeObjectFactory
</source>