Re: Replacing disconnected players with bots

97 views
Skip to first unread message

Drew Cranford

unread,
Apr 6, 2023, 1:51:19 AM4/6/23
to nodeGame
Hi Stefano,
Not trying to rush you or anything, but just wanted to check that you received my previous message about replacing disconnected players with bots.
Thanks,
Drew

--
Edward 'Drew' Cranford
Special Faculty Researcher
Dept of Psychology, ACT-R FMS group
Carnegie Mellon University
cran...@cmu.edu
edward.a...@gmail.com
601-329-6280


On Mon, Mar 27, 2023 at 5:44 PM Drew Cranford <ecra...@andrew.cmu.edu> wrote:
Hi Stefano,
I am trying to figure out how to replace disconnected players with a bot, but I am having difficulty understanding what to do.
The wiki says:
""
Replacing Disconnected Players with Bots.
To replace a disconnected player with a bot refer to the Server API.
""
And the Server API shows a detailed example of the  channel.connectBot() function. But there is not much more description than that.
I tried to figure this out. However, I can't seem to get it to work for me.

In logic.js, I am listening for a player disconnect in the callback of a stager.extendStep() function:

node.on('in.say.PDISCONNECT', function(msg) {
     console.log(msg);
     channel.connectBot({
         replaceID: msg.data.id
     })
});

It is catching the disconnect, because I can see the msg in the console (well, only if no other players/bots have already finished the step), but then it is creating a client with a different id than the player that disconnected, connecting them to the waiting room, and then throwing an error: "stepID not found".

Could you perhaps explain the appropriate way to replace disconnected clients with bots? I am not understanding how to properly listen for a disconnection, much less how to properly create the bot.
Thank you.
Best,
Drew

Drew Cranford

unread,
Apr 6, 2023, 1:51:48 AM4/6/23
to nodeGame
Hi Stefano,
After a lot of troubleshooting, I think I have mostly figured out how to properly replace the disconnected player with a bot. I had to add the room and gotoStep options, which I didn't initially realize. While the new bot's id is not the same as the player it replaced, which I was hoping for easier integration with other parts of my game, I can deal with that with a simple mapping.
The only issue I really have left is dealing with multiple listeners in the same callback. I currently have two listeners:

node.on('in.say.PDISCONNECT', function(msg) {...});
node.on.done(function(msg) {...});

I don't quite understand how this works, but am starting to read https://github.com/nodeGame/nodegame/wiki/Client-API-v7 which has some instructions on handling multiple listeners. My current issue is that if a player triggers the node.on.done() listener before a player disconnects, then the node.on('in.say.PDISCONNECT'...) listener does not fire. I should be able to figure it out after a bit of reading. Will let you know if I do.
Best,
Drew

--
Edward 'Drew' Cranford
Special Faculty Researcher
Dept of Psychology, ACT-R FMS group
Carnegie Mellon University
cran...@cmu.edu
edward.a...@gmail.com
601-329-6280

Drew Cranford

unread,
Apr 6, 2023, 1:51:55 AM4/6/23
to nodeGame

shakty

unread,
Apr 6, 2023, 1:53:54 AM4/6/23
to nodeGame
Hi Drew,

Sorry for the non-response, but in fact your messages had been automatically moderated and I was not notified. I saw them only now. Let me check what is still actual and I will reply asap.

Stefano

shakty

unread,
Apr 6, 2023, 2:29:04 AM4/6/23
to nodeGame
Hi Drew,

In general, it is no problem having multiple listeners in the same step callback,  however there are some cases in which they could influence each other. Without knowing what is going on inside those listeners, I have a couple of suggestions:

1. You could try to define a global or a stage listener instead of step listener. They have higher priority and don't get deleted if the step is done. To do so, you could add the listener in the init function of the game, or of the stage (stager.extendStage({ init: ... }). If those events should be valid only in a specific step you could check for the stage/step inside the disconnect callback.


2. You could access the different event emitters directly, e.g., node.events.game.on(...) for events valid throughout the game. This is equivalent to the above, but you have a bit more control.


3. You could use node.on.pdisconnect(id, function(){}); This should not change the behavior, but I find it a bit more readable.

Let me know if any of the above solves your issue and again sorry for the delay.

Cheers,
Stefano

shakty

unread,
Apr 6, 2023, 2:31:08 AM4/6/23
to nodeGame

shakty

unread,
Apr 6, 2023, 2:37:02 AM4/6/23
to nodeGame
And I also had a typo in the pdisconnect alias: 
node.on.pdisconnect(function(player){})

Drew Cranford

unread,
Apr 6, 2023, 1:06:56 PM4/6/23
to nodeGame
Thanks Stefano,
I used solution #1, and everything seems to work great now! I also implemented #3, which does make things more readable as you mentioned.
-Drew

shakty

unread,
Apr 6, 2023, 1:07:15 PM4/6/23
to nodeGame
Great!
Reply all
Reply to author
Forward
0 new messages