Marauroa Chat Tutorial: Difference between revisions

Content deleted Content added
imported>Hendrik Brummermann
No edit summary
imported>Ufizavipupu
No edit summary
Line 1:
----
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
----
=[http://ekygelymib.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
----
=[http://ekygelymib.co.cc CLICK HERE]=
----
</div>
{{Navigation for Marauroa Top|Using}}
{{Navigation for Marauroa Users}}
Line 19 ⟶ 27:
 
We will start with the following implementation of the RPWorld
<&lt;source lang="&quot;java">&quot;&gt;
import marauroa.server.game.rp.RPWorld;
import marauroa.server.game.rp.MarauroaRPZone;
Line 35 ⟶ 43:
public void onInit() {
super.onInit();
MarauroaRPZone zone = new MarauroaRPZone("&quot;lobby"&quot;);
addRPZone(zone);
}
}
<&lt;/source>&gt;
 
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 "&quot;lobby"&quot;). Zone is a general notion of Marauroa. It represents a game area. All clients residing in a certain area receive notification on data changes in this area only. Chat rooms are a good example, as if you are in a chat room you don't see the conversations in other rooms, i.e. you receive notifications on new things in your current room only.
 
Implementing IRPRuleProcessor will require more code, as we must implement all the methods of the interface. We will start with the following stub
<&lt;!-- Updated due to refactoring database access in Marauroa. -->&gt;
<&lt;!-- Please, see details here http://stendhal.game-host.org/wiki/index.php/Refactoring_Database_Access_in_Marauroa. -->&gt;
<&lt;source lang="&quot;java">&quot;&gt;
import java.util.List;
import java.sql.SQLException;
Line 87 ⟶ 95:
 
public boolean checkGameVersion(String game, String version) {
return game.equals("&quot;Chat"&quot;);
}
 
Line 100 ⟶ 108:
 
public synchronized boolean onInit(RPObject object) {
IRPZone zone = world.getRPZone(new IRPZone.ID("&quot;lobby"&quot;));
zone.add(object);
return true;
Line 108 ⟶ 116:
}
 
public boolean onActionAdd(RPObject caster, RPAction action, List<&lt;RPAction>&gt; actionList) {
return true;
}
Line 116 ⟶ 124:
 
public void execute(RPObject caster, RPAction action) {
if (action.get("&quot;type"&quot;).equals("&quot;chat"&quot;)) {
RPObject chat_entry = new RPObject();
chat_entry.put("&quot;text"&quot;, action.get("&quot;text"&quot;));
chat_entry.put("&quot;from"&quot;, caster.get("&quot;nick"&quot;));
chat_entry.put("&quot;turn"&quot;, manager.getTurn());
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID()));
zone.assignRPObjectID(chat_entry);
Line 153 ⟶ 161:
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template);
}
IRPZone zone = world.getRPZone(new IRPZone.ID("&quot;lobby"&quot;));
RPObject object = new RPObject(template);
object.put("&quot;nick"&quot;, character);
zone.assignRPObjectID(object);
characterDAO.addCharacter(trans, username, character, object);
Line 167 ⟶ 175:
}
}
<&lt;/source>&gt;
 
We again implement the static get() method which is used by Marauroa to retrieve the RuleProcessor instance.
Line 177 ⟶ 185:
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 "&quot;chat"&quot; action. As soon as we receive one - a new object is created that represents one chat message. We also set some other properties of the message: the nickname of the person who sent this message, the turn number when the message was sent.
 
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 "&quot;nick"&quot; property (the same as character name) and use the resulting one as an avatar object.
 
=== Deployment ===
Line 185 ⟶ 193:
 
You can compile them using command
<&lt;pre>&gt;
javac -cp marauroa.jar;log4j.jar;. *.java
<&lt;/pre>&gt;
 
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
<&lt;pre>&gt;
java -cp marauroa.jar marauroa.test.GenerateINI
<&lt;/pre>&gt;
 
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
<&lt;pre>&gt;
world=World
ruleprocessor=Rule
<&lt;/pre>&gt;
 
Now you are all set to get your server up and running. To start the server I usually use the following command
<&lt;pre>&gt;
java -cp marauroa.jar;mysql-connector.jar;log4j.jar;. marauroa.server.marauroad -c server.ini
<&lt;/pre>&gt;
 
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 211 ⟶ 219:
=== 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
<&lt;source lang="&quot;java">&quot;&gt;
import java.util.Map;
import java.util.List;
Line 231 ⟶ 239:
private PerceptionHandler handler;
protected static Client client;
private Map<&lt;RPObject.ID, RPObject>&gt; world_objects;
private String[] available_characters;
private List<&lt;String>&gt; quotes = new ArrayList<&lt;String>&gt;();
public static Client get() {
Line 243 ⟶ 251:
 
protected Client() {
super("&quot;log4j.properties"&quot;);
world_objects = new HashMap<&lt;RPObject.ID, RPObject>&gt;();
handler = new PerceptionHandler(new PerceptionListener());
}
Line 264 ⟶ 272:
RPAction action;
action = new RPAction();
action.put("&quot;type"&quot;, "&quot;chat"&quot;);
action.put("&quot;text"&quot;, text);
send(action);
}
Line 277 ⟶ 285:
}
 
