|
|
@ -33,7 +33,7 @@ public class TrainerService extends GenericService<Trainer> {
|
|
|
|
public Trainer addOne(@NotNull Trainer trainer) {
|
|
|
|
public Trainer addOne(@NotNull Trainer trainer) {
|
|
|
|
Trainer persistedTrainer = super.addOne(trainer);
|
|
|
|
Trainer persistedTrainer = super.addOne(trainer);
|
|
|
|
|
|
|
|
|
|
|
|
// If this trainer gained pokemongs, that pokemong's ex-trainer if any needs to lose said pokemong
|
|
|
|
// If this trainer gained pokemongs, that pokemong's ex-trainer if any needs to lose said pokemongs
|
|
|
|
transferNewlyArrivedTrainerPokemongs(new HashSet<>(), persistedTrainer.getPokemongs());
|
|
|
|
transferNewlyArrivedTrainerPokemongs(new HashSet<>(), persistedTrainer.getPokemongs());
|
|
|
|
// all owned pokemongs gain this trainer's reference
|
|
|
|
// all owned pokemongs gain this trainer's reference
|
|
|
|
pokemongService.batchUpdatePokemongTrainers(trainer.getPokemongs(), trainer.getId());
|
|
|
|
pokemongService.batchUpdatePokemongTrainers(trainer.getPokemongs(), trainer.getId());
|
|
|
@ -68,8 +68,7 @@ public class TrainerService extends GenericService<Trainer> {
|
|
|
|
|
|
|
|
|
|
|
|
if (pastOpponents == null) {
|
|
|
|
if (pastOpponents == null) {
|
|
|
|
errors.add("trainer past opponents collection was null");
|
|
|
|
errors.add("trainer past opponents collection was null");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else {
|
|
|
|
|
|
|
|
for (String trainerId : pastOpponents) {
|
|
|
|
for (String trainerId : pastOpponents) {
|
|
|
|
if (StringUtils.isBlankStringOrNull(trainerId)) {
|
|
|
|
if (StringUtils.isBlankStringOrNull(trainerId)) {
|
|
|
|
errors.add("trainer past opponents collection contained an invalid id: " + trainerId);
|
|
|
|
errors.add("trainer past opponents collection contained an invalid id: " + trainerId);
|
|
|
@ -81,14 +80,12 @@ public class TrainerService extends GenericService<Trainer> {
|
|
|
|
|
|
|
|
|
|
|
|
if (pokemongs == null) {
|
|
|
|
if (pokemongs == null) {
|
|
|
|
errors.add("trainer pokemongs collection was null or invalid");
|
|
|
|
errors.add("trainer pokemongs collection was null or invalid");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else {
|
|
|
|
|
|
|
|
for (TrainerPokemong pokemong : pokemongs) {
|
|
|
|
for (TrainerPokemong pokemong : pokemongs) {
|
|
|
|
String pokemongId = pokemong.getId();
|
|
|
|
String pokemongId = pokemong.getId();
|
|
|
|
if (StringUtils.isBlankStringOrNull(pokemongId) || !pokemongService.existsById(pokemongId)) {
|
|
|
|
if (StringUtils.isBlankStringOrNull(pokemongId) || !pokemongService.existsById(pokemongId)) {
|
|
|
|
errors.add("pokemong with id " + pokemongId + " does not exist");
|
|
|
|
errors.add("pokemong with id " + pokemongId + " does not exist");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else {
|
|
|
|
|
|
|
|
if (!pokemongService.isEvoValid(pokemongId, pokemong.getSpecies())) {
|
|
|
|
if (!pokemongService.isEvoValid(pokemongId, pokemong.getSpecies())) {
|
|
|
|
errors.add("pokemong with id " + pokemongId + " cannot be a " +
|
|
|
|
errors.add("pokemong with id " + pokemongId + " cannot be a " +
|
|
|
|
pokemong.getSpecies());
|
|
|
|
pokemong.getSpecies());
|
|
|
@ -97,8 +94,7 @@ public class TrainerService extends GenericService<Trainer> {
|
|
|
|
if (pokemong.getNickname() != null
|
|
|
|
if (pokemong.getNickname() != null
|
|
|
|
&& pokemongBehind != null
|
|
|
|
&& pokemongBehind != null
|
|
|
|
&& !pokemong.getNickname()
|
|
|
|
&& !pokemong.getNickname()
|
|
|
|
.equals(pokemongBehind.getNickname()))
|
|
|
|
.equals(pokemongBehind.getNickname())) {
|
|
|
|
{
|
|
|
|
|
|
|
|
errors.add("pokemong with id " + pokemongId + " already has a nickname");
|
|
|
|
errors.add("pokemong with id " + pokemongId + " already has a nickname");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -106,8 +102,7 @@ public class TrainerService extends GenericService<Trainer> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (trainer.getSchemaVersion() == null ||
|
|
|
|
if (trainer.getSchemaVersion() == null ||
|
|
|
|
!Objects.equals(trainer.getSchemaVersion(), Trainer.LATEST_SCHEMA_VERSION))
|
|
|
|
!Objects.equals(trainer.getSchemaVersion(), Trainer.LATEST_SCHEMA_VERSION)) {
|
|
|
|
{
|
|
|
|
|
|
|
|
errors.add("trainer schema version was null or not the latest version: " + Trainer.LATEST_SCHEMA_VERSION);
|
|
|
|
errors.add("trainer schema version was null or not the latest version: " + Trainer.LATEST_SCHEMA_VERSION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -166,23 +161,25 @@ public class TrainerService extends GenericService<Trainer> {
|
|
|
|
private void transferNewlyArrivedTrainerPokemongs(
|
|
|
|
private void transferNewlyArrivedTrainerPokemongs(
|
|
|
|
@NotNull Set<TrainerPokemong> oldPokemongs,
|
|
|
|
@NotNull Set<TrainerPokemong> oldPokemongs,
|
|
|
|
@NotNull Set<TrainerPokemong> newPokemongs
|
|
|
|
@NotNull Set<TrainerPokemong> newPokemongs
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
{
|
|
|
|
List<Trainer> trainersToUpdate = new ArrayList<>();
|
|
|
|
for (TrainerPokemong tp : newPokemongs) {
|
|
|
|
|
|
|
|
if (oldPokemongs.isEmpty() || !oldPokemongs.contains(tp)) {
|
|
|
|
for (TrainerPokemong newTrainerPokemong : newPokemongs) {
|
|
|
|
Pokemong pokemong = pokemongService.getOneById(tp.getId());
|
|
|
|
if (oldPokemongs.isEmpty() || !oldPokemongs.contains(newTrainerPokemong)) {
|
|
|
|
|
|
|
|
Pokemong pokemong = pokemongService.getOneById(newTrainerPokemong.getId());
|
|
|
|
if (pokemong != null) {
|
|
|
|
if (pokemong != null) {
|
|
|
|
String oldTrainerId = pokemong.getTrainer();
|
|
|
|
String oldTrainerId = pokemong.getTrainer();
|
|
|
|
// If the pokemong already had a trainer, remove it from the old trainer's pokemongs list
|
|
|
|
// If the pokemong already had a trainer, remove it from the old trainer's pokemongs list
|
|
|
|
if (oldTrainerId != null) {
|
|
|
|
if (oldTrainerId != null) {
|
|
|
|
Trainer oldTrainer = getOneById(oldTrainerId);
|
|
|
|
Trainer oldTrainer = getOneById(oldTrainerId);
|
|
|
|
if (oldTrainer != null) {
|
|
|
|
if (oldTrainer != null) {
|
|
|
|
oldTrainer.removePokemong(tp);
|
|
|
|
oldTrainer.removePokemong(newTrainerPokemong.getId());
|
|
|
|
updateOne(oldTrainer);
|
|
|
|
trainersToUpdate.add(oldTrainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAll(trainersToUpdate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|