Skip to content

Commit 45b55ac

Browse files
feat: send game update notifications (#771)
* feat: send game update notifications * refactor(update-notifications): check only hyperplay games * refactor(update-notifications): add ending . * refactor(update-notifications): remove extra } * add module level notifications sent flag --------- Co-authored-by: Brett <27568879+BrettCleary@users.noreply.github.com>
1 parent 000472c commit 45b55ac

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

public/locales/en/translation.json

+7
Original file line numberDiff line numberDiff line change
@@ -877,5 +877,12 @@
877877
},
878878
"release": "Release Date",
879879
"size": "Size"
880+
},
881+
"gameUpdateNotifications": {
882+
"title": "Game Updates Available",
883+
"body": {
884+
"single": "There is an update ready for {{gameName}}.",
885+
"multiple": "{{gameName}} and other games are ready to update."
886+
}
880887
}
881888
}

src/backend/main.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ import {
156156
autoUpdate,
157157
gameManagerMap,
158158
initStoreManagers,
159-
libraryManagerMap
159+
libraryManagerMap,
160+
sendGameUpdatesNotifications
160161
} from './storeManagers'
161162
import { legendarySetup } from 'backend/storeManagers/legendary/setup'
162163

@@ -810,6 +811,13 @@ ipcMain.handle('checkGameUpdates', async (): Promise<string[]> => {
810811
oldGames = [...oldGames, ...gamesToUpdate]
811812
}
812813

814+
sendGameUpdatesNotifications().catch((e) =>
815+
logError(
816+
`Something went wrong sending update notifications: ${e}`,
817+
LogPrefix.Backend
818+
)
819+
)
820+
813821
return oldGames
814822
})
815823

src/backend/storeManagers/index.ts

+59-9
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ import { ipcMain } from 'electron'
1919
import { sendFrontendMessage } from 'backend/main_window'
2020
import { loadEpicHyperPlayGameInfoMap } from './hyperplay/utils'
2121
import { isGameAvailable } from 'backend/api/helpers'
22-
interface GameManagerMap {
23-
[key: string]: GameManager
24-
}
22+
import { notify } from '../dialog/dialog'
23+
import i18next from 'i18next'
24+
25+
const MAX_GAMES_UPDATE_NOTIFICATIONS = 3
2526

26-
export const gameManagerMap: GameManagerMap = {
27+
export const gameManagerMap: Record<Runner, GameManager> = {
2728
hyperplay: HyperPlayGameManager,
2829
sideload: SideloadGameManager,
2930
gog: GOGGameManager,
3031
legendary: LegendaryGameManager,
3132
nile: NileGameManager
3233
}
3334

34-
interface LibraryManagerMap {
35-
[key: string]: LibraryManager
36-
}
37-
38-
export const libraryManagerMap: LibraryManagerMap = {
35+
export const libraryManagerMap: Record<Runner, LibraryManager> = {
3936
hyperplay: HyperPlayLibraryManager,
4037
sideload: SideloadLibraryManager,
4138
gog: GOGLibraryManager,
@@ -91,6 +88,59 @@ export function autoUpdate(runner: Runner, gamesToUpdate: string[]) {
9188
return gamesToUpdate
9289
}
9390

91+
let notificationsSent = false
92+
93+
// We only check hyperplay games for updates
94+
export async function sendGameUpdatesNotifications() {
95+
if (notificationsSent) {
96+
return
97+
}
98+
notificationsSent = true
99+
const gamesToUpdate: string[] = []
100+
const allGames = await libraryManagerMap.hyperplay.listUpdateableGames()
101+
const gamesToCheck = allGames.slice(0, MAX_GAMES_UPDATE_NOTIFICATIONS)
102+
103+
const gameSettings = await Promise.all(
104+
gamesToCheck.map(async (game) => gameManagerMap.hyperplay.getSettings(game))
105+
)
106+
107+
const notifiableGames = gamesToCheck.filter(async (_game, index) => {
108+
const { ignoreGameUpdates } = gameSettings[index]
109+
return !ignoreGameUpdates
110+
})
111+
112+
gamesToUpdate.push(...notifiableGames)
113+
114+
if (gamesToUpdate.length === 0) {
115+
return
116+
}
117+
118+
const leadGameInfo = gameManagerMap.hyperplay.getGameInfo(gamesToUpdate[0])
119+
120+
const title = i18next.t(
121+
'gameUpdateNotifications.title',
122+
'Game Updates Available'
123+
)
124+
125+
let body = ''
126+
127+
if (gamesToUpdate.length > 1) {
128+
body = i18next.t(
129+
'gameUpdateNotifications.body.multiple',
130+
`${leadGameInfo.title} and other games are ready to update.`,
131+
{ gameName: leadGameInfo.title }
132+
)
133+
} else {
134+
body = i18next.t(
135+
'gameUpdateNotifications.body.single',
136+
`There is an update ready for ${leadGameInfo.title}.`,
137+
{ gameName: leadGameInfo.title }
138+
)
139+
}
140+
141+
notify({ title, body })
142+
}
143+
94144
export async function initStoreManagers() {
95145
await LegendaryLibraryManager.initLegendaryLibraryManager()
96146
await GOGLibraryManager.refresh()

0 commit comments

Comments
 (0)