Skip to content

Commit

Permalink
add max for evole
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin-Morette committed Jun 29, 2024
1 parent c86b2aa commit e472180
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 45 deletions.
188 changes: 143 additions & 45 deletions backend/src/controllers/PokemonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,64 +166,162 @@ class PokemonController {
});
};

static evolvePokemon = (req, res) => {
const { namePokemon } = req.body;
const { idTrainer } = req.body;
const { isShiny } = req.body;
const { quantity } = req.body;
models.pokemon.findByName(namePokemon).then(([pokemon]) => {
static evolvePokemon = async (req, res) => {
const { namePokemon, idTrainer, isShiny, max } = req.body;
let { quantity } = req.body;

try {
const [pokemon] = await models.pokemon.findByName(namePokemon);
if (pokemon.length === 0) {
res.status(201).send({ status: "noExistPokemon" });
return;
}
if (pokemon[0].idEvolution === null) {
if (!pokemon[0].idEvolution) {
res.status(201).send({ status: "noEvolution" });
return;
}
models.pokemon_trainer
.updateDownQuantity(

if (max) {
const [result] = await models.pokemon_trainer.findQuantity(
pokemon[0].id,
idTrainer,
pokemon[0].numberEvolution * quantity,
isShiny
)
.then(([result]) => {
if (result.affectedRows === 0) {
res.status(201).send({
status: "noPokemon",
numberPokemon: pokemon[0].numberEvolution,
});
} else {
let { idEvolution } = pokemon[0];
// Evolution Evoli
if (idEvolution === 134) {
const randomNum = Math.floor(Math.random() * 3);
idEvolution = 134 + randomNum;
}
const pokemonTrainer = {
idPokemon: idEvolution,
idTrainer,
isShiny,
};
models.pokemon_trainer.insert(pokemonTrainer, quantity).then(() => {
models.pokemon.find(idEvolution).then(([resultPokemon]) => {
res.status(201).send({
status: "evolve",
isShiny,
pokemonPreEvolve: pokemon[0],
pokemonEvolve: resultPokemon[0],
});
});
});
}
})
.catch((err) => {
console.error(err);
res.sendStatus(500);
);
if (result.length === 0) {
res.status(201).send({
status: "noPokemon",
numberPokemon: pokemon[0].numberEvolution,
quantity: 1,
});
return;
}
quantity = Math.floor(result[0].quantity / pokemon[0].numberEvolution);
}

const updateResult = await models.pokemon_trainer.updateDownQuantity(
pokemon[0].id,
idTrainer,
pokemon[0].numberEvolution * quantity,
isShiny
);

if (updateResult[0].affectedRows === 0) {
res.status(201).send({
status: "noPokemon",
numberPokemon: pokemon[0].numberEvolution,
quantity,
});
});
return;
}

// Gestion de l'évolution spécifique d'Evoli
let idEvolution = pokemon[0].idEvolution;
if (idEvolution === 134) {
const randomNum = Math.floor(Math.random() * 3);
idEvolution += randomNum;
}

const pokemonTrainer = { idPokemon: idEvolution, idTrainer, isShiny };
await models.pokemon_trainer.insert(pokemonTrainer, quantity);

const [resultPokemon] = await models.pokemon.find(idEvolution);
res.status(201).send({
status: "evolve",
isShiny,
pokemonPreEvolve: pokemon[0],
pokemonEvolve: resultPokemon[0],
quantity,
});
} catch (err) {
console.error(err);
res.sendStatus(500);
}
};

// static evolvePokemon = async (req, res) => {
// const { namePokemon } = req.body;
// const { idTrainer } = req.body;
// const { isShiny } = req.body;
// let { quantity } = req.body;
// const { max } = req.body;
// models.pokemon.findByName(namePokemon).then(([pokemon]) => {
// if (pokemon.length === 0) {
// res.status(201).send({ status: "noExistPokemon" });
// return;
// }
// if (pokemon[0].idEvolution === null) {
// res.status(201).send({ status: "noEvolution" });
// return;
// }
// // if (max) {
// // models.pokemon_trainer
// // .findQuantity(pokemon[0].id, idTrainer, isShiny)
// // .then(([result]) => {
// // if (result.length === 0) {
// // res.status(201).send({ status: "noPokemon" });
// // return;
// // }
// // quantity = Math.floor(
// // result[0].quantity / pokemon[0].numberEvolution
// // );
// // });
// // }
// if (max) {
// const [result] = await models.pokemon_trainer.findQuantity(
// pokemon[0].id,
// idTrainer,
// isShiny
// );
// if (result.length === 0) {
// res.status(201).send({ status: "noPokemon" });
// return;
// }
// quantity = Math.floor(result[0].quantity / pokemon[0].numberEvolution);
// }
// models.pokemon_trainer
// .updateDownQuantity(
// pokemon[0].id,
// idTrainer,
// pokemon[0].numberEvolution * quantity,
// isShiny
// )
// .then(([result]) => {
// if (result.affectedRows === 0) {
// res.status(201).send({
// status: "noPokemon",
// numberPokemon: pokemon[0].numberEvolution,
// });
// } else {
// let { idEvolution } = pokemon[0];
// // Evolution Evoli
// if (idEvolution === 134) {
// const randomNum = Math.floor(Math.random() * 3);
// idEvolution = 134 + randomNum;
// }
// const pokemonTrainer = {
// idPokemon: idEvolution,
// idTrainer,
// isShiny,
// };
// models.pokemon_trainer.insert(pokemonTrainer, quantity).then(() => {
// models.pokemon.find(idEvolution).then(([resultPokemon]) => {
// res.status(201).send({
// status: "evolve",
// isShiny,
// pokemonPreEvolve: pokemon[0],
// pokemonEvolve: resultPokemon[0],
// });
// });
// });
// }
// })
// .catch((err) => {
// console.error(err);
// res.sendStatus(500);
// });
// });
// };

static infoPokemon = async (req, res) => {
try {
const { namePokemon } = req.body;
Expand Down
7 changes: 7 additions & 0 deletions backend/src/models/PokemonTrainerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class PokemonTrainerManager extends AbstractManager {
);
}

findQuantity(idPokemon, idTrainer, isShiny = false) {
return this.connection.query(
`select quantity from ${PokemonTrainerManager.table} where idPokemon = ? AND idTrainer = ? AND isShiny = ?`,
[idPokemon, idTrainer, isShiny]
);
}

updateDownQuantity(idPokemon, idTrainer, number, isShiny = false) {
return this.connection.query(
`update ${PokemonTrainerManager.table} set quantity = quantity - ? where idPokemon = ? AND idTrainer = ? AND quantity >= ? AND isShiny = ?`,
Expand Down

0 comments on commit e472180

Please sign in to comment.