cozuya / secret-hitler

A web adaptation of the social deduction board game Secret Hitler. https://secrethitler.io
Other
840 stars 202 forks source link

Bot Connection to Websocket #1522

Closed jackdreinhardt closed 4 years ago

jackdreinhardt commented 4 years ago

Hi,

I am working on a project to create an intelligent agent (bot) to play SH based on the DeepRole algorithm. The bot must be able to take actions during the game – voting, picking a chancellor, etc – and get the game state. I plan to use the websocket connection to do this, but I am running into some problems.

After running yarn dev and yarn create-accounts, running the code below connects successfully to the socket, receives the fetchUser event, and emits the sendUser event with the specified user. (I am new to javascript, so please forgive any stylistic sins)

const io = require("socket.io-client");
var socket = io.connect('http://localhost:8080');

socket.on('connect', () => {
    console.log("connection established with sid", socket.id)
});

socket.on('disconnect', () => {
    console.log("disconnected")
});

socket.on('fetchUser', () => {
    var user = {
      userName:'Rexxar',
      verified: false,
      staffRole: '',
      hasNotDismissedSignupModal: false,
      gameSettings: {
        gameFilters:{},
        gameNotes:{},
        blacklist:[],
        tournyWins:[],
        playerNotes:[],
        soundStatus:'pack2',
        isPrivate: false
      }
    };
    socket.emit('sendUser', user);
});

However, the server responds with an empty user list: socket.io-client:socket emitting event ["userList",{"list":[]}] +0ms.

Running DEBUG=socket.io-client:socket,socket.io-client:manager node client.js gives the following output:

  socket.io-client:manager readyState closed +0ms
  socket.io-client:manager opening http://localhost:8080 +2ms
  socket.io-client:manager connect attempt will timeout after 20000 +20ms
  socket.io-client:manager readyState opening +2ms
  socket.io-client:manager open +7ms
  socket.io-client:manager cleanup +0ms
  socket.io-client:socket transport is open - connecting +0ms
connection established with sid sK4obT3Gt2sQWjXEAAAw
  socket.io-client:socket emitting event ["version",{"current":{"number":"1.6.6","color":"onedotohone-13","date":"1-5-2020"}}] +14ms
  socket.io-client:socket emitting event ["generalChats",{"sticky":"","list":[]}] +0ms
  socket.io-client:socket emitting event ["gameList",[]] +0ms
  socket.io-client:socket emitting event ["fetchUser"] +106ms
  socket.io-client:manager writing packet {"type":2,"data":["sendUser",{"userName":"Rexxar","verified":false,"staffRole":"","hasNotDismissedSignupModal":false,"gameSettings":{"gameFilters":{},"gameNotes":{},"blacklist":[],"tournyWins":[],"playerNotes":[],"soundStatus":"pack2","isPrivate":false}}],"options":{"compress":true},"nsp":"/"} +121ms
  socket.io-client:socket emitting event ["emoteList",[["AngelPanda",[0,0]],["Ban",[1,0]],["BangBang",[2,0]],["BigNose",[3,0]],["BlobSweat",[4,0]],["Bump",[5,0]],["CNH",[6,0]],["CozFace",[7,0]],["Cucu",[8,0]],["DevilPanda",[0,1]],["Eyes",[1,1]],["FasAngry",[2,1]],["FasEvil",[3,1]],["FasFace",[4,1]],["FasFrown",[5,1]],["FasGlory",[6,1]],["FasHitler",[7,1]],["FasLizard",[8,1]],["FasPolicy",[0,2]],["FasSkull",[1,2]],["FasSmile",[2,2]],["FasSnake",[3,2]],["FascistSkull",[4,2]],["FeelsBadMan",[5,2]],["FeelsRiddleMan",[6,2]],["FinChat",[7,2]],["Grr",[8,2]],["Heart",[0,3]],["HeyFas",[1,3]],["HeyLibs",[2,3]],["Hug",[3,3]],["Iconic",[4,3]],["Ja",[5,3]],["LibBird",[6,3]],["LibFacepalm",[7,3]],["LibFrown",[8,3]],["LibGlory",[0,4]],["LibHat",[1,4]],["LibHmm",[2,4]],["LibPipe",[3,4]],["LibPolicy",[4,4]],["LibSmile",[5,4]],["LibTash",[6,4]],["LibWink",[7,4]],["LiberalBird",[8,4]],["Limes",[0,5]],["MaxFlame",[1,5]],["Nein",[2,5]],["NotHitler",[3,5]],["PBullet",[4,5]],["PDraw",[5,5]],["PIZZA",[6,5]],["PInvest",[7,5]],["PPres",[8,5]],["PandaHeil",[0,6]],["Peach",[1,6]],["PopCorn",[2,6]],["PopcornPanda",[3,6]],["Pray",[4,6]],["RIP",[5,6]],["RateTen",[6,6]],["RateZero",[7,6]],["RedFace",[8,6]],["SadHug",[0,7]],["Salty",[1,7]],["SecretHitler",[2,7]],["SendHelp",[3,7]],["Sheeped",[4,7]],["Shrug",[5,7]],["SillyLib",[6,7]],["Slop",[7,7]],["Squint",[8,7]],["ThinkFace",[0,8]],["ThumbsDown",[1,8]],["ThumbsUp",[2,8]],["TopDeck",[3,8]],["Trashy",[4,8]],["VetoPower",[5,8]],["WeThink",[6,8]],["Whip",[7,8]],["YayPanda",[8,8]]]] +4ms
  socket.io-client:socket emitting event ["userList",{"list":[]}] +0ms
  socket.io-client:socket emitting event ["gameList",[]] +13s
  socket.io-client:socket emitting event ["gameList",[]] +2m
  socket.io-client:socket emitting event ["gameList",[]] +2m

I have hypothesized that I am running into an auth problem and have tried to add the connect.sid cookie the user receives from /account/signin. This modification did not change the result of the above code (I did that version in python using the python-socketio module as I am more familiar with the language). I have been digging through the codebase trying to figure out how to authenticate the bot, but have been unable to come up with a solution.

If it's possible, how do I correctly authenticate a (bot) user to fully utilize the websocket? Any help is greatly appreciated, thanks.

TL;DR The client can connect successfully to the socket, but the server does not receive client events correctly. E.g. emitting a sendUser event does not result the expected userList. Authentication problem?

cozuya commented 4 years ago

App uses passport.js for auth uh its complicated - might want to come by our development channel on discord for this one. I would start by debugging from the nodejs side. Take a look at the user-requests file specifically.

jackdreinhardt commented 4 years ago

I've found some time to dig into this and have taken a look at passport.js and the user-requests file but am still not sure how to proceed. I see that using the local strategy is preferred, but I am not sure how to proceed. I noticed the use of socket.handshake.session from the express-socket.io-session package. Would it be possible to utilize this to get proper authentication?

Vigasaurus commented 4 years ago

Resolving in Discord channel