|
| 1 | +import { Entity, EntityManager, Enum, Filter, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core' |
| 2 | +import { Request, Required, ValidationCondition } from 'koa-clay' |
| 3 | +import { decrypt, encrypt } from '../lib/crypto/string-encryption' |
| 4 | +import Game from './game' |
| 5 | +import { createSteamworksLeaderboard, createSteamworksLeaderboardEntry, deleteSteamworksLeaderboard, deleteSteamworksLeaderboardEntry, setSteamworksStat, syncSteamworksLeaderboards, syncSteamworksStats } from '../lib/integrations/steamworks-integration' |
| 6 | +import Leaderboard from './leaderboard' |
| 7 | +import pick from 'lodash.pick' |
| 8 | +import LeaderboardEntry from './leaderboard-entry' |
| 9 | +import { PlayerAliasService } from './player-alias' |
| 10 | +import PlayerGameStat from './player-game-stat' |
| 11 | + |
| 12 | +export enum IntegrationType { |
| 13 | + STEAMWORKS = 'steamworks' |
| 14 | +} |
| 15 | + |
| 16 | +export type SteamIntegrationConfig = { |
| 17 | + apiKey: string |
| 18 | + appId: number |
| 19 | + syncLeaderboards: boolean |
| 20 | + syncStats: boolean |
| 21 | +} |
| 22 | + |
| 23 | +export type IntegrationConfig = SteamIntegrationConfig |
| 24 | + |
| 25 | +@Entity() |
| 26 | +@Filter({ name: 'active', cond: { deletedAt: null }, default: true }) |
| 27 | +export default class Integration { |
| 28 | + @PrimaryKey() |
| 29 | + id: number |
| 30 | + |
| 31 | + @Required({ |
| 32 | + methods: ['POST'], |
| 33 | + validation: async (val: unknown, req: Request): Promise<ValidationCondition[]> => { |
| 34 | + const keys = Object.keys(IntegrationType).map((key) => IntegrationType[key]) |
| 35 | + |
| 36 | + const { gameId, id } = req.params |
| 37 | + const duplicateIntegrationType = await (<EntityManager>req.ctx.em).getRepository(Integration).findOne({ |
| 38 | + id: { $ne: Number(id ?? null) }, |
| 39 | + type: val, |
| 40 | + game: Number(gameId) |
| 41 | + }) |
| 42 | + |
| 43 | + return [ |
| 44 | + { |
| 45 | + check: keys.includes(val), |
| 46 | + error: `Integration type must be one of ${keys.join(', ')}` |
| 47 | + }, |
| 48 | + { |
| 49 | + check: !duplicateIntegrationType, |
| 50 | + error: `This game already has an integration for ${val}` |
| 51 | + } |
| 52 | + ] |
| 53 | + } |
| 54 | + }) |
| 55 | + @Enum(() => IntegrationType) |
| 56 | + type: IntegrationType |
| 57 | + |
| 58 | + @ManyToOne(() => Game) |
| 59 | + game: Game |
| 60 | + |
| 61 | + @Required({ methods: ['POST', 'PATCH'] }) |
| 62 | + @Property({ type: 'json' }) |
| 63 | + private config: IntegrationConfig |
| 64 | + |
| 65 | + @Property({ nullable: true }) |
| 66 | + deletedAt: Date |
| 67 | + |
| 68 | + @Property() |
| 69 | + createdAt: Date = new Date() |
| 70 | + |
| 71 | + @Property({ onUpdate: () => new Date() }) |
| 72 | + updatedAt: Date = new Date() |
| 73 | + |
| 74 | + constructor(type: IntegrationType, game: Game, config: IntegrationConfig) { |
| 75 | + this.type = type |
| 76 | + this.game = game |
| 77 | + |
| 78 | + this.config = { |
| 79 | + ...config, |
| 80 | + apiKey: encrypt(config.apiKey, process.env.STEAM_INTEGRATION_SECRET) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + updateConfig(config: Partial<IntegrationConfig>) { |
| 85 | + if (config.apiKey) config.apiKey = encrypt(config.apiKey, process.env.STEAM_INTEGRATION_SECRET) |
| 86 | + |
| 87 | + this.config = { |
| 88 | + ...this.config, |
| 89 | + ...config |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + getConfig(): IntegrationConfig { |
| 94 | + switch (this.type) { |
| 95 | + case IntegrationType.STEAMWORKS: |
| 96 | + return pick(this.config, ['appId', 'syncLeaderboards', 'syncStats']) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + getSteamAPIKey(): string { |
| 101 | + return decrypt(this.config.apiKey, process.env.STEAM_INTEGRATION_SECRET) |
| 102 | + } |
| 103 | + |
| 104 | + async handleLeaderboardCreated(em: EntityManager, leaderboard: Leaderboard) { |
| 105 | + switch (this.type) { |
| 106 | + case IntegrationType.STEAMWORKS: |
| 107 | + if (this.config.syncLeaderboards) { |
| 108 | + await createSteamworksLeaderboard(em, this, leaderboard) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + async handleLeaderboardUpdated(em: EntityManager, leaderboard: Leaderboard) { |
| 114 | + switch (this.type) { |
| 115 | + case IntegrationType.STEAMWORKS: |
| 116 | + if (this.config.syncLeaderboards) { |
| 117 | + await createSteamworksLeaderboard(em, this, leaderboard) // create if doesn't exist |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + async handleLeaderboardDeleted(em: EntityManager, leaderboardInternalName: string) { |
| 123 | + switch (this.type) { |
| 124 | + case IntegrationType.STEAMWORKS: |
| 125 | + if (this.config.syncLeaderboards) { |
| 126 | + await deleteSteamworksLeaderboard(em, this, leaderboardInternalName) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + async handleLeaderboardEntryCreated(em: EntityManager, entry: LeaderboardEntry) { |
| 132 | + switch (this.type) { |
| 133 | + case IntegrationType.STEAMWORKS: |
| 134 | + if (entry.playerAlias.service === PlayerAliasService.STEAM && this.config.syncLeaderboards) { |
| 135 | + await createSteamworksLeaderboardEntry(em, this, entry) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + async handleLeaderboardEntryVisibilityToggled(em: EntityManager, entry: LeaderboardEntry) { |
| 141 | + switch (this.type) { |
| 142 | + case IntegrationType.STEAMWORKS: |
| 143 | + if (entry.playerAlias.service === PlayerAliasService.STEAM && this.config.syncLeaderboards) { |
| 144 | + if (entry.hidden) { |
| 145 | + await deleteSteamworksLeaderboardEntry(em, this, entry) |
| 146 | + } else { |
| 147 | + await createSteamworksLeaderboardEntry(em, this, entry) |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + async handleSyncLeaderboards(em: EntityManager) { |
| 154 | + switch (this.type) { |
| 155 | + case IntegrationType.STEAMWORKS: |
| 156 | + await syncSteamworksLeaderboards(em, this) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + async handleStatUpdated(em: EntityManager, playerStat: PlayerGameStat) { |
| 161 | + await playerStat.player.aliases.loadItems() |
| 162 | + const steamAlias = playerStat.player.aliases.getItems().find((alias) => alias.service === PlayerAliasService.STEAM) |
| 163 | + |
| 164 | + switch (this.type) { |
| 165 | + case IntegrationType.STEAMWORKS: |
| 166 | + if (steamAlias && this.config.syncStats) { |
| 167 | + await setSteamworksStat(em, this, playerStat, steamAlias) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + async handleSyncStats(em: EntityManager) { |
| 173 | + switch (this.type) { |
| 174 | + case IntegrationType.STEAMWORKS: |
| 175 | + await syncSteamworksStats(em, this) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + toJSON() { |
| 180 | + return { |
| 181 | + id: this.id, |
| 182 | + type: this.type, |
| 183 | + config: this.getConfig(), |
| 184 | + createdAt: this.createdAt, |
| 185 | + updatedAt: this.updatedAt |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments