Skip to content

Commit

Permalink
Merge pull request #977 from parlemonde/VIL-144
Browse files Browse the repository at this point in the history
Vil 144
  • Loading branch information
guillaume-pages authored Sep 4, 2024
2 parents 43ff2df + fa33883 commit 5684334
Show file tree
Hide file tree
Showing 79 changed files with 4,395 additions and 1,141 deletions.
Binary file not shown.
Binary file not shown.
Binary file added .yarn/cache/fsevents-patch-6b67494872-10.zip
Binary file not shown.
Binary file added .yarn/cache/fsevents-patch-afc6995412-10.zip
Binary file not shown.
36 changes: 18 additions & 18 deletions server/controllers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,24 @@ activityController.put({ path: '/:id', userType: UserType.TEACHER }, async (req:
res.sendJSON(activity);
});

// --- create a game ---
const createGame = async (data: GameData, activity: Activity): Promise<Game> => {
const id = data.gameId;
const game = id ? await AppDataSource.getRepository(Game).findOneOrFail({ where: { id: data.gameId || 0 } }) : new Game();
delete data['gameId'];
game.activityId = activity.id;
game.villageId = activity.villageId;
game.userId = activity.userId;
game.type = activity.subType;
game.signification = data.signification;
game.fakeSignification1 = data.fakeSignification1;
game.fakeSignification2 = data.fakeSignification2;
game.origine = data.origine;
game.video = data.video;
await AppDataSource.getRepository(Game).save(game);
return game;
};

