[feature] add List object

This commit is contained in:
Janis
2022-10-25 18:45:15 +02:00
parent 1315c39ae1
commit d599e5d8ab
6 changed files with 61 additions and 51 deletions

View File

@@ -14,13 +14,12 @@ function connectToTeamSpeak() {
}; };
ws.onopen = (event) => { ws.onopen = (event) => {
ws.send("Connected to TeamSpeak5"); // Send payload to TS5 client
ws.send(JSON.stringify(paylaod)); ws.send(JSON.stringify(paylaod));
}; };
ws.onmessage = (event) => { ws.onmessage = (event) => {
let data = JSON.parse(event.data); let data = JSON.parse(event.data);
//console.log(data);
switch (data.type) { switch (data.type) {
case "auth": case "auth":
@@ -39,19 +38,19 @@ function connectToTeamSpeak() {
console.log(`No handler for event type: ${data.type}`); console.log(`No handler for event type: ${data.type}`);
break; break;
} }
// Draw clientList in HTML object
drawClients(); drawClients();
console.log(clients);
//console.log(channels);
}; };
ws.onerror = (err) => { ws.onerror = (err) => {
console.error(err); console.error(err);
connectToTeamSpeak(); connectToTeamSpeak(); // Reconnected
}; };
ws.onclose = (event) => { ws.onclose = (event) => {
console.log("Disconnected: " + event.reason); console.log("Disconnected: " + event.reason);
connectToTeamSpeak(); connectToTeamSpeak(); // Reconnected
}; };
} }
connectToTeamSpeak(); connectToTeamSpeak();

View File

