Chat Tutorial in NetBeans/Server: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>SimonSmall
Added server files instructions
imported>SimonSmall
Some corrections
Line 7: Line 7:
= NetBeans Project (server) =
= NetBeans Project (server) =


We need some additional files that extend the marauroa engine to have the behaviours that we want; these are the World class and the Rule class (you can use your own names for these and those names must be entered in the server.ini file).
We need some additional files that extend the Marauroa engine to have the behaviours that we want; these are the World class and the Rule class (you can use your own names for these, and those names must be entered in the server.ini file).


Start the NetBeans IDE. Close any open projects except marauroa.
Start the NetBeans IDE. Close all open projects.


Click New Project, choose Java and Java Class Library. Click Next, then give the Project Name as server. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\server with DEVPATH being your chosen location. Check that Create Main Class is not ticked before clicking Finish.
Click New Project, choose Java and Java Class Library. Click Next, then give the Project Name as server. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\server with DEVPATH being your chosen location. Click Finish.


The empty Project is created in NetBeans. Under the Files tab you will see the directory structure it has also generated; check this using your file explorer.
The empty Project is created in NetBeans. Under the Files tab you will see the directory structure it has also generated; check this using your file explorer.
Line 19: Line 19:
== World.java ==
== World.java ==


Right-click on the client package and add a new Class. Give the class name as World. Replace the template text with the following:
Right-click on the default package branch and add a new Java Class. Give the class name as World and package as server. Replace the template text with the following:


<source lang="Java">
<source lang="Java">
Line 28: Line 28:
package server;
package server;


import marauroa.server.game.rp.RPWorld;
import marauroa.server.game.rp.MarauroaRPZone;
import marauroa.server.game.rp.MarauroaRPZone;
import marauroa.server.game.rp.RPWorld;


/**
/**
Line 46: Line 46:
public void onInit() {
public void onInit() {
super.onInit();
super.onInit();
MarauroaRPZone zone = new MarauroaRPZone("start");
MarauroaRPZone zone = new MarauroaRPZone("lobby");
addRPZone(zone);
addRPZone(zone);
}
}
Line 54: Line 54:
== Rule.java ==
== Rule.java ==


Right-click on the client package and add a new Class. Give the class name as Rule. Replace the template text with the following:
Right-click on the client package and add a new Java Class. Give the class name as Rule. Replace the template text with the following:


<source lang="Java">
<source lang="Java">
Line 63: Line 63:
package server;
package server;


import java.util.List;
import java.sql.SQLException;
import java.sql.SQLException;
import java.util.List;

import marauroa.common.crypto.Hash;
import marauroa.common.crypto.Hash;
import marauroa.common.game.AccountResult;
import marauroa.common.game.*;
import marauroa.common.game.CharacterResult;
import marauroa.server.db.DBTransaction;
import marauroa.common.game.IRPZone;
import marauroa.server.db.TransactionPool;
import marauroa.common.game.RPAction;
import marauroa.common.game.RPObject;
import marauroa.common.game.Result;
import marauroa.server.game.db.DAORegister;
import marauroa.server.game.db.AccountDAO;
import marauroa.server.game.db.AccountDAO;
import marauroa.server.game.db.CharacterDAO;
import marauroa.server.game.db.CharacterDAO;
import marauroa.server.db.TransactionPool;
import marauroa.server.game.db.DAORegister;
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 85: Line 79:
*/
*/
public class Rule implements IRPRuleProcessor {
public class Rule implements IRPRuleProcessor {
private static Rule instance;
private static Rule instance;
private World world = World.get();
private World world = World.get();

private RPServerManager manager;
private RPServerManager manager;

public static IRPRuleProcessor get() {
public static IRPRuleProcessor get() {
if (instance == null) {
if (instance == null) {
instance = new Rule();
instance = new Rule();
}
return instance;
}
}
return instance;
@Override
}
public void setContext(RPServerManager rpman) {
manager = rpman;
public void setContext(RPServerManager rpman) {
manager = rpman;
}
public boolean checkGameVersion(String game, String version) {
return game.equals("Chat");
}
public synchronized void onTimeout(RPObject object) {
onExit(object);
}
public synchronized boolean onExit(RPObject object) {
world.remove(object.getID());
return true;
}
public synchronized boolean onInit(RPObject object) {
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby"));
zone.add(object);
return true;
}
public synchronized void beginTurn() {
}
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) {
return true;
}
public synchronized void endTurn() {
}
public void execute(RPObject caster, RPAction action) {
if (action.get("type").equals("chat")) {
RPObject chat_entry = new RPObject();
chat_entry.put("text", action.get("text"));
chat_entry.put("from", caster.get("nick"));
chat_entry.put("turn", manager.getTurn());
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID()));
zone.assignRPObjectID(chat_entry);
zone.add(chat_entry);
}
}
}
@Override
public AccountResult createAccount(String username, String password, String email) {
public boolean checkGameVersion(String game, String version) {
return game.equals("Chat");
TransactionPool transactionPool = TransactionPool.get();
}
DBTransaction trans = transactionPool.beginWork();
AccountDAO accountDAO = DAORegister.get().get(AccountDAO.class);
try {
@Override
public synchronized void onTimeout(RPObject object) {
if (accountDAO.hasPlayer(trans, username)) {
onExit(object);
return new AccountResult(Result.FAILED_PLAYER_EXISTS, username);
}
}
accountDAO.addPlayer(trans, username, Hash.hash(password), email);
@Override
transactionPool.commit(trans);
public synchronized boolean onExit(RPObject object) {
return new AccountResult(Result.OK_CREATED, username);
world.remove(object.getID());
} catch (SQLException e1) {
return true;
transactionPool.rollback(trans);
}
return new AccountResult(Result.FAILED_EXCEPTION, username);
@Override
public synchronized boolean onInit(RPObject object) {
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby"));
zone.add(object);
return true;
}
@Override
public synchronized void beginTurn() {
}
@Override
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) {
return true;
}
@Override
public synchronized void endTurn() {
}
@Override
public void execute(RPObject caster, RPAction action) {
if (action.get("type").equals("chat")) {
RPObject chat_entry = new RPObject();
chat_entry.put("text", action.get("text"));
chat_entry.put("from", caster.get("nick"));
chat_entry.put("turn", manager.getTurn());
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID()));
zone.assignRPObjectID(chat_entry);
zone.add(chat_entry);
}
}
}
}


