Chat Tutorial in NetBeans/Server: Difference between revisions

From Arianne
Jump to navigation Jump to search
imported>SimonSmall
Added server files instructions
 
imported>SimonSmall
Added server files instructions
(No difference)

Revision as of 13:10, 14 January 2012

Introduction

There are several files required to run the Server, with several parts covering configuration. You can use the values suggested here, or your own. If using your own, make sure that you enter them correctly.

This example is using the h2 database to store data. MySQL can also be used but this is not covered here.

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

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

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.

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.

Right click the Libraries branch of the server Project tree, and select Add Project. Make sure you browse to the correct file location and select the marauroa project that you created earlier. This will include your marauroa library that you built.

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:

<source lang="Java"> /*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package server;

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

/**

*
*/

public class World extends RPWorld {

 private static World instance;

 public static World get() {
   if (instance == null) {
     instance = new World();
   }
   return instance;
 }

 public void onInit() {
   super.onInit();
   MarauroaRPZone zone = new MarauroaRPZone("start");
   addRPZone(zone);
 }

} </source>

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:

<source lang="Java"> /*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package server;

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

import marauroa.common.crypto.Hash; import marauroa.common.game.AccountResult; import marauroa.common.game.CharacterResult; import marauroa.common.game.IRPZone; 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.CharacterDAO; import marauroa.server.db.TransactionPool; import marauroa.server.db.DBTransaction; import marauroa.server.game.rp.IRPRuleProcessor; import marauroa.server.game.rp.RPServerManager;

/**

*
*/

public class Rule implements IRPRuleProcessor {

 private static Rule instance;

 private World world = World.get();

 private RPServerManager manager;

 public static IRPRuleProcessor get() {
   if (instance == null) {
     instance = new Rule();
   }
   return instance;
 }

 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);
   }
 }

 public AccountResult createAccount(String username, String password, String email) {
   TransactionPool transactionPool = TransactionPool.get();
   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) {
   throw new UnsupportedOperationException("Not supported yet.");
 }

} </source>

Building

If there are no errors in the package, select Run, Clean and Build Project from the NetBeans menu bar. This will create server.jar in the dist directory. That file is required for the deployment.

NetBeans Project (tools)

In the NetBeans IDE, click New Project, choose Java and Java Application. Click Next, then give the Project Name as tools. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\tools with DEVPATH being your chosen location. Check that Create Main Class is ticked, and the class is tools.GenerateKeys before clicking 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.

Right click the Libraries branch of the server Project tree, and select Add Project. Make sure you browse to the correct file location and select the marauroa project that you created earlier. This will include your marauroa library that you built.

GenerateKeys.java

Expand the Marauroa source file you have downloaded. Navigate to the src\marauroa\tools directory and open the GenerateKeys.java file there. Copy the contents of that into the NetBeans GenerateKeys.java file so that everything is replaced. Locate the package line, and change this from package marauroa.tools; to package tools;

Save the file, then select Run, Run Main Project from the top menu in NetBeans. Click in the Output window, and accept the suggest value of 512 (i.e. just press Return). You get:

run:
# How long should the key be? [512]: 

# Using key of 512 bits.
# Please wait while the key is generated.
# Moving your mouse around to generate randomness may speed up the process.

# Encryption key
n = 12345...
e = 15
d = 12345...
BUILD SUCCESSFUL (total time: 39 seconds)

where there are two long lines of numbers (shown above as 12345...). You need to copy those three number lines, and the comment line above them, for the server.ini file (see below).

server.ini

In the server Project tree, right click on the Source Packages, server branch and select New, Other, Other, Empty file. Give the name as server.ini and click Finish. A new empty file named server.ini is created. Paste the four lines from the output of GenerateKeys into this file.

Copy the following code into the server.ini window (note that the code below includes the Keys lines; don't copy them).

# Database information
database_adapter=marauroa.server.db.adapter.H2DatabaseAdapter
jdbc_url=jdbc:h2:h2db;AUTO_RECONNECT=TRUE;DB_CLOSE_ON_EXIT=FALSE
jdbc_class=org.h2.Driver

tcp_port=5555
turn_length=300
statistics_filename=server_stats.xml
log4j_url=log4j.properties

# World and RP configuration
world=server.World
ruleprocessor=server.Rule

# Server information
server_typeGame=Chat
server_name=Chat
server_version=0.1
server_contact=Hello

# Encryption key
n = 12345...
e = 15
d = 12345...

server.ini configuration lines

Several lines can be changed to use your own values. Make sure you do this correctly. Leave the port, turn length, statistics and log4j configuration as they are.

Database location

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 [1] for the technical details.

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.

Game Information

Use the four lines to specify your game name, version and message.

Log file configuration

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.

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, Console, File

# Paste all logger entries with a threshold of WARN to the console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%-4r %-5p %c %x - %m%n

log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=log/server.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Disabled debug messages... to avoid too many logs
log4j.logger.marauroa.server.game.RPServerManager=WARN

Completion

See the Deployment instructions for which files you will need for your game.