In [GameName]Extension create a package domain.domainname.gamename.models and inside add a new class called [GameName]Character.java. Inside this class we will create a property that is our entity and a constructor that takes the entity as a parameter:
public domain.domainname.gamename.entities.[GameName]Character character;
public [GameName]Character(domain.domainname.gamename.entities.[GameName]Character character)
{
this.character = character;
}
After that we are going to add 2 helper functions. These functions will help us by making SFSObjects from our class and turning SFSObjects back into our class so that we can quickly serialize and deserialize our objects:
public void loadFromSFSObject(ISFSObject data)
{
}
public ISFSObject createSFSObject()
{
ISFSObject data = new SFSObject();
data.putUtfString("name", character.getName());
data.putInt("level", character.getLevel().intValue());
return data;
}
Now that we have our class set up. It is time to create our handler and add it to our list of handlers. The handler class is as follows:
package com.cjrgaming.aegisborn.handlers;
import domain.domainname.gamename.entities.[GameName]Character;
import domain.domainname.gamename.simulation.[GameName]Account;
import domain.domainname.gamename.simulation.World;
import domain.domainname.gamename.util.RoomHelper;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.crypto.Cipher;
/**
*
* @author richach7
*/
public class CharacterListHandler extends BaseClientRequestHandler {
@Override
public void handleClientRequest(User u, ISFSObject data) {
try {
trace("got character list request");
World world = RoomHelper.getWorld(this);
[GameName]Account player = world.getPlayer(u);
trace("loading properties");
Properties serverProps = new Properties();
try
{
InputStream in = this.getClass().getResourceAsStream("/com/cjrgaming/aegisborn/ServerConfig.properties");
serverProps.load(in);
}
catch(IOException e)
{
trace(e);
}
trace("loaded properties");
ISFSObject outputObject = new SFSObject();
outputObject.putInt("maxCharacters", Integer.parseInt(serverProps.getProperty("MaxCharacters", "0")));
ISFSObject characterList = new SFSObject();
int i = 0;
trace("loading characters");
Cipher cipher = player.GetCipher();
for([GameName]Character character : player.getGuardUser().get[GameName]CharacterCollection())
{
domain.domainname.gamename.models.[GameName]Character ABCharacter = new domain.domainname.gamename.models.[GameName]Character(character);
trace("outputting character"+i);
characterList.putSFSObject("character"+i++, ABCharacter.createSFSObject());
}
outputObject.putSFSObject("characters", characterList);
send("characterlist", outputObject, u);
} catch (Exception e) {
trace(e);
}
}
}
With this in place we can add it to our list of handlers by going into [GameName]Extension and adding the following line:
addRequestHandler("getCharacters", CharacterListHandler.class);
With this in place, we are ready to add the code to Unity3d to ask the server for our list of characters. We will cover this in part 3.
As an aside, if you wish to follow along with this project as it is being worked on, I have added it to GitHub under an open source repository. Rather than charge people for all the code for this series, I've decided that if you like it, you can donate to me. Otherwise, why teach. GitHub Repository
No comments:
Post a Comment