Skip to content

Commit

Permalink
Merge pull request #322 from TaloDev/develop
Browse files Browse the repository at this point in the history
Release 0.38.0
  • Loading branch information
tudddorrr authored Aug 2, 2024
2 parents 94811ed + 6e2f5fc commit b338556
Show file tree
Hide file tree
Showing 116 changed files with 1,299 additions and 969 deletions.
11 changes: 5 additions & 6 deletions _templates/service/new/factory.ejs.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import <%= h.changeCase.pascal(name) %> from '../../src/entities/<%= name %>'

export default class <%= h.changeCase.pascal(name) %>Factory extends Factory<<%= h.changeCase.pascal(name) %>> {
constructor() {
super(<%= h.changeCase.pascal(name) %>, 'base')
this.register('base', this.base)
super(<%= h.changeCase.pascal(name) %>)
}

protected base(): Partial<<%= h.changeCase.pascal(name) %>> {
return {

}
protected definition(): void {
this.state(() => ({
// TODO
}))
}
}
666 changes: 488 additions & 178 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "game-services",
"version": "0.37.0",
"version": "0.38.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"watch": "tsx watch src/index.ts",
"build": "npx tsc -p tsconfig.build.json",
"dc": "docker-compose -f docker-compose.yml -f docker-compose.dev.yml",
"dc": "docker compose -f docker-compose.yml -f docker-compose.dev.yml",
"seed": "DB_HOST=127.0.0.1 tsx tests/seed.ts",
"test": "./tests/run-tests.sh",
"up": "npm run dc -- up --build -d",
Expand All @@ -22,7 +22,7 @@
"author": "Sleepy Studios",
"license": "MIT",
"devDependencies": {
"@mikro-orm/cli": "^6.2.9",
"@mikro-orm/cli": "^6.3.2",
"@stylistic/eslint-plugin": "^2.2.2",
"@types/koa": "^2.13.5",
"@types/koa-bodyparser": "^4.3.7",
Expand All @@ -35,7 +35,7 @@
"@vitest/coverage-v8": "^1.5.2",
"axios-mock-adapter": "^1.22.0",
"eslint": "^8.26.0",
"hefty": "^1.1.0",
"hefty": "^2.0.0",
"husky": "^9.0.11",
"hygen": "^6.2.11",
"lint-staged": ">=10",
Expand All @@ -49,10 +49,10 @@
"dependencies": {
"@dinero.js/currencies": "^2.0.0-alpha.14",
"@koa/cors": "^5.0.0",
"@mikro-orm/core": "^6.2.9",
"@mikro-orm/migrations": "^6.2.9",
"@mikro-orm/mysql": "^6.2.9",
"@mikro-orm/reflection": "^6.2.9",
"@mikro-orm/core": "^6.3.2",
"@mikro-orm/migrations": "^6.3.2",
"@mikro-orm/mysql": "^6.3.2",
"@mikro-orm/reflection": "^6.3.2",
"@sendgrid/mail": "^7.6.2",
"@sentry/node": "^7.47.0",
"@sentry/utils": "^7.47.0",
Expand Down Expand Up @@ -84,7 +84,6 @@
"uuid": "^9.0.0"
},
"mikro-orm": {
"useTsNode": true,
"configPaths": [
"./src/config/mikro-orm.config.ts",
"./config/mikro-orm.config.js"
Expand Down
2 changes: 1 addition & 1 deletion src/docs/player-auth-api.docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const PlayerAuthAPIDocs: APIDocs<PlayerAuthAPIService> = {
]
},
toggleVerification: {
description: 'Toggle if verification is required for a player account',
description: 'Toggle verification for a player account',
params: {
headers: {
'x-talo-player': 'The ID of the player',
Expand Down
4 changes: 2 additions & 2 deletions src/services/api/leaderboard-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export default class LeaderboardAPIService extends APIService {
return integration.handleLeaderboardEntryCreated(em, entry)
})

let query = em.createQueryBuilder(LeaderboardEntry, 'le')
const query = em.createQueryBuilder(LeaderboardEntry, 'le')
.select('le.*', true)
.where({ leaderboard: entry.leaderboard })
.orderBy({ score: entry.leaderboard.sortMode })

if (!req.ctx.state.includeDevData) {
query = query.andWhere({
query.andWhere({
playerAlias: {
player: devDataPlayerFilter(em)
}
Expand Down
1 change: 1 addition & 0 deletions src/services/api/player-auth-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ export default class PlayerAuthAPIService extends APIService {
}

@Validate({
headers: ['x-talo-player', 'x-talo-alias', 'x-talo-session'],
body: ['currentPassword', 'verificationEnabled']
})
@HasPermission(PlayerAuthAPIPolicy, 'toggleVerification')
Expand Down
18 changes: 12 additions & 6 deletions src/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ export default class EventService extends Service {
})
@HasPermission(EventPolicy, 'index')
async index(req: Request): Promise<Response> {
const { startDate, endDate } = req.query
const { startDate: startDateQuery, endDate: endDateQuery } = req.query
const em: EntityManager = req.ctx.em

const startDate = new Date(startDateQuery)
const endDate = new Date(endDateQuery)

const where: FilterQuery<Event> = {
game: req.ctx.state.game,
createdAt: {
$gte: new Date(startDate),
$lte: endOfDay(new Date(endDate))
$gte: startDate,
$lte: endOfDay(endDate)
}
}

Expand All @@ -51,16 +54,19 @@ export default class EventService extends Service {
// }

const data = groupBy(events, 'name')

for (const name in data) {
const processed: EventData[] = []

for (let date = new Date(startDate).getTime(); date <= new Date(endDate).getTime(); date += 86400000 /* 24 hours in ms */) {
const count = data[name].filter((event: Event) => isSameDay(new Date(date), new Date(event.createdAt))).length
for (let time = startDate.getTime(); time <= endDate.getTime(); time += 86400000 /* 24 hours in ms */) {
const dateFromTime = new Date(time)

const count = data[name].filter((event: Event) => isSameDay(dateFromTime, event.createdAt)).length
const change = processed.length > 0 ? this.calculateChange(count, processed[processed.length - 1]) : 0

processed.push({
name,
date,
date: time,
count,
change
})
Expand Down
15 changes: 12 additions & 3 deletions src/services/game-feedback.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import GameFeedbackPolicy from '../policies/game-feedback.policy'
import GameFeedbackCategory from '../entities/game-feedback-category'
import createGameActivity from '../lib/logging/createGameActivity'
import { GameActivityType } from '../entities/game-activity'
import { devDataPlayerFilter } from '../middlewares/dev-data-middleware'

const itemsPerPage = 50

Expand Down Expand Up @@ -40,14 +41,14 @@ export default class GameFeedbackService extends Service {
const { feedbackCategoryInternalName, search, page } = req.query
const em: EntityManager = req.ctx.em

let query = em.qb(GameFeedback, 'gf')
const query = em.qb(GameFeedback, 'gf')
.select('gf.*')
.orderBy({ createdAt: QueryOrder.DESC })
.limit(itemsPerPage)
.offset(Number(page) * itemsPerPage)

if (feedbackCategoryInternalName) {
query = query
query
.andWhere({
category: {
internalName: feedbackCategoryInternalName
Expand All @@ -56,7 +57,7 @@ export default class GameFeedbackService extends Service {
}

if (search) {
query = query.andWhere({
query.andWhere({
$or: [
{ comment: { $like: `%${search}%` } },
{
Expand All @@ -69,6 +70,14 @@ export default class GameFeedbackService extends Service {
})
}

if (!req.ctx.state.includeDevData) {
query.andWhere({
playerAlias: {
player: devDataPlayerFilter(em)
}
})
}

const [feedback, count] = await query
.andWhere({
category: {
Expand Down
31 changes: 14 additions & 17 deletions src/services/headline.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FilterQuery, EntityManager } from '@mikro-orm/mysql'
import { endOfDay, isSameDay } from 'date-fns'
import { Service, Request, Response, Validate, HasPermission, Routes } from 'koa-clay'
import groupBy from 'lodash.groupby'
import Event from '../entities/event'
import Player from '../entities/player'
import HeadlinePolicy from '../policies/headline.policy'
Expand Down Expand Up @@ -127,30 +126,28 @@ export default class HeadlineService extends Service {
const { startDate, endDate } = req.query
const em: EntityManager = req.ctx.em

const where: FilterQuery<Event> = {
game: req.ctx.state.game,
createdAt: {
$gte: new Date(startDate),
$lte: endOfDay(new Date(endDate))
}
}
const query = em.qb(Event, 'e')
.join('e.playerAlias', 'pa')
.count('pa.player_id', true)
.where({
game: req.ctx.state.game.id,
createdAt: { $gte: new Date(startDate), $lte: endOfDay(new Date(endDate)) }
})

if (!req.ctx.state.includeDevData) {
where.playerAlias = {
player: devDataPlayerFilter(em)
}
query.andWhere({
playerAlias: {
player: devDataPlayerFilter(em)
}
})
}

const events = await em.getRepository(Event).find(where, {
populate: ['playerAlias.player']
})

const count = Object.keys(groupBy(events, 'playerAlias.player.id')).length
const result = await query.execute('get')

return {
status: 200,
body: {
count
count: result.count
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/services/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ export default class PlayerService extends Service {
const { search, page } = req.query
const em: EntityManager = req.ctx.em

let query = em.qb(Player, 'p')
const query = em.qb(Player, 'p')
.select('p.*')
.orderBy({ lastSeenAt: QueryOrder.DESC })
.limit(itemsPerPage)
.offset(Number(page) * itemsPerPage)

if (search) {
query = query
query
.where({
props: {
$in: em.qb(PlayerProp).select('id').where({
Expand Down Expand Up @@ -167,7 +167,7 @@ export default class PlayerService extends Service {
}

if (groups.length > 0) {
query = query
query
.orWhere({
groups: {
$in: groups
Expand All @@ -177,7 +177,7 @@ export default class PlayerService extends Service {
}

if (!req.ctx.state.includeDevData) {
query = query.andWhere(devDataPlayerFilter(em))
query.andWhere(devDataPlayerFilter(em))
}

const [players, count] = await query
Expand Down Expand Up @@ -257,14 +257,14 @@ export default class PlayerService extends Service {
const em: EntityManager = req.ctx.em
const player: Player = req.ctx.state.player // set in the policy

let query = em.createQueryBuilder(Event, 'e')
const query = em.createQueryBuilder(Event, 'e')
.select('e.*')
.orderBy({ createdAt: QueryOrder.DESC })
.limit(itemsPerPage)
.offset(Number(page) * itemsPerPage)

if (search) {
query = query
query
.where('json_extract(props, \'$[*].value\') like ?', [`%${search}%`])
.orWhere({
name: {
Expand Down
8 changes: 4 additions & 4 deletions tests/entities/player-group-rule/casting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('PlayerGroupRule casting', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with((player) => ({
const player1 = await new PlayerFactory([game]).state((player) => ({
props: new Collection<PlayerProp>(player, [
new PlayerProp(player, 'currentLevel', '80')
])
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('PlayerGroupRule casting', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with((player) => ({
const player1 = await new PlayerFactory([game]).state((player) => ({
props: new Collection<PlayerProp>(player, [
new PlayerProp(player, 'currentLevel', '80')
])
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('PlayerGroupRule casting', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with((player) => ({
const player1 = await new PlayerFactory([game]).state((player) => ({
props: new Collection<PlayerProp>(player, [
new PlayerProp(player, 'firstLoginAt', '2022-05-03 00:00:00')
])
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('PlayerGroupRule casting', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with((player) => ({
const player1 = await new PlayerFactory([game]).state((player) => ({
props: new Collection<PlayerProp>(player, [
new PlayerProp(player, 'firstLoginAt', '2022-05-03 08:59:36')
])
Expand Down
8 changes: 4 additions & 4 deletions tests/entities/player-group-rule/equals-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('EQUALS rule', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with(() => ({ lastSeenAt: new Date(2022, 4, 3) })).one()
const player1 = await new PlayerFactory([game]).state(() => ({ lastSeenAt: new Date(2022, 4, 3) })).one()
const player2 = await new PlayerFactory([game]).one()
await (<EntityManager>global.em).persistAndFlush([player1, player2])

Expand All @@ -38,7 +38,7 @@ describe('EQUALS rule', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with(() => ({ lastSeenAt: new Date(2022, 4, 3) })).one()
const player1 = await new PlayerFactory([game]).state(() => ({ lastSeenAt: new Date(2022, 4, 3) })).one()
const player2 = await new PlayerFactory([game]).one()
await (<EntityManager>global.em).persistAndFlush([player1, player2])

Expand All @@ -65,7 +65,7 @@ describe('EQUALS rule', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with((player) => ({
const player1 = await new PlayerFactory([game]).state((player) => ({
props: new Collection<PlayerProp>(player, [
new PlayerProp(player, 'currentLevel', '80')
])
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('EQUALS rule', () => {
const [organisation, game] = await createOrganisationAndGame()
const [token] = await createUserAndToken({}, organisation)

const player1 = await new PlayerFactory([game]).with((player) => ({
const player1 = await new PlayerFactory([game]).state((player) => ({
props: new Collection<PlayerProp>(player, [
new PlayerProp(player, 'currentLevel', '80')
])
Expand Down
Loading

0 comments on commit b338556

Please sign in to comment.