Skip to content

Commit

Permalink
feat: send game update notifications (#771)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
eliobricenov and BrettCleary authored Feb 28, 2024
1 parent 000472c commit 45b55ac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
7 changes: 7 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -877,5 +877,12 @@
},
"release": "Release Date",
"size": "Size"
},
"gameUpdateNotifications": {
"title": "Game Updates Available",
"body": {
"single": "There is an update ready for {{gameName}}.",
"multiple": "{{gameName}} and other games are ready to update."
}
}
}
10 changes: 9 additions & 1 deletion src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ import {
autoUpdate,
gameManagerMap,
initStoreManagers,
libraryManagerMap
libraryManagerMap,
sendGameUpdatesNotifications
} from './storeManagers'
import { legendarySetup } from 'backend/storeManagers/legendary/setup'

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

sendGameUpdatesNotifications().catch((e) =>
logError(
`Something went wrong sending update notifications: ${e}`,
LogPrefix.Backend
)
)

return oldGames
})

Expand Down
68 changes: 59 additions & 9 deletions src/backend/storeManagers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ import { ipcMain } from 'electron'
import { sendFrontendMessage } from 'backend/main_window'
import { loadEpicHyperPlayGameInfoMap } from './hyperplay/utils'
import { isGameAvailable } from 'backend/api/helpers'
interface GameManagerMap {
[key: string]: GameManager
}
import { notify } from '../dialog/dialog'
import i18next from 'i18next'

const MAX_GAMES_UPDATE_NOTIFICATIONS = 3

export const gameManagerMap: GameManagerMap = {
export const gameManagerMap: Record<Runner, GameManager> = {
hyperplay: HyperPlayGameManager,
sideload: SideloadGameManager,
gog: GOGGameManager,
legendary: LegendaryGameManager,
nile: NileGameManager
}

interface LibraryManagerMap {
[key: string]: LibraryManager
}

export const libraryManagerMap: LibraryManagerMap = {
export const libraryManagerMap: Record<Runner, LibraryManager> = {
hyperplay: HyperPlayLibraryManager,
sideload: SideloadLibraryManager,
gog: GOGLibraryManager,
Expand Down Expand Up @@ -91,6 +88,59 @@ export function autoUpdate(runner: Runner, gamesToUpdate: string[]) {
return gamesToUpdate
}

let notificationsSent = false

// We only check hyperplay games for updates
export async function sendGameUpdatesNotifications() {
if (notificationsSent) {
return
}
notificationsSent = true
const gamesToUpdate: string[] = []
const allGames = await libraryManagerMap.hyperplay.listUpdateableGames()
const gamesToCheck = allGames.slice(0, MAX_GAMES_UPDATE_NOTIFICATIONS)

const gameSettings = await Promise.all(
gamesToCheck.map(async (game) => gameManagerMap.hyperplay.getSettings(game))
)

const notifiableGames = gamesToCheck.filter(async (_game, index) => {
const { ignoreGameUpdates } = gameSettings[index]
return !ignoreGameUpdates
})

gamesToUpdate.push(...notifiableGames)

if (gamesToUpdate.length === 0) {
return
}

const leadGameInfo = gameManagerMap.hyperplay.getGameInfo(gamesToUpdate[0])

const title = i18next.t(
'gameUpdateNotifications.title',
'Game Updates Available'
)

let body = ''

if (gamesToUpdate.length > 1) {
body = i18next.t(
'gameUpdateNotifications.body.multiple',
`${leadGameInfo.title} and other games are ready to update.`,
{ gameName: leadGameInfo.title }
)
} else {
body = i18next.t(
'gameUpdateNotifications.body.single',
`There is an update ready for ${leadGameInfo.title}.`,
{ gameName: leadGameInfo.title }
)
}

notify({ title, body })
}

export async function initStoreManagers() {
await LegendaryLibraryManager.initLegendaryLibraryManager()
await GOGLibraryManager.refresh()
Expand Down

0 comments on commit 45b55ac

Please sign in to comment.