Init
This commit is contained in:
commit
77ffe04e2f
|
@ -0,0 +1,43 @@
|
|||
Database/
|
||||
|
||||
lib-cov
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.gz
|
||||
*.swp
|
||||
|
||||
pids
|
||||
logs
|
||||
results
|
||||
tmp
|
||||
|
||||
# Build
|
||||
public/css/main.css
|
||||
|
||||
# Coverage reports
|
||||
coverage
|
||||
|
||||
# API keys and secrets
|
||||
.env
|
||||
|
||||
# Dependency directory
|
||||
node_modules
|
||||
bower_components
|
||||
|
||||
# Editors
|
||||
.idea
|
||||
*.iml
|
||||
|
||||
# OS metadata
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Ignore built ts files
|
||||
dist/**/*
|
||||
|
||||
# ignore yarn.lock
|
||||
yarn.lock
|
|
@ -0,0 +1,34 @@
|
|||
import { game } from "../game";
|
||||
import { publicUserData, userData } from "./userData";
|
||||
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){
|
||||
if(registered){
|
||||
console.log(`User ${(result as userData).Login} registerd and logged in`);
|
||||
}
|
||||
else{
|
||||
console.log(`User ${(result as userData).Login} logged in`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
socket.on('account/register', (data) => {
|
||||
let buff = Buffer.from(data, 'base64');
|
||||
let json = JSON.parse(buff.toString('utf-8'));
|
||||
let result = users.createUser(json.Login, json.Password);
|
||||
|
||||
emitLogin(result, true);
|
||||
});
|
||||
|
||||
socket.on('account/login', (data) => {
|
||||
let buff = Buffer.from(data, 'base64');
|
||||
let json = JSON.parse(buff.toString('utf-8'));
|
||||
let result = users.readUser(json.Login, json.Password);
|
||||
|
||||
emitLogin(result);
|
||||
});
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
export class userData{
|
||||
Id: number;
|
||||
Checksum: number;
|
||||
Login: string;
|
||||
Password: string;
|
||||
Characters: number[] = [];
|
||||
|
||||
constructor(id:number, login:string, password:string){
|
||||
this.Id = id;
|
||||
this.Login = login;
|
||||
this.Password = password;
|
||||
}
|
||||
}
|
||||
|
||||
export class publicUserData{
|
||||
Id: number;
|
||||
Login: string;
|
||||
SocketId: string;
|
||||
|
||||
constructor(user:userData, socket: string){
|
||||
if(user == null){
|
||||
return null;
|
||||
}
|
||||
this.Id = user.Id;
|
||||
this.Login = user.Login;
|
||||
this.SocketId = socket;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
import { game } from "../game";
|
||||
import { userData } from "./userData";
|
||||
const fs = require('fs');
|
||||
|
||||
export class users{
|
||||
static createUser(login, password){
|
||||
if(game.usersDB.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);
|
||||
return user;
|
||||
}
|
||||
|
||||
static readUser(login, password){
|
||||
let user = game.usersDB.filter(u=>u.Login == login && u.Password == password)[0];
|
||||
if(user != undefined){
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static readUserID(id : number){
|
||||
let user = game.usersDB.filter(u=>u.Id == id)[0];
|
||||
return user;
|
||||
}
|
||||
|
||||
static updateUser(userNew : userData){
|
||||
let user = this.readUserID(userNew.Id);
|
||||
let sum = Math.floor(Math.random() * 99999);
|
||||
while(sum == userNew.Checksum){
|
||||
sum = Math.floor(Math.random() * 99999);
|
||||
}
|
||||
userNew.Checksum = sum;
|
||||
game.usersDB[game.usersDB.indexOf(user)] = userNew;
|
||||
}
|
||||
|
||||
static saveUsers(){
|
||||
game.usersDB.forEach(user => {
|
||||
let data = JSON.stringify(user, null, 2);
|
||||
fs.writeFileSync(`Database/Users/${user.Id}_${user.Login}.json`, data);
|
||||
});
|
||||
}
|
||||
|
||||
static loadUsers(){
|
||||
fs.readdir(`Database/Users/`, (err, files) => {
|
||||
files.forEach(file => {
|
||||
fs.readFile(`Database/Users/${file}`, (err, data) => {
|
||||
if (err) throw err;
|
||||
let user = JSON.parse(data);
|
||||
game.usersDB.push(user);
|
||||
console.log(`Loaded ${user.Login}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
export class lobbyMessageData{
|
||||
Id: number;
|
||||
User: string;
|
||||
Timestamp: string;
|
||||
Content: string;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
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 (game.DBPlayers.hasOwnProperty(socket.id)){
|
||||
game.DBPlayers[socket.id].position = data["position"]
|
||||
}
|
||||
else{
|
||||
console.log("got incorrect request")
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('lobby/message', (data) => {
|
||||
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))
|
||||
|
||||
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,11 @@
|
|||
import { userData } from "../Account/userData";
|
||||
|
||||
export class playerData{
|
||||
id : string = ""
|
||||
position : number[] = []
|
||||
|
||||
constructor(id: string){
|
||||
this.id = id;
|
||||
this.position = [0,0,0];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
export enum BattleAction{
|
||||
ChargeRight,
|
||||
ChargeLeft,
|
||||
MoveRight,
|
||||
MoveLeft,
|
||||
AttackRightHigh,
|
||||
AttackRightMid,
|
||||
AttackRightLow,
|
||||
AttackLeftHigh,
|
||||
AttackLeftMid,
|
||||
AttackLeftLow,
|
||||
Wait,
|
||||
}
|
||||
|
||||
export enum CharacterState{
|
||||
Idle,
|
||||
Recovering,
|
||||
Training,
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { userData } from "./Account/userData";
|
||||
import { lobbyMessageData } from "./Lobby/lobbyMessageData";
|
||||
const fs = require('fs');
|
||||
|
||||
export class game{
|
||||
static sessionsTotal = 0;
|
||||
static usersDB : userData[] = [];
|
||||
static DBPlayers = {};
|
||||
static messages : lobbyMessageData[] = [];
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
import { registerAccountCallbacks } from "./Account/accountSocket";
|
||||
import { users } from "./Account/users";
|
||||
import { registerLobbyCallbacks } from "./Lobby/lobbySocket";
|
||||
import { playerData } from "./Player/playerData";
|
||||
import { game } from "./game";
|
||||
|
||||
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}`);
|
||||
});
|
||||
|
||||
const io = require("socket.io")(server, {
|
||||
cors: {
|
||||
origin: '*',
|
||||
methods: ["GET", "POST"]
|
||||
}
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
|
||||
socket.on('error',function(er){
|
||||
console.log(er);
|
||||
});
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
let list = []
|
||||
for (var key in connectedUsers) {
|
||||
if (game.DBPlayers.hasOwnProperty(key)){
|
||||
list.push(game.DBPlayers[key]);
|
||||
}
|
||||
}
|
||||
|
||||
let userCount = list.length
|
||||
if(userCount > 0){
|
||||
io.emit('lobby', JSON.stringify(list))
|
||||
}
|
||||
}, 1000/30)
|
||||
|
||||
setInterval(() => {
|
||||
console.log("Connected losers: " + Object.keys(connectedUsers).length);
|
||||
}, 60 * 1000)
|
||||
|
||||
process.on('SIGINT', function () {
|
||||
console.log('Ctrl-C...');
|
||||
process.exit(2);
|
||||
});
|
||||
process.on('exit', function () {
|
||||
users.saveUsers();
|
||||
console.log("exitted");
|
||||
});
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "clawsandpawpadsserver",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/node": "^17.0.25",
|
||||
"@types/socket.io": "^3.0.2",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.3",
|
||||
"socket.io": "^4.4.1",
|
||||
"ts-node": "^10.7.0",
|
||||
"typescript": "^4.6.3"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
export class responses{
|
||||
static BattleNotExist = {code: 400, message: "Battle session does not exist"}
|
||||
static BattleFinished = {code: 400, message: "The battle has finished"}
|
||||
static BattleAlreadyStarted = {code: 400, message: "Battle has already started"}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"sourceMap": true,
|
||||
"noImplicitAny": false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue