Answer the question
In order to leave comments, you need to log in
The concept of creating a bot in a multiplayer game on WebSocket?
Suppose there are browsers in the form of clients and there is a server. And they communicate with each other using WebSocket
.
As a rule, a simple variant of coordination in this variant looks like a bunch of ifs from both the client side and the server side. Client example:
ws = new WebSocket("wss://site.com:8888/");
ws.onopen = function() {
ws.send({'e':"new", 'x':lPlayer.getX(), 'y':lPlayer.getY()});
};
ws.onmessage = function(event){
var msg = event.data;
if(msg.e == 'new'){
var newPlayer = new Player(scene, {x,y,z}, msg.x, msg.y);
newPlayer.id = msg.id;
newPlayer.init(msg.id);
remotePlayers.push(newPlayer);
}else if(msg.e == 'move' ){
var movePlayer = playerById(msg.id);
movePlayer.set(msg.x, msg.y, msg.z);
}else if(msg.e == 'rotate' ){
var rotPlayer = playerById(msg.id);
rotPlayer.setA(msg.a);
rotPlayer.setB(msg.b);
}
}
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: 8888});
wss.on('connection', function(ws) {
clients[max_id] = ws;
this.id = max_id;
ws.on('message', function(e) {
if(e.e == 'new'){
var p = new Player(max_id, e.x, e.y);
this.player = p;
clients.send_all({'e':"new", 'id':p.id, 'x':p.x(), 'y':p.y,'z':p.z});
players.push(p);
}else if (e.e == 'move') {
var p = this.player;
p.set(e.x, e.y, e.z);
clients.send_all({'e':"move", 'id':p.id, 'x':p.getX(), 'y':p.getY(), 'z':p.getZ(), 'msg':'move'})
}
if (e.e == 'move')
or for each if (e.e == 'shot')
some kind of bot action. }else if (e.e == 'move') {
var p = this.player;
p.set(e.x, e.y, e.z);
clients.send_all({'e':"move", 'id':p.id, 'x':p.x, 'y':p.y, 'z':p.z})
var bot = this.player.bot;
clients.send_all({'e':"move", 'id':bot.id, 'x':bot.x, 'y':bot.y, 'z':bot.z})
}
Answer the question
In order to leave comments, you need to log in
Mixing server logic and bot logic is architecturally wrong. Depending on the type of game, a bot can be:
1. For a single-player game, a bot can be a separate module/class. When the game starts, the server launches all the necessary bots, sends them a link to itself, and then they act as clients, subscribe to server event notifications, work out their logic in the event handler and issue commands to the server through the same client interface. The server may know "its own", so as not to be confused with a live client, but you should not tie too much logic on the server to this.
2. For MMO - a bot is a separate OS process (maybe even on a different machine), it runs independently and its crash does not affect the server. A special scheduler monitors bots and restarts those that have fallen due to crooked scripts. Bots can be trusted to be written by Indians, but the server must be unsinkable, so you should strive to take as much code out of it as possible.
You can even develop the idea of separating objects: Game, Player.
The "player" object handles the connection with users, implements the exchange between the server and the client itself, and responds to requests from the "Game" object. In the "Game" object, in theory, it is not necessary to register any sockets, for the "game" there are only players who can somehow change and respond to state changes with their own methods.
It is possible to describe the "Bot" object, which will have all the same methods and properties as the "player", but at the same time there will be no web sockets inside.
That is, the "Game" pulls the methods of its connected players, but the "Game" does not care if it is a bot or a person. There should be no mention of web sockets in the code of the "game" object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question