Skip to content

Commit 7c119d6

Browse files
committed
lists of chats
1 parent e37db41 commit 7c119d6

File tree

11 files changed

+214
-77
lines changed

11 files changed

+214
-77
lines changed

TypescriptServer/src/cmd/handlers/party.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import Client from "#concepts/client";
44
import trace from "#util/logging";
55

66
addHandler('party join', (c, data) => {
7-
let partyid = data.partyid;
8-
if (partyExists(partyid))
9-
c.partyJoin(partyid);
7+
let party_id = data.party_id;
8+
if (partyExists(party_id))
9+
c.partyJoin(party_id);
1010
});
1111

1212
addHandler('party leave', (c) => {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SendStuff from "#cmd/sendStuff";
2+
import { IMessage } from "#schemas/chat";
3+
4+
declare module '#cmd/sendStuff' {
5+
interface SendStuff {
6+
sendChatMessage(chat_id:string, message:IMessage):void
7+
// sendSomething():void
8+
}
9+
}
10+
11+
/**
12+
* @param {}
13+
*/
14+
// SendStuff.prototype.sendSomething = function() {
15+
// this.send({ cmd: '', })
16+
// }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addValidator } from "#cmd/validator";
22

33
addValidator(['party join'], {
4-
partyid: 'string'
4+
party_id: 'string'
55
});

TypescriptServer/src/concepts/chat.ts

Lines changed: 108 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,137 @@ import Account, { IProfile } from "#schemas/profile";
33

44
import { ObjectId } from "mongoose";
55

6+
import ChatLog, { IChatLog, IMessage } from "#schemas/chat";
67

7-
export interface IMessage {
8-
profile_id: ObjectId | string;
9-
name: string;
10-
content: string;
8+
export { IChatLog, IMessage };
9+
10+
11+
export function chatFind(chat_id:string) {
12+
return global.chats[chat_id];
1113
}
1214

13-
export class Message implements IMessage {
14-
id: number;
15-
profile_id: string;
16-
name: string;
17-
content: string;
15+
export function chatCreate(members:IProfile[] = []) {
16+
let chat = new Chat(new ChatLog());
17+
18+
for(let member of members) {
19+
chat.addMember(member, true);
20+
}
21+
22+
chat.save();
23+
24+
let chat_id = chat.chat_id;
25+
global.chats[chat_id] = chat;
1826
}
1927

20-
// export class Chat {
21-
// chat_id: string; // id of the chat room
22-
// online_members: Client[];
23-
// members: string[]; // profile_id[]
24-
// messages: Message[];
25-
26-
// constructor(members?: Client[]) {
27-
// if (members) {
28-
// for(let member in members) {
29-
// this.addMember(member);
30-
// }
31-
// }
32-
33-
// }
28+
export class Chat {
29+
chatlog: IChatLog;
3430

35-
// async save() {
31+
online_members: Client[];
32+
get messages(): IMessage[] {
33+
return this.chatlog.messages;
34+
}
3635

37-
// }
3836

39-
// addMember(member: Client) {
40-
// this.members.push(member);
41-
// }
37+
// id of the chat room
38+
get chat_id(): string {
39+
return this.chatlog.id.toString();
40+
}
4241

43-
// kickMember(member: Client) {
44-
// let idx = this.members.indexOf(member);
45-
// this.members.splice(idx, 1);
46-
// }
42+
get members():ObjectId[] {
43+
return this.chatlog.members;
44+
}
45+
46+
constructor(chatlog: IChatLog) {
47+
this.chatlog = chatlog;
48+
}
49+
50+
save() {
51+
return this.chatlog.save();
52+
}
53+
54+
55+
addMember(profile: IProfile, initial = false) {
56+
if (this.members.includes(profile.id))
57+
return;
58+
59+
profile.chats.push(this.chatlog.id);
60+
this.members.push(profile.id);
4761

48-
// disconnectMember(member: Client) {
62+
if (!initial)
63+
this.save();
64+
}
65+
66+
kickMember(profile: IProfile) {
67+
let idx = this.members.indexOf(profile.id);
68+
if (idx !== -1)
69+
this.members.splice(idx, 1);
70+
71+
// disconnect the client
72+
idx = this.online_members.indexOf(profile.id);
73+
if (idx !== -1) {
74+
let client = this.online_members[idx];
75+
this.online_members.splice(idx, 1);
76+
77+
idx = client.chats.indexOf(this);
78+
if (idx !== -1)
79+
client.chats.splice(idx, 1);
80+
}
4981

50-
// }
51-
// }
82+
83+
// cut this chat from the profile's chats list
84+
idx = profile.chats.indexOf(this.chatlog.id);
85+
if (idx !== -1)
86+
profile.chats.splice(idx, 1);
87+
88+
this.save();
89+
}
90+
91+
connectMember(client: Client) {
92+
if (this.online_members.includes(client))
93+
return;
94+
95+
this.online_members.push(client);
96+
client.chats.push(this);
97+
}
98+
99+
disconnectMember(client: Client) {
100+
let idx = this.online_members.indexOf(client);
101+
if (idx !== -1)
102+
this.online_members.splice(idx, 1);
103+
104+
idx = client.chats.indexOf(this);
105+
if (idx !== -1)
106+
client.chats.splice(idx, 1);
107+
}
108+
109+
writeMessage(client: Client, content: string) {
110+
const message:IMessage = {
111+
profile_id: client.profile.id,
112+
name: client.name,
113+
content
114+
};
115+
116+
this.messages.push(message);
117+
this.save();
118+
}
119+
}
120+
121+
export default Chat;
52122

53123
// export class GlobalChat extends Chat {
54124
// constructor() {
55-
// super(global.clients);
125+
// // super(global.clients);
56126
// }
57127
// }
58128

59129
// export class DirectChat extends Chat {
60130
// constructor(client1:Client, client2:Client) {
61-
// super([client1, client2]);
131+
// // super([client1, client2]);
62132
// }
63133
// }
64134

65135
// export class GroupChat extends Chat {
66136
// constructor(members: Client[]) {
67-
// super(members);
137+
// // super(members);
68138
// }
69139
// }

TypescriptServer/src/concepts/client.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import MatchMaker from '#matchmaking/matchmaker';
2424
import Ticket, { MatchRequirements } from '#matchmaking/ticket';
2525
import Match, { MatchOutcome } from '#matchmaking/match';
2626
import { Names } from '#util/names';
27+
import { Chat, chatFind } from '#concepts/chat';
2728

2829
export type ClientInfo = {
2930
name: string;
30-
partyid: string;
31+
party_id: string;
3132
lobbyid: string;
3233
room_name: string;
3334
};
@@ -61,6 +62,9 @@ export default class Client extends SendStuff implements IClient {
6162
/** @type {Match} */
6263
match: Match = null;
6364

65+
/** @type {Chat[]} */
66+
chats: Chat[] = [];
67+
6468

6569
/** @type {Account} */
6670
account: IAccount = null;
@@ -324,7 +328,7 @@ export default class Client extends SendStuff implements IClient {
324328
getInfo():ClientInfo {
325329
return {
326330
name: this.name,
327-
partyid: this.party?.partyid,
331+
party_id: this.party?.party_id,
328332
lobbyid: this.lobby?.lobbyid,
329333
room_name: this.room?.level.name
330334
};
@@ -533,12 +537,12 @@ export default class Client extends SendStuff implements IClient {
533537
}
534538

535539
/**
536-
* @param {string} partyid
540+
* @param {string} party_id
537541
*/
538-
partyJoin(partyid: string) {
542+
partyJoin(party_id: string) {
539543
this.matchMakingStop();
540544

541-
let party = partyGet(partyid);
545+
let party = partyGet(party_id);
542546
party.addMember(this);
543547
}
544548

@@ -643,5 +647,26 @@ export default class Client extends SendStuff implements IClient {
643647
this.onLogin();
644648
}
645649

650+
651+
chatJoin(chat_id: string) {
652+
if (!this.profile)
653+
return;
654+
655+
let chat = chatFind(chat_id);
656+
if (chat) {
657+
chat.addMember(this.profile);
658+
}
659+
}
660+
661+
chatConnectAll() {
662+
if (!this.profile)
663+
return;
664+
665+
this.profile.chats.forEach(chat_id => {
666+
let chat = global.chats[chat_id.toString()];
667+
chat.connectMember(this);
668+
});
669+
}
670+
646671
// you add any new methods below
647672
}

TypescriptServer/src/concepts/matchmaking/party.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Match from "#matchmaking/match";
77

88

99
export type PartyInfo = {
10-
partyid: string;
10+
party_id: string;
1111
members: string[];
1212
leader: string;
1313
};
@@ -18,36 +18,36 @@ export function partyCreate(leader:Client):Party {
1818

1919
while(true) {
2020
// a random 6-digit number
21-
let partyid = crypto.randomInt(100000, 999999).toString();
22-
if (partyid in global.parties) { // just in case of a collision
21+
let party_id = crypto.randomInt(100000, 999999).toString();
22+
if (party_id in global.parties) { // just in case of a collision
2323
continue;
2424
}
2525
else {
26-
global.parties[partyid] = party;
27-
party.partyid = partyid;
26+
global.parties[party_id] = party;
27+
party.party_id = party_id;
2828
break;
2929
}
3030
}
3131

3232
return party;
3333
}
3434

35-
export function partyGet(partyid: string):Party {
36-
return global.parties[partyid];
35+
export function partyGet(party_id: string):Party {
36+
return global.parties[party_id];
3737
}
3838

39-
export function partyExists(partyid: string) {
40-
return global.parties.hasOwnProperty(partyid);
39+
export function partyExists(party_id: string) {
40+
return global.parties.hasOwnProperty(party_id);
4141
}
4242

43-
export function partyDelete(partyid: string):void {
44-
let party = global.parties[partyid];
43+
export function partyDelete(party_id: string):void {
44+
let party = global.parties[party_id];
4545
party.disband();
4646
}
4747

4848

4949
export default class Party {
50-
partyid: string;
50+
party_id: string;
5151
members: Client[];
5252
leader: Client;
5353
max_members: number; // inherited from config.party.max_members
@@ -120,7 +120,7 @@ export default class Party {
120120
}
121121

122122
private delete() {
123-
delete global.parties[this.partyid];
123+
delete global.parties[this.party_id];
124124
}
125125

126126
matchMakingStart(req:MatchRequirements):Ticket|string {
@@ -174,7 +174,7 @@ export default class Party {
174174

175175
getInfo():PartyInfo {
176176
return {
177-
partyid: this.partyid,
177+
party_id: this.party_id,
178178
members: this.members.map(m => m.name),
179179
leader: this.leader?.name
180180
};

TypescriptServer/src/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Party from '#matchmaking/party';
66
import { Config } from '#root/config';
77
import GameMode from '#concepts/game_mode';
88
import GameMap from '#concepts/map';
9+
import Chat from '#concepts/chat';
910
import MatchMaker from '#matchmaking/matchmaker';
1011
import { Middleware } from '#cmd/middleware';
1112
import { ValidatorFunction } from '#cmd/validator'
@@ -18,6 +19,7 @@ declare global {
1819
var levels:{[key:string]: GameLevel};
1920
var lobbies:{[key: string]: Lobby};
2021
var parties:{[key: string]: Party};
22+
var chats:{[key: string]: Chat};
2123
var entities:EntityType[];
2224
var game_modes:{[key:string]: GameMode};
2325
var maps:GameMap[];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Chat from "#concepts/chat";
2+
import ChatLog from "#schemas/chat";
3+
4+
const chatLogs = await ChatLog.find({});
5+
chatLogs.forEach(chatlog => {
6+
let chat = new Chat(chatlog);
7+
global.chats[chat.chat_id] = chat;
8+
})

0 commit comments

Comments
 (0)