Skip to content

Commit 355423c

Browse files
committed
v7.0 Release
1 parent 4b584b5 commit 355423c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1166
-851
lines changed

JavascriptServer/cmd/handlers/auth.js

+113-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,126 @@
11
import { addHandler } from "#cmd/handlePacket";
2+
import { accountActivate, accountCreate, accountLogin, accountRegister, profileRename, sessionCreate, sessionGet, sessionLogin } from "#util/auth";
3+
import trace from "#util/logging";
4+
import { Names } from "#util/names";
25

36
addHandler('name get', (c) => {
47
c.sendName();
58
});
69

10+
addHandler('name set', async (c, data) => {
11+
let name = data.name;
12+
13+
if (c.logged_in) {
14+
try {
15+
await profileRename(c.profile, name);
16+
c.sendName();
17+
}
18+
catch (e) { }
19+
}
20+
else {
21+
if (!Names.isValid(name)) {
22+
return;
23+
}
24+
if (global.clients.some(client => client.name === name)) {
25+
return;
26+
}
27+
28+
c.name = name;
29+
c.sendName();
30+
}
31+
});
32+
33+
// create a brand new (temporary) account
34+
35+
addHandler('session create', async (c, data) => {
36+
let name = c.name; // default name
37+
38+
try {
39+
c.account = accountCreate(name, '', true);
40+
c.session = sessionCreate(c.account);
41+
42+
await c.register(c.account);
43+
44+
c.sendSessionCreate(true, '', c.session.token);
45+
c.sendLogin(true);
46+
}
47+
catch (reason) {
48+
c.sendSessionCreate(false, reason.toString());
49+
}
50+
});
51+
52+
addHandler('session login', async (c, data) => {
53+
let token = data.session;
54+
55+
try {
56+
c.session = await sessionGet(token);
57+
c.account = await sessionLogin(c.session);
58+
59+
await c.login(c.account);
60+
c.sendSession(true);
61+
c.sendLogin(true);
62+
63+
// another client logged into the same session?
64+
let old_client = global.clients.find((client) => client !== c && client.session?.token === c.session?.token);
65+
66+
if (old_client !== undefined) {
67+
if (old_client.connected) {
68+
old_client.disconnect();
69+
}
70+
71+
old_client.reconnect(c);
72+
}
73+
}
74+
catch (reason) {
75+
trace('error: ' + reason.toString());
76+
c.sendSession(false, reason);
77+
// c.send({ cmd: 'session login', success: false, reason: reason.toString() });
78+
}
79+
});
80+
81+
782
addHandler('login', (c, data) => {
8-
var { username, password } = data;
9-
c.tryLogin(username, password);
83+
let { username, password } = data;
84+
username = username.toLowerCase();
85+
86+
accountLogin(username, password)
87+
.then((account) => {
88+
c.login(account);
89+
c.session = sessionCreate(account);
90+
91+
c.sendLogin(true);
92+
c.sendSession(true);
93+
})
94+
.catch((reason) => {
95+
c.sendLogin(false, reason);
96+
});
1097
});
1198

1299
addHandler('register', (c, data) => {
13-
var { username, password } = data;
14-
c.tryRegister(username, password);
100+
let { username, password } = data;
101+
username = username.toLowerCase();
102+
103+
let promise;
104+
105+
if (c.session && c.account && c.account.temporary) {
106+
promise = accountActivate(c.account, username, password);
107+
}
108+
else {
109+
promise = accountRegister(username, password);
110+
}
111+
112+
promise
113+
.then((account) => {
114+
c.register(account);
115+
c.session = sessionCreate(c.account);
116+
117+
c.sendRegister(true);
118+
c.sendLogin(true);
119+
c.sendSession(true);
120+
})
121+
.catch((reason) => {
122+
c.sendRegister(false, reason);
123+
});
15124
});
16125

17126
// addHandler('logout', (c, data) => {

JavascriptServer/cmd/handlers/chat.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import addHandler from "#cmd/handlePacket";
2+
3+
// list of all the chats of this player
4+
addHandler('chats list', (c, data) => {
5+
c.sendChatsList();
6+
});
+4-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
import { addHandler } from "#cmd/handlePacket";
2-
import { clamp } from "#util/maths";
3-
42

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

