Skip to content

Commit

Permalink
hotfix: fix fake data generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien MAIRE authored and Julien MAIRE committed Feb 3, 2025
1 parent e8152bc commit b629b03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
23 changes: 14 additions & 9 deletions tools/boardStateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ const games = [
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 2, 0, 0],
[2, 2, 0, 1, 1, 0, 0],
[2, 2, 2, 1, 1, 0, 0],
],
fourLineCoordinates: [
[2, 3],
[3, 2],
[3, 3],
[4, 3],
[5, 3],
[3, 4],
[3, 5],
],
isDraw: false,
winner: 1,
currentPlayer: 1,
},

{
Expand All @@ -24,17 +25,18 @@ const games = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 2, 2, 2, 2, 1, 0],
],
fourLineCoordinates: [
[5, 1],
[5, 2],
[5, 3],
[5, 4],
[1, 5],
[2, 5],
[3, 5],
[4, 5],
],
isDraw: false,
winner: 2,
currentPlayer: 2,
},

{
Expand All @@ -49,6 +51,7 @@ const games = [
fourLineCoordinates: [],
isDraw: true,
winner: 0,
currentPlayer: 2,
},

{
Expand All @@ -63,6 +66,7 @@ const games = [
fourLineCoordinates: [],
isDraw: false,
winner: 0,
currentPlayer: 2,
},

{
Expand All @@ -77,6 +81,7 @@ const games = [
fourLineCoordinates: [],
isDraw: false,
winner: 0,
currentPlayer: 2,
},
];

Expand Down
47 changes: 26 additions & 21 deletions tools/populateDbWithFakeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ dotenv.config({ path: path.resolve(__dirname, "../.env") });

const client = new pg.Client(process.env.SUPABASE_DB_URL);

const generateSimpleUsername = () => {
const name = faker.person.firstName().toLowerCase().slice(0, 5); // Prénom en minuscule
const number = faker.number.int({ min: 10, max: 99 }); // Petit nombre
return `${name}${number}`;
};

async function populateDatabase() {
try {
await client.connect();
Expand Down Expand Up @@ -39,10 +45,10 @@ async function populateDatabase() {

const userIds: number[] = [];

for (let i = 0; i < 10; i++) {
for (let i = 0; i < 50; i++) {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const username = faker.internet.username();
const username = generateSimpleUsername();
const email = faker.internet.email();
const passwordHash = faker.internet.password();
const leagueId = leagueIds[Math.floor(Math.random() * leagueIds.length)];
Expand All @@ -65,32 +71,31 @@ async function populateDatabase() {
console.log(`User created: ${firstName} ${lastName}`);
}

for (let i = 0; i < 25; i++) {
for (let i = 0; i < 200; i++) {
const creationDate = faker.date.past();
const lastUpdateDate = faker.date.recent();
const isDraw = faker.datatype.boolean();
const randomGame = games[Math.floor(Math.random() * games.length)];
const secondPlayerId =
randomGame.winner === 2
? randomGame.winner
: userIds[Math.floor(Math.random() * userIds.length)];

const firstPlayerId =
randomGame.winner === 1
? randomGame.winner
: userIds[Math.floor(Math.random() * userIds.length)];
const isDraw = randomGame.isDraw;
const firstPlayerId = userIds[Math.floor(Math.random() * userIds.length)];
let secondPlayerId = 0;
do {
secondPlayerId = userIds[Math.floor(Math.random() * userIds.length)];
} while (secondPlayerId === firstPlayerId);
const winnerId =
randomGame.winner === 0
? null
: randomGame.winner === 1
? firstPlayerId
: secondPlayerId;

const gameState = JSON.stringify({
boardState: randomGame.boardState,
currentPlayer: randomGame.winner === 2 ? secondPlayerId : firstPlayerId,
currentPlayer:
randomGame.currentPlayer === 1 ? firstPlayerId : secondPlayerId,
victoryState: {
player: isDraw
? null
: randomGame.winner === 1
? firstPlayerId
: secondPlayerId,
fourLineCoordinates: isDraw ? [] : randomGame.fourLineCoordinates,
isDraw: randomGame.isDraw,
player: winnerId,
fourLineCoordinates: randomGame.fourLineCoordinates,
isDraw: isDraw,
},
});

Expand Down

0 comments on commit b629b03

Please sign in to comment.