Skip to content

Commit

Permalink
fix: idle timer not working properly when blurred
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraviolet-jordan committed Aug 6, 2024
1 parent c04cf27 commit 5e455bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
20 changes: 16 additions & 4 deletions src/js/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ class Game extends Client {
this.hintType = 0;
this.menuSize = 0;
this.menuVisible = false;
this.idleCycles = 0;
this.idleCycles = Date.now();
for (let i: number = 0; i < 100; i++) {
this.messageText[i] = null;
}
Expand Down Expand Up @@ -1296,12 +1296,24 @@ class Game extends Client {
}

await this.handleInputKey();
this.idleCycles++;
if (this.idleCycles > 4500) {
// idlecycles refactored to use date to circumvent browser throttling the
// timers when a different tab is active, or the window has been minimized.
// afk logout has to still happen after 90s of no activity (if allowed).
// https://developer.chrome.com/blog/timer-throttling-in-chrome-88/
if (Date.now() - this.idleCycles > 90_000) {
// 4500 ticks * 20ms = 90000ms
this.idleTimeout = 250;
this.idleCycles -= 500;
// 500 ticks * 20ms = 10000ms
this.idleCycles = Date.now() - 10_000;
this.out.p1isaac(ClientProt.IDLE_TIMER);
}
// === original code ===
// this.idleCycles++;
// if (this.idleCycles > 4500) {
// this.idleTimeout = 250;
// this.idleCycles -= 500;
// this.out.p1isaac(ClientProt.IDLE_TIMER);
// }

this.cameraOffsetCycle++;
if (this.cameraOffsetCycle > 500) {
Expand Down
14 changes: 7 additions & 7 deletions src/js/jagex2/client/GameShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default abstract class GameShell {

protected ingame: boolean = false;

protected idleCycles: number = 0;
protected idleCycles: number = Date.now();
protected mouseButton: number = 0;
protected mouseX: number = 0;
protected mouseY: number = 0;
Expand Down Expand Up @@ -340,7 +340,7 @@ export default abstract class GameShell {
private onkeydown = (e: KeyboardEvent): void => {
const key: string = e.key;

this.idleCycles = 0;
this.idleCycles = Date.now();

const keyCode: {code: number; ch: number} = KeyCodes[key];
if (!keyCode || (e.code.length === 0 && !e.isTrusted)) {
Expand Down Expand Up @@ -418,7 +418,7 @@ export default abstract class GameShell {
private onkeyup = (e: KeyboardEvent): void => {
const key: string = e.key;

this.idleCycles = 0;
this.idleCycles = Date.now();

const keyCode: {code: number; ch: number} = KeyCodes[key];
if (!keyCode || (e.code.length === 0 && !e.isTrusted)) {
Expand Down Expand Up @@ -485,7 +485,7 @@ export default abstract class GameShell {
//Don't 'reset' position (This fixes right click in Android)
if (e.clientX > 0 || e.clientY > 0) this.setMousePosition(e);

this.idleCycles = 0;
this.idleCycles = Date.now();
this.mouseClickX = this.mouseX;
this.mouseClickY = this.mouseY;

Expand Down Expand Up @@ -521,7 +521,7 @@ export default abstract class GameShell {

private onmouseup = (e: MouseEvent): void => {
this.setMousePosition(e);
this.idleCycles = 0;
this.idleCycles = Date.now();
this.mouseButton = 0;

if (InputTracking.enabled) {
Expand All @@ -541,7 +541,7 @@ export default abstract class GameShell {
this.setMousePosition(e);

// mapview applet
this.idleCycles = 0;
this.idleCycles = Date.now();
this.mouseX = -1;
this.mouseY = -1;

Expand All @@ -557,7 +557,7 @@ export default abstract class GameShell {

private onmousemove = (e: MouseEvent): void => {
this.setMousePosition(e);
this.idleCycles = 0;
this.idleCycles = Date.now();

if (InputTracking.enabled) {
InputTracking.mouseMoved(this.mouseX, this.mouseY);
Expand Down

0 comments on commit 5e455bb

Please sign in to comment.