@Override
@Override
public CharacterResult createCharacter(String username, String character, RPObject template) {
public AccountResult createAccount(String username, String password, String email) {
TransactionPool transactionPool = TransactionPool.get();
throw new UnsupportedOperationException("Not supported yet.");
DBTransaction trans = transactionPool.beginWork();
}
AccountDAO accountDAO = DAORegister.get().get(AccountDAO.class);
try {
if (accountDAO.hasPlayer(trans, username)) {
return new AccountResult(Result.FAILED_PLAYER_EXISTS, username);
}
accountDAO.addPlayer(trans, username, Hash.hash(password), email);
transactionPool.commit(trans);
return new AccountResult(Result.OK_CREATED, username);
} catch (SQLException e1) {
transactionPool.rollback(trans);
return new AccountResult(Result.FAILED_EXCEPTION, username);
}
}
@Override
public CharacterResult createCharacter(String username, String character, RPObject template) {
TransactionPool transactionPool = TransactionPool.get();
DBTransaction trans = transactionPool.beginWork();
CharacterDAO characterDAO = DAORegister.get().get(CharacterDAO.class);
try {
if (characterDAO.hasCharacter(trans, username, character)) {
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template);
}
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby"));
RPObject object = new RPObject(template);
object.put("nick", character);
zone.assignRPObjectID(object);
characterDAO.addCharacter(trans, username, character, object);
transactionPool.commit(trans);
return new CharacterResult(Result.OK_CREATED, character, object);
} catch (Exception e1) {
transactionPool.rollback(trans);

return new CharacterResult(Result.FAILED_EXCEPTION, character, template);
}
}
}
}
</source>
</source>
Line 212: Line 233:
# Database information
# Database information
database_adapter=marauroa.server.db.adapter.H2DatabaseAdapter
database_adapter=marauroa.server.db.adapter.H2DatabaseAdapter
jdbc_url=jdbc:h2:h2db;AUTO_RECONNECT=TRUE;DB_CLOSE_ON_EXIT=FALSE
jdbc_url=jdbc:h2:Chat;AUTO_RECONNECT=TRUE;DB_CLOSE_ON_EXIT=FALSE
jdbc_class=org.h2.Driver
jdbc_class=org.h2.Driver


Line 244: Line 265:
This is using the h2 database. Configuration of others is possible.
This is using the h2 database. Configuration of others is possible.


The line starting jdbc_url=jdbc:h2:h2db gives the location and name of the database. This creates a database named h2db in the current (i.e. server) directory. See here [http://www.h2database.com/html/features.html#database_url] for the technical details.
The line starting jdbc_url=jdbc:h2:Chat gives the location and name of the database. This creates a database named Chat in the current (i.e. server) directory. See here [http://www.h2database.com/html/features.html#database_url] for the technical details.


=== Game classes ===
=== Game classes ===


The lines world= and rule= specify the classes to be used for these features. If you chose different class names above, use those names (and package) here.
The lines world= and ruleprocessor= specify the classes to be used for these features. If you chose different class names above, use those names (and package) here.


=== Game Information ===
=== Game Information ===
Line 258: Line 279:
Although you don't have to provide a Log configuration file, you can if you wish.
Although you don't have to provide a Log configuration file, you can if you wish.


Create another empty file, as you did with the server.ini file, with a name of log4j.properties and copy the following code into it.
Create another empty file, as you did with the server.ini file, with a name of log4j.properties and copy the following code into it (NetBeans remembers the file types you created, so Empty will be on the first selection screen)


<pre>
<pre>