Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Database Hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Mafrans committed Jun 19, 2020
1 parent db4fa03 commit 5c061f6
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 43 deletions.
12 changes: 11 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
"version": "2.3.4",
"author": "Malte Klüft (Mafrans)",
"description": "Extends Google's Stadia gaming platform with additional features, such as custom filters and in game network monitoring.",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj4rm88698t+T5Pe5jWP8edM6wPBErxwCu0k3Ona/Xc3lt3NJpvW5dabUdcpP3MYBjMQ41V/iq4Q64rObL9csaruxP4Ex3S8SaxBc/sDBxAUjRdYebF76t+APSeTRDs0WiKJgjUoEGBlOtSqffVYO7wpLr/IWUn9z1PfkaV5LY5jrlHJ4o0HUOoVW5v2gTUZV143hdOXQgMH9IFf1MppMzg0m7AVq+8j7L7O5334T0tKzhb2sd9uHp74jv2jTWBK1ykkoDt4ST18ZC6zdXH0/4rI3cJ/r0YlganD6wfNvJTWJ3WMCrvR/7S//qBr9iNrD65BQDFln90JPEeBScjtd8wIDAQAB",
"permissions": [
"declarativeContent",
"storage",
"https://stadiagamedb.com/data/gamedb.json"
"identity",
"https://stadiagamedb.com/data/gamedb.json",
"http://localhost:3000/*"
],
"oauth2": {
"client_id": "401608975485-29rkti0stvi4odvnn6hlomkjs0lrtqbj.apps.googleusercontent.com",
"scopes": [
"http://localhost:3000/auth/google",
"http://localhost:3000/auth/google/callback"
]
},
"background": {
"scripts": [
"background.js"
Expand Down
44 changes: 42 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"homepage": "https://github.com/Mafrans/StadiaPlus#readme",
"devDependencies": {
"@types/es6-promise": "^3.3.0",
"file-loader": "^5.1.0",
"sass": "^1.26.3",
"sass-loader": "^8.0.2",
Expand All @@ -41,6 +42,7 @@
"webpack-cli": "^3.3.11"
},
"dependencies": {
"axios": "^0.19.2",
"bootstrap-vue": "^2.11.0",
"css-loader": "^3.4.2",
"deepmerge": "^4.2.2",
Expand Down
102 changes: 102 additions & 0 deletions src/StadiaPlusDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { LocalStorage } from './Storage';
import axios from 'axios';

const chrome = (window as any).chrome;

export class StadiaPlusDB {
static LFGConnector: LFGConnector;

static url: string;
static authToken: string;
static connected: boolean;

static connect(url: string): Promise<boolean> {
StadiaPlusDB.url = url;
StadiaPlusDB.LFGConnector = new LFGConnector();

return new Promise((resolve, reject) => {
this.testConnection()
.then(connected => {
StadiaPlusDB.connected = connected;

resolve(connected);
})
})
}

static testConnection(): Promise<boolean> {
return new Promise((resolve) => {
axios.get(StadiaPlusDB.url)
.then(() => resolve(true))
.catch(() => resolve(false));
})
}

static getProfile(): Promise<boolean> {
return new Promise((resolve, reject) => {
axios.get(`${StadiaPlusDB.url}/profile?authToken=${StadiaPlusDB.authToken}`)
.then(res => resolve(res.data))
.catch(() => reject({ error: 'Could not connect to profile server' }));
})
}

static isConnected(): boolean {
return StadiaPlusDB.connected && StadiaPlusDB.url != null;
}

static isAuthenticated(): boolean {
return StadiaPlusDB.authToken != null;
}

static authenticate(): Promise<any> {
return new Promise((resolve: Function, reject: Function) => {
if(!StadiaPlusDB.isConnected()) {
reject({
error: 'Not connected to any database'
});
}
if(!chrome.hasOwnProperty('identity') || !chrome.identity.hasOwnProperty('launchWebAuthFlow')) {
reject({
error: 'Current environment does not permit using launchWebAuthFlow'
})
}

chrome.identity.launchWebAuthFlow(
{
url: `${StadiaPlusDB.url}/auth/google?redirect=${chrome.identity.getRedirectURL('database')}`,
interactive: true,
},
(responseUrl: string) => {
const url = new URL(responseUrl);
StadiaPlusDB.authToken = url.hash.substring(1);
LocalStorage.AUTH_TOKEN.set(StadiaPlusDB.authToken, () => {
resolve(StadiaPlusDB.authToken);
});
}
);
});
}
}

export class LFGConnector {
get(game: string): Promise<any> {
return axios.get(`${StadiaPlusDB.url}/lfg/${game}`);
}

post(game: string): Promise<any> {
if(!StadiaPlusDB.isConnected()) {
return new Promise((resolve, reject) => reject({ error: 'Not connected to the StadiaPlusDB database' }));
}
if(!StadiaPlusDB.isAuthenticated()) {
return new Promise((resolve, reject) => reject({ error: 'Not authenticated with StadiaPlusDB' }));
}
return axios({
method: 'post',
url: `${StadiaPlusDB.url}/lfg`,
data: {
authToken: StadiaPlusDB.authToken,
game: game
},
})
}
}
42 changes: 28 additions & 14 deletions src/components/LookingForGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import axios from 'axios';
import { LocalStorage } from '../dist/Storage';
import 'slim-select/dist/slimselect.min.css';
import '../ui/styles/Select.scss';
import { StadiaPlusDB } from '../StadiaPlusDB';

const { chrome, RTCPeerConnection } = (window as any);

Expand Down Expand Up @@ -55,6 +56,8 @@ export class LookingForGroup extends Component {
*/
lookingForGroup: boolean = false;

gameUUID: string = "";

constructor() {
super();
}
Expand All @@ -75,11 +78,11 @@ export class LookingForGroup extends Component {
`
<div class='CTvDXd QAAyWd Fjy05d ivWUhc QSDHyc rpgZzc RkyH1e stadiaplus_button stadiaplus_lookingforgroup-toggle-button' id='${this.id}-togglebutton'>${Language.get('looking-for-group.toggle-button.start')}</div>
<div class='stadiaplus_lookingforgroup-groups'>
<div id='${this.id}-groups' class='stadiaplus_lookingforgroup-groups'>
<h6>Groups</h6>
<span id='${this.id}-refresh' class='material-icons-extended refresh'>refresh</span>
<div class='group-list'></div>
<div id='${this.id}-group-list' class='group-list'></div>
</div>
`,
this.id,
Expand Down Expand Up @@ -142,6 +145,22 @@ export class LookingForGroup extends Component {
Logger.component(Language.get('component.disabled', { name: this.name }));
}

updateGroups(): void {
StadiaPlusDB.LFGConnector.get(this.gameUUID).then(res => {
let html = '';
for(const user of res.data) {
html += `
<div class='stadiaplus_lookingforgroup-group'>
<span class='email'>${user}</span>
</div>
`;
}

this.component.element.querySelector(`#${this.id}-group-list`).innerHTML = html;
})
.catch((err: any) => Logger.error(err.error));
}

/**
* Called every second, makes sure to create components if they don't already exist.
*
Expand All @@ -155,23 +174,18 @@ export class LookingForGroup extends Component {
this.updateRenderer();
this.component.create();

this.gameUUID = location.href.split('/')[location.href.split('/').length-1];

const toggleButton = document.getElementById(this.id + '-togglebutton');

toggleButton.addEventListener('click', () => {
this.lookingForGroup = !this.lookingForGroup;
const splitLoc = location.href.split('/');
const uuid = splitLoc[splitLoc.length-1];

LocalStorage.AUTH_TOKEN.get((result: any) => {
axios({
method: 'post',
url: 'http://localhost:3000/lfg',
data: {
authToken: result[LocalStorage.AUTH_TOKEN.tag],
game: this.lookingForGroup ? uuid : null
},
}).then(console.log);
this.component.element.querySelector(`#${this.id}-groups`).classList.toggle('visible', this.lookingForGroup);

StadiaPlusDB.LFGConnector.post(this.gameUUID).then(() => {
this.updateGroups();
})
.catch((err: any) => Logger.error(err.error));

toggleButton.innerHTML = Language.get('looking-for-group.toggle-button.' + (this.lookingForGroup ? 'stop' : 'start'))
});
Expand Down
5 changes: 5 additions & 0 deletions src/components/styles/LookingForGroup.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
padding: .5rem;
margin-top: 1rem;
border-radius: .25rem;
display: none;

&.visible {
display: block;
}

>h6 {
display: inline-block;
Expand Down
34 changes: 9 additions & 25 deletions src/popup/src/MainPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import logo from './assets/logo.png';
import { Language } from '../../Language';
import axios from 'axios';
import { LocalStorage } from '../../Storage';
import { StadiaPlusDB } from '../../StadiaPlusDB';
export default {
data() {
return {
Expand All @@ -97,35 +98,18 @@ export default {
window.open(url, '_blank');
},
testAuth() {
chrome.identity.launchWebAuthFlow(
{
url:
'http://localhost:3000/auth/google?redirect=' +
chrome.identity.getRedirectURL('database'),
interactive: true,
},
(responseUrl) => {
const url = new URL(responseUrl);
console.log(url);
LocalStorage.AUTH_TOKEN.set(url.hash.substring(1), () => {
LocalStorage.AUTH_TOKEN.get(console.log);
});
}
);
StadiaPlusDB.authenticate()
.then(() => {
StadiaPlusDB.getProfile().then(console.log);
});
},
testLFG() {
LocalStorage.AUTH_TOKEN.get((result) => {
axios({
method: 'post',
url: 'http://localhost:3000/lfg',
data: {
authToken: result[LocalStorage.AUTH_TOKEN.tag],
game: "gameString"
},
}).then(console.log);
})
StadiaPlusDB.LFGConnector.post('test');
},
},
mounted() {
StadiaPlusDB.connect('http://localhost:3000').then(console.log).catch(console.error);
}
};
</script>

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"sourceMap": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"target": "es2015",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node"
Expand Down

0 comments on commit 5c061f6

Please sign in to comment.