discord-client/webpack/init/rooms.js

154 lines
3.4 KiB
JavaScript

/* (1) Initialisation
---------------------------------------------------------*/
/* (1) Init @room AVAILABLE types */
gs.set('room', {
text: { list: [], current: 0, visible: true },
voice: { list: [], current: null, visible: true }
});
/* (2) Init room functions */
gs.set('rfunc', {});
/* (3) Initialize room navigation */
gs.get.rfunc.open = function(){};
/* (4) Initialize adding fetched room */
gs.get.rfunc.dump = function(){};
/* (5) Initialize accessor for room data */
gs.get.rfunc.get = function(){};
/* (2) room navigation
*
* @type<int> room type
* @id<int> room ID (NULL uses get())
*
* @return status<boolean> Whether the navigation has been successful
*
---------------------------------------------------------*/
gs.get.rfunc.nav = function(type=null, id=null){
console.log(`room.nav(${type}, ${id})`);
/* (1) Try to get current room from id */
let room = gs.get.rfunc.get(type, id);
/* (2) Manage invalid room */
if( room == null )
return false;
/* (3) Close last room */
/* (4) Open new room */
/* (5) Update @active room */
this[type].current = room.id;
console.log(`[room.${type}.opened] ${room.name} (${room.id})`, room.id);
return true;
}.bind(gs.get.room);
/* (3) Dump/Update room data (raw format -> tree structure)
*
* @rooms<array> rooms data (raw format)
*
* {raw_format}
* [
* { rid: <int>, type: 'text', name: <string> }
* { rid: <int>, type: 'voice', name: <string> }
* { rid: <int>, type: 'video', name: <string> }
* ]
*
* @return udpated<boolean> Whether rooms have been updated
*
---------------------------------------------------------*/
gs.get.rfunc.dump = function(rooms){
console.log(`room.dump([${rooms instanceof Array?rooms.length:0}])`);
/* (1) Check @rooms type */
if( !(rooms instanceof Array) )
return false;
/* (2) Clear data */
for( let type in this ){
this[type].list = [];
this[type].current = null;
}
/* (3) Browse each room */
for(let r of rooms){
// {1} Ignore: if not object //
if( typeof r !== 'object' )
continue;
// {2} Ignore: if missing field //
if( isNaN(r.rid) || typeof r.type !== 'string' || typeof r.name !== 'string' )
continue;
// {3} Ignore: not available type //
if( this[r.type] == null )
continue;
this[r.type].list.push({ id: r.rid, name: r.name, type: r.type });
}
return true;
}.bind(gs.get.room);
/* (4) Get room data
*
* @type<int> room type
* @id<int> room ID
* NULL: current room || from URL (and set current)
*
* @return room<array> room data
*
---------------------------------------------------------*/
gs.get.rfunc.get = function(type=null, id=null){
console.log(`room.get(${type}, ${id})`);
/* (1) Manage invalid @type */
if( typeof type !== 'string' || this[type] == null )
return null;
/* (1) Get @current room: if id is null
---------------------------------------------------------*/
if( id === null ){
/* (1) If @current is set */
if( !isNaN(this[type].current) ){
/* (2) Return matching id in list */
for( let r of this[type].list ){
if( r.id === this[type].current )
return r; // exit point
}
}
}
/* (2) Get room data
---------------------------------------------------------*/
/* (1) Return room matching id */
for( let r of this[type].list )
if( r.id === id )
return r; // exit point
/* (2) Return default: if ID not found */
this[type].current = null;
return null;
}.bind(gs.get.room);