Unit Persistence outside of user's sessions

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

Moderators: Lapo, Bax

PanagiotisMilios
Posts: 7
Joined: 22 Jan 2023, 21:48

Unit Persistence outside of user's sessions

Postby PanagiotisMilios » 24 Jan 2023, 12:32

Hello,

I have a question with regards to object ownership & persistence. My use-case follows the RTS logic where a player owns multiple units and beyond that they must also always exist outside of user's sessions. They should also be owned by a specific-user so other players can't use them.

How can i achieve that with Smartfox?
PanagiotisMilios
Posts: 7
Joined: 22 Jan 2023, 21:48

Re: Unit Persistence outside of user's sessions

Postby PanagiotisMilios » 24 Jan 2023, 23:40

Also, a follow up question. I'm looking at custom extensions and i'm not sure how can i implement async calls to external services (like using REST). I haven't found any examples on that, so i'd be grateful for any help at this.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unit Persistence outside of user's sessions

Postby Lapo » 25 Jan 2023, 08:44

Hi,
as regards object persistence across multiple sessions, it is something that should be implemented via Extension and database, based on the requirements of your game.
Example: in the game players can create new units that persist across sessions until destroyed in battle. When a new unit is created it is added into a "units" DB table which is linked to the primary player's DB record. When a unit is destroyed in battle the unit is also removed from the DB table, etc...

I'm looking at custom extensions and i'm not sure how can i implement async calls to external services (like using REST).

Essentially you need to perform HTTP calls to your services, which is pretty simple in Java.
If you use Java 11 you can leverage the java.net.http package from the JDK. Examples here:
https://mkyong.com/java/java-11-httpclient-examples/

Otherwise if you're using Java 8 you can use the Apache HTTPClient libs which are already provided with SFS2X.
Examples here: https://smartfoxserver.com/blog/integra ... xtensions/

Hoep it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
PanagiotisMilios
Posts: 7
Joined: 22 Jan 2023, 21:48

Re: Unit Persistence outside of user's sessions

Postby PanagiotisMilios » 25 Jan 2023, 10:25

Lapo wrote:Hi,
as regards object persistence across multiple sessions, it is something that should be implemented via Extension and database, based on the requirements of your game.
Example: in the game players can create new units that persist across sessions until destroyed in battle. When a new unit is created it is added into a "units" DB table which is linked to the primary player's DB record. When a unit is destroyed in battle the unit is also removed from the DB table, etc...

I'm looking at custom extensions and i'm not sure how can i implement async calls to external services (like using REST).

Essentially you need to perform HTTP calls to your services, which is pretty simple in Java.
If you use Java 11 you can leverage the java.net.http package from the JDK. Examples here:
https://mkyong.com/java/java-11-httpclient-examples/

Otherwise if you're using Java 8 you can use the Apache HTTPClient libs which are already provided with SFS2X.
Examples here: https://smartfoxserver.com/blog/integra ... xtensions/

Hoep it helps


Thanks. The last link was very helpful. My biggest problem was that I thought that the http calls would run asynchronously (like c# callbacks).

Now concerning object persistence, as far as i understand the server syncs to players server variables. I need units to be synced across players in a room or in a zone. Even if i have the game state in the database, how will the server know to sync it to the players.

PS: i'm using MongoDB for my external db. the article to connect to external databases didn't mention MongoDB but i'm assuming the process is the same.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unit Persistence outside of user's sessions

Postby Lapo » 25 Jan 2023, 15:24

My biggest problem was that I thought that the http calls would run asynchronously (like c# callbacks).

You can do both, but default is synchronous and we recommend it as the server already handles the multi-threading behind it.

I need units to be synced across players in a room or in a zone.

In a Room yes. In Zone I would say no. Why? Because you potentially have 1000s of players in the whole Zone and syncing every object with everyone else is too expensive in terms of network usage.

If your game is an MMO-like game then you should check MMORooms which optimize the syncing with an area-of-Interest.
http://docs2x.smartfoxserver.com/Advanc ... /mmo-rooms

Even if i have the game state in the database, how will the server know to sync it to the players.

Supposing you're using User Variables to keep the state I would suggest something like this:
- Player logs in
- Extension manages the login event and checks the DB
- Populate the necessary User Vars from the DB
- Player joins game Room... all other clients get updated
- Game goes on for a while...
- User logs out or disconnects
- Extension handles the event and stores the User Vars to the DB

PS: i'm using MongoDB for my external db. the article to connect to external databases didn't mention MongoDB but i'm assuming the process is the same.

It's probably going to be different because MongoDB is not a relational database.
Our DBManager API works with all kinds of relations dbs (mysql, postgre, mssql etc...) but for NoSQL databases you will need to consul their documentation.

I think you will need to add one or more jar files provided by Mongo and import them in your Extension code.
You won't be able to configure the database from our AdminTool but rather through the API provided by MongoDB.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Google [Bot] and 41 guests