I have a project that is multiplayer and uses SmartFoxServer. I am writing a server extension but not sure what the best practice is. I am seeing some odd behavior and I am not sure if this is due to how I have my classes set up. In my extension, I have a main class like this:
public class MyExtension extends SFSExtension
{
private int numberOfPlayersReady = 0;
@Override
public void init()
{
addRequestHandler("dealingHand",MoveHandler.class );
}
//...some code
public int getNumberOfPlayersReady(){
return numberOfPlayersReady;
}
void setNumberOfPlayersReady(){
numberOfPlayersReady++;
}
}
MoveHandler.java:
@Instantiation(InstantiationMode.SINGLE_INSTANCE)
public class MoveHandler extends BaseClientRequestHandler{
@Override
public void handleClientRequest(User user, ISFSObject params)
{
MyExtension gameExt = (MyExtension) getParentExtension();
int currentNumber = gameExt.getNumberOfPlayersReady();
trace("another player is ready " + currentNumber);
if(currentNumber == 3){
//do something
}else{
int newCurrentNumber = (int)currentNumber + 1;
gameExt.setNumberOfPlayersReady();
}
}
}
Then my server extension code is called in my Unity C# code like this:
SFSObject obj = new SFSObject();
obj.PutInt("playerCheckingInID", sfs.MySelf.Id);
sfs.Send(new ExtensionRequest("dealingHand", obj, sfs.LastJoinedRoom));
But, when I look at my SmartFoxServer standalone command line, to view the trace, I see:
"another player is ready 0"
"another player is ready 0"
"another player is ready 0"
"another player is ready 3"
I would imagine I would see:
"another player is ready 0"
"another player is ready 1"
"another player is ready 2"
"another player is ready 3"
Can anyone let me know whats going on here? Does this have to do with using a Single_Instance class?
Edit: Or, does this have to do with synchronizing threads? If so, what is the best way to manage that? This is multiplayer, turn based game. There will always be four players. Requests sent to the Server Extension should be managed in the order they are received.