Skip to content

Commit c47ca4b

Browse files
committed
lobbyid -> lobby_id
1 parent 61cb5ba commit c47ca4b

File tree

18 files changed

+108
-75
lines changed

18 files changed

+108
-75
lines changed

Client/objects/oLobbiesDemo/Draw_0.gml

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ draw_set_valign(fa_middle)
66
for(var i = 0; i < array_length(global.lobbies); i++) {
77
var lobby = global.lobbies[i]
88
var lobby_string = string(i+1) + ")" + string({
9-
lobbyid: lobby.lobbyid,
9+
lobby_id: lobby.lobby_id,
1010
//level: lobby.level.name,
1111
//mode: lobby.level.mode,
1212
//room_name: lobby.level.room_name,
@@ -21,11 +21,11 @@ for(var i = 0; i < array_length(global.lobbies); i++) {
2121
draw_text(room_width/2, room_height/2+100, "Press buttons 1-9 to join lobbies\nL to leave\nR to refresh")
2222

2323
if (global.lobby) {
24-
draw_text(room_width/2, room_height-150, "joined lobbyid: " + global.lobby.lobbyid + "\n"
24+
draw_text(room_width/2, room_height-150, "joined lobby_id: " + global.lobby.lobby_id + "\n"
2525
+ "players: " + string(global.lobby.player_count) + "/"
2626
+ string(global.lobby.max_players))
2727
//draw_text(room_width/2, room_height-150,
28-
// str_format("joined lobbyid: %\n players: %/%", global.lobby.lobbyid,
28+
// str_format("joined lobby_id: %\n players: %/%", global.lobby.lobby_id,
2929
// global.lobby.player_count, global.lobby.max_players))
3030
}
3131
else {

Client/objects/oLobbiesDemo/Step_0.gml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ for(var i = 1; i <= 9; i++) {
55
if (keyboard_check_pressed(ord(string(i)))) {
66
trace("pressed %", i)
77
if (i-1 < array_length(global.lobbies)) {
8-
sendLobbyJoin(global.lobbies[i-1].lobbyid)
8+
sendLobbyJoin(global.lobbies[i-1].lobby_id)
99
}
1010
}
1111
}

Client/scripts/gameplaySenders/gameplaySenders.gml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function sendPlayerControls(inputs) {
44
var input_names = variable_struct_get_names(inputs)
55
for(var i = 0; i < variable_struct_names_count(inputs); i++) {
66
var input_name = input_names[i]
7-
data[$ input_name] = self.inputs[input_name]
7+
data[$ input_name] = self.inputs[$ input_name]
88
}
99

1010

Client/scripts/lobbyHandlers/lobbyHandlers.gml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ addHandler("lobby info", function(data) {
99
var lobby = data.lobby
1010
for(var i = 0; i < array_length(global.lobbies); i++) {
1111
var _lobby = global.lobbies[i]
12-
if (_lobby.lobbyid == lobby.lobbyid) {
12+
if (_lobby.lobby_id == lobby.lobby_id) {
1313
global.lobbies[i] = lobby
1414
}
1515
}

Client/scripts/lobbySenders/lobbySenders.gml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
function sendLobbyJoin(lobbyid) {
2-
send({cmd: "lobby join", lobbyid: lobbyid})
1+
function sendLobbyJoin(lobby_id) {
2+
send({cmd: "lobby join", lobby_id: lobby_id})
33
}
44

55
function sendLobbyLeave() {
66
send({cmd: "lobby leave"})
77
}
88

9-
function sendLobbyRequestInfo(lobbyid) {
10-
send({cmd: "lobby info", lobbyid: lobbyid})
9+
function sendLobbyRequestInfo(lobby_id) {
10+
send({cmd: "lobby info", lobby_id: lobby_id})
1111
}
1212

1313
function sendLobbyRequestList() {

TypescriptServer/src/cmd/handlers/auth.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ addHandler('session create', async (c, data) => {
2020

2121
await c.register(c.account);
2222

23-
c.send({ cmd: 'session create', success: true, session: c.session.token });
23+
c.sendSessionCreate(true, '', c.session.token);
2424
c.sendLogin(true);
2525
}
2626
catch (reason) {
27-
c.send({ cmd: 'session create', success: false, reason: reason.toString() });
27+
c.sendSessionCreate(false, reason.toString());
2828
}
2929
});
3030

TypescriptServer/src/cmd/handlers/custom.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { addHandler } from "#cmd/handlePacket";
2+
import { PlayerInputs } from "#entities/player";
23
import Point from "#types/point";
34
import { clamp } from "#util/maths";
45

TypescriptServer/src/cmd/handlers/lobby.ts

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

99
addHandler('lobby info', (c, data) => {
10-
const 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-
const 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) => {

TypescriptServer/src/cmd/senders/auth.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare module "#cmd/sendStuff" {
99
sendRegister(success:boolean, reason?:string)
1010
sendLogin(success:boolean, reason?:string)
1111
sendSession(success:boolean, reason?:string, session_token?:string)
12+
sendSessionCreate(success:boolean, reason?:string, session_token?:string)
1213
}
1314
}
1415

@@ -37,4 +38,11 @@ SendStuff.prototype.sendSession = function(success, reason:string = '', token =
3738
token = this.session.token;
3839
}
3940
this.send({ cmd: 'session login', success, reason, session: token });
41+
}
42+
43+
SendStuff.prototype.sendSessionCreate = function(success, reason:string = '', token = undefined) {
44+
if (token === undefined && success) {
45+
token = this.session.token;
46+
}
47+
this.send({ cmd: 'session create', success: true, reason, session: token });
4048
}

TypescriptServer/src/cmd/senders/chat.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { IMessage } from "#schemas/chat";
44
declare module '#cmd/sendStuff' {
55
interface SendStuff {
66
sendChatMessage(chat_id:string, message:IMessage):void
7-
// sendSomething():void
7+
sendChatHistory(chat_id:string, messages:IMessage[]):void
88
}
99
}
1010

11-
/**
12-
* @param {}
13-
*/
14-
// SendStuff.prototype.sendSomething = function() {
15-
// this.send({ cmd: '', })
16-
// }
11+
SendStuff.prototype.sendChatMessage = function(chat_id:string, message:IMessage) {
12+
this.send({ cmd: 'chat msg', chat_id, message });
13+
}
14+
15+
SendStuff.prototype.sendChatHistory = function(chat_id:string, messages:IMessage[]) {
16+
this.send({ cmd: 'chat history', chat_id, history: messages });
17+
}

TypescriptServer/src/cmd/senders/lobby.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare module '#cmd/sendStuff' {
88
sendLobbyReject(lobby:Lobby, reason?:string):void
99
sendLobbyLeave(lobby:Lobby, reason?:string, forced?:boolean):void
1010
sendLobbyUpdate(lobby: Lobby):void
11-
sendLobbyInfo(lobbyid: string):void
11+
sendLobbyInfo(lobby_id: string):void
1212
sendLobbyList():void
1313
}
1414
}
@@ -49,8 +49,8 @@ SendStuff.prototype.sendLobbyList = function() {
4949
}
5050

5151
/**
52-
* @param {string} lobbyid
52+
* @param {string} lobby_id
5353
*/
54-
SendStuff.prototype.sendLobbyInfo = function(lobbyid:string) {
55-
this.send({ cmd: 'lobby info', lobby: global.lobbies[lobbyid].getInfo()})
54+
SendStuff.prototype.sendLobbyInfo = function(lobby_id:string) {
55+
this.send({ cmd: 'lobby info', lobby: global.lobbies[lobby_id].getInfo()})
5656
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addValidator } from "#cmd/validator";
22

33
addValidator(['lobby info', 'lobby join'], {
4-
lobbyid: { type: 'string', optional: true }
4+
lobby_id: { type: 'string', optional: true }
55
});

TypescriptServer/src/concepts/chat.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Account, { IProfile } from "#schemas/profile";
44
import { ObjectId } from "mongoose";
55

66
import ChatLog, { IChatLog, IMessage } from "#schemas/chat";
7+
import { randomInt } from "crypto";
78

89
export { IChatLog, IMessage };
910

@@ -13,15 +14,28 @@ export function chatFind(chat_id:string) {
1314
}
1415

1516
export function chatCreate(members:IProfile[] = []) {
16-
let chat = new Chat(new ChatLog());
17+
let chatlog = new ChatLog();
18+
let chat_id:string;
19+
20+
while(true) {
21+
// a random 6-digit number
22+
chat_id = randomInt(100000, 999999).toString();
23+
if (chat_id in global.chats) { // just in case of a collision
24+
continue;
25+
}
26+
else {
27+
chatlog._id = chat_id;
28+
break;
29+
}
30+
}
31+
32+
let chat = new Chat(chatlog);
1733

1834
for(let member of members) {
1935
chat.addMember(member, true);
2036
}
2137

2238
chat.save();
23-
24-
let chat_id = chat.chat_id;
2539
global.chats[chat_id] = chat;
2640
}
2741

@@ -112,9 +126,13 @@ export class Chat {
112126
name: client.name,
113127
content
114128
};
115-
116129
this.messages.push(message);
117130
this.save();
131+
132+
// broadcast to all online users
133+
this.online_members.forEach(
134+
member => member.sendChatMessage(this.chat_id, message)
135+
);
118136
}
119137
}
120138

TypescriptServer/src/concepts/client.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Chat, chatFind } from '#concepts/chat';
2929
export type ClientInfo = {
3030
name: string;
3131
party_id: string;
32-
lobbyid: string;
32+
lobby_id: string;
3333
room_name: string;
3434
};
3535

@@ -329,18 +329,18 @@ export default class Client extends SendStuff implements IClient {
329329
return {
330330
name: this.name,
331331
party_id: this.party?.party_id,
332-
lobbyid: this.lobby?.lobbyid,
332+
lobby_id: this.lobby?.lobby_id,
333333
room_name: this.room?.level.name
334334
};
335335
}
336336

337337
// Below are some preset functions (you probably don't want to change them
338338

339339

340-
lobbyJoin(lobbyid?:string) {
340+
lobbyJoin(lobby_id?:string) {
341341
let lobby:Lobby;
342-
if (lobbyid) {
343-
lobby = lobbyFind(lobbyid);
342+
if (lobby_id) {
343+
lobby = lobbyFind(lobby_id);
344344
}
345345
else {
346346
lobby = MatchMaker.findNonfullLobby(this);
@@ -595,9 +595,9 @@ export default class Client extends SendStuff implements IClient {
595595
});
596596
}
597597
if (this.profile !== null) {
598-
// save the current lobbyid
598+
// save the current lobby_id
599599
if (this.lobby !== null) {
600-
this.profile.state.lobbyid = this.lobby.lobbyid;
600+
this.profile.state.lobby_id = this.lobby.lobby_id;
601601
}
602602

603603
await this.profile.save()

TypescriptServer/src/concepts/lobby.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,40 @@ import Match from '#matchmaking/match';
1111

1212
// note: only create lobbies with createLobby(), don't call the constructor directly
1313
export function lobbyCreate(map:GameMap) { // returns the lobby instance
14+
if (Object.keys(global.lobbies).length > 900000) return null;
15+
1416
let lobby = new Lobby(map);
1517

18+
// get the ID
1619
while(true) {
1720
// a random 6-digit number
18-
let lobbyid = crypto.randomInt(100000, 999999).toString();
19-
if (lobbyid in global.lobbies) { // just in case of a collision
21+
let lobby_id = crypto.randomInt(100000, 999999).toString();
22+
if (lobby_id in global.lobbies) { // just in case of a collision
2023
continue;
2124
}
2225
else {
23-
global.lobbies[lobbyid] = lobby;
24-
lobby.lobbyid = lobbyid;
26+
global.lobbies[lobby_id] = lobby;
27+
lobby.lobby_id = lobby_id;
2528
break;
2629
}
2730
}
2831

2932
return lobby;
3033
}
3134

32-
export function lobbyFind(lobbyid:string) {
33-
return global.lobbies[lobbyid];
35+
export function lobbyFind(lobby_id:string) {
36+
return global.lobbies[lobby_id];
3437
}
3538

36-
export function lobbyExists(lobbyid:string) {
37-
return global.lobbies.hasOwnProperty(lobbyid);
39+
export function lobbyExists(lobby_id:string) {
40+
return global.lobbies.hasOwnProperty(lobby_id);
3841
}
3942

40-
export function lobbyDelete(lobbyid:string) {
41-
let lobby = global.lobbies[lobbyid];
43+
export function lobbyDelete(lobby_id:string) {
44+
let lobby = global.lobbies[lobby_id];
4245
lobby.close();
4346

44-
delete global.lobbies[lobbyid];
47+
delete global.lobbies[lobby_id];
4548
}
4649

4750
export function lobbyList():Lobby[] {
@@ -51,7 +54,7 @@ export function lobbyList():Lobby[] {
5154
export type LobbyStatus = 'open' | 'closed';
5255

5356
export type SerializedLobby = {
54-
lobbyid: string,
57+
lobby_id: string,
5558
status: LobbyStatus,
5659
max_players: number,
5760
player_count: number,
@@ -60,7 +63,7 @@ export type SerializedLobby = {
6063
}
6164

6265
export type LobbyInfo = {
63-
lobbyid: string,
66+
lobby_id: string,
6467
status: LobbyStatus,
6568
max_players: number,
6669
player_count: number,
@@ -72,7 +75,7 @@ export type LobbyInfo = {
7275

7376
// in context of an MMO this is a shard/separated world
7477
export default class Lobby extends EventEmitter {
75-
lobbyid:string = '-1'; // assigned when created
78+
lobby_id:string = '-1'; // assigned when created
7679
status:LobbyStatus = 'open';
7780
/** @type {Client[]} */
7881
players:Client[] = [];
@@ -242,7 +245,7 @@ export default class Lobby extends EventEmitter {
242245
// (e.x. we don't want to send functions and everything about every player)
243246
serialize():SerializedLobby {
244247
return {
245-
lobbyid: this.lobbyid,
248+
lobby_id: this.lobby_id,
246249
rooms: global.config.rooms_enabled
247250
? this.rooms.map(r => r.serialize())
248251
: undefined,
@@ -255,7 +258,7 @@ export default class Lobby extends EventEmitter {
255258

256259
getInfo():LobbyInfo {
257260
return {
258-
lobbyid: this.lobbyid,
261+
lobby_id: this.lobby_id,
259262
rooms: global.config.rooms_enabled
260263
? this.rooms.map(r => r.getInfo())
261264
: undefined,

0 commit comments

Comments
 (0)