@@ -1,45 +1,39 @@
function handleAuthMessage(data) { function handleAuthMessage(data) {
console.log("Handling auth message"); console.log("Handling auth message");
channels = parseChannelInfos(data.payload.connections[0].channelInfos); channelList.items = parseChannelInfos(
clients = parseClientInfos(data.payload.connections[0].clientInfos); data.payload.connections[0].channelInfos
thisClient = clients.filter((obj) => { );
return obj.id === data.payload.connections[0].clientId; clientList.items = parseClientInfos(data.payload.connections[0].clientInfos);
})[0]; thisClient = clientList.getById(data.payload.connections[0].clientId);
} }
function handleClientMoved(data) { function handleClientMoved(data) {
const client = clients.filter((obj) => { const client = clientList.getById(data.payload.clientId);
return obj.id === data.payload.clientId;
})[0];
if (data.payload.newChannelId == 0) { if (data.payload.newChannelId == 0) {
// User disconnected // User disconnected
if (client) { if (client) {
console.log(`${client.name} disconnected`); console.log(`${client.name} disconnected`);
clients.splice(clients.indexOf(client), 1); clientList.remove(client);
} }
if (data.payload.clientId == thisClient.id) { if (data.payload.clientId == thisClient.id) {
console.log("You disconnected"); console.log("You disconnected");
clients = []; clientList.clear();
//! Maybe handle channel list here too //! Maybe handle channel list here too
} }
} else { } else {
// User moved channel // User moved channel
if (client) { if (client) {
// Client already exists in list // Client already exists in list
clients.filter((obj) => { clientList.getById(data.payload.clientId).channel = channelList.getById(
return obj.id === data.payload.clientId; data.payload.newChannelId
})[0].channel = channels.filter((obj) => { );
return obj.id === data.payload.newChannelId;
})[0];
} else { } else {
// New Client has to be created // New Client has to be created
clients.push( clientList.add(
new Client( new Client(
data.payload.clientId, data.payload.clientId,
channels.filter((obj) => { channelList.getById(data.payload.newChannelId),
return obj.id === data.payload.newChannelId;
})[0],
data.payload.properties.nickname data.payload.properties.nickname
) )
); );
@@ -48,33 +42,27 @@ function handleClientMoved(data) {
} }
function handleClientPropertiesUpdate(data) { function handleClientPropertiesUpdate(data) {
let client = clients.filter((obj) => { let client = clientList.getById(data.payload.clientId);
return obj.id === data.payload.clientId;
})[0];
if (data.payload.properties.channelGroupInheritedChannelId == 0) { if (data.payload.properties.channelGroupInheritedChannelId == 0) {
if (client) { if (client) {
clients.splice(clients.indexOf(client), 1); clientList.remove(client);
} }
} else { } else {
if (client) { if (client) {
client.channel = channels.filter((obj) => { client.channel = channelList.getById(
return ( data.payload.properties.channelGroupInheritedChannelId
obj.id === data.payload.properties.channelGroupInheritedChannelId );
);
})[0];
client.name = data.payload.properties.nickname; client.name = data.payload.properties.nickname;
client.inputMuted = data.payload.properties.inputMuted; client.inputMuted = data.payload.properties.inputMuted;
client.outputMuted = data.payload.properties.outputMuted; client.outputMuted = data.payload.properties.outputMuted;
} else { } else {
clients.push( clientList.add(
new Client( new Client(
data.payload.clientId, data.payload.clientId,
channels.filter((obj) => { channelList.getById(
return ( data.payload.properties.channelGroupInheritedChannelId
obj.id === data.payload.properties.channelGroupInheritedChannelId ),
);
})[0],
data.payload.properties.nickname, data.payload.properties.nickname,
data.payload.properies.inputMuted, data.payload.properies.inputMuted,
data.payload.properies.outputMuted data.payload.properies.outputMuted
@@ -85,9 +73,7 @@ function handleClientPropertiesUpdate(data) {
} }
function handleTalkStatusChanged(data) { function handleTalkStatusChanged(data) {
let client = clients.filter((obj) => { let client = clientList.getById(data.payload.clientId);
return obj.id === data.payload.clientId;
})[0];
if (client) { if (client) {
client.talkStatus = data.payload.status; client.talkStatus = data.payload.status;
} }

View File

@@ -23,3 +23,31 @@ class Client {
console.log(`Client created: ${this.id} - ${this.name}`); console.log(`Client created: ${this.id} - ${this.name}`);
} }
} }
class List {
constructor(items = []) {
this.items = items;
}
getById(id) {
return this.items.filter((obj) => {
return obj.id === id;
})[0];
}
add(item) {
if (!this.getById(item.id)) {
this.items.push(item);
} else {
console.error(`An item with id ${item.id} already exists in list`);
}
}
remove(item) {
this.items.splice(this.items.indexOf(item), 1);
}
clear() {
this.items = [];
}
}

View File

@@ -21,9 +21,7 @@ function parseClientInfos(clientInfos) {
result.push( result.push(
new Client( new Client(
e.id, e.id,
channels.filter((obj) => { channelList.getById(e.channelId),
return obj.id === e.channelId;
})[0],
e.properties.nickname, e.properties.nickname,
e.properties.inputMuted, e.properties.inputMuted,
e.properties.outputMuted e.properties.outputMuted

View File

@@ -1,11 +1,10 @@
function getClientsInChannel(channel) { function getClientsInChannel(channel) {
let result = []; let result = [];
clients.forEach((e) => { clientList.items.forEach((e) => {
if (e.channel.id == channel.id) { if (e.channel.id == channel.id) {
result.push(e); result.push(e);
} }
}); });
console.log(result);
return result; return result;
} }

View File

@@ -8,16 +8,16 @@
<title>TS5 - OBS Overlay</title> <title>TS5 - OBS Overlay</title>
</head> </head>
<body> <body>
<script src="js/objects.js"></script>
<script> <script>
let clients = []; let clientList = new List();
let channels = []; let channelList = new List();
let thisClient; let thisClient;
</script> </script>
<div id="content"></div> <div id="content"></div>
<!--Scripts--> <!--Scripts-->
<script src="js/utils.js"></script> <script src="js/utils.js"></script>
<script src="js/objects.js"></script>
<script src="js/display_content.js"></script> <script src="js/display_content.js"></script>
<script src="js/parser.js"></script> <script src="js/parser.js"></script>
<script src="js/event_handlers.js"></script> <script src="js/event_handlers.js"></script>