Skip to content

Commit

Permalink
Implement lock-champ & hover-champ
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsmid committed Aug 15, 2020
1 parent b1c587c commit b737d29
Show file tree
Hide file tree
Showing 16 changed files with 1,189 additions and 177 deletions.
3 changes: 3 additions & 0 deletions desktop/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Local cache
/twitch-auth
116 changes: 0 additions & 116 deletions desktop/express-server/index.js
Original file line number Diff line number Diff line change
@@ -1,116 +0,0 @@
const express = require('express');
const fetch = require('node-fetch');
const LCUConnector = require('lcu-connector');
const ngrok = require('ngrok');

Buffer = Buffer || require('buffer').Buffer;

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

const btoa = string => Buffer.from(string).toString('base64');
const jsonResponse = res => res.json();

let lcuConnector;

const PORT = 6969;
let server;

const TWITCH_APP_CLIENT_ID = 'vgg3y1iox13ljlp25hogabv408qz0m';
const TWITCH_APP_CLIENT_SECRET = 'apl0hcj3qpdb5bialb7hq3twhoruol'; // TODO: Hide this.
const TWITCH_REDIRECT_URL = `http://localhost:${PORT}/twitch/oauth/redirect`;

module.exports.startExpressServer = () => {
return new Promise((resolve, reject) => {
try {
lcuConnector = new LCUConnector();
server = express();
global.expressServerPort = PORT;

server.get('/', (req, res) => {
res.send('Hello, world!');
});

server.get('/request', async (req, res) => {
handleRequest(req, res, 'GET');
});

server.post('/request', (req, res) => {
handleRequest(req, res, 'POST');
});

server.get('/twitch/oauth/redirect', async (req, res) => {
const {code} = req.query;

const accessToken = await fetchTwitchAccessToken(code);

global.twitchAuthStorage.setItem('accessToken', JSON.stringify(accessToken));

global.mainWindow.webContents.send('gotTwitchAccessToken', accessToken);
});

server.listen(PORT, async () => {
global.ngrokUrl = await ngrok.connect(PORT);

lcuConnector.on('connect', data => {
console.log('LCU data: ', data);

global.LCU_data = data;
global.LCU_auth = `${btoa(`${global.LCU_data.username}:${global.LCU_data.password}`)}`;

resolve(true);
});

lcuConnector.on('disconnect', () => {
console.warn(`WARN: LCU connector disconnected.`);
});

lcuConnector.start();

// TODO: [later] Mention the timeout in UI
// TODO: [later] Allow user to customize duration to timeout
// TODO: Figure out a nicer way to signal timeout to UI
setTimeout(() => {
reject(`[LCU_TIMEOUT]: Express server timed out while attempting to read LCU data. Probably because no running League Client was found.`);
}, 10000);
});
} catch (error) {
reject(error);
}
});
};

const handleRequest = async (req, res, type = 'GET') => {
const {endpoint} = req.query;
console.log(`Handling [${type}] request for endpoint '${endpoint}'...`);

const response = await sendRequest(endpoint, {method: type});

console.log(`Response for endpoint '${endpoint}': `, response);

res.json(response);
};

const sendRequest = (endpoint, options = {}) => {
const {protocol, address, port} = global.LCU_data;
const url = `${protocol}://${address}:${port}${endpoint}`;

options = {
...options,
headers: {
Accept: 'application/json',
Authorization: `Basic ${global.LCU_auth}`
}
};

return fetch(url, options)
.then(jsonResponse)
.catch(err => console.log(`ERROR: `, err));
};

const fetchTwitchAccessToken = code => {
const url = `https://id.twitch.tv/oauth2/token?client_id=${TWITCH_APP_CLIENT_ID}&client_secret=${TWITCH_APP_CLIENT_SECRET}&code=${code}&grant_type=authorization_code&redirect_uri=${TWITCH_REDIRECT_URL}`;

return fetch(url, {method: 'POST'})
.then(jsonResponse)
.catch(console.error);
};
14 changes: 14 additions & 0 deletions desktop/express-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions desktop/express-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "MIT",
"dependencies": {
"buffer": "^5.6.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"lcu-connector": "^2.1.3",
"ngrok": "^3.2.7",
Expand Down
Loading

0 comments on commit b737d29

Please sign in to comment.