activityController.put({ path: '/:id/askSame', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
const user = req.user;
if (!user) {
Expand Down Expand Up @@ -404,24 +422,6 @@ activityController.put({ path: '/:id/askSame', userType: UserType.TEACHER }, asy
res.sendJSON(activity);
});

// --- create a game ---
const createGame = async (data: GameData, activity: Activity): Promise<Game> => {
const id = data.gameId;
const game = id ? await AppDataSource.getRepository(Game).findOneOrFail({ where: { id: data.gameId || 0 } }) : new Game();
delete data['gameId'];
game.activityId = activity.id;
game.villageId = activity.villageId;
game.userId = activity.userId;
game.type = activity.subType;
game.signification = data.signification;
game.fakeSignification1 = data.fakeSignification1;
game.fakeSignification2 = data.fakeSignification2;
game.origine = data.origine;
game.video = data.video;
await AppDataSource.getRepository(Game).save(game);
return game;
};

// --- create a image ---
const createStory = async (data: StoryElement, activity: Activity, type: ImageType, inspiredStoryId: number = 0): Promise<Image> => {
const id = data.imageId;
Expand Down
273 changes: 242 additions & 31 deletions server/controllers/game.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { JSONSchemaType } from 'ajv';
import type { NextFunction, Request, Response } from 'express';

import type { ActivityContent, AnyData } from '../../types/activity.type';
import { ActivityStatus } from '../../types/activity.type';
import { Activity } from '../entities/activity';
import { Game } from '../entities/game';
import { GameResponse } from '../entities/gameResponse';
import { UserType } from '../entities/user';
Expand Down Expand Up @@ -156,29 +159,31 @@ gameController.get({ path: '/ableToPlay', userType: UserType.TEACHER }, async (r
next();
return;
}
const games = await AppDataSource.getRepository(Game)
.createQueryBuilder('game')
.leftJoinAndSelect('game.responses', 'responses')
.andWhere('`game`.`villageId` = :villageId', { villageId: villageId })
.andWhere('`game`.`type` = :type', { type: type })
.andWhere(
(qb) => {
const subQuery = qb
.subQuery()
.select()
.from(GameResponse, 'response')
.where(`response.userId = :userId`, { userId: userId })
.andWhere(`response.gameId = game.id`)
.andWhere(`response.isOldResponse = 0`)
.getQuery();
return 'NOT EXISTS ' + subQuery;
},
{ userId: req.user.id },
)
.getMany();
res.sendJSON({
games: games,
});
if (type === 0) {
const games = await AppDataSource.getRepository(Game)
.createQueryBuilder('game')
.leftJoinAndSelect('game.responses', 'responses')
.andWhere('`game`.`villageId` = :villageId', { villageId: villageId })
.andWhere('`game`.`type` = :type', { type: type })
.andWhere(
(qb) => {
const subQuery = qb
.subQuery()
.select()
.from(GameResponse, 'response')
.where(`response.userId = :userId`, { userId: userId })
.andWhere(`response.gameId = game.id`)
.andWhere(`response.isOldResponse = 0`)
.getQuery();
return 'NOT EXISTS ' + subQuery;
},
{ userId: req.user.id },
)
.getMany();
res.sendJSON({
games: games,
});
}
});

//--- retrieve answers to the mimic with this id ---
Expand All @@ -201,6 +206,7 @@ gameController.get({ path: '/stats/:gameId', userType: UserType.TEACHER }, async

type UpdateActivity = {
value: string;
villageId: number;
};

const ANSWER_M_SCHEMA: JSONSchemaType<UpdateActivity> = {
Expand All @@ -209,9 +215,12 @@ const ANSWER_M_SCHEMA: JSONSchemaType<UpdateActivity> = {
value: {
type: 'string',
},
villageId: {
type: 'number',
},
},
required: [],
additionalProperties: false,
additionalProperties: true,
};

const answerGameValidator = ajv.compile(ANSWER_M_SCHEMA);
Expand Down Expand Up @@ -245,11 +254,6 @@ gameController.put({ path: '/play/:id', userType: UserType.TEACHER }, async (req
return;
}

const game = await AppDataSource.getRepository(Game).findOne({ where: { id: id } });
if (!game) {
next();
return;
}
const responses = await AppDataSource.getRepository(GameResponse).find({ where: { userId: userId, id: id } });
if (responses.length > 2) {
next();
Expand All @@ -258,12 +262,219 @@ gameController.put({ path: '/play/:id', userType: UserType.TEACHER }, async (req

const gameResponse = new GameResponse();
gameResponse.value = data.value;
gameResponse.gameId = game.id;
gameResponse.villageId = game.villageId;
gameResponse.gameId = id;
gameResponse.villageId = data.villageId;
gameResponse.userId = userId;

await AppDataSource.getRepository(GameResponse).save(gameResponse);
res.sendJSON(GameResponse);
});

//--- Create a standardised game ---

gameController.post({ path: '/standardGame', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}

const data = req.body;
const game1 = data.game1;
const game2 = data.game2;
const game3 = data.game3;

createGame(game1, data.userId, data.villageId, data.type, data.subType, data.selectedPhase);
createGame(game2, data.userId, data.villageId, data.type, data.subType, data.selectedPhase);
createGame(game3, data.userId, data.villageId, data.type, data.subType, data.selectedPhase);

res.sendStatus(200);
});

async function createGame(data: ActivityContent[], userId: number, villageId: number, type: number, subType: number, selectedPhase: number) {
const activity = new Activity();
activity.type = type;
activity.subType = subType;
activity.status = ActivityStatus.PUBLISHED;
// TODO: Travailler sur le type de data
activity.data = data as unknown as AnyData;
activity.phase = selectedPhase;
activity.content = data;
activity.userId = userId;
activity.villageId = villageId;
activity.responseActivityId = null;
activity.responseType = null;
activity.isPinned = false;
activity.displayAsUser = false;
activity.publishDate = new Date();

await AppDataSource.getRepository(Activity).save(activity);
}

// --- Get all games standardised by subType ---

gameController.get({ path: '/allStandardGame', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}

const subType = parseInt(getQueryString(req.query.subType) || '0', 10);
const villageId = req.user.type <= UserType.TEACHER ? parseInt(getQueryString(req.query.villageId) || '0', 10) || null : req.user.villageId;

const subQueryBuilder = AppDataSource.getRepository(Activity)
.createQueryBuilder('activity')
.where('activity.villageId = :villageId', { villageId: villageId })
.andWhere('activity.type = :type', { type: 4 })
.andWhere('activity.subType = :subType', { subType: subType });

const games = await subQueryBuilder
.orderBy('activity.createDate', 'DESC')
.limit(200)
.offset(0 * 200)
.getMany();

res.sendJSON(games);
});

// --- Get one game standardised ---

gameController.get({ path: '/standardGame/:id', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}
const id = parseInt(req.params.id, 10) || 0;
const game = await AppDataSource.getRepository(Activity).findOne({ where: { id } });
if (!game || (req.user.type === UserType.TEACHER && req.user.villageId !== game.villageId)) {
next();
return;
}
res.sendJSON(game);
});

// --- Get one random game standardised to play ---
gameController.get({ path: '/playStandardGame', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}
const villageId = req.user.type <= UserType.TEACHER ? parseInt(getQueryString(req.query.villageId) || '0', 10) || null : req.user.villageId;
const type = 4;
const subType = parseInt(getQueryString(req.query.subType) || '0', 10);
const game = await AppDataSource.getRepository(Activity)
.createQueryBuilder('activity')
.andWhere('activity.villageId = :villageId', { villageId: villageId })
.andWhere('activity.type = :type', { type: type })
.andWhere('activity.subType = :subType', { subType: subType })
.orderBy('RAND()')
.getOne();
if (!game) {
next();
return;
}
res.sendJSON(game);
});

// --- Get the last created game standardised ---

gameController.get({ path: '/latestStandard', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}
const villageId = req.user.type <= UserType.TEACHER ? parseInt(getQueryString(req.query.villageId) || '0', 10) || null : req.user.villageId;
const type = 4;
const subType = parseInt(getQueryString(req.query.subType) || '0', 10);
const latestGame = await AppDataSource.getRepository(Activity)
.createQueryBuilder('activity')
.where('activity.villageId = :villageId', { villageId: villageId })
.andWhere('activity.type = :type', { type: type })
.andWhere('activity.subType = :subType', { subType: subType })
.orderBy('activity.createDate', 'DESC')
.getOne();

if (!latestGame) {
next();
return;
}

res.sendJSON(latestGame);
});

