MWSE/Source/Client.js

202 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { CLIENT_SEND_MESSAGE, CLIENT_UPDATE_PROP } = require("./IPC");
const stats = require("./stats");
function Client()
{
/**
* @type {string}
*/
this.id = null;
/**
* @type {import("websocket").connection}
*/
this.socket = null;
/**
* @type {Date}
*/
this.created_at = null;
/**
* @type {Map<string,any>}
*/
this.info = new Map();
/**
* @type {Map<string,any>}
*/
this.store = new Map();
/**
* @type {Set<string>}
*/
this.rooms = new Set();
this.pairs = new Set();
this.requiredPair = false;
this.APNumber = 0;
this.APShortCode = 0;
this.APIPAddress = 0;
this.isProxy = false;
this.proxyProcess = null;
this.sync = function(...args){
process.nextTick(()=>{
for (const name of args) {
CLIENT_UPDATE_PROP(this.id, name, this[name]);
}
})
};
};
/**
* @type {Map<string, Client>}
*/
Client.clients = new Map();
/**
* @param {Client} client
*/
Client.prototype.peerRequest = function(client){
let info = {};
this.store.forEach((value, name) => info[name] = value);
this.pairs.add(client.id);
this.sync('pairs');
client.send([{
from: this.id,
info
},'request/pair']);
};
Client.prototype.match = function(filterObject){
let keys = Object.keys(filterObject);
let size = keys.length;
if(size > this.info.size)
{
return false
}
for (const key of keys)
{
if(this.info.has(key))
{
if(this.info.get(key) != filterObject[key])
{
return false
}
}else{
return false
}
};
return true
};
/**
* @param {Client|string} client
*/
Client.prototype.isSecure = function(client)
{
const { Room } = require("./Services/Room");
if(typeof client == "string")
{
if(Client.clients.has(client))
{
client = Client.clients.get(client);
}else return false;
}else if(!(client instanceof Client)){
return false;
};
// Eşleştirilmiş kullanıcı
if(this.isPaired(client))
{
return true;
}
// Aynı odada bulunan kullanıcı
for (const id of this.rooms) {
let room = Room.rooms.get(id);
if(room)
{
if(room.clients.has(id))
{
return true
}
}
};
return false;
}
/**
* @returns {{pairs:Map<string, Client>,roompairs:Map<string, Client>}}
*/
Client.prototype.getSucureClients = function()
{
const { Room } = require("./Services/Room");
let pairs = new Map();
let roompairs = new Map();
for (const [id, client] of this.pairs)
{
map.set(id, client)
}
// Aynı odada bulunan kullanıcı
for (const id of this.rooms) {
let room = Room.rooms.get(id);
if(room)
{
for (const [id, client] of room.clients)
{
if(id == this.id) continue;
roompairs.set(id, client)
};
}
};
return {
pairs,
roompairs
};
}
/**
* @param {Client} client
*/
Client.prototype.acceptPeerRequest = function(client){
this.pairs.add(client.id);
this.sync('pairs');
client.send([{
from: this.id
},'accepted/pair']);
};
/**
* @param {Client} client
*/
Client.prototype.rejectPeerRequest = function(client){
this.pairs.delete(client.id);
client.pairs.delete(this.id);
this.sync('pairs');
client.send([{
from: this.id
},'rejected/pair']);
};
/**
* @param {Client|string} client
* @returns {Boolean}
*/
Client.prototype.isPaired = function(client){
if(typeof client == "string")
{
return Client.clients.get(client)?.pairs.has(this.id) && this.pairs.has(client)
}
return client.pairs.has(this.id) && this.pairs.has(client.id);
};
Client.prototype.pairList = function(){
return [...this.pairs.values()].filter(e => this.isPaired(e));
};
Client.prototype.send = function(obj){
if(this.isProxy)
{
CLIENT_SEND_MESSAGE(this.id, obj, this.proxyProcess)
}else{
stats.ws_sended_packs++;
this.socket.sendUTF(JSON.stringify(obj));
}
};
exports.Client = Client;