Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client Tracking AFK #257

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import Notifications from "@/components/notifications/Notifications.vue";
import PromptContainer from "@/components/prompts/PromptContainer.vue";
import { defaultMaps } from "@/config/default-maps";
import { defaultEngineVersion, defaultGameVersion } from "@/config/default-versions";
import { setIdleTimer } from "@/utils/idle-timer";
import { playRandomMusic } from "@/utils/play-random-music";

const router = useRouter();
Expand Down Expand Up @@ -100,6 +101,13 @@ provide("toggleFriends", toggleFriends);
const toggleDownloads: Ref<((open?: boolean) => void) | undefined> = ref();
provide("toggleDownloads", toggleDownloads);

const setIdleBehavior = setIdleTimer({
onBack: () => {
console.log("back");
},
});
provide("idleTimer", setIdleBehavior);

playRandomMusic();

router.afterEach(async (to, from) => {
Expand Down
13 changes: 12 additions & 1 deletion src/renderer/components/battle/BattleComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
import { Icon } from "@iconify/vue";
import cogIcon from "@iconify-icons/mdi/cog";
import listIcon from "@iconify-icons/mdi/format-list-bulleted";
import { computed, Ref, ref } from "vue";
import { computed, inject, Ref, ref } from "vue";

import BattleChat from "@/components/battle/BattleChat.vue";
import BattleTitleComponent from "@/components/battle/BattleTitleComponent.vue";
Expand All @@ -215,6 +215,7 @@ import { AbstractBattle } from "@/model/battle/abstract-battle";
import { StartPosType } from "@/model/battle/battle-types";
import { LuaOptionSection } from "@/model/lua-options";
import { CurrentUser } from "@/model/user";
import { IdleBehavior } from "@/utils/idle-timer";
import { StartBoxOrientation } from "@/utils/start-boxes";
import { isOfflineBattle, isSpadsBattle } from "@/utils/type-checkers";

Expand All @@ -237,6 +238,16 @@ const gameOptionsOpen = ref(false);
const gameOptions: Ref<LuaOptionSection[]> = ref([]);
const isGameRunning = api.game.isGameRunning;

const updateIdleBehavior: ((update: IdleBehavior) => void) | undefined = inject("idleTimer");

if (updateIdleBehavior != undefined) {
updateIdleBehavior({
onBack: () => {
console.log("Back in lobby!");
},
});
}

function openMapList() {
mapListOpen.value = true;
}
Expand Down
62 changes: 62 additions & 0 deletions src/renderer/utils/idle-timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { onMounted, onUnmounted } from "vue";

export interface IdleBehavior {
onIdle?: () => void;
onBack?: () => void;
seconds?: number;
}

export function setIdleTimer(initial: IdleBehavior) {
let timer, timer_running;

const { onIdle, onBack, seconds } = initial;

let currentOnIdle =
onIdle ??
(() => {
let x;
});
let currentOnBack =
onBack ??
(() => {
let x;
});
let currentSeconds = seconds ?? 5;

setupTimer();

onMounted(() => {
window.addEventListener("mousemove", resetTimer);
window.addEventListener("keypress", resetTimer);
});

onUnmounted(() => {
window.removeEventListener("mousemove", resetTimer);
window.removeEventListener("keypress", resetTimer);
});

function setupTimer() {
timer = setTimeout(() => {
currentOnIdle();
timer_running = false;
}, currentSeconds * 1000);
timer_running = true;
}

function resetTimer() {
if (!timer_running) {
currentOnBack();
}
clearTimeout(timer);
setupTimer();
}

function setIdleBehavior(update: IdleBehavior) {
const { onIdle, onBack, seconds } = update;
if (onIdle != null) currentOnIdle = onIdle;
if (onBack != null) currentOnBack = onBack;
if (seconds != null) currentSeconds = seconds;
}

return setIdleBehavior;
}
Loading