Skip to content

Commit 5e26a70

Browse files
authored
Merge pull request #174 from WildCodeSchool/feature/bulle-info
Feature/bulle info
2 parents 989ae7e + 0bd0331 commit 5e26a70

File tree

18 files changed

+215
-150
lines changed

18 files changed

+215
-150
lines changed

packages/backend/api/src/game/creature/post.buy-creature.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,6 @@ import { db } from '@app/backend-shared';
55

66
const postBuyCreature = Router();
77

8-
// check how many creatures we can unlock in this zone
9-
function getTotalCreaturesByZone(zoneId: number) {
10-
return db
11-
.selectFrom('creatures')
12-
.select([db.fn.count('creatures.id').as('quantityCreature')])
13-
.where('creatures.zone_id', '=', zoneId)
14-
.executeTakeFirst();
15-
}
16-
17-
//check how many creatures user has unlocked in this zone
18-
function getCreaturesUnlockedByZone(zoneId: number, parkId: number) {
19-
return db
20-
.selectFrom('park_creatures')
21-
.leftJoin('creatures', 'park_creatures.creature_id', 'creatures.id')
22-
.select([
23-
sql<number>`COUNT(DISTINCT ${sql.ref('park_creatures.creature_id')})`.as(
24-
'countCreaturesUnlockedByZone',
25-
),
26-
])
27-
.where('creatures.zone_id', '=', zoneId)
28-
.where('park_creatures.park_id', '=', parkId)
29-
.executeTakeFirst();
30-
}
31-
328
//Check if we have already the next zone in bdd
339
function getIsNextZoneUnlocked(parkId: number, zoneId: number) {
3410
return db
@@ -161,12 +137,7 @@ postBuyCreature.post('/buy', async (req: Request, res) => {
161137
})
162138
.executeTakeFirst();
163139

164-
const [isNextZoneUnlocked] = await Promise.all([
165-
getTotalCreaturesByZone(zoneId),
166-
getCreaturesUnlockedByZone(zoneId, parkId),
167-
getIsNextZoneUnlocked(parkId, zoneId),
168-
]);
169-
140+
const isNextZoneUnlocked = await getIsNextZoneUnlocked(parkId, zoneId);
170141
const zone = await db
171142
.selectFrom('zones')
172143
.select(['name'])

packages/backend/api/src/game/get.gift.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ getGiftRoute.get('/gift', async (req: Request, res) => {
202202
//else draw a visitor from the unlocked ones
203203
const chosen = visitors[Math.floor(Math.random() * visitors.length)];
204204

205-
await insertGift(parkId, 'visitor', `visitor- ${chosen.category}`);
205+
await insertGift(parkId, 'visitor', `visitor-${chosen.category}`);
206206
await insertVisitorToPark(parkId, chosen);
207207
await updateWallet(parkId, chosen.entry_price);
208208

packages/backend/api/src/game/get.wallet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ getWallet.get('/wallet', async (req: Request, res) => {
2929
return;
3030
}
3131

32+
if (park.wallet === null) {
33+
res.json({
34+
ok: false,
35+
message: 'no wallet for this user',
36+
});
37+
return;
38+
}
39+
3240
res.json({
3341
ok: true,
3442
park,

packages/backend/api/src/game/get.zones-count.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function getZones(parkId: number) {
1818
'zones.src_image',
1919
'zones.link',
2020
'park_zones.id as park_zone_id',
21+
'zones.required_qty',
2122
])
2223
.execute();
2324
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { type Kysely, sql } from 'kysely';
2+
3+
import type { DB } from '@app/shared';
4+
5+
export async function up(db: Kysely<DB>): Promise<void> {
6+
// Migration code that update the database to the desired state.
7+
await db.transaction().execute(async (trx) => {
8+
await sql`
9+
ALTER TABLE zones
10+
ADD COLUMN required_qty INT NOT NULL;
11+
`.execute(trx);
12+
});
13+
}
14+
15+
export async function down(db: Kysely<DB>): Promise<void> {
16+
// Migration code that reverts the database to the previous state.
17+
await db.transaction().execute(async (trx) => {
18+
await sql`
19+
ALTER TABLE zones
20+
DROP COLUMN required_qty;
21+
`.execute(trx);
22+
});
23+
}

packages/frontend/web/src/components/barrier.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type BarrierProps = {
1919
};
2020

2121
export default function Barrier({ barrier, refetch }: BarrierProps) {
22-
const { wallet, walletRefetch } = useGameInfoContext();
22+
const { wallet, refetchWallet } = useGameInfoContext();
2323
const hasEnoughMoons = wallet >= barrier.price;
2424
const priceFormatted = formatNumber(barrier.price);
2525

@@ -41,7 +41,7 @@ export default function Barrier({ barrier, refetch }: BarrierProps) {
4141

4242
if (result.ok === true) {
4343
await refetch();
44-
await walletRefetch();
44+
await refetchWallet();
4545
}
4646
} catch (error) {
4747
// eslint-disable-next-line no-console

packages/frontend/web/src/components/buy-creature.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ export default function BuyCreature({
2222
const [name, setName] = useState('');
2323
const [nameError, setNameError] = useState('');
2424
const [isBought, setIsBought] = useState(false);
25+
2526
const {
2627
wallet,
27-
zonesRefetch,
28-
creaturesRefetch,
29-
visitorsRefetch,
30-
parkRefetch,
31-
walletRefetch,
28+
refetchZones,
29+
refetchCreatures,
30+
refetchVisitors,
31+
refetchPark,
32+
refetchWallet,
3233
creaturesEnclos,
34+
refetchCreaturesMenu,
3335
} = useGameInfoContext();
36+
3437
const { zone_id: zoneId } = useParams();
3538

3639
const creaturesEnclosId = creaturesEnclos.find(
@@ -76,12 +79,13 @@ export default function BuyCreature({
7679

7780
if (result.ok === true) {
7881
await Promise.all([
79-
parkRefetch(),
80-
zonesRefetch(),
82+
refetchPark(),
83+
refetchZones(),
8184
fetchCreatures(),
82-
creaturesRefetch(),
83-
visitorsRefetch(),
84-
walletRefetch(),
85+
refetchCreatures(),
86+
refetchVisitors(),
87+
refetchWallet(),
88+
refetchCreaturesMenu(),
8589
]);
8690

8791
setName('');

packages/frontend/web/src/components/create-park.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export default function CreatePark() {
1212

1313
const { hasParkId, setHasParkId } = useAuth();
1414
const {
15-
parkRefetch,
16-
zonesRefetch,
17-
creaturesRefetch,
18-
visitorsRefetch,
19-
decorationsRefetch,
15+
refetchPark,
16+
refetchZones,
17+
refetchCreatures,
18+
refetchVisitors,
19+
refetchDecorations,
2020
} = useGameInfoContext();
2121

2222
// if we have a parkid go Home
@@ -44,11 +44,11 @@ export default function CreatePark() {
4444
setHasParkId(true);
4545
//Refetch necessary fetch
4646
await Promise.all([
47-
parkRefetch(),
48-
zonesRefetch(),
49-
creaturesRefetch(),
50-
visitorsRefetch(),
51-
decorationsRefetch(),
47+
refetchPark(),
48+
refetchZones(),
49+
refetchCreatures(),
50+
refetchVisitors(),
51+
refetchDecorations(),
5252
]);
5353
}
5454
};

packages/frontend/web/src/components/creature-line.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function CreatureLine({
3737
creature,
3838
potionPrice,
3939
}: CreatureLineProps) {
40-
const { wallet, parkRefetch, walletRefetch } = useGameInfoContext();
40+
const { wallet, refetchPark, refetchWallet } = useGameInfoContext();
4141
const [isClicked, setIsClicked] = useState(false);
4242
const hasEnoughMoons = wallet >= Number(potionPrice);
4343

@@ -58,7 +58,7 @@ export default function CreatureLine({
5858

5959
const result = await response.json();
6060
if (result.ok === true) {
61-
await Promise.all([fetchCreatures(), parkRefetch(), walletRefetch()]);
61+
await Promise.all([fetchCreatures(), refetchPark(), refetchWallet()]);
6262
}
6363
} catch (error) {
6464
// eslint-disable-next-line no-console

packages/frontend/web/src/components/dashboard.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { useGameInfoContext } from '@/contexts/game-info-context';
2-
import useCreaturesMenu from '@/hooks/use-creatures-menu';
32
import useVisitors from '@/hooks/use-visitors';
4-
import useZones from '@/hooks/use-zones';
53
import { formatNumber } from '@/utils/number-formatter';
64

75
import moon from '../assets/images/icons-buttons/moon.png';
@@ -15,10 +13,14 @@ type DashboardProps = {
1513
};
1614

1715
export default function Dashboard({ closeDashboard }: DashboardProps) {
18-
const { parkName, wallet, countVisitorActiveFormated } = useGameInfoContext();
16+
const {
17+
parkName,
18+
wallet,
19+
countVisitorActiveFormated,
20+
creaturesMenu,
21+
unlockedZones,
22+
} = useGameInfoContext();
1923
const { visitors } = useVisitors();
20-
const { unlockedZones } = useZones();
21-
const { creaturesMenu } = useCreaturesMenu();
2224

2325
const countCreaturesIdUnlocked = creaturesMenu.reduce((count, element) => {
2426
return count + (Number(element.quantityCreature) >= 1 ? 1 : 0);

0 commit comments

Comments
 (0)