Marauroa Chat Tutorial: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
imported>Ufizavipupu No edit summary |
imported>Blacklads Undo revision 11675 by Ufizavipupu (Talk) |
||
| Line 1: | 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 Top|Using}} |
||
{{Navigation for Marauroa Users}} |
{{Navigation for Marauroa Users}} |
||
| Line 27: | Line 19: | ||
We will start with the following implementation of the RPWorld |
We will start with the following implementation of the RPWorld |
||
<source lang="java"> |
|||
import marauroa.server.game.rp.RPWorld; |
import marauroa.server.game.rp.RPWorld; |
||
import marauroa.server.game.rp.MarauroaRPZone; |
import marauroa.server.game.rp.MarauroaRPZone; |
||
| Line 43: | Line 35: | ||
public void onInit() { |
public void onInit() { |
||
super.onInit(); |
super.onInit(); |
||
MarauroaRPZone zone = new MarauroaRPZone( |
MarauroaRPZone zone = new MarauroaRPZone("lobby"); |
||
addRPZone(zone); |
addRPZone(zone); |
||
} |
} |
||
} |
} |
||
</source> |
|||
Don't forget to always implement the static get method which is used by Marauroa framework to retrieve an instance of your class. |
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 |
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 "lobby"). 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 |
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. --> |
|||
<source lang="java"> |
|||
import java.util.List; |
import java.util.List; |
||
import java.sql.SQLException; |
import java.sql.SQLException; |
||
| Line 95: | Line 87: | ||
public boolean checkGameVersion(String game, String version) { |
public boolean checkGameVersion(String game, String version) { |
||
return game.equals( |
return game.equals("Chat"); |
||
} |
} |
||
| Line 108: | Line 100: | ||
public synchronized boolean onInit(RPObject object) { |
public synchronized boolean onInit(RPObject object) { |
||
IRPZone zone = world.getRPZone(new IRPZone.ID( |
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby")); |
||
zone.add(object); |
zone.add(object); |
||
return true; |
return true; |
||
| Line 116: | Line 108: | ||
} |
} |
||
public boolean onActionAdd(RPObject caster, RPAction action, List |
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) { |
||
return true; |
return true; |
||
} |
} |
||
| Line 124: | Line 116: | ||
public void execute(RPObject caster, RPAction action) { |
public void execute(RPObject caster, RPAction action) { |
||
if (action.get( |
if (action.get("type").equals("chat")) { |
||
RPObject chat_entry = new RPObject(); |
RPObject chat_entry = new RPObject(); |
||
chat_entry.put( |
chat_entry.put("text", action.get("text")); |
||
chat_entry.put( |
chat_entry.put("from", caster.get("nick")); |
||
chat_entry.put( |
chat_entry.put("turn", manager.getTurn()); |
||
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID())); |
IRPZone zone = world.getRPZone(new IRPZone.ID(caster.getID().getZoneID())); |
||
zone.assignRPObjectID(chat_entry); |
zone.assignRPObjectID(chat_entry); |
||
| Line 161: | Line 153: | ||
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template); |
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, template); |
||
} |
} |
||
IRPZone zone = world.getRPZone(new IRPZone.ID( |
IRPZone zone = world.getRPZone(new IRPZone.ID("lobby")); |
||
RPObject object = new RPObject(template); |
RPObject object = new RPObject(template); |
||
object.put( |
object.put("nick", character); |
||
zone.assignRPObjectID(object); |
zone.assignRPObjectID(object); |
||
characterDAO.addCharacter(trans, username, character, object); |
characterDAO.addCharacter(trans, username, character, object); |
||
| Line 175: | Line 167: | ||
} |
} |
||
} |
} |
||
</source> |
|||
We again implement the static get() method which is used by Marauroa to retrieve the RuleProcessor instance. |
We again implement the static get() method which is used by Marauroa to retrieve the RuleProcessor instance. |
||
| Line 185: | Line 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. |
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 |
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 "chat" 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 |
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 === |
||
| Line 193: | Line 185: | ||
You can compile them using command |
You can compile them using command |
||
<pre> |
|||
javac -cp marauroa.jar;log4j.jar;. *.java |
javac -cp marauroa.jar;log4j.jar;. *.java |
||
</pre> |
|||
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. |
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 |
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 |
||
<pre> |
|||
java -cp marauroa.jar marauroa.test.GenerateINI |
java -cp marauroa.jar marauroa.test.GenerateINI |
||
</pre> |
|||
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 |
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 |
||
<pre> |
|||
world=World |
world=World |
||
ruleprocessor=Rule |
ruleprocessor=Rule |
||
</pre> |
|||
Now you are all set to get your server up and running. To start the server I usually use the following command |
Now you are all set to get your server up and running. To start the server I usually use the following command |
||
<pre> |
|||
java -cp marauroa.jar;mysql-connector.jar;log4j.jar;. marauroa.server.marauroad -c server.ini |
java -cp marauroa.jar;mysql-connector.jar;log4j.jar;. marauroa.server.marauroad -c server.ini |
||
</pre> |
|||
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. |
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: | Line 211: | ||
=== Code === |
=== 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 |
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 |
||
<source lang="java"> |
|||
import java.util.Map; |
import java.util.Map; |
||
import java.util.List; |
import java.util.List; |
||
| Line 239: | Line 231: | ||
private PerceptionHandler handler; |
private PerceptionHandler handler; |
||
protected static Client client; |
protected static Client client; |
||
private Map |
private Map<RPObject.ID, RPObject> world_objects; |
||
private String[] available_characters; |
private String[] available_characters; |
||
private List |
private List<String> quotes = new ArrayList<String>(); |
||
public static Client get() { |
public static Client get() { |
||
| Line 251: | Line 243: | ||
protected Client() { |
protected Client() { |
||
super( |
super("log4j.properties"); |
||
world_objects = new HashMap |
world_objects = new HashMap<RPObject.ID, RPObject>(); |
||
handler = new PerceptionHandler(new PerceptionListener()); |
handler = new PerceptionHandler(new PerceptionListener()); |
||
} |
} |
||
| Line 272: | Line 264: | ||
RPAction action; |
RPAction action; |
||
action = new RPAction(); |
action = new RPAction(); |
||
action.put( |
action.put("type", "chat"); |
||
action.put( |
action.put("text", text); |
||
send(action); |
send(action); |
||
} |
} |
||
| Line 285: | Line 277: | ||
} |
} |
||
protected List |
protected List<TransferContent> onTransferREQ(List<TransferContent> items) { |
||
return items; |
return items; |
||
} |
} |
||
protected void onTransfer(List |
protected void onTransfer(List<TransferContent> items) { |
||
} |
} |
||
| Line 303: | Line 295: | ||
protected String getGameName() { |
protected String getGameName() { |
||
return |
return "Chat"; |
||
} |
} |
||
protected String getVersionNumber() { |
protected String getVersionNumber() { |
||
return |
return "0.5"; |
||
} |
} |
||
protected void onPreviousLogins(List |
protected void onPreviousLogins(List<String> previousLogins) { |
||
} |
} |
||
class PerceptionListener implements IPerceptionListener { |
class PerceptionListener implements IPerceptionListener { |
||
public boolean onAdded(RPObject object) { |
public boolean onAdded(RPObject object) { |
||
if (object.has( |
if (object.has("text")) { |
||
quotes.add( |
quotes.add("*" + object.get("from") + "* : " + object.get("text")); |
||
} |
} |
||
return false; |
return false; |
||
| Line 359: | Line 351: | ||
} |
} |
||
} |
} |
||
</source> |
|||
This is mostly a boilerplate code. We claim that we are |
This is mostly a boilerplate code. We claim that we are "Chat" client, version "0.5" As you remember, our server will accept any "Chat" 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. |
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: | Line 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 |
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 |
||
<source lang="java"> |
|||
import java.io.IOException; |
import java.io.IOException; |
||
import java.lang.Thread; |
import java.lang.Thread; |
||
| Line 385: | Line 377: | ||
Client my = Client.get(); |
Client my = Client.get(); |
||
try { |
try { |
||
my.connect( |
my.connect("localhost", 555); |
||
if (args.length == 3) { |
if (args.length == 3) { |
||
my.createAccount(args[0], args[1], args[2]); |
my.createAccount(args[0], args[1], args[2]); |
||
| Line 403: | Line 395: | ||
my.loop(0); |
my.loop(0); |
||
if (i % 100 == 50) { |
if (i % 100 == 50) { |
||
my.SendMessage( |
my.SendMessage("test" + i); |
||
} |
} |
||
String s = my.popQuote(); |
String s = my.popQuote(); |
||
| Line 418: | Line 410: | ||
} |
} |
||
} |
} |
||
</source> |
|||
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. |
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: | Line 419: | ||
=== Deployment === |
=== 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 |
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 |
||
<pre> |
|||
javac -cp marauroa.jar;log4j.jar;. *.java |
javac -cp marauroa.jar;log4j.jar;. *.java |
||
</pre> |
|||
Make sure that required jars are in the current directory. To run use |
Make sure that required jars are in the current directory. To run use |
||
<pre> |
|||
java -cp marauroa.jar;log4j.jar;. Test login password |
java -cp marauroa.jar;log4j.jar;. Test login password |
||
</pre> |
|||
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). |
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 === |
=== Output === |
||
Ideally you should see something like |
Ideally you should see something like |
||
<pre> |
|||
>java -cp marauroa.jar;log4j.jar;. Test test1 test1 |
|||
Cannot find log4j.properties in classpath. Using default properties. |
Cannot find log4j.properties in classpath. Using default properties. |
||
0.5 |
0.5 |
||
| Line 448: | Line 440: | ||
*test1* : test50 |
*test1* : test50 |
||
*test1* : test150 |
*test1* : test150 |
||
</pre> |
|||
Note the message about log4j.properties: you can create that file in order to configure the Log4J usage inside Marauroa framework. |
Note the message about log4j.properties: you can create that file in order to configure the Log4J usage inside Marauroa framework. |
||
| Line 454: | Line 446: | ||
=== Code === |
=== 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 |
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 |
||
<source lang="java"> |
|||
import javax.swing.*; |
import javax.swing.*; |
||
import java.awt.event.*; |
import java.awt.event.*; |
||
| Line 478: | Line 470: | ||
public void actionPerformed(ActionEvent e) { |
public void actionPerformed(ActionEvent e) { |
||
String message = jInputField.getText(); |
String message = jInputField.getText(); |
||
jInputField.setText( |
jInputField.setText(""); |
||
client.SendMessage(message); |
client.SendMessage(message); |
||
} |
} |
||
| Line 487: | Line 479: | ||
try { |
try { |
||
client = Client.get(); |
client = Client.get(); |
||
client.connect( |
client.connect("127.0.0.1", 555); |
||
client.login( |
client.login("test1", "test1"); |
||
client.chooseCharacter( |
client.chooseCharacter("test1"); |
||
jSayButton.setEnabled(true); |
jSayButton.setEnabled(true); |
||
jInputField.setEnabled(true); |
jInputField.setEnabled(true); |
||
} catch (Exception exception) { |
} catch (Exception exception) { |
||
JOptionPane.showMessageDialog( |
JOptionPane.showMessageDialog( |
||
View.this, |
View.this, "Error", exception.toString(), JOptionPane.WARNING_MESSAGE); |
||
client = null; |
client = null; |
||
} |
} |
||
| Line 510: | Line 502: | ||
String s = client.popQuote(); |
String s = client.popQuote(); |
||
while (s != null) { |
while (s != null) { |
||
jChatArea.append(s + |
jChatArea.append(s + "\n"); |
||
s = client.popQuote(); |
s = client.popQuote(); |
||
} |
} |
||
| Line 524: | Line 516: | ||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
||
setTitle( |
setTitle("Chat 0.5"); |
||
setName( |
setName("main"); |
||
jChatArea.setColumns(20); |
jChatArea.setColumns(20); |
||
jChatArea.setEditable(false); |
jChatArea.setEditable(false); |
||
| Line 531: | Line 523: | ||
jScrollPane1.setViewportView(jChatArea); |
jScrollPane1.setViewportView(jChatArea); |
||
jSayButton.setText( |
jSayButton.setText("Say"); |
||
jSayButton.setEnabled(false); |
jSayButton.setEnabled(false); |
||
jConnectButton.setText( |
jConnectButton.setText("Connect"); |
||
jInputField.setEnabled(false); |
jInputField.setEnabled(false); |
||
| Line 569: | Line 561: | ||
} |
} |
||
} |
} |
||
</source> |
|||
To create the layout of the elements NetBeans was used, but we concentrate just on the source code here. |
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 |
The client establishes a connection to a hard-coded location upon Connect button is pressed. There is no "Create new account" 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. |
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 |
The only thing left for the Swing client now is a main method. Here is the source code |
||
<source lang="java"> |
|||
final class Chat { |
final class Chat { |
||
private Chat() {} |
private Chat() {} |
||
| Line 586: | Line 578: | ||
} |
} |
||
} |
} |
||
</source> |
|||
=== Deployment === |
=== Deployment === |
||
You still use the same command for compilation |
You still use the same command for compilation |
||
<pre> |
|||
javac -cp marauroa.jar;log4j.jar;. *.java |
javac -cp marauroa.jar;log4j.jar;. *.java |
||
</pre> |
|||
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 |
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 |
||
<pre> |
|||
java -cp marauroa.jar;log4j.jar;. Chat |
java -cp marauroa.jar;log4j.jar;. Chat |
||
</pre> |
|||
== TODO == |
== 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.}} |
{{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.}} |
||