Marauroa Chat Tutorial/Server: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
imported>Hendrik Brummermann splitted Marauroa Tutorial |
imported>Hendrik Brummermann added missing call to world.initialize() as reported by maxgmer |
||
| (34 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
{{Navigation for Marauroa Top|Using}} |
|||
{{Navigation for Marauroa Users}} |
|||
{{Marauroa Chat Tutorial}} |
|||
== Server == |
|||
== Code == |
|||
In order to create a Marauroa-based game server you must provide at least implementation of the marauroa.server.game.rp.IRPRuleProcessor interface and a class for zone management, i.e. marauroa.server.game.rp.RPWorld descendant. You can use the marauroa.server.game.rp.RPWorld itself, but it doesn't provide you with any RPZones, while you will almost certainly need at least one. |
In order to create a Marauroa-based game server you must provide at least implementation of the marauroa.server.game.rp.IRPRuleProcessor interface and a class for zone management, i.e. marauroa.server.game.rp.RPWorld descendant. You can use the marauroa.server.game.rp.RPWorld itself, but it doesn't provide you with any RPZones, while you will almost certainly need at least one. |
||
| Line 15: | Line 18: | ||
if (instance == null) { |
if (instance == null) { |
||
instance = new World(); |
instance = new World(); |
||
instance.initialize(); |
|||
} |
} |
||
return instance; |
return instance; |
||
} |
} |
||
@Override |
|||
public void onInit() { |
public void onInit() { |
||
super.onInit(); |
super.onInit(); |
||
| Line 35: | Line 40: | ||
<!-- Please, see details here http://stendhal.game-host.org/wiki/index.php/Refactoring_Database_Access_in_Marauroa. --> |
<!-- Please, see details here http://stendhal.game-host.org/wiki/index.php/Refactoring_Database_Access_in_Marauroa. --> |
||
<source lang="java"> |
<source lang="java"> |
||
import java.util.List; |
|||
import java.sql.SQLException; |
import java.sql.SQLException; |
||
| Line 68: | Line 72: | ||
} |
} |
||
@Override |
|||
public void setContext(RPServerManager rpman) { |
public void setContext(RPServerManager rpman) { |
||
manager = rpman; |
manager = rpman; |
||
} |
} |
||
@Override |
|||
public boolean checkGameVersion(String game, String version) { |
public boolean checkGameVersion(String game, String version) { |
||
return game.equals("Chat"); |
return game.equals("Chat"); |
||
} |
} |
||
@Override |
|||
public synchronized void onTimeout(RPObject |
public synchronized void onTimeout(RPObject character) { |
||
onExit( |
onExit(character); |
||
} |
} |
||
@Override |
|||
public synchronized boolean onExit(RPObject |
public synchronized boolean onExit(RPObject character) { |
||
world.remove( |
world.remove(character.getID()); |
||
return true; |
return true; |
||
} |
} |
||
@Override |
|||
public synchronized boolean onInit(RPObject |
public synchronized boolean onInit(RPObject character) { |
||
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby")); |
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby")); |
||
zone.add( |
zone.add(character); |
||
return true; |
return true; |
||
} |
} |
||
@Override |
|||
public synchronized void beginTurn() { |
public synchronized void beginTurn() { |
||
} |
} |
||
@Override |
|||
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) { |
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) { |
||
return true; |
return true; |
||
} |
} |
||
@Override |
|||
public synchronized void endTurn() { |
public synchronized void endTurn() { |
||
} |
} |
||
@Override |
|||
public void execute(RPObject caster, RPAction action) { |
public void execute(RPObject caster, RPAction action) { |
||
if (action.get("type").equals("chat")) { |
if (action.get("type").equals("chat")) { |
||
RPObject |
RPObject chatEntry = new RPObject(); |
||
chatEntry.put("text", action.get("text")); |
|||
chatEntry.put("from", caster.get("nick")); |
|||
chatEntry.put("turn", manager.getTurn()); |
|||
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID())); |
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID())); |
||
zone.assignRPObjectID( |
zone.assignRPObjectID(chatEntry); |
||
zone.add( |
zone.add(chatEntry); |
||
} |
} |
||
} |
} |
||
@Override |
|||
public AccountResult createAccount(String username, String password, String email) { |
public AccountResult createAccount(String username, String password, String email) { |
||
TransactionPool transactionPool = TransactionPool.get(); |
TransactionPool transactionPool = TransactionPool.get(); |
||
| Line 131: | Line 145: | ||
} |
} |
||
@Override |
|||
public CharacterResult createCharacter(String username, String |
public CharacterResult createCharacter(String username, String characterName, RPObject template) { |
||
TransactionPool transactionPool = TransactionPool.get(); |
TransactionPool transactionPool = TransactionPool.get(); |
||
DBTransaction trans = transactionPool.beginWork(); |
DBTransaction trans = transactionPool.beginWork(); |
||
CharacterDAO characterDAO = DAORegister.get().get(CharacterDAO.class); |
CharacterDAO characterDAO = DAORegister.get().get(CharacterDAO.class); |
||
try { |
try { |
||
if (characterDAO.hasCharacter(trans, username, |
if (characterDAO.hasCharacter(trans, username, characterName)) { |
||
return new CharacterResult(Result. |
return new CharacterResult(Result.FAILED_CHARACTER_EXISTS, characterName, template); |
||
} |
} |
||
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby")); |
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby")); |
||
RPObject |
RPObject character = new RPObject(template); |
||
character.put("nick", characterName); |
|||
zone.assignRPObjectID( |
zone.assignRPObjectID(character); |
||
characterDAO.addCharacter(trans, username, |
characterDAO.addCharacter(trans, username, characterName, character); |
||
transactionPool.commit(trans); |
transactionPool.commit(trans); |
||
return new CharacterResult(Result.OK_CREATED, |
return new CharacterResult(Result.OK_CREATED, characterName, character); |
||
} catch (Exception e1) { |
} catch (Exception e1) { |
||
transactionPool.rollback(trans); |
transactionPool.rollback(trans); |
||
return new CharacterResult(Result.FAILED_EXCEPTION, |
return new CharacterResult(Result.FAILED_EXCEPTION, characterName, template); |
||
} |
} |
||
} |
} |
||
| Line 167: | Line 182: | ||
Functions for creating account and character should find out whether to create a new account/character or not. In our case we just always do it (not for duplicates of course). Result of this actions is instantly written to the database. Note that client can provide a template for the avatar object (an RPObject associated with the character). It is up to you how to use it while constructing the actual avatar object. We take what client provides, add a "nick" property (the same as character name) and use the resulting one as an avatar object. |
Functions for creating account and character should find out whether to create a new account/character or not. In our case we just always do it (not for duplicates of course). Result of this actions is instantly written to the database. Note that client can provide a template for the avatar object (an RPObject associated with the character). It is up to you how to use it while constructing the actual avatar object. We take what client provides, add a "nick" property (the same as character name) and use the resulting one as an avatar object. |
||
== Deployment == |
|||
So, we have two files, World.java and Rule.java, which contain the classes mentioned above. |
So, we have two files, World.java and Rule.java, which contain the classes mentioned above. |
||
On Windows, you can compile them using command |
|||
<pre> |
<pre> |
||
javac -cp marauroa.jar;log4j.jar;. *.java |
javac -cp marauroa.jar;log4j.jar;. *.java |
||
</pre> |
|||
On Linux and MacOSX, you have to replace the ";" with ":". |
|||
<pre> |
|||
javac -cp marauroa.jar:log4j.jar:. *.java |
|||
</pre> |
</pre> |
||
| Line 203: | Line 222: | ||
<pre> |
<pre> |
||
java -cp marauroa.jar;h2.jar;log4j.jar;. marauroa.server.marauroad -c server.ini |
java -cp marauroa.jar;h2.jar;log4j.jar;. marauroa.server.marauroad -c server.ini |
||
</pre> |
|||
Again, on Linux and MacOSX, you have to replace the ";" with ":". |
|||
<pre> |
|||
java -cp marauroa.jar:h2.jar:log4j.jar:. marauroa.server.marauroad -c server.ini |
|||
</pre> |
</pre> |
||
Of course, make sure that all the jars are in the current directory. Marauroa framework will parse server.ini file, find and load your classes World and Rule. |
Of course, make sure that all the jars are in the current directory. Marauroa framework will parse server.ini file, find and load your classes World and Rule. |
||
== Next Steps == |
|||
In the next section of this tutorial, we will write the '''[[Marauroa Chat Tutorial/Text Client|client]]''' which will connect to our server. |
|||
[[Category:Marauroa]] |
|||
{{#breadcrumbs: [[Marauroa]] | [[Navigation for Marauroa Users|Using]] | [[Marauroa Chat Tutorial|Tutorial]] | [[Marauroa Chat Tutorial/Server|Server]]}} |
|||