Skip to content

Commit

Permalink
Fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazcash committed Aug 30, 2020
1 parent 8ce3a75 commit ff367a4
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions windows/overlay/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ export class Overlay {

private setup(){
overwolf.games.onGameInfoUpdated.addListener((res) => {
console.debug("1", res);
if (res.gameChanged){
this.players = [];
}
if (this.gameLaunched(res)) {
this.registerEvents();
setTimeout(() => this.setFeatures(), 1000);
}
});

overwolf.games.getRunningGameInfo((res) => {
console.debug("2", res);
if (this.gameRunning(res)) {
this.registerEvents();
setTimeout(() => this.setFeatures(), 1000);
Expand Down Expand Up @@ -87,36 +92,34 @@ export class Overlay {

players.sort((a, b) => a.index - b.index);

if (players.reduce((total, curr) => total + curr.health, 0) === 800){
this.reset();
}

if (this.players.length){
this.updateRoster(players);
} else {
this.setupRoster(players);
if (players.length){
if (this.players.length && players.length){
this.updateRoster(players);
} else {
this.setupRoster(players);
}
}
}

private setupRoster(players: PlayerData[]){
console.info("Initialising roster");
private async setupRoster(players: PlayerData[]){
console.info("Initialising roster", players);

overwolf.games.launchers.events.getInfo(10902, info => {
if (info.success){
this.selfName = info.res.summoner_info.display_name;

players.forEach((data, i) => {
if (data.name !== this.selfName && data.health > 0){
const box = document.querySelector(`#box-${i}`) as HTMLElement;
const player: Player = { name: data.name, box: box };
player.box.style.borderColor = "green";
this.players.push(player);
}
});
if (!this.selfName){
this.selfName = await this.getSummonerName();
}

this.setPosition();
this.reset();

players.forEach((data, i) => {
if (data.name !== this.selfName){
const box = document.querySelector(`#box-${i}`) as HTMLElement;
const player: Player = { name: data.name, box: box };
player.box.style.borderColor = "green";
this.players.push(player);
}
});

this.setPosition();
}

private updateRoster(players: PlayerData[]){
Expand Down Expand Up @@ -145,7 +148,7 @@ export class Overlay {

if (index !== -1){
this.players.splice(index, 1);
player.box.style.visibility = "hidden";
player.box.style.borderColor = "transparent";
this.clear();
}
}
Expand Down Expand Up @@ -189,6 +192,8 @@ export class Overlay {
private reset(){
overwolf.log.info("Reset");

document.querySelectorAll(`.box`).forEach((box: any) => box.style.borderColor = "transparent");

this.players = [];
}

Expand Down Expand Up @@ -256,6 +261,11 @@ export class Overlay {

private async setPosition() {
const gameRes = await this.getGameResolution();

if (gameRes === null){
return;
}

const appRes = await this.getAppResolution();

overwolf.windows.changeSize("overlay", 350, 170)
Expand All @@ -265,10 +275,14 @@ export class Overlay {
private getGameResolution(): Promise<{ width: number, height: number }> {
return new Promise(resolve => {
overwolf.games.getRunningGameInfo((result) => {
resolve({
width: result.logicalWidth,
height: result.logicalHeight
});
if (result && result.logicalWidth){
resolve({
width: result.logicalWidth,
height: result.logicalHeight
});
} else {
resolve(null);
}
});
});
}
Expand All @@ -283,6 +297,21 @@ export class Overlay {
});
});
}

private getSummonerName() : Promise<string> {
return new Promise(resolve => {
overwolf.games.launchers.events.getInfo(10902, info => {
if (info.success){
resolve(info.res.summoner_info.display_name);
} else {
setTimeout(() => {
console.log("Failed to get summoner name, trying again in 2s");
resolve(this.getSummonerName());
}, 2000);
}
});
});
}
}

export const overlay = new Overlay();
Expand Down

0 comments on commit ff367a4

Please sign in to comment.