rewriting... woo

This commit is contained in:
hydrabolt
2015-08-24 17:07:41 +01:00
parent 344f8d73a4
commit 1f77ed226a
10 changed files with 614 additions and 131 deletions

View File

@@ -1,6 +1,8 @@
//discord.js modules
var Endpoints = require("./Endpoints.js");
var User = require("./User.js");
var Server = require("./Server.js");
var Channel = require("./Channel.js");
//node modules
var request = require("superagent");
@@ -34,28 +36,68 @@ class Client {
4 - disconnected
*/
this.userCache = new Map();
this.channelCache = new Map();
this.serverCache = new Map();
this.userCache = [];
this.channelCache = [];
this.serverCache = [];
}
get ready() {
return this.state === 3;
}
get servers() {
return this.serverCache;
}
get channels() {
return this.channelCache;
}
get users() {
return this.userCache;
}
sendPacket(JSONObject){
if(this.websocket.readyState === 1){
this.websocket.send(JSON.stringify(JSONObject));
}
}
//def debug
debug(message) {
console.log(message);
}
on(event, fn){
this.events.set(event, fn);
}
off(event, fn){
this.events.delete(event);
}
keepAlive(){
this.debug("keep alive triggered");
this.sendPacket({
op: 1,
d: Date.now()
});
}
//def trigger
trigger(event) {
var args = [];
for(var arg in arguments){
args.push(arguments[arg]);
}
var evt = this.events.get(event);
if(evt){
evt.apply(this, args.slice(1));
}
}
//def login
login(email = "foo@bar.com", password = "pass1234s", callback = function () { }) {
login(email = "foo@bar.com", password = "pass1234", callback = function () { }) {
var self = this;
@@ -131,6 +173,19 @@ class Client {
self.user = self.addUser( data.user );
for(var _server of data.guilds){
self.addServer(_server);
}
self.trigger("ready");
self.debug(`cached ${self.serverCache.length} servers, ${self.channelCache.length} channels and ${self.userCache.length} users.`);
console.log(self.channelCache[0]);
setInterval(function () {
self.keepAlive.apply(self);
}, data.heartbeat_interval);
break;
default:
@@ -146,10 +201,56 @@ class Client {
//def addUser
addUser(data) {
if (!this.userCache.has(data.id)){
this.userCache.set(data.id, new User(data));
if (!this.getUser("id", data.id)){
this.userCache.push(new User(data));
}
return this.userCache.get(data.id);
return this.getUser("id", data.id);
}
//def addChannel
addChannel(data, serverId) {
if (!this.getChannel("id", data.id)){
this.channelCache.push(new Channel(data, this.getServer("id", serverId)));
}
return this.getChannel("id", data.id);
}
//def addServer
addServer(data){
if(!this.getServer("id", data.id)){
this.serverCache.push(new Server(data, this));
}
return this.getServer("id", data.id);
}
//def getUser
getUser(key, value){
for(var user of this.userCache){
if(user[key] === value){
return user;
}
}
return null;
}
//def getChannel
getChannel(key, value){
for(var channel of this.channelCache){
if(channel[key] === value){
return channel;
}
}
return null;
}
//def getServer
getServer(key = "id", value = "abc123"){
for(var server of this.serverCache){
if(server[key] === value){
return server;
}
}
return null;
}
//def trySendConnData

View File

@@ -1,28 +1,21 @@
var List = require("./list.js").List;
class Channel {
exports.Channel = function(name, server, type, id, isPrivate){
if(!type){ //there's no second argument
var channel = name;
name = channel.name;
server = server;
type = channel.type;
id = channel.id;
isPrivate = channel.is_private;
constructor(data, server) {
this.server = server;
this.name = data.name;
this.type = data.type;
this.id = data.id;
//this.isPrivate = isPrivate; //not sure about the implementation of this...
}
get client() {
return this.server.client;
}
equals(object) {
return object.id === this.id;
}
this.name = name;
this.server = server;
this.type = type;
this.id = id;
this.isPrivate = isPrivate;
this.messages = new List("id", 5000);
}
exports.Channel.equals = function(otherChannel){
if(otherChannel.id === this.id){
return true;
} else {
return false;
}
}
module.exports = Channel;

View File

@@ -1,39 +1,73 @@
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
exports.Server = function( data ) {
this.region = data.region;
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Map();
this.channels = new Map();
this.icon = data.icon;
this.afkTimeout = data.afk_timeout;
this.afkChannelId = data.afk_channel_id;
class Server {
constructor(data, client) {
this.client = client;
this.region = data.region;
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Set();
this.channels = new Set();
this.icon = data.icon;
this.afkTimeout = data.afk_timeout;
this.afkChannelId = data.afk_channel_id;
for ( var x in members ) {
var member = members[ x ].user;
this.members.add( new User( member ) );
for (var member of data.members) {
// first we cache the user in our Discord Client,
// then we add it to our list. This way when we
// get a user from this server's member list,
// it will be identical (unless an async change occurred)
// to the client's cache.
this.members.add(client.addUser(member.user));
}
for (var channel of data.channels) {
this.channels.add(client.addChannel(channel, this.id));
}
}
get iconURL() {
if (!this.icon)
return null;
return `https://discordapp.com/api/guilds/${this.id}/icons/${this.icon}.jpg`;
}
get afkChannel() {
if (!this.afkChannelId)
return false;
return this.getChannel("id", this.afkChannelId);
}
get defaultChannel() {
return this.getChannel("name", "general");
}
get owner() {
return this.client.getUser("id", this.ownerID);
}
// get/set
getChannel(key, value) {
for (var channel of this.channels) {
if (channel[key] === value) {
return channel;
}
}
return null;
}
getMember(key, value){
for (var member of this.members) {
if (member[key] === value) {
return member;
}
}
return null;
}
}
exports.Server.prototype.getIconURL = function(){
if(!this.icon)
return false;
return "https://discordapp.com/api/guilds/"+this.id+"/icons/"+this.icon+".jpg";
}
exports.Server.prototype.getAFKChannel = function(){
if(!this.afkChannelId)
return false;
return this.channels.filter("id", this.afkChannelId, true);
}
exports.Server.prototype.getDefaultChannel = function() {
return this.channels.filter( "name", "general", true );
}
module.exports = Server;