Marauroa Chat Tutorial: Difference between revisions
Content deleted Content added
imported>Ufizavipupu No edit summary |
imported>Blacklads Undo revision 11675 by Ufizavipupu (Talk) |
||
Line 1:
{{Navigation for Marauroa Top|Using}}
{{Navigation for Marauroa Users}}
Line 27 ⟶ 19:
We will start with the following implementation of the RPWorld
import marauroa.server.game.rp.RPWorld;
import marauroa.server.game.rp.MarauroaRPZone;
Line 43 ⟶ 35:
public void onInit() {
super.onInit();
MarauroaRPZone zone = new MarauroaRPZone(
addRPZone(zone);
}
}
Don't forget to always implement the static get method which is used by Marauroa framework to retrieve an instance of your class.
onInit method is called when world is created. The only thing we do there is creating our zone (as we have a chat application we call it
Implementing IRPRuleProcessor will require more code, as we must implement all the methods of the interface. We will start with the following stub
import java.util.List;
import java.sql.SQLException;
Line 95 ⟶ 87:
public boolean checkGameVersion(String game, String version) {
return game.equals(
}
Line 108 ⟶ 100:
public synchronized boolean onInit(RPObject object) {
IRPZone zone = world.getRPZone(new IRPZone.ID(
zone.add(object);
return true;
Line 116 ⟶ 108:
}
public boolean onActionAdd(RPObject caster, RPAction action, List
return true;
}
Line 124 ⟶ 116:
public void execute(RPObject caster, RPAction action) {
if (action.get(
RPObject chat_entry = new RPObject();
chat_entry.put(
chat_entry.put(
chat_entry.put(
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID()));
zone.assignRPObjectID(chat_entry);
Line 161 ⟶ 153:
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template);
}
IRPZone zone = world.getRPZone(new IRPZone.ID(
RPObject object = new RPObject(template);
object.put(
zone.assignRPObjectID(object);
characterDAO.addCharacter(trans, username, character, object);
Line 175 ⟶ 167:
}
}
We again implement the static get() method which is used by Marauroa to retrieve the RuleProcessor instance.
Line 185 ⟶ 177:
Function onInit() is invoked each time a player logins successfully into the game. His character is loaded from the database. We add the player to the lobby zone immediately, because everybody who connects to the chat gets to the lobby first.
The most important function is execute() which is invoked each time server receives action from one of the clients. In this case we are waiting for a
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
=== Deployment ===
Line 193 ⟶ 185:
You can compile them using command
javac -cp marauroa.jar;log4j.jar;. *.java
This command assumes that you have source files, marauroa.jar and log4j.jar in the same directory. You will receive World.class and Rule.class output files.
In order to run the server you will also need a MySQL database and server.ini file. You can generate server.ini using the built-in class
java -cp marauroa.jar marauroa.test.GenerateINI
Follow the on-screen instructions and you will eventually get the server.ini file. It is a simple text file which you can easily edit. Actually in order to get server running you must modify world and ruleprocessor settings so that they look like
world=World
ruleprocessor=Rule
Now you are all set to get your server up and running. To start the server I usually use the following command
java -cp marauroa.jar;mysql-connector.jar;log4j.jar;. marauroa.server.marauroad -c server.ini
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.
Line 219 ⟶ 211:
=== Code ===
In order to create a client using Marauroa frameword you should extend the marauroa.client.ClientFramework class with your logic. Here is the source code for the chat client
import java.util.Map;
import java.util.List;
Line 239 ⟶ 231:
private PerceptionHandler handler;
protected static Client client;
private Map
private String[] available_characters;
private List
public static Client get() {
Line 251 ⟶ 243:
protected Client() {
super(
world_objects = new HashMap
handler = new PerceptionHandler(new PerceptionListener());
}
Line 272 ⟶ 264:
RPAction action;
action = new RPAction();
action.put(
action.put(
send(action);
}
Line 285 ⟶ 277:
}
protected List
return items;
}
protected void onTransfer(List
}
Line 303 ⟶ 295:
protected String getGameName() {
return
}
protected String getVersionNumber() {
return
}
protected void onPreviousLogins(List
}
class PerceptionListener implements IPerceptionListener {
public boolean onAdded(RPObject object) {
if (object.has(
quotes.add(
}
return false;
Line 359 ⟶ 351:
}
}
This is mostly a boilerplate code. We claim that we are
What you should note is that we use a Marauroa-provided perception handler, see onPerception. Still we introduce our own perception listener to be able to take actions when objects are added, modified, deleted.
Line 372 ⟶ 364:
Marauroa frameword provides the main method for server, so that you don't need to care about execution loop. It is not true for server, so we will also need to implement the main module of our client. Here is a very easy solution
import java.io.IOException;
import java.lang.Thread;
Line 385 ⟶ 377:
Client my = Client.get();
try {
my.connect(
if (args.length == 3) {
my.createAccount(args[0], args[1], args[2]);
Line 403 ⟶ 395:
my.loop(0);
if (i % 100 == 50) {
my.SendMessage(
}
String s = my.popQuote();
Line 418 ⟶ 410:
}
}
Our main method does a couple of things. First of all, we create a new account if three command-line arguments are provided (login, password, email). Otherwise we just login with login and password specified.
Line 427 ⟶ 419:
=== Deployment ===
Running a client is very simple, all you need is a bunch of jars (database is not required on the client side) and compiled client code. I use following line for compilation
javac -cp marauroa.jar;log4j.jar;. *.java
Make sure that required jars are in the current directory. To run use
java -cp marauroa.jar;log4j.jar;. Test login password
Don't forget to replace login and password with the actual ones. To create a new account just add a third command-line parameter (that should be an email, but no validation at the moment).
=== Output ===
Ideally you should see something like
Cannot find log4j.properties in classpath. Using default properties.
0.5
Line 448 ⟶ 440:
*test1* : test50
*test1* : test150
Note the message about log4j.properties: you can create that file in order to configure the Log4J usage inside Marauroa framework.
Line 454 ⟶ 446:
=== Code ===
The design of our Client class allows us to easily use it in something more complex then Test class above. Here is the example of a simple Swing application that uses Client as a communication tool
import javax.swing.*;
import java.awt.event.*;
Line 478 ⟶ 470:
public void actionPerformed(ActionEvent e) {
String message = jInputField.getText();
jInputField.setText(
client.SendMessage(message);
}
Line 487 ⟶ 479:
try {
client = Client.get();
client.connect(
client.login(
client.chooseCharacter(
jSayButton.setEnabled(true);
jInputField.setEnabled(true);
} catch (Exception exception) {
JOptionPane.showMessageDialog(
View.this,
client = null;
}
Line 510 ⟶ 502:
String s = client.popQuote();
while (s != null) {
jChatArea.append(s +
s = client.popQuote();
}
Line 524 ⟶ 516:
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(
setName(
jChatArea.setColumns(20);
jChatArea.setEditable(false);
Line 531 ⟶ 523:
jScrollPane1.setViewportView(jChatArea);
jSayButton.setText(
jSayButton.setEnabled(false);
jConnectButton.setText(
jInputField.setEnabled(false);
Line 569 ⟶ 561:
}
}
To create the layout of the elements NetBeans was used, but we concentrate just on the source code here.
The client establishes a connection to a hard-coded location upon Connect button is pressed. There is no
We use timer to regularly check for the new messages in the Client queue. If there are some - they are put to the large text box. Sending the new message also relies on the Client facilities.
The only thing left for the Swing client now is a main method. Here is the source code
final class Chat {
private Chat() {}
Line 586 ⟶ 578:
}
}
=== Deployment ===
You still use the same command for compilation
javac -cp marauroa.jar;log4j.jar;. *.java
Make sure that source files with code for Swing client are in the same directory with jars mentioned and Client.java file. You can run the client with the following command
java -cp marauroa.jar;log4j.jar;. Chat
== TODO ==
{{TODO|As soon as you finished reading this tutorial you would probably like to make one of this exercises. You are welcome to introduce your results back to the tutorial.}}
| |||