Send() to NPC user

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

Moderators: Lapo, Bax

anniyan137
Posts: 25
Joined: 20 Nov 2012, 10:49

Send() to NPC user

Postby anniyan137 » 28 Feb 2013, 05:39

I am working on an NPC player that plays against a user in a card game. While I am sending the turn specific information to all the users joined in a particular room, that also includes the NPC right? So when I do this:

Code: Select all

send("cmd", data, getParentRoom().getPlayersList());


What exactly happens? I know that the players receive the message. But since there is no TCP connection for the NPC, how does the send() function behave?

Edit: Also, can anyone post a link to where I can find a list of events that and NPC user triggers?

Thanks!
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send() to NPC user

Postby Lapo » 28 Feb 2013, 09:05

The send(...) method actually sends the message only to Users that have a "real" connection to the server, whether it is socket, websocket or BlueBox.

So NPC, technically will not receive anything, because they already reside on the server side.
Because of this you can easily update them via a separate method call. In other words: since NPC are managed by the Extension itself all you need to do is calling a custom method that notifies the NPC logic the the game state has been updated.

There are dozens of ways to do it, the first the comes to mind is to create an NPCManager class that handles the NPC A.I. and which you can pass updates to when a new move has been done.

hope it's clear
Lapo
--
gotoAndPlay()
...addicted to flash games
anniyan137
Posts: 25
Joined: 20 Nov 2012, 10:49

Re: Send() to NPC user

Postby anniyan137 » 01 Mar 2013, 07:13

Thanks for the fast reply. :)

I am already using an AiManager to manage creation and interactions with game logic. But I was wondering whether there will be any negative effect by including the NPC users in the playerList argument of the send() method. I thought I had to run a loop to filter out the NPC players before sending the client response.

On a related note, is the disconnectUser() method enough to remove the NPC when it is no longer required?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send() to NPC user

Postby Lapo » 01 Mar 2013, 08:54

No, you should not worry about the NPCs in the list of recipients, they will be skipped.

On a related note, is the disconnectUser() method enough to remove the NPC when it is no longer required?

Yes, it is enough.
That, and releasing the reference to the User object in your code.
Lapo

--

gotoAndPlay()

...addicted to flash games
anniyan137
Posts: 25
Joined: 20 Nov 2012, 10:49

Re: Send() to NPC user

Postby anniyan137 » 05 Mar 2013, 07:27

Lapo wrote:Yes, it is enough.
That, and releasing the reference to the User object in your code.


Hi Lapo, I have successfully made the NPC play with the players. However, Disconnecting the NPC user and setting other references to null are not working.

PS : I am using the SFSApi to disconnect the user. I even tried with the npcUser.disconnect() method too. But neither of them seem to work.
Here is the function that is called to "kill" the NPC.

Code: Select all

public void killNPC()
{
   //npcUser.disconnect(ClientDisconnectionReason.KICK);
   gExt.getApi().disconnectUser(npcUser, ClientDisconnectionReason.KICK);
   npcUser = null; // Reference to the npcUser stored in the AiManager
   gExt.trace("[AI] NPC killed"); // This is being printed. But user still exists in the room when checked through the admin console
}
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send() to NPC user

Postby Lapo » 05 Mar 2013, 09:41

I am using this one:

Code: Select all

getApi().disconnectUser(npcUser);

and it works correctly. Give it a try. :)

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
anniyan137
Posts: 25
Joined: 20 Nov 2012, 10:49

Re: Send() to NPC user

Postby anniyan137 » 05 Mar 2013, 11:41

Thanks Lapo! :D

Works like a charm. :^_^
anniyan137
Posts: 25
Joined: 20 Nov 2012, 10:49

Re: Send() to NPC user

Postby anniyan137 » 19 Apr 2013, 10:55

Hi Lapo,

I observed some unusual trace messages on my server logs today.

Code: Select all

08:52:28,522 INFO  [pool-2-thread-1] response.SFSResponseApi     - Room Recipients: [{ Id: 17, Type: DEFAULT, Logged: Yes, IP: ##.##.##.##:1197 }, { Id: 18, Type: VOID, Logged: Yes, IP: NO_IP }]


They appear to be occurring whenever a Send() message is sent to a player list with an NPC in it. Should I be worried? :?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send() to NPC user

Postby Lapo » 19 Apr 2013, 11:51

No, you should really not. That's perfectly fine. :)
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 47 guests