简体   繁体   中英

Unity3d When to use Single_Instance class

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.

If you are going to make a multiplayer game , join each player that connect to sfs to a room , and then check total player that joined in room , when number of them reaches 4 , then leave them from that room , and create another room and put them in that room. Crete a static room for match making then create dynamic rooms for each match. for example , create a room name MatchFindingRoom , then join each player that connects.

        Room matchFindRoom= getParentExtension().getParentZone()
                .getRoomByName("MatchFindingRoom");
        getApi().joinRoom(user, getParentExtension().getParentZone().getRoomByName("MatchFindingRoom"));
        List<User> waiterUsers = matchFindRoom.getUserList();
        if(waiterUsers.size() >= 4)
        {
            CreateRoomSettings crs = new CreateRoomSettings();
            crs.setName("testRoom");
            crs.setMaxUsers(4);
            crs.setMaxVariablesAllowed(50);
            crs.setAutoRemoveMode(SFSRoomRemoveMode.WHEN_EMPTY);
            getApi().createRoom(getParentExtension().getParentZone(), crs,
                    null, false, null, false, false);
            Room matchRoom = getParentExtension().getParentZone().getRoomByName("testRoom");
            getApi().joinRoom(waiterUsers.get(0), matchRoom);
            getApi().joinRoom(waiterUsers.get(1), matchRoom);
            getApi().joinRoom(waiterUsers.get(2), matchRoom);
            getApi().joinRoom(waiterUsers.get(3), matchRoom);
            // do other stuff
        }

when you use room for users , then u don't need treads for tracking them , just get room and get the player inside it , and then do what ever you want. Highly recommended that don't use treads on server , because if every user create one of it , your server cpu can't reach them properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM