Skip to content
Permalink
c4b33a957b
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
152 lines (129 sloc) 4.26 KB
const dotenv = require('dotenv');
const electron = require('electron');
const {app, BrowserWindow, ipcMain, globalShortcut} = electron;
const PubNub = require('pubnub');
const config = require('./config.json');
dotenv.config();
let mainWindow = null;
const pubnub = new PubNub({
publishKey: process.env.PUBNUB_PUBLISH_KEY,
subscribeKey: process.env.PUBNUB_SUBSCRIBE_KEY
});
const myName = process.env.DISCORD_USERNAME;
// Key is made of name (sender's username) and time (when message was sent)
// Value stores the index of the message (how far it is from the top most message widget of this user)
const msgs = new Map();
// Creates the main window
const createWindow = (width, height) => {
mainWindow = new BrowserWindow({
width: width,
height: height,
resizable: false,
frame: false,
//frame: true,
transparent: true,
hasShadow: false,
webPreferences: {
nodeIntegration: true
}
});
mainWindow.loadFile('index.html');
mainWindow.setIgnoreMouseEvents(true);
//mainWindow.setIgnoreMouseEvents(false);
mainWindow.setAlwaysOnTop(true, 'screen-saver');
mainWindow.setVisibleOnAllWorkspaces(true);
mainWindow.maximize();
//mainWindow.toggleDevTools();
mainWindow.show();
}
/*
Receives a new message (log) in the following format: username|date|time|message|metadata
for example: admin|12/03/2021|22:49:27|Hello
and displayes it on the screen
*/
function newMsg(log) {
if (log.length <= 1) return;
const args = log.toString().split('|');
const name = args[0];
const date = args[1];
const time = args[2];
const msg = args[3];
const meta = args[4];
if (name.length <= 1 || msg.length <= 1) return;
if(msg.startsWith(config.prefix)) {
processCommand(name, time, msg);
return;
}
mainWindow.webContents.send('newMsg', name, msg);
// Increment the index of each message (new message is added at the top of the list)
msgs.forEach((value, key) => {
if (key.name == name) {
msgs.set(key, value + 1);
}
});
// Add the new message to the map of messages
const newKey = {
name: name,
time: time
};
msgs.set(newKey, 0);
// Set a timer calling an event in the ipc renderer to remove this message from the screen
setTimeout(() => {
removeMsg(name, time);
}, 6000);
}
// Removes a message from the screen
function removeMsg(name, time) {
msgs.forEach((value, key) => {
if(name == key.name && time == key.time) {
mainWindow.webContents.send('removeMsg', name, value);
msgs.delete(key);
}
});
}
function clearMsgsByUser(name) {
msgs.forEach((value, key) => {
if(name == key.name) {
msgs.delete(key);
}
});
mainWindow.webContents.send('clearMsgsByUser', name);
}
function processCommand(name, time, msg) {
let state = 0;
// User stopped speaking
if(msg.includes('voice 0')) {
state = 0;
}
if(msg.includes('voice 1')) {
state = 1;
}
mainWindow.webContents.send('setUserVoiceState', name, time, state);
}
app.commandLine.appendSwitch('enable-transparent-visuals');
app.disableHardwareAcceleration();
// Called as soon as the electron app is ready
app.on('ready', () => {
setTimeout(() => {
const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize
createWindow(width, height);
pubnub.subscribe({
channels: ['main']
});
pubnub.addListener({
message: (log) => {
newMsg(log.message);
}
});
// User is experiencing frustration/confusion due to poor communication
globalShortcut.register('Shift+=', () => {
mainWindow.webContents.send('frustration');
// Increment the counter on the server side
pubnub.publish({
// TODO: Send the time as well
message: `${myName}|${new Date().toLocaleDateString()}|${new Date().toLocaleTimeString()}|FRUSTRATION`,
channel: 'experiment'
});
});
}, 300);
});