9-
c.entity.inputs = {
10-
move: data.move,
11-
keys: {
12-
kright: data.kright,
13-
kleft: data.kleft,
14-
kup: data.kup,
15-
kdown: data.kdown,
16-
17-
kjump: data.kjump,
18-
kjump_rel: data.kjump_rel,
19-
kjump_press: data.kjump_press
20-
}
21-
};
22-
23-
c.entity.inputs.move.x = clamp(c.entity.inputs.move.x, -1, 1);
24-
c.entity.inputs.move.y = clamp(c.entity.inputs.move.y, -1, 1);
7+
for (let input_name in c.entity.inputs) {
8+
if (data[input_name] !== undefined)
9+
c.entity.inputs[input_name] = data[input_name];
10+
}
2511
});
26-

JavascriptServer/cmd/handlers/friends.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ addHandler('friend list', async (c, data) => {
1717
});
1818

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

2323
c.send({ cmd: 'friend req sent', to: data.friend.name });

JavascriptServer/cmd/handlers/lobby.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ addHandler('lobby list', (c) => {
77
});
88

99
addHandler('lobby info', (c, data) => {
10-
var lobbyid = data.lobbyid;
11-
if (lobbyExists(lobbyid))
12-
c.sendLobbyInfo(lobbyid);
10+
const lobby_id = data.lobby_id;
11+
if (lobbyExists(lobby_id))
12+
c.sendLobbyInfo(lobby_id);
1313
});
1414

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

19-
var lobbyid = data.lobbyid;
20-
if (lobbyExists(lobbyid))
21-
c.lobbyJoin(lobbyid);
19+
const lobby_id = data.lobby_id;
20+
if (lobbyExists(lobby_id))
21+
c.lobbyJoin(lobby_id);
2222
});
2323

