Skip to content

Commit 3fbdcc0

Browse files
committed
chats working??
1 parent c47ca4b commit 3fbdcc0

File tree

16 files changed

+111
-71
lines changed

16 files changed

+111
-71
lines changed

Client/Client.resource_order

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
{"name":"__WarpConfig","order":3,"path":"scripts/__WarpConfig/__WarpConfig.yy",},
5454
{"name":"authHandlers","order":3,"path":"scripts/authHandlers/authHandlers.yy",},
5555
{"name":"authSenders","order":2,"path":"scripts/authSenders/authSenders.yy",},
56+
{"name":"chatHandlers","order":7,"path":"scripts/chatHandlers/chatHandlers.yy",},
5657
{"name":"entityHandlers","order":4,"path":"scripts/entityHandlers/entityHandlers.yy",},
5758
{"name":"friendHandlers","order":6,"path":"scripts/friendHandlers/friendHandlers.yy",},
5859
{"name":"friendSenders","order":6,"path":"scripts/friendSenders/friendSenders.yy",},

Client/Client.yyp

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
addHandler("chat msg", function(data) {
2+
//data.chat_id
3+
//data.message.profile_id
4+
//data.message.name
5+
//data.message.content
6+
trace(data)
7+
})
8+
9+
addHandler("chat history", function(data) {
10+
trace(data.messages)
11+
})

