Marauroa Chat Tutorial: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Kymara
TODO: use template for todo not just the word todo
imported>Terin
Line 37: Line 37:


Implementing IRPRuleProcessor will require more code, as we must implement all the methods of the interface. We will start with the following stub
Implementing IRPRuleProcessor will require more code, as we must implement all the methods of the interface. We will start with the following stub
<!-- Updated due to refactoring database access in Marauroa. -->
<!-- Please, see details here http://stendhal.game-host.org/wiki/index.php/Refactoring_Database_Access_in_Marauroa. -->
<pre>
<pre>
import java.util.List;
import java.util.List;
Line 48: Line 50:
import marauroa.common.game.RPObject;
import marauroa.common.game.RPObject;
import marauroa.common.game.Result;
import marauroa.common.game.Result;
import marauroa.server.game.db.DatabaseFactory;
import marauroa.server.game.db.DAORegister;
import marauroa.server.game.db.IDatabase;
import marauroa.server.game.db.AccountDAO;
import marauroa.server.game.db.Transaction;
import marauroa.server.game.db.CharacterDAO;
import marauroa.server.db.TransactionPool;
import marauroa.server.db.DBTransaction;
import marauroa.server.game.rp.IRPRuleProcessor;
import marauroa.server.game.rp.IRPRuleProcessor;
import marauroa.server.game.rp.RPServerManager;
import marauroa.server.game.rp.RPServerManager;
Line 115: Line 119:


public AccountResult createAccount(String username, String password, String email) {
public AccountResult createAccount(String username, String password, String email) {
TransactionPool transactionPool = TransactionPool.get();
IDatabase database = DatabaseFactory.getDatabase();
Transaction trans = database.getTransaction();
DBTransaction trans = transactionPool.beginWork();
AccountDAO accountDAO = DAORegister.get().get(AccountDAO.class);
try {
try {
if (accountDAO.hasPlayer(trans, username)) {
trans.begin();
if (database.hasPlayer(trans, username)) {
// Account already exists
return new AccountResult(Result.FAILED_PLAYER_EXISTS, username);
return new AccountResult(Result.FAILED_PLAYER_EXISTS, username);
}
}
database.addPlayer(trans, username, Hash.hash(password), email);
accountDAO.addPlayer(trans, username, Hash.hash(password), email);
trans.commit();
transactionPool.commit(trans);
return new AccountResult(Result.OK_CREATED, username);
return new AccountResult(Result.OK_CREATED, username);
} catch (SQLException e1) {
} catch (SQLException e1) {
transactionPool.rollback(trans);
try {

trans.rollback();
} catch (SQLException e2) {
// Rollback failed
}
return new AccountResult(Result.FAILED_EXCEPTION, username);
return new AccountResult(Result.FAILED_EXCEPTION, username);
}
}
Line 137: Line 137:


public CharacterResult createCharacter(String username, String character, RPObject template) {
public CharacterResult createCharacter(String username, String character, RPObject template) {
TransactionPool transactionPool = TransactionPool.get();
IDatabase database = DatabaseFactory.getDatabase();
Transaction trans = database.getTransaction();
DBTransaction trans = transactionPool.beginWork();
CharacterDAO characterDAO = DAORegister.get().get(CharacterDAO.class);
try {
try {
if (characterDAO.hasCharacter(trans, username, character)) {
trans.begin();
if (database.hasCharacter(trans, username, character)) {
// Charecter already exists
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template);
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template);
}
}
Line 149: Line 148:
object.put("nick", character);
object.put("nick", character);
zone.assignRPObjectID(object);
zone.assignRPObjectID(object);
database.addCharacter(trans, username, character, object);
characterDAO.addCharacter(trans, username, character, object);
trans.commit();
transactionPool.commit(trans);
return new CharacterResult(Result.OK_CREATED, character, object);
return new CharacterResult(Result.OK_CREATED, character, object);
} catch (Exception e1) {
} catch (Exception e1) {
transactionPool.rollback(trans);
try {

trans.rollback();
} catch (SQLException e2) {
// Rollback failed
}
return new CharacterResult(Result.FAILED_EXCEPTION, character, template);
return new CharacterResult(Result.FAILED_EXCEPTION, character, template);
}
}
Line 175: Line 171:


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 ===
=== 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.