Skip to content

Commit

Permalink
chats working??
Browse files Browse the repository at this point in the history
  • Loading branch information
evolutionleo committed Dec 3, 2024
1 parent c47ca4b commit 3fbdcc0
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 71 deletions.
1 change: 1 addition & 0 deletions Client/Client.resource_order
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{"name":"__WarpConfig","order":3,"path":"scripts/__WarpConfig/__WarpConfig.yy",},
{"name":"authHandlers","order":3,"path":"scripts/authHandlers/authHandlers.yy",},
{"name":"authSenders","order":2,"path":"scripts/authSenders/authSenders.yy",},
{"name":"chatHandlers","order":7,"path":"scripts/chatHandlers/chatHandlers.yy",},
{"name":"entityHandlers","order":4,"path":"scripts/entityHandlers/entityHandlers.yy",},
{"name":"friendHandlers","order":6,"path":"scripts/friendHandlers/friendHandlers.yy",},
{"name":"friendSenders","order":6,"path":"scripts/friendSenders/friendSenders.yy",},
Expand Down
1 change: 1 addition & 0 deletions Client/Client.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Client/scripts/chatHandlers/chatHandlers.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
addHandler("chat msg", function(data) {
//data.chat_id
//data.message.profile_id
//data.message.name
//data.message.content
trace(data)
})