Client/scripts/chatHandlers/chatHandlers.yy

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TypescriptServer/src/cmd/handlers/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ addHandler('session login', async (c, data) => {
4040
c.sendLogin(true);
4141

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

4545
if (old_client !== undefined) {
4646
if (old_client.connected) {

TypescriptServer/src/concepts/chat.ts

+19-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +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";
7+
import { getRandomId } from "#util/random_id";
88

99
export { IChatLog, IMessage };
1010

@@ -14,35 +14,28 @@ export function chatFind(chat_id:string) {
1414
}
1515

1616
export function chatCreate(members:IProfile[] = []) {
17+
let chat_id:string = getRandomId(global.chats);
18+
if (chat_id === null) return null;
19+
1720
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-
}
3121

22+
chatlog._id = chat_id;
3223
let chat = new Chat(chatlog);
3324

3425
for(let member of members) {
35-
chat.addMember(member, true);
26+
chat.addMember(member, null, true);
3627
}
3728

3829
chat.save();
3930
global.chats[chat_id] = chat;
31+
32+
return chat;
4033
}
4134

4235
export class Chat {
4336
chatlog: IChatLog;
4437

45-
online_members: Client[];
38+
online_members: Client[] = [];
4639
get messages(): IMessage[] {
4740
return this.chatlog.messages;
4841
}
@@ -66,13 +59,16 @@ export class Chat {
6659
}
6760

6861

69-
addMember(profile: IProfile, initial = false) {
62+
addMember(profile: IProfile, client = null, initial = false) {
7063
if (this.members.includes(profile.id))
7164
return;
7265

7366
profile.chats.push(this.chatlog.id);
7467
this.members.push(profile.id);
7568

69+
if (client !== null)
70+
this.connectMember(client);
71+
7672
if (!initial)
7773
this.save();
7874
}
@@ -83,14 +79,10 @@ export class Chat {
8379
this.members.splice(idx, 1);
8480

8581
// disconnect the client
86-
idx = this.online_members.indexOf(profile.id);
82+
idx = this.online_members.findIndex(c => c.profile === profile);
8783
if (idx !== -1) {
8884
let client = this.online_members[idx];
89-
this.online_members.splice(idx, 1);
90-
91-
idx = client.chats.indexOf(this);
92-
if (idx !== -1)
93-
client.chats.splice(idx, 1);
85+
this.disconnectMember(client);
9486
}
9587

9688

@@ -103,11 +95,11 @@ export class Chat {
10395
}
10496

10597
connectMember(client: Client) {
106-
if (this.online_members.includes(client))
107-
return;
98+
if (!this.online_members.some(c => (c === client || c.profile?.id === client.profile?.id)))
99+
this.online_members.push(client);
108100

109-
this.online_members.push(client);
110-
client.chats.push(this);
101+
if (!client.chats.includes(this))
102+
client.chats.push(this);
111103
}
112104

113105
disconnectMember(client: Client) {

TypescriptServer/src/concepts/client.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export default class Client extends SendStuff implements IClient {
188188
this.profile.last_online = new Date();
189189
this.name = this.profile.name;
190190

191+
this.chatConnectAll();
192+
191193
this.save();
192194
}
193195

@@ -294,6 +296,8 @@ export default class Client extends SendStuff implements IClient {
294296
if (idx != -1)
295297
global.clients.splice(idx, 1);
296298

299+
this.chatDisconnectAll();
300+
297301
this.disconnect();
298302
}
299303

@@ -654,7 +658,7 @@ export default class Client extends SendStuff implements IClient {
654658

655659
let chat = chatFind(chat_id);
656660
if (chat) {
657-
chat.addMember(this.profile);
661+
chat.addMember(this.profile, this);
658662
}
659663
}
660664

@@ -665,6 +669,13 @@ export default class Client extends SendStuff implements IClient {
665669
this.profile.chats.forEach(chat_id => {
666670
let chat = global.chats[chat_id.toString()];
667671
chat.connectMember(this);
672+
673+
});
674+
}
675+
676+
chatDisconnectAll() {
677+
this.chats.forEach(chat => {
678+
chat.disconnectMember(this);
668679
});
669680
}
670681

TypescriptServer/src/concepts/lobby.ts

+6-15
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@ import * as crypto from 'crypto';
77
import GameMode, { gameModeFind } from '#concepts/game_mode';
88
import GameMap, { GameMapInfo } from './map';
99
import Match from '#matchmaking/match';
10+
import { getRandomId } from '#util/random_id';
1011

1112

1213
// note: only create lobbies with createLobby(), don't call the constructor directly
1314
export function lobbyCreate(map:GameMap) { // returns the lobby instance
14-
if (Object.keys(global.lobbies).length > 900000) return null;
15-
15+
let lobby_id = getRandomId();
16+
if (lobby_id === null) return null;
17+
1618
let lobby = new Lobby(map);
19+
lobby.lobby_id = lobby_id;
1720

18-
// get the ID
19-
while(true) {
20-
// a random 6-digit number
21-
let lobby_id = crypto.randomInt(100000, 999999).toString();
22-
if (lobby_id in global.lobbies) { // just in case of a collision
23-
continue;
24-
}
25-
else {
26-
global.lobbies[lobby_id] = lobby;
27-
lobby.lobby_id = lobby_id;
28-
break;
29-
}
30-
}
21+
global.lobbies[lobby_id] = lobby;
3122

3223
return lobby;
3324
}

TypescriptServer/src/concepts/matchmaking/party.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ProfileInfo } from "#schemas/profile";
44
import Ticket, { MatchRequirements } from "#matchmaking/ticket";
55
import MatchMaker from "#matchmaking/matchmaker";
66
import Match from "#matchmaking/match";
7+
import { getRandomId } from "#util/random_id";
78

89

910
export type PartyInfo = {
@@ -14,20 +15,13 @@ export type PartyInfo = {
1415

1516
// use this instead of calling the new Party() contructor directly
1617
export function partyCreate(leader:Client):Party {
17-
const party = new Party(leader);
18+
let party_id = getRandomId(global.parties);
19+
if (party_id === null) return null;
20+
21+
let party = new Party(leader);
1822

19-
while(true) {
20-
// a random 6-digit number
21-
let party_id = crypto.randomInt(100000, 999999).toString();
22-
if (party_id in global.parties) { // just in case of a collision
23-
continue;
24-
}
25-
else {
26-
global.parties[party_id] = party;
27-
party.party_id = party_id;
28-
break;
29-
}
30-
}
23+
global.parties[party_id] = party;
24+
party.party_id = party_id;
3125

3226
return party;
3327
}

TypescriptServer/src/initializers/00_exit_handler.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import trace from '#util/logging';
22
import chalk from 'chalk';
33

4-
function cleanup() {
5-
// note: only synchronous operations here!
4+
async function cleanup() {
5+
await Promise.all(clients.map(c => c.save()));
6+
trace('saved all client data!');
67
}
78

8-
function onProcessExit(exitCode:number = undefined) {
9+
async function onProcessExit(exitCode:number = undefined) {
910
trace('Running onProcessExit()');
1011

1112
if (exitCode !== undefined)
1213
trace('Exit code:', exitCode);
1314

1415
trace('Running cleanup...');
15-
cleanup();
16+
await cleanup();
1617
trace('Cleanup finished.');
1718

1819

1920
trace('Exiting the process...');
20-
process.exit();
21+
if (this.noexit === undefined)
22+
process.exit();
2123
}
2224

2325
// do something when app is closing
24-
// process.on('exit', onProcessExit.bind({ noexit: true }));
26+
// process.on('exit', () => trace('Exited!'));
2527

2628
//catches ctrl+c event
2729
process.on('SIGINT', onProcessExit);

TypescriptServer/src/initializers/03_console.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import * as readline from 'readline';
44
if (global.config.shell_enabled) {
55
trace('starting the eval console...');
66

7-
const rl = readline.createInterface(process.stdin, process.stdout);
7+
const rl = readline.createInterface({
8+
input: process.stdin,
9+
output: process.stdout
10+
});
811
rl.on('line', async (line) => {
912
try {
1013
let result = eval(line);
@@ -18,7 +21,7 @@ if (global.config.shell_enabled) {
1821
}
1922
});
2023
rl.on('SIGINT', () => {
21-
process.exit();
24+
process.emit('SIGINT');
2225
});
2326
trace('> type right into the console to execute JS code in real time <');
2427
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import Chat from "#concepts/chat";
1+
import Chat, { chatCreate } from "#concepts/chat";
22
import ChatLog from "#schemas/chat";
33

44
const chatLogs = await ChatLog.find({});
55
chatLogs.forEach(chatlog => {
66
let chat = new Chat(chatlog);
77
global.chats[chat.chat_id] = chat;
8-
})
8+
});
9+
10+
11+
// chatCreate([]);

TypescriptServer/src/schemas/chat.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface IChatLog extends Document {
2525

2626
// you can edit this schema!
2727
const chatSchema = new Schema<IChatLog>({
28-
_id: String,
28+
_id: { type: String, unique: true, index: true },
2929

3030
messages: [messageSchema],
3131
members: [

TypescriptServer/src/schemas/profile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IProfile extends Document {
99
last_online: Date,
1010

1111
friends: ObjectId[],
12-
chats: ObjectId[],
12+
chats: string[],
1313

1414
mmr: number,
1515

@@ -32,7 +32,7 @@ const profileSchema = new Schema<IProfile>({
3232
last_online: { type: Date, default: Date.now },
3333

3434
friends: [{ type: Schema.Types.ObjectId, ref: 'Profile' }],
35-
chats: [{ type: Schema.Types.ObjectId, ref: 'Chat' }],
35+
chats: [{ type: String, ref: 'Chat' }], // chat ids are strings
3636
mmr: { type: Number, required: false }, // matchmaking rating
3737

3838

TypescriptServer/src/schemas/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ISession extends Document {
88
}
99

1010
export const SessionSchema = new Schema<ISession>({
11-
token: String,
11+
token: { type: String, index: true },
1212
account_id: { type: Schema.Types.ObjectId, ref: 'Account' }
1313
}, { timestamps: true });
1414

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { randomInt } from "crypto";
2+
3+
export function getRandomId(avoid_keys:Object = {}, digits = 6) {
4+
let id = '';
5+
let a = Math.pow(10, digits-1); // 100'000
6+
let b = Math.pow(10, digits)-1; // 999'999
7+
8+
if (Object.keys(avoid_keys).length >= b/2) return null;
9+
10+
11+
// get a random id, without collisions
12+
while(id == '' || id in avoid_keys) {
13+
// a random k-digit number
14+
id = randomInt(a, b).toString();
15+
}
16+
17+
return id;
18+
}

0 commit comments

Comments
 (0)