Restructured the server code so packets can be sent to the server without joining as a player

This commit is contained in:
2023-03-22 11:18:07 -07:00
parent d424aea7a1
commit f1d83ade56

View File

@ -34,22 +34,7 @@ var port = 8080;
var max_player_speed = 500;
function message(type, data, from) {
if (type === "init") {
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 (type === "movement") {
if ("position" in data) {
try {
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);
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();
characters.add(this_player);
socket.send(JSON.stringify({"type": "server_data", "data": {"fps": server_fps}}));
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) {
let boffer = Buffer.from(msg);
let data = JSON.parse(decoder.write(boffer));
message(data.type, data.data, this_player);
});
socket.on("close", function() {
//characters = characters.filter(p => p.player.id !== thing.id);
let char_array = Array.from(characters);
@ -123,6 +127,8 @@ server.on("connection", function (socket, request) {
}
console.log(this_player.username + " has disconnected");
});
}
});
});
function do_collisions() {