1
\$\begingroup\$

I have built a realtime multiplayer web game using WebSocket protocol.

In my game I create about 100 AI bot players to simulate playing the game with the real players. Every time a bot is dead the server will create a new one. I wonder if I need to simulate the Websocket connection and implement Websocket listeners like what I am doing at the moment.

I understand that creating new Websocket connections will use a lot of resources and maintaining the connections eat up resources on the server too. As I don't need to send anything to bot players I probably don't need to create real connections on the server.

The game is working fine. But I am looking for ways to improve. Let me know what you think.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think you answered your own question when you said I don't need to simulate a real connection and it's taking up a lot of resources. The obvious answer is don't do it. \$\endgroup\$
    – Charanor
    Commented Apr 5, 2018 at 8:51
  • \$\begingroup\$ @Charanor Yes maybe I did but if I change my current code it will be a big change on the server so I just want to get some advice. \$\endgroup\$
    – newguy
    Commented Apr 5, 2018 at 9:33

1 Answer 1

1
\$\begingroup\$

Hm... You shouldn't open those for AI characters, at least not for all of them, all the time. But this depends on your game.

Some assumptions: 1) There are more than one real player in the game. 2) They have to interact with AI players 3) There is some graphical user interface for the interaction.

What you have here is human players, AI players and "state of the world". If this was classical client-server game, it would have worked like this: Human player sends command to the server, server does the simulation of AI player, sends response. AI players literally "live" on a server and open connections towards player(s) only when they have to interact with them, either by touching them or by being seen by them.

Example: I'll take the example of the simplest socket 2D game there is: Agar.io . You have players, they eat each other. You have state of the world (which is, who ate how much, where is the location on the grid of each player etc). In this example, we have 1 human player. Thing is, the human player only needs to know the part of the world state which effects him/her. Let's say a human player is in position 0 and can see everything in a square of -10,-10 up to 10, 10 on x,y axes.

Basically, what happens, AI character opens a connection towards a player only if its location is such that is in that camera visible square or about to enter it. If it is not seen, it is being simulated on a server. Once it enters the camera, it starts to really exist and has to interface with the open connection to interact with the player. Now, the fun part: you only need one server connection with the single human player to simulate any number of AI players interacting with the human one! Why? Because AI opponents are on a server and what the player doesn't see, (s)he has to take on trust.

I could tell you that I am answering this question to you from the beach bar in Hawaii, there is no way for you to check. And that is true until you actually look (move the camera where I can actually be seen) in which case I have to prove it to you by opening socket connection to you and actually sending you a "picture". Basically, you could make AI character be in the first place and say it had eaten 100 other players and grown 10 times and it will be true until player actually comes to the location where the virtual AI player can be seen. Then your game would have to open a socket and show that monstrous, extremely good AI player to Human player. And then it turns from invisible simulation of 100 players to visible simulation of 10 visible AI players + human player.

\$\endgroup\$
5
  • \$\begingroup\$ Thanks for your answer. I am making Agar.io type game. What you say here seems more complicated than I thought it would be. I thought I can just create AI players without actually using websocket connections. Maybe I should give more thoughts to this topic. If I open a new websocket connection when a real player can see the AI player, then I need to close the connection when the bot leaves the screen (of the real player). If there are 100 real players. I need to do the open/close connection operations many times? That would be more resources-hungry. \$\endgroup\$
    – newguy
    Commented Apr 5, 2018 at 11:54
  • \$\begingroup\$ Not really. You have one socket connection for server-Human player. All the AI "players" use the same connection. So, the truth is, you can create the AI players without each one using socket connection... The truth is also that you don't need to simulate the AI players more than absolutely necessary. \$\endgroup\$
    – jo1storm
    Commented Apr 5, 2018 at 12:40
  • \$\begingroup\$ You mean the bots and the real player will share the connection? That makes sense. I will try that and let you know the result. \$\endgroup\$
    – newguy
    Commented Apr 6, 2018 at 15:53
  • \$\begingroup\$ How did it go? @newguy \$\endgroup\$
    – jo1storm
    Commented Apr 8, 2018 at 18:56
  • \$\begingroup\$ I have changed the code to not use socket for AI players. But I don't see any performance improvement for now, still need more test. I will mark your answer as correct. \$\endgroup\$
    – newguy
    Commented Apr 9, 2018 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .