Skip to content

Commit 3714ba2

Browse files
committed
big login refactoring + change var -> let
1 parent 0c17ef3 commit 3714ba2

File tree

39 files changed

+517
-346
lines changed

39 files changed

+517
-346
lines changed

Client/objects/oLoginMenu/Create_0.gml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
username = ""
55
password = ""
6-
global.login_status = ""
76
global.login_result = ""
87

98
global.canvas = new SUICanvas()
@@ -13,8 +12,8 @@ global.canvas.appendChild(new SUIBackButton())
1312
txt_title = global.canvas.appendChild(new SUITitle(0, room_height/2 - 120, "Login"))
1413
txt_title.set("center", room_width/2)
1514

16-
txt_status = global.canvas.appendChild(new SUIText(0, 100, SUIBind("global.login_result"), { halign: fa_center }))
17-
txt_status.set("center", room_width/2)
15+
txt_result = global.canvas.appendChild(new SUIText(0, 100, SUIBind("global.login_result"), { halign: fa_center }))
16+
txt_result.set("center", room_width/2)
1817

1918
var tb_w = 192 * 1.5
2019
var tb_h = 48 * 1.5

Client/options/extensions/MultiClient.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"$GMExtensionConfigSet": "GMExtensionConfigSet",
33
"configurables": {
4-
"5d9f79ea-1b81-4c14-9cdb-bd884ee607e4": null,
4+
"5d9f79ea-1b81-4c14-9cdb-bd884ee607e4": {
5+
"Dev": {
6+
"value": "2"
7+
}
8+
},
59
"76c270e6-7809-4838-ade8-701846fc795d": null,
610
"f506fe0e-5a82-4155-8b2a-69e6e6053844": null,
711
"a19af317-c0f4-479d-8f1c-5c292032fe3c": null,

Client/scripts/authHandlers/authHandlers.gml

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,46 @@ addHandler("name set", function(data) {
33
})
44

55
addHandler("login", function(data) {
6-
var status = data.status
7-
if (status == "fail") {
8-
var reason = data.reason
9-
global.login_status = ("Login failed. Reason: " + reason)
10-
}
11-
else if (status == "success") {
6+
var success = data.success
7+
if (success) {
128
global.profile = data.profile
139
global.account = data.account
1410
global.username = global.account.username
1511
global.login_result = ("Login success!")
1612
}
1713
else {
18-
global.login_result = ("Error: invalid login status")
14+
global.login_result = ("Login failed. Reason: " + data.reason)
15+
1916
}
20-
21-
//show_message_async(global.login_result)
2217
})
2318

2419

2520
addHandler("register", function(data) {
26-
var status = data.status
27-
if (status == "fail") {
28-
global.login_result = ("Registration failed.")
29-
}
30-
else if (status == "success") {
21+
var success = data.success
22+
if (success) {
3123
global.login_result = ("Registration successful! You can now login.")
3224
}
3325
else {
34-
global.login_result = ("Error: invalid registration status")
26+
global.login_result = ("Registration failed. Reason: " + data.reason)
3527
}
3628

37-
//show_message_async(global.login_result)
29+
trace(global.login_result)
30+
})
31+
32+
33+
addHandler("session create", function(data) {
34+
if (data.success) {
35+
var file = file_text_open_write("session.token")
36+
file_text_write_string(file, data.session)
37+
file_text_close(file)
38+
}
39+
else {
40+
trace("Failed to create a session. Reason: %", data.reason)
41+
}
42+
})
43+
44+
addHandler("session login", function(data) {
45+
var success = data.success
46+
47+
3848
})

Client/scripts/gameLoopHandlers/gameLoopHandlers.gml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
addHandler("match finding", function(data) {
2+
trace("started matchmaking!")
3+
})
4+
15
addHandler("match found", function(data) {
26
var match = data.match
37

TypescriptServer/src/cmd/handlers/auth.ts

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
import { addHandler } from "#cmd/handlePacket";
2+
import { Account, getAccountInfo, IAccount } from "#schemas/account";
3+
import Session from "#schemas/session";
4+
import { accountCreate, accountLogin, accountRegister, sessionCreate, sessionGet, sessionLogin } from "#util/auth";
5+
import { Names } from "#util/names";
26

37
addHandler('name get', (c) => {
48
c.sendName();
5-
})
9+
});
10+
11+
// create a brand new (temporary) account
12+
addHandler('session create', async (c, data) => {
13+
let name = c.name; // default name
14+
15+
try {
16+
c.account = accountCreate(name, '', true);
17+
c.session = sessionCreate(c.account);
18+
19+
await c.register(c.account);
20+
21+
c.send({ cmd: 'session create', success: true, account: getAccountInfo(c.account), session: c.session.token });
22+
}
23+
catch (reason) {
24+
c.send({ cmd: 'session create', success: false, reason: reason });
25+
}
26+
});
27+
28+
addHandler('session login', async (c, data) => {
29+
let token = data.session;
30+
31+
try {
32+
c.session = await sessionGet(token);
33+
c.account = await sessionLogin(c.session);
34+
35+
await c.login(c.account);
36+
c.send({ cmd: 'session login', success: true });
37+
}
38+
catch(reason) {
39+
c.send({ cmd: 'session login', success: false, reason: reason });
40+
}
41+
});
42+
643

744
addHandler('login', (c, data) => {
8-
var { username, password } = data;
9-
c.tryLogin(username, password);
45+
let { username, password } = data;
46+
username = username.toLowerCase();
47+
48+
accountLogin(username, password)
49+
.then((account:IAccount) => {
50+
// this also sends the message
51+
c.login(account);
52+
c.sendLogin(true);
53+
})
54+
.catch((reason) => {
55+
c.sendLogin(false, reason);
56+
});
1057
});
1158

1259
addHandler('register', (c, data) => {
13-
var { username, password } = data;
14-
c.tryRegister(username, password);
60+
let { username, password } = data;
61+
username = username.toLowerCase();
62+
63+
accountRegister(username, password)
64+
.then((account:IAccount) => {
65+
c.register(account);
66+
c.sendRegister(true);
67+
})
68+
.catch((reason) => {
69+
c.sendRegister(false, reason);
70+
});
1571
});
1672

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

TypescriptServer/src/cmd/handlers/custom.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ addHandler('player controls', (c, data) => {
88

99
c.entity.inputs = {
1010
move: data.move as Point,
11-
keys: {
12-
kright: data.kright,
13-
kleft: data.kleft,
14-
kup: data.kup,
15-
kdown: data.kdown,
11+
kright: data.kright,
12+
kleft: data.kleft,
13+
kup: data.kup,
14+
kdown: data.kdown,
1615

17-
kjump: data.kjump,
18-
kjump_rel: data.kjump_rel,
19-
kjump_press: data.kjump_press
20-
}
16+
kjump: data.kjump,
17+
kjump_rel: data.kjump_rel,
18+
kjump_press: data.kjump_press
2119
}
2220

2321
c.entity.inputs.move.x = clamp(c.entity.inputs.move.x, -1, 1);

TypescriptServer/src/cmd/handlers/friends.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ addHandler('friend list', async (c, data) => {
1818
});
1919

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

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

TypescriptServer/src/cmd/handlers/lobby.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ addHandler('lobby list', (c) => {
77
});
88

99
addHandler('lobby info', (c, data) => {
10-
var lobbyid = data.lobbyid;
10+
const lobbyid = data.lobbyid;
1111
if (lobbyExists(lobbyid))
1212
c.sendLobbyInfo(lobbyid);
1313
});
@@ -16,13 +16,13 @@ addHandler('lobby join', (c, data) => {
1616
if (!global.config.lobby.allow_join_by_id)
1717
return;
1818

19-
var lobbyid = data.lobbyid;
19+
const lobbyid = data.lobbyid;
2020
if (lobbyExists(lobbyid))
2121
c.lobbyJoin(lobbyid);
2222
});
2323

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

TypescriptServer/src/cmd/handlers/party.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Client from "#concepts/client";
44
import trace from "#util/logging";
55

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

TypescriptServer/src/cmd/handlers/system.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ addHandler('client info', (c, data) => {
1313

1414
const client_game_version = data.game_version;
1515
const client_warp_version = data.warp_version;
16-
const compatible_versions = global.config.meta.compatible_game_versions
16+
const compatible_versions = global.config.meta.compatible_game_versions;
1717
const warp_version = global.config.meta.warp_version;
1818

1919
const warp_version_match = semver.cmp(client_warp_version, '=', warp_version);
@@ -30,7 +30,7 @@ addHandler('client info', (c, data) => {
3030
// immediately close the socket after 1 last packet
3131
setImmediate(() => {
3232
// close the socket
33-
if (c.type == 'tcp') {
33+
if (c.socket_type == 'tcp') {
3434
const s = c.socket as TCPSocket;
3535
s.destroy();
3636
}

0 commit comments

Comments
 (0)