Skip to content

Commit

Permalink
v7.0 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
evolutionleo committed Dec 4, 2024
1 parent 4b584b5 commit 355423c
Show file tree
Hide file tree
Showing 76 changed files with 1,166 additions and 851 deletions.
117 changes: 113 additions & 4 deletions JavascriptServer/cmd/handlers/auth.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,126 @@
import { addHandler } from "#cmd/handlePacket";
import { accountActivate, accountCreate, accountLogin, accountRegister, profileRename, sessionCreate, sessionGet, sessionLogin } from "#util/auth";
import trace from "#util/logging";
import { Names } from "#util/names";

addHandler('name get', (c) => {
c.sendName();
});

addHandler('name set', async (c, data) => {
let name = data.name;

if (c.logged_in) {
try {
await profileRename(c.profile, name);
c.sendName();
}
catch (e) { }
}
else {
if (!Names.isValid(name)) {
return;
}
if (global.clients.some(client => client.name === name)) {
return;
}

c.name = name;
c.sendName();
}
});

// create a brand new (temporary) account

addHandler('session create', async (c, data) => {
let name = c.name; // default name

try {
c.account = accountCreate(name, '', true);
c.session = sessionCreate(c.account);

await c.register(c.account);

c.sendSessionCreate(true, '', c.session.token);
c.sendLogin(true);
}
catch (reason) {
c.sendSessionCreate(false, reason.toString());
}
});

addHandler('session login', async (c, data) => {
let token = data.session;

try {
c.session = await sessionGet(token);
c.account = await sessionLogin(c.session);

await c.login(c.account);
c.sendSession(true);
c.sendLogin(true);

// another client logged into the same session?
let old_client = global.clients.find((client) => client !== c && client.session?.token === c.session?.token);

if (old_client !== undefined) {
if (old_client.connected) {
old_client.disconnect();
}

old_client.reconnect(c);
}
}
catch (reason) {
trace('error: ' + reason.toString());
c.sendSession(false, reason);
// c.send({ cmd: 'session login', success: false, reason: reason.toString() });
}
});


addHandler('login', (c, data) => {
var { username, password } = data;
c.tryLogin(username, password);
let { username, password } = data;
username = username.toLowerCase();

accountLogin(username, password)
.then((account) => {
c.login(account);
c.session = sessionCreate(account);

c.sendLogin(true);
c.sendSession(true);
})
.catch((reason) => {
c.sendLogin(false, reason);
});
});

addHandler('register', (c, data) => {
var { username, password } = data;
c.tryRegister(username, password);
let { username, password } = data;
username = username.toLowerCase();

let promise;

if (c.session && c.account && c.account.temporary) {
promise = accountActivate(c.account, username, password);
}
else {
promise = accountRegister(username, password);
}

promise
.then((account) => {
c.register(account);
c.session = sessionCreate(c.account);

c.sendRegister(true);
c.sendLogin(true);
c.sendSession(true);
})
.catch((reason) => {
c.sendRegister(false, reason);
});
});

// addHandler('logout', (c, data) => {
Expand Down
6 changes: 6 additions & 0 deletions JavascriptServer/cmd/handlers/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import addHandler from "#cmd/handlePacket";

// list of all the chats of this player
addHandler('chats list', (c, data) => {
c.sendChatsList();
});
23 changes: 4 additions & 19 deletions JavascriptServer/cmd/handlers/custom.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import { addHandler } from "#cmd/handlePacket";
import { clamp } from "#util/maths";


addHandler('player controls', (c, data) => {
if (!c.entity)
return;

c.entity.inputs = {
move: data.move,
keys: {
kright: data.kright,
kleft: data.kleft,
kup: data.kup,
kdown: data.kdown,

kjump: data.kjump,
kjump_rel: data.kjump_rel,
kjump_press: data.kjump_press
}
};

c.entity.inputs.move.x = clamp(c.entity.inputs.move.x, -1, 1);
c.entity.inputs.move.y = clamp(c.entity.inputs.move.y, -1, 1);
for (let input_name in c.entity.inputs) {
if (data[input_name] !== undefined)
c.entity.inputs[input_name] = data[input_name];
}
});

