Skip to content

Commit

Permalink
Merge pull request #327 from TaloDev/develop
Browse files Browse the repository at this point in the history
Release 0.40.0
  • Loading branch information
tudddorrr authored Aug 30, 2024
2 parents 8a2e29a + 29b2abf commit 3f70730
Show file tree
Hide file tree
Showing 22 changed files with 258 additions and 75 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
image: [backend, clickhouse]
steps:
- run: echo "TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

Expand All @@ -22,18 +25,19 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
id: docker_build
- name: Build and push (${{ matrix.image }})
id: build-image
uses: docker/build-push-action@v5
with:
push: true
file: ${{ matrix.image == 'backend' && 'Dockerfile' || 'clickhouse/Dockerfile' }}
tags: |
ghcr.io/talodev/backend:latest
ghcr.io/talodev/backend:${{ env.TAG }}
target: prod
ghcr.io/talodev/${{ matrix.image }}:latest
ghcr.io/talodev/${{ matrix.image }}:${{ env.TAG }}
target: ${{ matrix.image == 'backend' && 'prod' || '' }}

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
- name: Image digest (${{ matrix.image }})
run: echo ${{ steps.build-image.outputs.digest }}

deploy:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ COPY src ./src
RUN npm run build

FROM base AS prod
ENV NODE_ENV production
ENV NODE_ENV=production
RUN npm ci
COPY --from=build /usr/backend/dist .
CMD [ "node", "index.js" ]
66 changes: 33 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "game-services",
"version": "0.39.0",
"version": "0.40.0",
"description": "",
"main": "src/index.ts",
"scripts": {
Expand All @@ -22,7 +22,7 @@
"author": "Sleepy Studios",
"license": "MIT",
"devDependencies": {
"@mikro-orm/cli": "^6.3.2",
"@mikro-orm/cli": "^6.3.7",
"@stylistic/eslint-plugin": "^2.2.2",
"@types/koa": "^2.13.5",
"@types/koa-bodyparser": "^4.3.7",
Expand Down Expand Up @@ -50,10 +50,10 @@
"@clickhouse/client": "^1.4.1",
"@dinero.js/currencies": "^2.0.0-alpha.14",
"@koa/cors": "^5.0.0",
"@mikro-orm/core": "^6.3.2",
"@mikro-orm/migrations": "^6.3.2",
"@mikro-orm/mysql": "^6.3.2",
"@mikro-orm/reflection": "^6.3.2",
"@mikro-orm/core": "^6.3.7",
"@mikro-orm/migrations": "^6.3.7",
"@mikro-orm/mysql": "^6.3.7",
"@mikro-orm/reflection": "^6.3.7",
"@sendgrid/mail": "^7.6.2",
"@sentry/node": "^7.47.0",
"@sentry/utils": "^7.47.0",
Expand Down
4 changes: 4 additions & 0 deletions src/config/api-routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Koa, { Context, Next } from 'koa'
import { service } from 'koa-clay'
import HealthCheckAPIService from '../services/api/health-check-api.service'
import GameFeedbackAPIService from '../services/api/game-feedback-api.service'
import GameConfigAPIService from '../services/api/game-config-api.service'
import GameStatAPIService from '../services/api/game-stat-api.service'
Expand All @@ -13,6 +14,7 @@ import { apiRouteAuthMiddleware, getRouteInfo } from '../middlewares/route-middl
import apiKeyMiddleware from '../middlewares/api-key-middleware'
import playerAuthMiddleware from '../middlewares/player-auth-middleware'
import PlayerAuthAPIService from '../services/api/player-auth-api.service'
import continunityMiddleware from '../middlewares/continunity-middleware'

export default (app: Koa) => {
app.use(apiKeyMiddleware)
Expand All @@ -27,7 +29,9 @@ export default (app: Koa) => {

app.use(currentPlayerMiddleware)
app.use(playerAuthMiddleware)
app.use(continunityMiddleware)

app.use(service('/v1/health-check', new HealthCheckAPIService()))
app.use(service('/v1/game-feedback', new GameFeedbackAPIService()))
app.use(service('/v1/game-config', new GameConfigAPIService()))
app.use(service('/v1/game-stats', new GameStatAPIService()))
Expand Down
1 change: 1 addition & 0 deletions src/entities/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Game from './game'
import User from './user'

export enum APIKeyScope {
WRITE_CONTINUITY_REQUESTS = 'write:continuityRequests',
READ_GAME_FEEDBACK = 'read:gameFeedback',
WRITE_GAME_FEEDBACK = 'write:gameFeedback',
READ_GAME_CONFIG = 'read:gameConfig',
Expand Down
20 changes: 20 additions & 0 deletions src/middlewares/continunity-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isValid } from 'date-fns'
import { Context, Next } from 'koa'
import { APIKeyScope } from '../entities/api-key'
import { isAPIRoute } from './route-middleware'
import checkScope from '../policies/checkScope'

export default async (ctx: Context, next: Next): Promise<void> => {
if (isAPIRoute(ctx) && checkScope(ctx.state.key, APIKeyScope.WRITE_CONTINUITY_REQUESTS)) {
const header = ctx.headers['x-talo-continuity-timestamp']

if (header) {
const date = new Date(Number(header))
if (isValid(date)) {
ctx.state.continuityDate = date
}
}
}

await next()
}
7 changes: 0 additions & 7 deletions src/migrations/20210725211129InitialMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export class InitialMigration extends Migration {
this.addSql('create table `player_alias` (`id` int unsigned not null auto_increment primary key, `service` varchar(255) not null, `identifier` varchar(255) not null, `player_id` varchar(255) null, `created_at` datetime not null, `updated_at` datetime not null) default character set utf8mb4 engine = InnoDB')
this.addSql('alter table `player_alias` add index `player_alias_player_id_index`(`player_id`)')

this.addSql('create table `event` (`id` int unsigned not null auto_increment primary key, `name` varchar(255) not null, `props` json not null, `game_id` int(11) unsigned not null, `player_alias_id` int(11) unsigned not null, `created_at` datetime not null, `updated_at` datetime not null) default character set utf8mb4 engine = InnoDB')
this.addSql('alter table `event` add index `event_game_id_index`(`game_id`)')
this.addSql('alter table `event` add index `event_player_alias_id_index`(`player_alias_id`)')

this.addSql('create table `apikey` (`id` int unsigned not null auto_increment primary key, `scopes` text not null, `game_id` int(11) unsigned not null, `created_by_user_id` int(11) unsigned not null, `created_at` datetime not null, `revoked_at` datetime null) default character set utf8mb4 engine = InnoDB')
this.addSql('alter table `apikey` add index `apikey_game_id_index`(`game_id`)')
this.addSql('alter table `apikey` add index `apikey_created_by_user_id_index`(`created_by_user_id`)')
Expand All @@ -46,9 +42,6 @@ export class InitialMigration extends Migration {

this.addSql('alter table `player_alias` add constraint `player_alias_player_id_foreign` foreign key (`player_id`) references `player` (`id`) on delete cascade')

this.addSql('alter table `event` add constraint `event_game_id_foreign` foreign key (`game_id`) references `game` (`id`) on update cascade')
this.addSql('alter table `event` add constraint `event_player_alias_id_foreign` foreign key (`player_alias_id`) references `player_alias` (`id`) on update cascade')

this.addSql('alter table `apikey` add constraint `apikey_game_id_foreign` foreign key (`game_id`) references `game` (`id`) on update cascade')
this.addSql('alter table `apikey` add constraint `apikey_created_by_user_id_foreign` foreign key (`created_by_user_id`) references `user` (`id`) on update cascade')
}
Expand Down
12 changes: 0 additions & 12 deletions src/migrations/20211221195514CascadeDeletePlayerAliasEvents.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CreateDataExportsTable } from './20210926160859CreateDataExportsTable'
import { CreateLeaderboardsTable } from './20211107233610CreateLeaderboardsTable'
import { CreateUserTwoFactorAuthTable } from './20211205171927CreateUserTwoFactorAuthTable'
import { CreateUserRecoveryCodeTable } from './20211209003017CreateUserRecoveryCodeTable'
import { CascadeDeletePlayerAliasEvents } from './20211221195514CascadeDeletePlayerAliasEvents'
import { AddLeaderboardEntryHiddenColumn } from './20211224154919AddLeaderboardEntryHiddenColumn'
import { CreateGameSavesTable } from './20220109144435CreateGameSavesTable'
import { CreateGameActivitiesTable } from './20220125220401CreateGameActivitiesTable'
Expand Down Expand Up @@ -49,10 +48,6 @@ export default [
name: 'CreateUserRecoveryCodeTable',
class: CreateUserRecoveryCodeTable
},
{
name: 'CascadeDeletePlayerAliasEvents',
class: CascadeDeletePlayerAliasEvents
},
{
name: 'AddLeaderboardEntryHiddenColumn',
class: AddLeaderboardEntryHiddenColumn
Expand Down
3 changes: 3 additions & 0 deletions src/services/api/game-feedback-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export default class GameFeedbackAPIService extends APIService {
const feedback = new GameFeedback(category, req.ctx.state.playerAlias)
feedback.comment = comment
feedback.anonymised = category.anonymised
if (req.ctx.state.continuityDate) {
feedback.createdAt = req.ctx.state.continuityDate
}

await em.persistAndFlush(feedback)

Expand Down
4 changes: 4 additions & 0 deletions src/services/api/game-stat-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default class GameStatAPIService extends APIService {

if (!playerStat) {
playerStat = new PlayerGameStat(req.ctx.state.player, req.ctx.state.stat)
if (req.ctx.state.continuityDate) {
playerStat.createdAt = req.ctx.state.continuityDate
}

em.persist(playerStat)
}

Expand Down
15 changes: 15 additions & 0 deletions src/services/api/health-check-api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Response, Routes } from 'koa-clay'
import APIService from './api-service'

@Routes([
{
method: 'GET'
}
])
export default class HealthCheckAPIService extends APIService {
async index(): Promise<Response> {
return {
status: 204
}
}
}
5 changes: 4 additions & 1 deletion src/services/api/leaderboard-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export default class LeaderboardAPIService extends APIService {
const entry = new LeaderboardEntry(req.ctx.state.leaderboard)
entry.playerAlias = req.ctx.state.playerAlias
entry.score = req.body.score
if (req.ctx.state.continuityDate) {
entry.createdAt = req.ctx.state.continuityDate
}

await em.persistAndFlush(entry)

Expand Down Expand Up @@ -67,7 +70,7 @@ export default class LeaderboardAPIService extends APIService {

if ((leaderboard.sortMode === LeaderboardSortMode.ASC && score < entry.score) || (leaderboard.sortMode === LeaderboardSortMode.DESC && score > entry.score)) {
entry.score = score
entry.createdAt = new Date()
entry.createdAt = req.ctx.state.continuityDate ?? new Date()
await em.flush()

updated = true
Expand Down
2 changes: 1 addition & 1 deletion src/services/billing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default class BillingService extends Service {
)

return {
status: 200
status: 204
}
}

Expand Down
Loading

0 comments on commit 3f70730

Please sign in to comment.