// --- Get all games standardised available ---

gameController.get({ path: '/ableToPlayStandardGame', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}
const userId = req.user.id;
const villageId = req.user.type <= UserType.TEACHER ? parseInt(getQueryString(req.query.villageId) || '0', 10) || null : req.user.villageId;
const type = 4;
const subType = parseInt(getQueryString(req.query.subType) || '0', 10);

const games = await AppDataSource.getRepository(Activity)
.createQueryBuilder('activity')
.leftJoin(GameResponse, 'GameResponse')
.andWhere('`activity`.`villageId` = :villageId', { villageId: villageId })
.andWhere('`activity`.`type` = :type', { type: type })
.andWhere('`activity`.`subType` = :subType', { subType: subType })
.andWhere(
(qb) => {
const subQuery = qb
.subQuery()
.select()
.from(GameResponse, 'response')
.where(`response.userId = :userId`, { userId: userId })
.andWhere(`response.gameId = activity.id`)
.andWhere(`response.isOldResponse = 0`)
.getQuery();
return 'NOT EXISTS ' + subQuery;
},
{ userId: req.user.id },
)
.getMany();
res.sendJSON({
activities: games,
});
});

// --- Get number of games standardised available ---

gameController.get({ path: '/countAbleToPlayStandardGame', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
next();
return;
}
const userId = req.user.id;
const villageId = req.user.type <= UserType.TEACHER ? parseInt(getQueryString(req.query.villageId) || '0', 10) || null : req.user.villageId;
const type = 4;
const subType = parseInt(getQueryString(req.query.subType) || '0', 10);

const gameCount = await AppDataSource.getRepository(Activity)
.createQueryBuilder('activity')
.leftJoin(GameResponse, 'GameResponse')
.andWhere('`activity`.`villageId` = :villageId', { villageId: villageId })
.andWhere('`activity`.`type` = :type', { type: type })
.andWhere('`activity`.`subType` = :subType', { subType: subType })
.andWhere(
(qb) => {
const subQuery = qb
.subQuery()
.select()
.from(GameResponse, 'response')
.where(`response.userId = :userId`, { userId: userId })
.andWhere(`response.gameId = activity.id`)
.andWhere(`response.isOldResponse = 0`)
.getQuery();
return 'NOT EXISTS ' + subQuery;
},
{ userId: req.user.id },
)
.getCount();
res.sendJSON({
count: gameCount,
});
});

export { gameController };
15 changes: 15 additions & 0 deletions server/migrations/1710427662589-AlterGameResponseTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { MigrationInterface, QueryRunner } from 'typeorm';

export class AlterGameResponseTable1710427662589 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE game_response DROP FOREIGN KEY FK_3a0c737a217bbd6bbd268203fb8`);
await queryRunner.query(`ALTER TABLE game_response DROP COLUMN gameId`);
await queryRunner.query(`ALTER TABLE game_response ADD COLUMN gameId INT`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE game_response DROP COLUMN gameId`);
await queryRunner.query(`ALTER TABLE game_response ADD COLUMN gameId INT`);
await queryRunner.query(`ALTER TABLE game_response ADD CONSTRAINT FK_3a0c737a217bbd6bbd268203fb8 FOREIGN KEY (gameId) REFERENCES game(id)`);
}
}
2 changes: 1 addition & 1 deletion server/utils/iso-4217-currencies-french.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const currencies: Currency[] = [
{ name: 'Dollars du Zimbabwe', code: 'ZWL', iso: '932' },
{ name: 'Dong', code: 'VND', iso: '704' },
{ name: 'Dram Armenien', code: 'AMD', iso: '51' },
{ name: 'Euro', code: 'EUR', iso: '978' },
{ name: 'Euros', code: 'EUR', iso: '978' },
{ name: 'Florin des Antilles néerlandaises', code: 'ANG', iso: '532' },
{ name: 'Forint', code: 'HUF', iso: '348' },
{ name: 'Franc Burundi', code: 'BIF', iso: '108' },
Expand Down
Loading

0 comments on commit 5684334

Please sign in to comment.