2 changes: 1 addition & 1 deletion JavascriptServer/cmd/handlers/friends.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ addHandler('friend list', async (c, data) => {
});

addHandler('friend req send', (c, data) => {
if (!c.profile.friends.includes(data.friend._id) && !FriendRequest.exists({ sender: data.friend._id }))
if (!c.profile.friends.includes(data.friend.id) && !FriendRequest.exists({ sender: data.friend.id }))
c.friendRequestSend(data.friend);

c.send({ cmd: 'friend req sent', to: data.friend.name });
Expand Down
14 changes: 7 additions & 7 deletions JavascriptServer/cmd/handlers/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ addHandler('lobby list', (c) => {
});

addHandler('lobby info', (c, data) => {
var lobbyid = data.lobbyid;
if (lobbyExists(lobbyid))
c.sendLobbyInfo(lobbyid);
const lobby_id = data.lobby_id;
if (lobbyExists(lobby_id))
c.sendLobbyInfo(lobby_id);
});

addHandler('lobby join', (c, data) => {
if (!global.config.lobby.allow_join_by_id)
return;

var lobbyid = data.lobbyid;
if (lobbyExists(lobbyid))
c.lobbyJoin(lobbyid);
const lobby_id = data.lobby_id;
if (lobbyExists(lobby_id))
c.lobbyJoin(lobby_id);
});

addHandler('lobby leave', (c, data) => {
var lobby = c.lobby;
let lobby = c.lobby;
if (lobby !== null) {
lobby.kickPlayer(c, 'you left the lobby', false);
}
Expand Down
18 changes: 9 additions & 9 deletions JavascriptServer/cmd/handlers/party.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { addHandler } from "#cmd/handlePacket";
import { partyExists } from "#matchmaking/party";

addHandler('party join', (c, data) => {
var partyid = data.partyid;
if (partyExists(partyid))
c.partyJoin(partyid);
let party_id = data.party_id;
if (partyExists(party_id))
c.partyJoin(party_id);
});

addHandler('party leave', (c) => {
Expand All @@ -19,12 +19,12 @@ addHandler('party kick', (c, data) => {
if (!c.party.isLeader(c))
return;

let { profileid, username } = data;
let { profile_id, username } = data;
let reason = data.reason ?? '';
let member = null;

if (profileid) {
member = global.clients.find(u => u.profile.id === profileid);
if (profile_id) {
member = global.clients.find(u => u.profile.id === profile_id);
}
else {
member = global.clients.find(u => u.name === username);
Expand All @@ -45,11 +45,11 @@ addHandler('party disband', (c) => {
});

addHandler('party invite', (c, data) => {
let { profileid, username } = data;
let { profile_id, username } = data;
let user = null;

if (profileid) {
user = global.clients.find(u => u.profile.id === profileid);
if (profile_id) {
user = global.clients.find(u => u.profile.id === profile_id);
}
else {
user = global.clients.find(u => u.name === username);
Expand Down
2 changes: 1 addition & 1 deletion JavascriptServer/cmd/handlers/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ addHandler('client info', (c, data) => {
// immediately close the socket after 1 last packet
setImmediate(() => {
// close the socket
if (c.type == 'tcp') {
if (c.socket_type == 'tcp') {
const s = c.socket;
s.destroy();
}
Expand Down
12 changes: 10 additions & 2 deletions JavascriptServer/cmd/sendStuff.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ export class SendStuff {
* basic send
* @param {any} data
*/
write(data) {
write(data, shouldQueue = false) {
if (!this.connected) {
if (shouldQueue) {
this.packetQueue.push(data);
}

return;
}

if (global.config.timestamps_enabled) { // { t: ms passed since the server started }
data.t = Date.now() - global.start_time;
}

if (this.type === 'ws') {
if (this.socket_type === 'ws') {
this.socket.send(packet.ws_build(data));
}
else {
Expand Down
26 changes: 20 additions & 6 deletions JavascriptServer/cmd/senders/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@ SendStuff.prototype.sendName = function () {
};

/**
* @param {string} status
* @param {string} success
* @param {string} [reason='']
*/
SendStuff.prototype.sendRegister = function (status, reason = '') {
this.send({ cmd: 'register', status, reason });
SendStuff.prototype.sendRegister = function (success, reason = '') {
this.send({ cmd: 'register', success, reason });
};

/**
* @param {string} status
* @param {string} success
* @param {string} [reason='']
*/
SendStuff.prototype.sendLogin = function (status, reason = '') {
this.send({ cmd: 'login', status, reason, account: getAccountInfo(this.account), profile: getProfileInfo(this.profile) });
SendStuff.prototype.sendLogin = function (success, reason = '') {
this.send({ cmd: 'login', success, reason, account: getAccountInfo(this.account), profile: getProfileInfo(this.profile) });
};

SendStuff.prototype.sendSession = function (success, reason = '', token = undefined) {
if (token === undefined && success) {
token = this.session.token;
}
this.send({ cmd: 'session login', success, reason, session: token });
};

SendStuff.prototype.sendSessionCreate = function (success, reason = '', token = undefined) {
if (token === undefined && success) {
token = this.session.token;
}
this.send({ cmd: 'session create', success: true, reason, session: token });
};
19 changes: 19 additions & 0 deletions JavascriptServer/cmd/senders/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import SendStuff from "#cmd/sendStuff";
import { messageSerialize } from "#schemas/chat";

SendStuff.prototype.sendChatMessage = function (chat_id, message) {
this.send({ cmd: 'chat msg', chat_id, message: messageSerialize(message) });
};

SendStuff.prototype.sendChatHistory = function (chat_id, messages) {
// this.send({ cmd: 'chat history', chat_id, history: messages.map(m => ({ content: m.content, name: m.name, profile_id: null })) });
this.send({ cmd: 'chat history', chat_id, history: messages.map(messageSerialize) });
};

SendStuff.prototype.sendChatInfo = function (chat) {
this.send({ cmd: 'chat info', chat: chat.serialize() });
};

SendStuff.prototype.sendChatsList = function () {
this.send({ cmd: 'chats list', chats: this.chats.map(chat => chat.chat_id) });
};
6 changes: 3 additions & 3 deletions JavascriptServer/cmd/senders/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ SendStuff.prototype.sendLobbyList = function () {
};

/**
* @param {string} lobbyid
* @param {string} lobby_id
*/
SendStuff.prototype.sendLobbyInfo = function (lobbyid) {
this.send({ cmd: 'lobby info', lobby: global.lobbies[lobbyid].getInfo() });
SendStuff.prototype.sendLobbyInfo = function (lobby_id) {
this.send({ cmd: 'lobby info', lobby: global.lobbies[lobby_id].getInfo() });
};
4 changes: 4 additions & 0 deletions JavascriptServer/cmd/senders/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ SendStuff.prototype.sendInvalidInput = function (from_cmd, errors) {
SendStuff.prototype.sendError = function (error, details = '') {
this.send({ cmd: 'error', error, details });
};

SendStuff.prototype.sendKick = function () {
this.send({ cmd: 'server kick' });
};
7 changes: 0 additions & 7 deletions JavascriptServer/cmd/senders/types/auth.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions JavascriptServer/cmd/senders/types/custom.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions JavascriptServer/cmd/senders/types/friends.d.ts

This file was deleted.

10 changes: 0 additions & 10 deletions JavascriptServer/cmd/senders/types/game_loop.d.ts

This file was deleted.

Loading

0 comments on commit 355423c

Please sign in to comment.