Restructured the server code so packets can be sent to the server without joining as a player
This commit is contained in:
46
server.js
46
server.js
@ -34,22 +34,7 @@ var port = 8080;
|
|||||||
var max_player_speed = 500;
|
var max_player_speed = 500;
|
||||||
|
|
||||||
function message(type, data, from) {
|
function message(type, data, from) {
|
||||||
if (type === "init") {
|
if (type === "movement") {
|
||||||
let this_username = Engine.create_id();
|
|
||||||
let this_variant = 0;
|
|
||||||
if ("username" in data) {
|
|
||||||
this_username = data.username;
|
|
||||||
console.log(data.username);
|
|
||||||
}
|
|
||||||
if ("variant" in data) {
|
|
||||||
if (data.variant >= 0 < 1) {
|
|
||||||
this_variant = Math.round(data.variant);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
from.username = this_username;
|
|
||||||
from.variant = this_variant;
|
|
||||||
}
|
|
||||||
else if (type === "movement") {
|
|
||||||
if ("position" in data) {
|
if ("position" in data) {
|
||||||
try {
|
try {
|
||||||
let new_position = new Engine.vector(data.position.x, data.position.y);
|
let new_position = new Engine.vector(data.position.x, data.position.y);
|
||||||
@ -95,21 +80,40 @@ var server = new WebSocket.Server({
|
|||||||
console.log("Server has started on port " + port);
|
console.log("Server has started on port " + port);
|
||||||
|
|
||||||
server.on("connection", function (socket, request) {
|
server.on("connection", function (socket, request) {
|
||||||
let this_player = new Engine.character(new Engine.vector(0, 0), 0, 0, Engine.create_id(), socket);
|
|
||||||
|
|
||||||
|
/*socket.on("message", function(msg) {
|
||||||
|
let boffer = Buffer.from(msg);
|
||||||
|
let data = JSON.parse(decoder.write(boffer));
|
||||||
|
message(data.type, data.data, this_player);
|
||||||
|
});*/
|
||||||
|
socket.on("message", function(msg) {
|
||||||
|
let boffer = Buffer.from(msg);
|
||||||
|
let data = JSON.parse(decoder.write(boffer));
|
||||||
|
if (data.type === "init") { //character joins
|
||||||
|
let this_username = "Anonymous";
|
||||||
|
let this_variant = 0;
|
||||||
|
if ("username" in data.data) {
|
||||||
|
this_username = data.data.username;
|
||||||
|
}
|
||||||
|
if ("variant" in data.data) {
|
||||||
|
if (data.data.variant >= 0 < game_data.character.variants.length) {
|
||||||
|
this_variant = Math.round(data.data.variant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let this_player = new Engine.character(new Engine.vector(0, 0), 0, 0, this_username, socket);
|
||||||
this_player.id = Engine.create_id();
|
this_player.id = Engine.create_id();
|
||||||
characters.add(this_player);
|
characters.add(this_player);
|
||||||
|
|
||||||
socket.send(JSON.stringify({"type": "server_data", "data": {"fps": server_fps}}));
|
socket.send(JSON.stringify({"type": "server_data", "data": {"fps": server_fps}}));
|
||||||
socket.send(JSON.stringify({"type": "world", "data": Array.from(nodes)}));
|
socket.send(JSON.stringify({"type": "world", "data": Array.from(nodes)}));
|
||||||
console.log("a client has connected");
|
console.log(this_username + " joined the game");
|
||||||
|
|
||||||
|
|
||||||
socket.on("message", function(msg) {
|
socket.on("message", function(msg) {
|
||||||
let boffer = Buffer.from(msg);
|
let boffer = Buffer.from(msg);
|
||||||
let data = JSON.parse(decoder.write(boffer));
|
let data = JSON.parse(decoder.write(boffer));
|
||||||
message(data.type, data.data, this_player);
|
message(data.type, data.data, this_player);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("close", function() {
|
socket.on("close", function() {
|
||||||
//characters = characters.filter(p => p.player.id !== thing.id);
|
//characters = characters.filter(p => p.player.id !== thing.id);
|
||||||
let char_array = Array.from(characters);
|
let char_array = Array.from(characters);
|
||||||
@ -123,6 +127,8 @@ server.on("connection", function (socket, request) {
|
|||||||
}
|
}
|
||||||
console.log(this_player.username + " has disconnected");
|
console.log(this_player.username + " has disconnected");
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function do_collisions() {
|
function do_collisions() {
|
||||||
|
Reference in New Issue
Block a user