protected List<&lt;TransferContent>&gt; onTransferREQ(List<&lt;TransferContent>&gt; items) {
return items;
}
 
protected void onTransfer(List<&lt;TransferContent>&gt; items) {
}
 
Line 295 ⟶ 303:
 
protected String getGameName() {
return "&quot;Chat"&quot;;
}
 
protected String getVersionNumber() {
return "&quot;0.5"&quot;;
}
 
protected void onPreviousLogins(List<&lt;String>&gt; previousLogins) {
}
class PerceptionListener implements IPerceptionListener {
public boolean onAdded(RPObject object) {
if (object.has("&quot;text"&quot;)) {
quotes.add("&quot;*"&quot; + object.get("&quot;from"&quot;) + "&quot;* : "&quot; + object.get("&quot;text"&quot;));
}
return false;
Line 351 ⟶ 359:
}
}
<&lt;/source>&gt;
 
This is mostly a boilerplate code. We claim that we are "&quot;Chat"&quot; client, version "&quot;0.5"&quot; As you remember, our server will accept any "&quot;Chat"&quot; client, without version restrictions.
 
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 364 ⟶ 372:
 
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
<&lt;source lang="&quot;java">&quot;&gt;
import java.io.IOException;
import java.lang.Thread;
Line 377 ⟶ 385:
Client my = Client.get();
try {
my.connect("&quot;localhost"&quot;, 555);
if (args.length == 3) {
my.createAccount(args[0], args[1], args[2]);
Line 395 ⟶ 403:
my.loop(0);
if (i % 100 == 50) {
my.SendMessage("&quot;test"&quot; + i);
}
String s = my.popQuote();
Line 410 ⟶ 418:
}
}
<&lt;/source>&gt;
 
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 419 ⟶ 427:
=== 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
<&lt;pre>&gt;
javac -cp marauroa.jar;log4j.jar;. *.java
<&lt;/pre>&gt;
 
Make sure that required jars are in the current directory. To run use
<&lt;pre>&gt;
java -cp marauroa.jar;log4j.jar;. Test login password
<&lt;/pre>&gt;
 
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
<&lt;pre>&gt;
>&gt;java -cp marauroa.jar;log4j.jar;. Test test1 test1
Cannot find log4j.properties in classpath. Using default properties.
0.5
Line 440 ⟶ 448:
*test1* : test50
*test1* : test150
<&lt;/pre>&gt;
 
Note the message about log4j.properties: you can create that file in order to configure the Log4J usage inside Marauroa framework.
Line 446 ⟶ 454:
=== 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
<&lt;source lang="&quot;java">&quot;&gt;
import javax.swing.*;
import java.awt.event.*;
Line 470 ⟶ 478:
public void actionPerformed(ActionEvent e) {
String message = jInputField.getText();
jInputField.setText(""&quot;&quot;);
client.SendMessage(message);
}
Line 479 ⟶ 487:
try {
client = Client.get();
client.connect("&quot;127.0.0.1"&quot;, 555);
client.login("&quot;test1"&quot;, "&quot;test1"&quot;);
client.chooseCharacter("&quot;test1"&quot;);
jSayButton.setEnabled(true);
jInputField.setEnabled(true);
} catch (Exception exception) {
JOptionPane.showMessageDialog(
View.this, "&quot;Error"&quot;, exception.toString(), JOptionPane.WARNING_MESSAGE);
client = null;
}
Line 502 ⟶ 510:
String s = client.popQuote();
while (s != null) {
jChatArea.append(s + "&quot;\n"&quot;);
s = client.popQuote();
}
Line 516 ⟶ 524:
 
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("&quot;Chat 0.5"&quot;);
setName("&quot;main"&quot;);
jChatArea.setColumns(20);
jChatArea.setEditable(false);
Line 523 ⟶ 531:
jScrollPane1.setViewportView(jChatArea);
 
jSayButton.setText("&quot;Say"&quot;);
jSayButton.setEnabled(false);
 
jConnectButton.setText("&quot;Connect"&quot;);
 
jInputField.setEnabled(false);
Line 561 ⟶ 569:
}
}
<&lt;/source>&gt;
 
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 "&quot;Create new account"&quot; functionality, so account should be created beforehand.
 
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
<&lt;source lang="&quot;java">&quot;&gt;
final class Chat {
private Chat() {}
Line 578 ⟶ 586:
}
}
<&lt;/source>&gt;
=== Deployment ===
You still use the same command for compilation
<&lt;pre>&gt;
javac -cp marauroa.jar;log4j.jar;. *.java
<&lt;/pre>&gt;
 
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
<&lt;pre>&gt;
java -cp marauroa.jar;log4j.jar;. Chat
<&lt;/pre>&gt;
== 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.}}