Serverside Player Login & Logout event ?

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

Moderators: Lapo, Bax

genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Serverside Player Login & Logout event ?

Postby genar » 26 Jun 2018, 12:39

Hey there !

Well ... my server needs a list of all online users to save their progress ( inventory, stats etc ) into my database.
Its not the best way to do this everytime a variable changes or a pickup event happens... this just overlaods the database.

So what methods are called serverside once a player logs into a zone or enters a room ?
Futhermore i wanna know what methods are called serverside once a player leaves a room or logs out of the zone...

I already have a login handler, that one is called during login, the problem is... that i dont receive any users. The User i gain during this process is just null [SFSEvent.User]...
Is there a way to acess those events ? Or what else could i do ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serverside Player Login & Logout event ?

Postby Lapo » 26 Jun 2018, 14:14

Hello,
genar wrote:Well ... my server needs a list of all online users to save their progress ( inventory, stats etc ) into my database.
Its not the best way to do this everytime a variable changes or a pickup event happens... this just overlaods the database.

This is correct: instead of hitting the database for every change, you should load all the user's items in memory when they login and save all their changes when they leave the server.
In other words, instead of saving every time a variable is updated, you should listen to the USER_LOGOUT and USER_DISCONNECT events and save all those variables at that moment (both events can be handled by the same class/object).

So what methods are called serverside once a player logs into a zone or enters a room ?
Futhermore i wanna know what methods are called serverside once a player leaves a room or logs out of the zone...

I would recommend checking the list of events provided by the SFSEventType class.
http://docs2x.smartfoxserver.com/api-do ... tType.html

I already have a login handler, that one is called during login, the problem is... that i dont receive any users. The User i gain during this process is just null [SFSEvent.User]...
Is there a way to acess those events ? Or what else could i do ?

The login handler uses the SFSEventType.USER_LOGIN which is fired before the client has even logged in, so a User object does not exist yet.

There's another event, SFSEventType.USER_JOIN_ZONE, which is fired after a successful login when the client has joined the Zone.
More about this here:
https://smartfoxserver.com/blog/how-to- ... tom-login/

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Re: Serverside Player Login & Logout event ?

Postby genar » 27 Jun 2018, 17:21

Lapo wrote:Hello,
genar wrote:Well ... my server needs a list of all online users to save their progress ( inventory, stats etc ) into my database.
Its not the best way to do this everytime a variable changes or a pickup event happens... this just overlaods the database.

This is correct: instead of hitting the database for every change, you should load all the user's items in memory when they login and save all their changes when they leave the server.
In other words, instead of saving every time a variable is updated, you should listen to the USER_LOGOUT and USER_DISCONNECT events and save all those variables at that moment (both events can be handled by the same class/object).

So what methods are called serverside once a player logs into a zone or enters a room ?
Futhermore i wanna know what methods are called serverside once a player leaves a room or logs out of the zone...

I would recommend checking the list of events provided by the SFSEventType class.
http://docs2x.smartfoxserver.com/api-do ... tType.html

I already have a login handler, that one is called during login, the problem is... that i dont receive any users. The User i gain during this process is just null [SFSEvent.User]...
Is there a way to acess those events ? Or what else could i do ?

The login handler uses the SFSEventType.USER_LOGIN which is fired before the client has even logged in, so a User object does not exist yet.

There's another event, SFSEventType.USER_JOIN_ZONE, which is fired after a successful login when the client has joined the Zone.
More about this here:
https://smartfoxserver.com/blog/how-to- ... tom-login/

Cheers


Thanks a lot ! That worked great so far.

But one problem is still remaining... during the registration or login of one player ( i mean the registration / login extensions i wrote by myself ), once the SFSEventType.User_Login is called , i check if their name etc are already existing, if not i insert a new row for this player into my database. Because the player table auto increments, i receive back a id for this player. I want to assign this id to the user... when the user login event was called and the database query returned the id. When my user leaves the regristation/login zone and joins another one he should already have this attached ID. Basically i need this id for loading all the player data out of the database.

So how could i do that ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serverside Player Login & Logout event ?

Postby Lapo » 28 Jun 2018, 09:29

Hi,
the User object uses its own auto-increment ID which is only for internal usage, accesible via getId(). You can attach your DB id to the User or Session object via the setProperty/getProperty methods, which allow you to attach any property/object relevant to that client.

When my user leaves the regristation/login zone and joins another one he should already have this attached ID.

If the client moves from one Zone to another his User object will change but his Session object will stay the same, so I recommend attaching the database id to the Session object.

When inside the registration zone you can do this:

Code: Select all

currentUser.getSession().setProperty("dbid", value);


Later in another Zone you can do the following:

Code: Select all

int dbid = (Integer) currentUser.getSession().getProperty("dbid");


Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Re: Serverside Player Login & Logout event ?

Postby genar » 28 Jun 2018, 17:05

Lapo wrote:Hi,
the User object uses its own auto-increment ID which is only for internal usage, accesible via getId(). You can attach your DB id to the User or Session object via the setProperty/getProperty methods, which allow you to attach any property/object relevant to that client.

When my user leaves the regristation/login zone and joins another one he should already have this attached ID.

If the client moves from one Zone to another his User object will change but his Session object will stay the same, so I recommend attaching the database id to the Session object.

When inside the registration zone you can do this:

Code: Select all

currentUser.getSession().setProperty("dbid", value);


Later in another Zone you can do the following:

Code: Select all

int dbid = (Integer) currentUser.getSession().getProperty("dbid");


Cheers



Thats exactly what i wanted ! Thank you so much :D

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 55 guests