2424
addHandler('lobby leave', (c, data) => {
25-
var lobby = c.lobby;
25+
let lobby = c.lobby;
2626
if (lobby !== null) {
2727
lobby.kickPlayer(c, 'you left the lobby', false);
2828
}

JavascriptServer/cmd/handlers/party.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { addHandler } from "#cmd/handlePacket";
22
import { partyExists } from "#matchmaking/party";
33

44
addHandler('party join', (c, data) => {
5-
var partyid = data.partyid;
6-
if (partyExists(partyid))
7-
c.partyJoin(partyid);
5+
let party_id = data.party_id;
6+
if (partyExists(party_id))
7+
c.partyJoin(party_id);
88
});
99

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

22-
let { profileid, username } = data;
22+
let { profile_id, username } = data;
2323
let reason = data.reason ?? '';
2424
let member = null;
2525

26-
if (profileid) {
27-
member = global.clients.find(u => u.profile.id === profileid);
26+
if (profile_id) {
27+
member = global.clients.find(u => u.profile.id === profile_id);
2828
}
2929
else {
3030
member = global.clients.find(u => u.name === username);
@@ -45,11 +45,11 @@ addHandler('party disband', (c) => {
4545
});
4646

4747
addHandler('party invite', (c, data) => {
48-
let { profileid, username } = data;
48+
let { profile_id, username } = data;
4949
let user = null;
5050

51-
if (profileid) {
52-
user = global.clients.find(u => u.profile.id === profileid);
51+
if (profile_id) {
52+
user = global.clients.find(u => u.profile.id === profile_id);
5353
}
5454
else {
5555
user = global.clients.find(u => u.name === username);

JavascriptServer/cmd/handlers/system.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ addHandler('client info', (c, data) => {
2828
// immediately close the socket after 1 last packet
2929
setImmediate(() => {
3030
// close the socket
31-
if (c.type == 'tcp') {
31+
if (c.socket_type == 'tcp') {
3232
const s = c.socket;
3333
s.destroy();
3434
}

JavascriptServer/cmd/sendStuff.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ export class SendStuff {
1212
* basic send
1313
* @param {any} data
1414
*/
15-
write(data) {
15+
write(data, shouldQueue = false) {
16+
if (!this.connected) {
17+
if (shouldQueue) {
18+
this.packetQueue.push(data);
19+
}
20+
21+
return;
22+
}
23+
1624
if (global.config.timestamps_enabled) { // { t: ms passed since the server started }
1725
data.t = Date.now() - global.start_time;
1826
}
1927

20-
if (this.type === 'ws') {
28+
if (this.socket_type === 'ws') {
2129
this.socket.send(packet.ws_build(data));
2230
}
2331
else {

JavascriptServer/cmd/senders/auth.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@ SendStuff.prototype.sendName = function () {
77
};
88

99
/**
10-
* @param {string} status
10+
* @param {string} success
1111
* @param {string} [reason='']
1212
*/
13-
SendStuff.prototype.sendRegister = function (status, reason = '') {
14-
this.send({ cmd: 'register', status, reason });
13+
SendStuff.prototype.sendRegister = function (success, reason = '') {
14+
this.send({ cmd: 'register', success, reason });
1515
};
1616

1717
/**
18-
* @param {string} status
18+
* @param {string} success
1919
* @param {string} [reason='']
2020
*/
21-
SendStuff.prototype.sendLogin = function (status, reason = '') {
22-
this.send({ cmd: 'login', status, reason, account: getAccountInfo(this.account), profile: getProfileInfo(this.profile) });
21+
SendStuff.prototype.sendLogin = function (success, reason = '') {
22+
this.send({ cmd: 'login', success, reason, account: getAccountInfo(this.account), profile: getProfileInfo(this.profile) });
23+
};
24+
25+
SendStuff.prototype.sendSession = function (success, reason = '', token = undefined) {
26+
if (token === undefined && success) {
27+
token = this.session.token;
28+
}
29+
this.send({ cmd: 'session login', success, reason, session: token });
30+
};
31+
32+
SendStuff.prototype.sendSessionCreate = function (success, reason = '', token = undefined) {
33+
if (token === undefined && success) {
34+
token = this.session.token;
35+
}
36+
this.send({ cmd: 'session create', success: true, reason, session: token });
2337
};

JavascriptServer/cmd/senders/chat.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import SendStuff from "#cmd/sendStuff";
2+
import { messageSerialize } from "#schemas/chat";
3+
4+
SendStuff.prototype.sendChatMessage = function (chat_id, message) {
5+
this.send({ cmd: 'chat msg', chat_id, message: messageSerialize(message) });
6+
};
7+
8+
SendStuff.prototype.sendChatHistory = function (chat_id, messages) {
9+
// this.send({ cmd: 'chat history', chat_id, history: messages.map(m => ({ content: m.content, name: m.name, profile_id: null })) });
10+
this.send({ cmd: 'chat history', chat_id, history: messages.map(messageSerialize) });
11+
};
12+
13+
SendStuff.prototype.sendChatInfo = function (chat) {
14+
this.send({ cmd: 'chat info', chat: chat.serialize() });
15+
};
16+
17+
SendStuff.prototype.sendChatsList = function () {
18+
this.send({ cmd: 'chats list', chats: this.chats.map(chat => chat.chat_id) });
19+
};

JavascriptServer/cmd/senders/lobby.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ SendStuff.prototype.sendLobbyList = function () {
3737
};
3838

3939
/**
40-
* @param {string} lobbyid
40+
* @param {string} lobby_id
4141
*/
42-
SendStuff.prototype.sendLobbyInfo = function (lobbyid) {
43-
this.send({ cmd: 'lobby info', lobby: global.lobbies[lobbyid].getInfo() });
42+
SendStuff.prototype.sendLobbyInfo = function (lobby_id) {
43+
this.send({ cmd: 'lobby info', lobby: global.lobbies[lobby_id].getInfo() });
4444
};

JavascriptServer/cmd/senders/system.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ SendStuff.prototype.sendInvalidInput = function (from_cmd, errors) {
3131
SendStuff.prototype.sendError = function (error, details = '') {
3232
this.send({ cmd: 'error', error, details });
3333
};
34+
35+
SendStuff.prototype.sendKick = function () {
36+
this.send({ cmd: 'server kick' });
37+
};

JavascriptServer/cmd/senders/types/auth.d.ts

-7
This file was deleted.

JavascriptServer/cmd/senders/types/custom.d.ts

-6
This file was deleted.

JavascriptServer/cmd/senders/types/friends.d.ts

-7
This file was deleted.

JavascriptServer/cmd/senders/types/game_loop.d.ts

-10
This file was deleted.

0 commit comments

Comments
 (0)