Skip to content

Commit d36e5d3

Browse files
committed
session login fixed
1 parent de985f3 commit d36e5d3

File tree

13 files changed

+128
-28
lines changed

13 files changed

+128
-28
lines changed

Client/objects/oLoginMenu/Create_0.gml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ global.canvas = new SUICanvas()
99

1010
global.canvas.appendChild(new SUIBackButton())
1111

12+
txt_account = global.canvas.appendChild(new SUIText(20, room_height-80,
13+
SUIBind(function() { return $"account: {global.account}\nprofile: {global.profile}" })))
14+
1215
txt_title = global.canvas.appendChild(new SUITitle(0, room_height/2 - 120, "Login"))
1316
txt_title.set("center", room_width/2)
1417

@@ -32,5 +35,8 @@ var btn_w = 120
3235
var btn_h = 40
3336
btn_register = global.canvas.appendChild(new SUIButton(tb_login.get("left"), _y, "register", function() { sendRegister(username, password) }, {w: btn_w, h: btn_h}))
3437
btn_login = global.canvas.appendChild(new SUIButton(0, _y, "log in", function() { sendLogin(username, password) }, {w: btn_w, h: btn_h}))
38+
btn_delete = global.canvas.appendChild(new SUIButton(0, 60, "delete session", function() { file_delete(SESSION_FILE); }, {w: 160, h: btn_h}))
39+
40+
btn_delete.set("right", room_width-20)
3541

3642
btn_login.set("right", tb_password.get("right"))

Client/options/windows/options_windows.yy

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Client/scripts/WarpEvents/WarpEvents.gml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ function onConnect() {
44
sendHello()
55
sendClientInfo()
66
sendRequestTime()
7-
sendNameGet()
7+
//sendNameGet()
8+
9+
sendSession()
810
}
911