addHandler("chat history", function(data) {
trace(data.messages)
})
13 changes: 13 additions & 0 deletions Client/scripts/chatHandlers/chatHandlers.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion TypescriptServer/src/cmd/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ addHandler('session login', async (c, data) => {
c.sendLogin(true);

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

if (old_client !== undefined) {
if (old_client.connected) {
Expand Down
46 changes: 19 additions & 27 deletions TypescriptServer/src/concepts/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Account, { IProfile } from "#schemas/profile";
import { ObjectId } from "mongoose";

import ChatLog, { IChatLog, IMessage } from "#schemas/chat";
import { randomInt } from "crypto";
import { getRandomId } from "#util/random_id";

export { IChatLog, IMessage };

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

export function chatCreate(members:IProfile[] = []) {
let chat_id:string = getRandomId(global.chats);
if (chat_id === null) return null;

let chatlog = new ChatLog();
let chat_id:string;

while(true) {
// a random 6-digit number
chat_id = randomInt(100000, 999999).toString();
if (chat_id in global.chats) { // just in case of a collision
continue;
}
else {
chatlog._id = chat_id;
break;
}
}

chatlog._id = chat_id;
let chat = new Chat(chatlog);

for(let member of members) {
chat.addMember(member, true);
chat.addMember(member, null, true);
}

chat.save();
global.chats[chat_id] = chat;

return chat;
}

export class Chat {
chatlog: IChatLog;

online_members: Client[];
online_members: Client[] = [];
get messages(): IMessage[] {
return this.chatlog.messages;
}
Expand All @@ -66,13 +59,16 @@ export class Chat {
}


addMember(profile: IProfile, initial = false) {
addMember(profile: IProfile, client = null, initial = false) {
if (this.members.includes(profile.id))
return;

profile.chats.push(this.chatlog.id);
this.members.push(profile.id);

if (client !== null)
this.connectMember(client);

if (!initial)
this.save();
}
Expand All @@ -83,14 +79,10 @@ export class Chat {
this.members.splice(idx, 1);

// disconnect the client
idx = this.online_members.indexOf(profile.id);
idx = this.online_members.findIndex(c => c.profile === profile);
if (idx !== -1) {
let client = this.online_members[idx];
this.online_members.splice(idx, 1);

idx = client.chats.indexOf(this);
if (idx !== -1)
client.chats.splice(idx, 1);
this.disconnectMember(client);
}


Expand All @@ -103,11 +95,11 @@ export class Chat {
}

connectMember(client: Client) {
if (this.online_members.includes(client))
return;
if (!this.online_members.some(c => (c === client || c.profile?.id === client.profile?.id)))
this.online_members.push(client);

this.online_members.push(client);
client.chats.push(this);
if (!client.chats.includes(this))
client.chats.push(this);
}

disconnectMember(client: Client) {
Expand Down
13 changes: 12 additions & 1 deletion TypescriptServer/src/concepts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export default class Client extends SendStuff implements IClient {
this.profile.last_online = new Date();
this.name = this.profile.name;

this.chatConnectAll();

this.save();
}

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

this.chatDisconnectAll();

this.disconnect();
}

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

let chat = chatFind(chat_id);
if (chat) {
chat.addMember(this.profile);
chat.addMember(this.profile, this);
}
}

Expand All @@ -665,6 +669,13 @@ export default class Client extends SendStuff implements IClient {
this.profile.chats.forEach(chat_id => {
let chat = global.chats[chat_id.toString()];
chat.connectMember(this);

});
}

chatDisconnectAll() {
this.chats.forEach(chat => {
chat.disconnectMember(this);
});
}

Expand Down
21 changes: 6 additions & 15 deletions TypescriptServer/src/concepts/lobby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@ import * as crypto from 'crypto';
import GameMode, { gameModeFind } from '#concepts/game_mode';
import GameMap, { GameMapInfo } from './map';
import Match from '#matchmaking/match';
import { getRandomId } from '#util/random_id';


// note: only create lobbies with createLobby(), don't call the constructor directly
export function lobbyCreate(map:GameMap) { // returns the lobby instance
if (Object.keys(global.lobbies).length > 900000) return null;

let lobby_id = getRandomId();
if (lobby_id === null) return null;

let lobby = new Lobby(map);
lobby.lobby_id = lobby_id;

// get the ID
while(true) {
// a random 6-digit number
let lobby_id = crypto.randomInt(100000, 999999).toString();
if (lobby_id in global.lobbies) { // just in case of a collision
continue;
}
else {
global.lobbies[lobby_id] = lobby;
lobby.lobby_id = lobby_id;
break;
}
}
global.lobbies[lobby_id] = lobby;

return lobby;
}
Expand Down
20 changes: 7 additions & 13 deletions TypescriptServer/src/concepts/matchmaking/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProfileInfo } from "#schemas/profile";
import Ticket, { MatchRequirements } from "#matchmaking/ticket";
import MatchMaker from "#matchmaking/matchmaker";
import Match from "#matchmaking/match";
import { getRandomId } from "#util/random_id";


export type PartyInfo = {
Expand All @@ -14,20 +15,13 @@ export type PartyInfo = {

// use this instead of calling the new Party() contructor directly
export function partyCreate(leader:Client):Party {
const party = new Party(leader);
let party_id = getRandomId(global.parties);
if (party_id === null) return null;

let party = new Party(leader);

while(true) {
// a random 6-digit number
let party_id = crypto.randomInt(100000, 999999).toString();
if (party_id in global.parties) { // just in case of a collision
continue;
}
else {
global.parties[party_id] = party;
party.party_id = party_id;
break;
}
}
global.parties[party_id] = party;
party.party_id = party_id;

return party;
}
Expand Down
14 changes: 8 additions & 6 deletions TypescriptServer/src/initializers/00_exit_handler.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import trace from '#util/logging';
import chalk from 'chalk';

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

function onProcessExit(exitCode:number = undefined) {
async function onProcessExit(exitCode:number = undefined) {
trace('Running onProcessExit()');

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

trace('Running cleanup...');
cleanup();
await cleanup();
trace('Cleanup finished.');


trace('Exiting the process...');
process.exit();
if (this.noexit === undefined)
process.exit();
}

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

//catches ctrl+c event
process.on('SIGINT', onProcessExit);
Expand Down
7 changes: 5 additions & 2 deletions TypescriptServer/src/initializers/03_console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import * as readline from 'readline';
if (global.config.shell_enabled) {
trace('starting the eval console...');

const rl = readline.createInterface(process.stdin, process.stdout);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', async (line) => {
try {
let result = eval(line);
Expand All @@ -18,7 +21,7 @@ if (global.config.shell_enabled) {
}
});
rl.on('SIGINT', () => {
process.exit();
process.emit('SIGINT');
});
trace('> type right into the console to execute JS code in real time <');
}
Expand Down
7 changes: 5 additions & 2 deletions TypescriptServer/src/initializers/15_chats.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Chat from "#concepts/chat";
import Chat, { chatCreate } from "#concepts/chat";
import ChatLog from "#schemas/chat";

const chatLogs = await ChatLog.find({});
chatLogs.forEach(chatlog => {
let chat = new Chat(chatlog);
global.chats[chat.chat_id] = chat;
})
});


// chatCreate([]);
2 changes: 1 addition & 1 deletion TypescriptServer/src/schemas/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface IChatLog extends Document {

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

messages: [messageSchema],
members: [
Expand Down
4 changes: 2 additions & 2 deletions TypescriptServer/src/schemas/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IProfile extends Document {
last_online: Date,

friends: ObjectId[],
chats: ObjectId[],
chats: string[],

mmr: number,

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

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


Expand Down
2 changes: 1 addition & 1 deletion TypescriptServer/src/schemas/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ISession extends Document {
}

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

Expand Down
18 changes: 18 additions & 0 deletions TypescriptServer/src/util/random_id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { randomInt } from "crypto";

export function getRandomId(avoid_keys:Object = {}, digits = 6) {
let id = '';
let a = Math.pow(10, digits-1); // 100'000
let b = Math.pow(10, digits)-1; // 999'999

if (Object.keys(avoid_keys).length >= b/2) return null;


// get a random id, without collisions
while(id == '' || id in avoid_keys) {
// a random k-digit number
id = randomInt(a, b).toString();
}

return id;
}

0 comments on commit 3fbdcc0

Please sign in to comment.