Skip to content

Commit

Permalink
Merge pull request #55 from x-team/develop
Browse files Browse the repository at this point in the history
Merging develop into main!
  • Loading branch information
ccmoralesj authored Jul 18, 2022
2 parents bde51a8 + 1320418 commit d1cefdd
Show file tree
Hide file tree
Showing 21 changed files with 1,299 additions and 100 deletions.
14 changes: 9 additions & 5 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ HOST=0.0.0.0
PORT=3000

# DATABASE
DB_USERNAME=postgres
DB_PASSWORD=*games-2021
DB_NAME=gameshq_api_test
DB_HOSTNAME=127.0.0.1
DB_PORT=5435
DB_USERNAME=postgres
DB_PASSWORD=*games-2021
DB_NAME=gameshq_api_test
DB_HOSTNAME=127.0.0.1
DB_PORT=5435

SLACK_ARENA_SIGNING_SECRET=fake
SLACK_ARENA_XHQ_CHANNEL=fake
SLACK_ARENA_TOKEN=fake

# FIREBASE
GOOGLE_APPLICATION_CREDENTIALS={"type": "fake", "project_id": "fake", "private_key": "fake", "client_email": "fake"}
Expand Down
6 changes: 6 additions & 0 deletions src/api-utils/schemas/gameDev/achievementsSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export const getAchievementProgressRequestSchema = Joi.object({
}).optional(),
}).optional();

export const updateAchievementProgressRequestSchema = Joi.object({
_userId: Joi.number().required(),
isUnlocked: Joi.boolean().required(),
progress: Joi.number().required(),
}).required();