1012
function onDisconnect() {

Client/scripts/__WarpConfig/__WarpConfig.gml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ if (!CONFIGS_SET) {
7777
}
7878

7979

80+
#macro SESSION_FILE $"session{MultiClientGetID()}.token"
81+
#macro OLD_SESSIONS_FILE "sessions_old.token"
8082
#macro CONNECT_TIMEOUT 60 * 5 // 5 seconds
8183

8284

Client/scripts/authHandlers/authHandlers.gml

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,48 @@ addHandler("register", function(data) {
3030
})
3131

3232

33-
addHandler("session create", function(data) {
33+
34+
var sessionHandler = function(data) {
3435
if (data.success) {
35-
var file = file_text_open_write("session.token")
36-
file_text_write_string(file, data.session)
36+
var session = data.session
37+
38+
// we save the history of all old sessions just in case
39+
if (file_exists(SESSION_FILE) and OLD_SESSIONS_FILE != "") {
40+
var f1 = file_text_open_read(SESSION_FILE)
41+
var f2 = file_text_open_append(OLD_SESSIONS_FILE)
42+
file_text_write_string(f2, file_text_read_string(f1))
43+
file_text_writeln(f2)
44+
file_text_close(f1)
45+
file_text_close(f2)
46+
}
47+
48+
// write the new received session
49+
var file = file_text_open_write(SESSION_FILE)
50+
file_text_write_string(file, session)
3751
file_text_close(file)
52+
53+
global.session = session
54+
55+
if (data.cmd == "session create") {
56+
trace("Session created successfully.")
57+
}
58+
else if (data.cmd == "session login") {
59+
trace("Session login successful.")
60+
}
3861
}
3962
else {
40-
trace("Failed to create a session. Reason: %", data.reason)
63+
if (data.cmd == "session create") {
64+
trace("Failed to create a session. Reason: %", data.reason)
65+
}
66+
else if (data.cmd == "session login") {
67+
trace("Failed to login with a session. Reason: %", data.reason)
68+
69+
// create a new session if the old one didn't work
70+
//sendSessionCreate()
71+
}
4172
}
42-
})
73+
}
4374

44-
addHandler("session login", function(data) {
45-
var success = data.success
46-
47-
48-
})
75+
76+
addHandler("session create", sessionHandler)
77+
addHandler("session login", sessionHandler)

Client/scripts/authSenders/authSenders.gml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,29 @@ function sendRegister(username, password) {
88

99
function sendNameGet() {
1010
send({cmd: "name get"})
11+
}
12+
13+
function sendSessionCreate() {
14+
send({cmd: "session create"})
15+
}
16+
17+
function sendSessionLogin(session) {
18+
send({cmd: "session login", session})
19+
}
20+
21+
function sendSession() {
22+
if (file_exists(SESSION_FILE)) {
23+
var file = file_text_open_read(SESSION_FILE)
24+
var session = file_text_read_string(file)
25+
file_text_close(file)
26+
27+
trace(SESSION_FILE)
28+
trace("read session: %", session)
29+
30+
global.session = session
31+
sendSessionLogin(session)
32+
}
33+
else {
34+
sendSessionCreate()
35+
}
1136
}

TypescriptServer/src/cmd/handlers/auth.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { addHandler } from "#cmd/handlePacket";
22
import { Account, getAccountInfo, IAccount } from "#schemas/account";
33
import Session from "#schemas/session";
44
import { accountCreate, accountLogin, accountRegister, sessionCreate, sessionGet, sessionLogin } from "#util/auth";
5+
import trace from "#util/logging";
56
import { Names } from "#util/names";
67

78
addHandler('name get', (c) => {
89
c.sendName();
910
});
1011

1112
// create a brand new (temporary) account
13+
1214
addHandler('session create', async (c, data) => {
1315
let name = c.name; // default name
1416

@@ -18,25 +20,29 @@ addHandler('session create', async (c, data) => {
1820

1921
await c.register(c.account);
2022

21-
c.send({ cmd: 'session create', success: true, account: getAccountInfo(c.account), session: c.session.token });
23+
c.send({ cmd: 'session create', success: true, session: c.session.token });
24+
c.sendLogin(true);
2225
}
2326
catch (reason) {
24-
c.send({ cmd: 'session create', success: false, reason: reason });
27+
c.send({ cmd: 'session create', success: false, reason: reason.toString() });
2528
}
2629
});
2730

2831
addHandler('session login', async (c, data) => {
2932
let token = data.session;
33+
trace('session token login: ' + token);
3034

3135
try {
3236
c.session = await sessionGet(token);
3337
c.account = await sessionLogin(c.session);
3438

3539
await c.login(c.account);
36-
c.send({ cmd: 'session login', success: true });
40+
c.send({ cmd: 'session login', success: true, session: c.session.token });
41+
c.sendLogin(true);
3742
}
3843
catch(reason) {
39-
c.send({ cmd: 'session login', success: false, reason: reason });
44+
trace('error: ' + reason.toString());
45+
c.send({ cmd: 'session login', success: false, reason: reason.toString() });
4046
}
4147
});
4248

TypescriptServer/src/concepts/client.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default class Client extends SendStuff implements IClient {
8383
ping: number;
8484

8585
room_join_timer: number = -1; // if >0 - joined a room recently
86+
reconnect_timer: number = -1;
8687

8788
/** @type {boolean} */
8889
get logged_in() {
@@ -237,17 +238,19 @@ export default class Client extends SendStuff implements IClient {
237238
else {
238239
(this.socket as TCPSocket).destroy();
239240
}
241+
}
240242

243+
onDisconnect() {
241244
this.socket = null;
242245
this.connected = false;
243-
}
244246

245-
onDisconnect() {
246-
247+
this.reconnect_timer = config.reconnect_timeout;
248+
247249
// go offline
248250
if (this.logged_in) {
249251
this.profile.online = false;
250252
this.profile.last_online = new Date();
253+
this.save();
251254
}
252255
}
253256

@@ -265,6 +268,8 @@ export default class Client extends SendStuff implements IClient {
265268

266269

267270
global.clients.splice(global.clients.indexOf(this), 1);
271+
272+
this.disconnect();
268273
}
269274

270275
// insert this socket into a "dead" (disconnected) client
@@ -282,6 +287,13 @@ export default class Client extends SendStuff implements IClient {
282287
return old_client;
283288
}
284289

290+
onReconnect() {
291+
if (this.lobby)
292+
this.sendLobbyJoin(this.lobby);
293+
if (this.room && this.entity)
294+
this.sendPlay(this.lobby, this.room, this.entity.pos, this.entity.uuid);
295+
}
296+
285297

286298
getInfo():ClientInfo {
287299
return {
@@ -546,7 +558,7 @@ export default class Client extends SendStuff implements IClient {
546558
if (this.account !== null) {
547559
this.account.save()
548560
.then(() => {
549-
trace('Saved the account successfully');
561+
// trace('Saved the account successfully');
550562
})
551563
.catch((err) => {
552564
trace('Error while saving account: ' + err);
@@ -560,12 +572,21 @@ export default class Client extends SendStuff implements IClient {
560572

561573
this.profile.save()
562574
.then(() => {
563-
trace('Saved the profile successfully.');
575+
// trace('Saved the profile successfully.');
564576
})
565577
.catch((err) => {
566578
trace('Error while saving profile: ' + err);
567579
});
568580
}
581+
if (this.session !== null) {
582+
this.session.save()
583+
.then(() => {
584+
// trace('Saved the session successfully');
585+
})
586+
.catch((err) => {
587+
trace('Error while saving session: ' + err);
588+
})
589+
}
569590
}
570591

571592
/**

TypescriptServer/src/concepts/room.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ class Room extends EventEmitter {
271271
}
272272
// find a new start position
273273
else {
274-
const p = this.level.getStartPos(this.players.length-1);
274+
// const p = this.level.getStartPos(this.players.length-1);
275+
const p = this.level.getStartPos(this.players.indexOf(player));
275276
x = p.x;
276277
y = p.y;
277278
}

TypescriptServer/src/schemas/account.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ export default Account;
2222

2323

2424
export type AccountInfo = {
25+
id: string,
2526
username: string
2627
}
2728

2829
export function getAccountInfo(a:IAccount):AccountInfo {
2930
if (a === null) return null;
3031

3132
return {
33+
id: a.id,
3234
username: a.username
3335
};
3436
}

0 commit comments

Comments
 (0)