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