Physics sync support
This commit is contained in:
parent
77ffe04e2f
commit
2bcdb780c3
|
@ -5,8 +5,8 @@ import { users } from "./users";
|
||||||
export function registerAccountCallbacks(socket){
|
export function registerAccountCallbacks(socket){
|
||||||
function emitLogin(result : userData, registered:boolean = false){
|
function emitLogin(result : userData, registered:boolean = false){
|
||||||
socket.emit("account/login", JSON.stringify(new publicUserData(result, socket.id)));
|
socket.emit("account/login", JSON.stringify(new publicUserData(result, socket.id)));
|
||||||
console.log(result)
|
|
||||||
if(result != null){
|
if(result != null){
|
||||||
|
socket.userId = result.Id;
|
||||||
if(registered){
|
if(registered){
|
||||||
console.log(`User ${(result as userData).Login} registerd and logged in`);
|
console.log(`User ${(result as userData).Login} registerd and logged in`);
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,9 @@ export function registerAccountCallbacks(socket){
|
||||||
console.log(`User ${(result as userData).Login} logged in`);
|
console.log(`User ${(result as userData).Login} logged in`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
console.log("Login Failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('account/register', (data) => {
|
socket.on('account/register', (data) => {
|
||||||
|
|
|
@ -4,18 +4,18 @@ const fs = require('fs');
|
||||||
|
|
||||||
export class users{
|
export class users{
|
||||||
static createUser(login, password){
|
static createUser(login, password){
|
||||||
if(game.usersDB.filter(u=>u.Login == login)[0] != undefined){
|
if(game.accountsDB.filter(u=>u.Login == login)[0] != undefined){
|
||||||
console.log(`User ${login} alread exists`);
|
console.log(`User ${login} alread exists`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = new userData(game.usersDB.length, login, password);
|
var user = new userData(game.accountsDB.length, login, password);
|
||||||
game.usersDB.push(user);
|
game.accountsDB.push(user);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
static readUser(login, password){
|
static readUser(login, password){
|
||||||
let user = game.usersDB.filter(u=>u.Login == login && u.Password == password)[0];
|
let user = game.accountsDB.filter(u=>u.Login == login && u.Password == password)[0];
|
||||||
if(user != undefined){
|
if(user != undefined){
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ export class users{
|
||||||
}
|
}
|
||||||
|
|
||||||
static readUserID(id : number){
|
static readUserID(id : number){
|
||||||
let user = game.usersDB.filter(u=>u.Id == id)[0];
|
let user = game.accountsDB.filter(u=>u.Id == id)[0];
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,11 +34,11 @@ export class users{
|
||||||
sum = Math.floor(Math.random() * 99999);
|
sum = Math.floor(Math.random() * 99999);
|
||||||
}
|
}
|
||||||
userNew.Checksum = sum;
|
userNew.Checksum = sum;
|
||||||
game.usersDB[game.usersDB.indexOf(user)] = userNew;
|
game.accountsDB[game.accountsDB.indexOf(user)] = userNew;
|
||||||
}
|
}
|
||||||
|
|
||||||
static saveUsers(){
|
static saveUsers(){
|
||||||
game.usersDB.forEach(user => {
|
game.accountsDB.forEach(user => {
|
||||||
let data = JSON.stringify(user, null, 2);
|
let data = JSON.stringify(user, null, 2);
|
||||||
fs.writeFileSync(`Database/Users/${user.Id}_${user.Login}.json`, data);
|
fs.writeFileSync(`Database/Users/${user.Id}_${user.Login}.json`, data);
|
||||||
});
|
});
|
||||||
|
@ -50,7 +50,7 @@ export class users{
|
||||||
fs.readFile(`Database/Users/${file}`, (err, data) => {
|
fs.readFile(`Database/Users/${file}`, (err, data) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
let user = JSON.parse(data);
|
let user = JSON.parse(data);
|
||||||
game.usersDB.push(user);
|
game.accountsDB.push(user);
|
||||||
console.log(`Loaded ${user.Login}`);
|
console.log(`Loaded ${user.Login}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
export class physicsObject{
|
||||||
|
name : string
|
||||||
|
//array of floats [3]
|
||||||
|
position : number[] = []
|
||||||
|
rotation : number[] = []
|
||||||
|
velocity : number[] = []
|
||||||
|
angularVelocity : number[] = []
|
||||||
|
}
|
|
@ -1,31 +1,44 @@
|
||||||
|
import { playerData } from "../Player/playerData"
|
||||||
import { game } from "../game";
|
import { game } from "../game";
|
||||||
|
|
||||||
export function registerLobbyCallbacks(socket){
|
export function registerLobbyCallbacks(socket){
|
||||||
socket.on('lobby/playerUpdate', (data) => {
|
socket.on('lobby/playerUpdate', (data) => {
|
||||||
let buff = Buffer.from(data, 'base64');
|
if(!socket.hasOwnProperty("userId")) return;
|
||||||
data = JSON.parse(buff.toString('utf-8'));
|
|
||||||
|
|
||||||
if (game.DBPlayers.hasOwnProperty(socket.id)){
|
let buff = Buffer.from(data, 'base64');
|
||||||
game.DBPlayers[socket.id].position = data["position"]
|
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
||||||
|
data1.socketId = socket.id;
|
||||||
|
|
||||||
|
let idx = game.lobbyState.players.findIndex((pl) => pl.socketId == socket.id)
|
||||||
|
if(idx == -1){
|
||||||
|
game.lobbyState.players.push(data1)
|
||||||
|
socket.emit('lobby/messages', JSON.stringify(game.messages))
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
console.log("got incorrect request")
|
game.lobbyState.players[idx] = data1
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('lobby/lobbyUpdate', (data) => {
|
||||||
|
if(!socket.hasOwnProperty("userId")) return;
|
||||||
|
|
||||||
|
let buff = Buffer.from(data, 'base64');
|
||||||
|
let data1 = JSON.parse(buff.toString('utf-8'));
|
||||||
|
|
||||||
|
game.lobbyState.objects = data1.objects
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('lobby/message', (data) => {
|
socket.on('lobby/message', (data) => {
|
||||||
|
if(!socket.hasOwnProperty("userId")) return;
|
||||||
|
|
||||||
let buff = Buffer.from(data, 'base64');
|
let buff = Buffer.from(data, 'base64');
|
||||||
data = JSON.parse(buff.toString('utf-8'));
|
data = JSON.parse(buff.toString('utf-8'));
|
||||||
|
data.timestamp = new Date().toLocaleString();
|
||||||
console.log(socket.conn.transport.name);
|
|
||||||
console.log("received message: " + JSON.stringify(data))
|
|
||||||
|
|
||||||
while(game.messages.length >= 20){
|
while(game.messages.length >= 20){
|
||||||
game.messages.pop();
|
game.messages.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("broadcastin");
|
|
||||||
|
|
||||||
game.messages.unshift(data)
|
game.messages.unshift(data)
|
||||||
socket.io.emit('lobby/messages', JSON.stringify(game.messages))
|
socket.io.emit('lobby/messages', JSON.stringify(game.messages))
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { physicsObject } from "../Classes/physicsObject";
|
||||||
|
import { playerData } from "../Player/playerData";
|
||||||
|
|
||||||
|
export class lobbyState{
|
||||||
|
hostSocket : string
|
||||||
|
players : playerData[] = [];
|
||||||
|
objects : physicsObject[] = [];
|
||||||
|
}
|
|
@ -1,11 +1,9 @@
|
||||||
import { userData } from "../Account/userData";
|
import { publicUserData } from "../Account/userData";
|
||||||
|
import { physicsObject } from "../Classes/physicsObject";
|
||||||
|
|
||||||
export class playerData{
|
export class playerData{
|
||||||
id : string = ""
|
socketId : string
|
||||||
position : number[] = []
|
userName : string
|
||||||
|
characterId : number
|
||||||
constructor(id: string){
|
rigidbody : physicsObject
|
||||||
this.id = id;
|
|
||||||
this.position = [0,0,0];
|
|
||||||
}
|
|
||||||
}
|
}
|
11
game.ts
11
game.ts
|
@ -1,10 +1,15 @@
|
||||||
import { userData } from "./Account/userData";
|
import { userData } from "./Account/userData";
|
||||||
import { lobbyMessageData } from "./Lobby/lobbyMessageData";
|
import { lobbyMessageData } from "./Lobby/lobbyMessageData";
|
||||||
|
import { lobbyState } from "./Lobby/lobbyState";
|
||||||
|
import { playerData } from "./Player/playerData";
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
export class game{
|
export class game{
|
||||||
static sessionsTotal = 0;
|
static accountsDB : userData[] = []
|
||||||
static usersDB : userData[] = [];
|
|
||||||
static DBPlayers = {};
|
/** Key: socket ID*/
|
||||||
|
static lobbyState : lobbyState = new lobbyState()
|
||||||
|
|
||||||
|
/** Max items: 20 */
|
||||||
static messages : lobbyMessageData[] = [];
|
static messages : lobbyMessageData[] = [];
|
||||||
}
|
}
|
29
index.ts
29
index.ts
|
@ -8,7 +8,6 @@ const express = require("express");
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
const server = require('http').Server(app);
|
const server = require('http').Server(app);
|
||||||
var connectedUsers = {}
|
|
||||||
|
|
||||||
server.listen(port, () => {
|
server.listen(port, () => {
|
||||||
console.log(`Server listening at port ${port}`);
|
console.log(`Server listening at port ${port}`);
|
||||||
|
@ -26,19 +25,20 @@ users.loadUsers();
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
console.log("Got connection!");
|
console.log("Got connection!");
|
||||||
socket.emit("connectSuccess", "");
|
socket.emit("connectSuccess", "");
|
||||||
|
|
||||||
registerAccountCallbacks(socket);
|
registerAccountCallbacks(socket);
|
||||||
registerLobbyCallbacks(socket);
|
registerLobbyCallbacks(socket);
|
||||||
|
|
||||||
socket.io = io;
|
socket.io = io;
|
||||||
let id = socket.id;
|
|
||||||
let player = new playerData(id)
|
|
||||||
connectedUsers[id] = true;
|
|
||||||
game.DBPlayers[id] = player
|
|
||||||
|
|
||||||
socket.on("disconnect", () => {
|
socket.on("disconnect", () => {
|
||||||
delete connectedUsers[id]
|
|
||||||
socket.disconnect()
|
socket.disconnect()
|
||||||
console.log(id + " disconnected");
|
let idx = game.lobbyState.players.findIndex((pl) => pl.socketId == socket.id)
|
||||||
|
if(idx != -1){
|
||||||
|
game.lobbyState.players.splice(idx, 1)
|
||||||
|
}
|
||||||
|
delete socket.userId
|
||||||
|
console.log(socket.id + " disconnected");
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('error',function(er){
|
socket.on('error',function(er){
|
||||||
|
@ -46,22 +46,17 @@ io.on('connection', (socket) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Send player updates to everyone
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
let list = []
|
let userCount = game.lobbyState.players.length
|
||||||
for (var key in connectedUsers) {
|
|
||||||
if (game.DBPlayers.hasOwnProperty(key)){
|
|
||||||
list.push(game.DBPlayers[key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let userCount = list.length
|
|
||||||
if(userCount > 0){
|
if(userCount > 0){
|
||||||
io.emit('lobby', JSON.stringify(list))
|
game.lobbyState.hostSocket = game.lobbyState.players[0].socketId
|
||||||
|
io.emit('lobby', JSON.stringify(game.lobbyState))
|
||||||
}
|
}
|
||||||
}, 1000/30)
|
}, 1000/30)
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
console.log("Connected losers: " + Object.keys(connectedUsers).length);
|
console.log("Connected losers: " + game.lobbyState.players.length);
|
||||||
}, 60 * 1000)
|
}, 60 * 1000)
|
||||||
|
|
||||||
process.on('SIGINT', function () {
|
process.on('SIGINT', function () {
|
||||||
|
|
Loading…
Reference in New Issue