export const postAchievementProgressResponseSchema = Joi.object({
_achievementId: Joi.number().required(),
_userId: Joi.number().required(),
Expand Down
73 changes: 51 additions & 22 deletions src/api-utils/schemas/gameDev/leaderboardSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ export const getLeaderboardRankResponseSchema = Joi.array()
)
.required();

export const postLeaderboardSchema = Joi.object({
id: Joi.number().optional(),
name: Joi.string().required(),
scoreStrategy: Joi.string()
.valid(...Object.values(ScoreStrategy))
.optional(),
resetStrategy: Joi.string()
.valid(...Object.values(ResetStrategy))
.optional(),
}).required();

export const leaderboardSchema = Joi.object({
id: Joi.number(),
name: Joi.string(),
scoreStrategy: Joi.string(),
resetStrategy: Joi.string(),
_gameTypeId: Joi.number(),
createdAt: Joi.date(),
updatedAt: Joi.date(),
}).optional();

export const multipleLeaderboardSchema = Joi.array().items(leaderboardSchema).optional();

export const getUserLeaderboardResultScoreResponseSchema = Joi.object({
id: Joi.number().required(),
_leaderboardEntryId: Joi.number().required(),
Expand All @@ -28,6 +51,10 @@ export const getLeaderboardResultScorePageSchema = Joi.object({
_leaderboardResultsMeta: Joi.array().items(Joi.object()).optional(),
}).optional();

export const multipleLeaderboardResultScoreSchema = Joi.array()
.items(getLeaderboardResultScorePageSchema)
.optional();

export const postLeaderboardResultScoreResquestSchema = Joi.object({
id: Joi.number().optional(),
score: Joi.number().required(),
Expand All @@ -45,28 +72,30 @@ export const postLeaderboardResultScoreResponseSchema = Joi.object({
newEntry: Joi.boolean().required(),
}).required();

export const postLeaderboardSchema = Joi.object({
id: Joi.number().optional(),
name: Joi.string().required(),
scoreStrategy: Joi.string()
.valid(...Object.values(ScoreStrategy))
.optional(),
resetStrategy: Joi.string()
.valid(...Object.values(ResetStrategy))
export const updateLeaderboardResultRequestSchema = Joi.object({
id: Joi.number().required(),
score: Joi.number().required(),
_leaderboardResultsMeta: Joi.array()
.items(
Joi.object({
attribute: Joi.string().required(),
value: Joi.string().required(),
})
)
.optional(),
}).required();

export const leaderboardSchema = Joi.object({
id: Joi.number(),
name: Joi.string(),
scoreStrategy: Joi.string(),
resetStrategy: Joi.string(),
_gameTypeId: Joi.number(),
createdAt: Joi.date(),
updatedAt: Joi.date(),
}).optional();

export const multipleLeaderboardSchema = Joi.array().items(leaderboardSchema).optional();
export const multipleLeaderboardResultScoreSchema = Joi.array()
.items(getLeaderboardResultScorePageSchema)
.optional();
export const updateLeaderboardResultResponseSchema = Joi.object({
id: Joi.number().required(),
score: Joi.number().required(),
_userId: Joi.number().required(),
_leaderboardEntryId: Joi.number().required(),
_leaderboardResultsMeta: Joi.array()
.items(
Joi.object({
attribute: Joi.string().required(),
value: Joi.string().required(),
})
)
.optional(),
}).required();
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
addTowerFloorEnemies,
addTowerFloorEnemy,
deleteTowerFloorEnemyByTowerFloor,
findTowerFloorEnemyById,
} from '../../../../../../models/TowerFloorEnemy';
import { removeActionsByFloorBattlefieldEnemy } from '../../../../../../models/TowerRoundAction';
Expand Down Expand Up @@ -156,6 +157,7 @@ export async function addEnemiesToFloor(
if (!towerFloor) {
return getGameError(towerCommandReply.floorNumberNotValid());
}
await deleteTowerFloorEnemyByTowerFloor(towerFloor.id, transaction);
await addTowerFloorEnemies(towerFloor.id, enemyIds, transaction);
return;
});
Expand Down
15 changes: 14 additions & 1 deletion src/models/AchievementUnlocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class AchievementUnlocked
_achievement: Association<AchievementUnlocked, Achievement>;
};
}
interface AchievementUnlockedUnlockedEditorData {
export interface AchievementUnlockedUnlockedEditorData {
_userId: number;
_achievementId: number;
isUnlocked: boolean;
Expand All @@ -99,6 +99,19 @@ export async function createOrUpdateAchievementUnlocked(
return await AchievementUnlocked.upsert({ ...achievementUnlockedData });
}

export async function findAchievementUnlocked(_userId: number, _achievementId: number) {
return await AchievementUnlocked.findOne({ where: { _userId, _achievementId } });
}

export function deleteAchievementUnlocked(_userId: number, _achievementId: number) {
return AchievementUnlocked.destroy({
where: {
_userId,
_achievementId,
},
});
}

export function getAchievementUnlockedFromAchievement(
achievement: Achievement,
transaction?: Transaction
Expand Down
7 changes: 6 additions & 1 deletion src/models/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
PrimaryKey,
AutoIncrement,
HasOne,
HasMany,
} from 'sequelize-typescript';

import { GAME_TYPE } from '../games/consts/global';
Expand All @@ -19,7 +20,7 @@ import { GameError } from '../games/utils/GameError';

import { findGameTypeByName } from './GameType';

import { User, ArenaGame, GameType, TowerGame, TowerFloor, TowerFloorEnemy } from './';
import { ArenaPlayer, User, ArenaGame, GameType, TowerGame, TowerFloor, TowerFloorEnemy } from './';

function includeArrayByGameType(gameTypeName: string) {
return gameTypeName === GAME_TYPE.ARENA
Expand Down Expand Up @@ -126,11 +127,15 @@ export class Game extends Model<GameAttributes, GameCreationAttributes> implemen
@HasOne(() => TowerGame)
declare _tower?: TowerGame;

@HasMany(() => ArenaPlayer, '_gameId')
declare _arenaPlayers?: ArenaPlayer[];

static associations: {
_arena: Association<Game, ArenaGame>;
_tower: Association<Game, TowerGame>;
_createdBy: Association<Game, User>;
_gameType: Association<Game, GameType>;
_arenaPlayers: Association<Game, ArenaPlayer>;
};

async endGame(transaction?: Transaction) {
Expand Down
45 changes: 45 additions & 0 deletions src/models/LeaderboardResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ export function createOrUpdateLeaderBoardResult(data: LeaderboardResultsCreation
});
}

export function updateLeaderBoardResult(
data: LeaderboardResultsCreationAttributes & { id: number }
) {
return withTransaction(async (transaction) => {
const [, rslt] = await LeaderboardResults.update(
{
score: data.score,
},
{
where: {
id: data.id,
},
transaction,
returning: true,
}
);

if (rslt.length && data?._leaderboardResultsMeta) {
for (const meta of data._leaderboardResultsMeta) {
await LeaderboardResultsMeta.upsert(
{
...meta,
_leaderboardResultsId: data.id,
},
{
transaction,
conflictFields: ['attribute', '_leaderboardResultsId'],
}
);
}
}

return rslt[0];
});
}

function shouldUpsert(
data: LeaderboardResultsCreationAttributes,
lbrInDb: LeaderboardResults | null
Expand Down Expand Up @@ -287,3 +323,12 @@ function getUserLeaderboardResultWithScoreStrategy(
transaction,
});
}

export function deleteLeaderboardResult(id: number, leaderboardId: number) {
return LeaderboardResults.destroy({
where: {
id,
_leaderboardEntryId: leaderboardId,
},
});
}
10 changes: 10 additions & 0 deletions src/models/TowerFloorEnemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ export async function addTowerFloorEnemies(
export async function findTowerFloorEnemyById(id: number, transaction: Transaction) {
return TowerFloorEnemy.findByPk(id, { transaction });
}

export async function deleteTowerFloorEnemyByTowerFloor(
towerFloorId: number,
transaction: Transaction
) {
return TowerFloorEnemy.destroy({
where: { _towerFloorId: towerFloorId },
transaction,
});
}
31 changes: 16 additions & 15 deletions src/modules/dashboard/admin/adminHandlers/arenaAdminHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import { ArenaGame, findActiveArenaGame } from '../../../../models/ArenaGame';

export const getCurrentArenaGameState: Lifecycle.Method = async (_request, h) => {
const activeGame = await findActiveArenaGame();

await activeGame?.reload({
include: {
model: ArenaGame,
include: [
{
model: ArenaPlayer,
include: [
{
association: ArenaPlayer.associations._weapons,
include: [Item.associations._weapon, Item.associations._traits],
as: '_weapons',
},
],
},
],
},
include: [
{
model: ArenaGame,
},
{
model: ArenaPlayer,
include: [
{
association: ArenaPlayer.associations._weapons,
include: [Item.associations._weapon, Item.associations._traits],
as: '_weapons',
},
],
},
],
});

return h.response({ arenaGame: activeGame }).code(200);
Expand Down
Loading

0 comments on commit d1cefdd

Please sign in to comment.