Help about create bots,countdown timer before matchmaking

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
esondinh
Posts: 5
Joined: 28 May 2021, 06:37

Help about create bots,countdown timer before matchmaking

Postby esondinh » 29 Jun 2021, 03:40

Hi everyone,
I have a problem and i need someone can help me.
I have a game in which I need some bots add to room,
The problem is,when i used to matchmaking expression to find players
and this is code block i used to create room:

Code: Select all

public class StartGameHandler extends BaseClientRequestHandler
{

@Override
public void handleClientRequest(User player, ISFSObject params) {

        Racing3dExtension parentExt = (Racing3dExtension) getParentExtension();   
        parentExt.trace("Matchmaking");
     // MatchExpression retrieve all the Game Rooms still waiting for players to join them
          MatchExpression exp = new MatchExpression(RoomProperties.IS_GAME, BoolMatch.EQUALS, true)
          .and(RoomProperties.HAS_FREE_PLAYER_SLOTS, BoolMatch.EQUALS, true)
          .and("isGameStarted", BoolMatch.EQUALS, false);

        // Search Rooms from MatchExpression on above
        List<Room> availableRooms = parentExt.getApi().findRooms(parentExt.getParentZone().getRoomList(), exp, 0);
        if (availableRooms.size() > 0) {
            Room room = (Room) availableRooms.get(0);
            parentExt.trace("Room found:" + room.getName());
            int totalUserList = room.getUserList().size(); //Get total number of user joining in the game
            int totalMaxUsers = room.getMaxUsers();       // Check total Max Users allow to play game in the room
            try {
               
                if (totalUserList < totalMaxUsers) // if the room is not full,add player to room
                {
                    room.addUser(player);
                }
                //Refresh size() of totalUserList again
                totalUserList = room.getUserList().size();
                if (totalUserList == totalMaxUsers)  //if the room is full,stop add player to room and will start game
                {
                    CreateRoomSettings settings = new CreateRoomSettings();
                    settings.setName("TestRoom");
                    settings.setMaxUsers(5);
                    settings.setMaxSpectators(10);
                    settings.setExtension(new CreateRoomSettings.RoomExtensionSettings("RacingDemo", "com.LExtension.RaceExtension"));
                    // settings.setExtension(new RoomExtensionSettings("3dRacingDemo", "com.LExtension.Racing3dExtension"));
                    settings.setAutoRemoveMode(SFSRoomRemoveMode.DEFAULT);
                    try
                    {
                        //Create room
                        parentExt.getParentZone().createRoom(settings);
                       
                    } catch (SFSCreateRoomException e)
                    {
                        e.printStackTrace();
                    }                   
                                 
                     parentExt.trace("Room members count after join:" + room.getUserList().size());
                     ISFSObject obj = new SFSObject();
                     obj.putUtfString("status", "Game Starting in... ");
     
                } else
                {
                    ISFSObject obj = new SFSObject();
                    obj.putUtfString("status", "need more than one Player to start");
                }

            } catch (Exception e) {
                trace(ExtensionLogLevel.WARN, "Message Failed : " + e.toString());
            }
        }
    }

}



If after the process does not find matching players in room
and there are insufficient players in the room I will be adding bots to the room and game will start.

Matching requirements are as follows:

1.Player starts with 1000 points

2.At each rank level, players are only allowed to use the required Vehicle Class as shown in the image link below.

https://ibb.co/c3VLBGK

How can i do it? Or does anyone have a sample code to handle this, if so, can you share it with me?
Thank you very much.
Attachments
2021-06-29_9-54-36.png
Help about create bots,countdown timer before matchmaking
(6.7 KiB) Not downloaded yet
Best regards,
sakura (Mr.)
Email : esondinh@gmail.com
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Help about create bots,countdown timer before matchmaking

Postby Lapo » 29 Jun 2021, 09:50

Hi,
for starters I noticed an issue in your code. Doing this is incorrect:

Code: Select all

 room.addUser(player);

This code adds the player to the Room but it doesn't generate any events or updates for other players.

Instead you should always look into the server main API object. In this case it would be:

Code: Select all

getApi().joinRoom(player, room);


Make sure to consult the docs for more details:
http://docs2x.smartfoxserver.com/Extens ... ension-api

As regards NPCs we two in-depth articles on this subject that should help you:
https://smartfoxserver.com/blog/ways-of ... -game-p-1/
https://smartfoxserver.com/blog/ways-of ... -game-p-2/

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
esondinh
Posts: 5
Joined: 28 May 2021, 06:37

Re: Help about create bots,countdown timer before matchmaking

Postby esondinh » 30 Jun 2021, 02:25

Ok,i got it.
Thank you a lot for your help,Lapo.
I have another question,could you explain it to me about MatchExpression in sfs2x.

Example :

Code: Select all

 MatchExpression exp = new MatchExpression(RoomProperties.IS_GAME, BoolMatch.EQUALS, true)
              .and(RoomProperties.HAS_FREE_PLAYER_SLOTS, BoolMatch.EQUALS, true)
             .and("CLASS=D", BoolMatch.EQUALS, false)
             .and("Rank=Bronze", BoolMatch.EQUALS, false)
             .and("Points=1000", BoolMatch.EQUALS, false);
 

Before the match starts, I need to create a MatchExpression to find players, the match making requirement is:
1.Player starts with 1000 points
2.At each rank level, players are only allowed to use the required Vehicle Class as shown in the image link below.

https://ibb.co/c3VLBGK

What expression should I write correctly?
Best regards,
sakura (Mr.)
Email : esondinh@gmail.com
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Help about create bots,countdown timer before matchmaking

Postby Lapo » 30 Jun 2021, 07:19

Hi,
the match expression is incorrect.
I would recommend checking the documentation page to better understand how Match Expressions work:
http://docs2x.smartfoxserver.com/Advanc ... i#matchExp

Also see this article with more examples:
https://smartfoxserver.com/blog/the-pow ... pressions/

What expression should I write correctly?
There's a problem with comparing vehicle classes. They are expressed as characters which are easy to compare by human but trickier to express as conditionals in code.

In particular MatchExpressions don't support comparing characters. so you won't be able to check if 'B' >= A or something like that. These comparisons can only be done with numbers, not strings.
I'd recommend converting these characters to numeric values, if you need to compare them via MatchExpressions.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 37 guests