Skip to content

Commit

Permalink
Fix #541
Browse files Browse the repository at this point in the history
Also removed capacity to keep track of where a unit comes from, or goes to
  • Loading branch information
KevinGuancheDarias committed Feb 16, 2024
1 parent 4f8d437 commit 320df8d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public Mission findDeployedMissionOrCreate(ObtainedUnit unit) {
if (existingMissionOpt.isPresent()) {
var existingMission = existingMissionOpt.get();
existingMission.getInvolvedUnits().add(unit);
unit.setFirstDeploymentMission(null);
unit.setMission(existingMission);
obtainedUnitRepository.save(unit);
return existingMission;
Expand All @@ -54,21 +53,11 @@ public Mission findDeployedMissionOrCreate(ObtainedUnit unit) {
deployedMission.setUser(user);
deployedMission.setInvolvedUnits(new ArrayList<>());
deployedMission.getInvolvedUnits().add(unit);
if (unit.getFirstDeploymentMission() == null) {
deployedMission.setSourcePlanet(origin);
deployedMission.setTargetPlanet(target);
var savedDeployedMission = missionRepository.save(deployedMission);
unit.setFirstDeploymentMission(deployedMission);
obtainedUnitRepository.save(unit);
return savedDeployedMission;
} else {
missionRepository.findById(unit.getFirstDeploymentMission().getId())
.ifPresent(firstDeploymentMission -> {
deployedMission.setSourcePlanet(firstDeploymentMission.getSourcePlanet());
deployedMission.setTargetPlanet(firstDeploymentMission.getTargetPlanet());
});
return missionRepository.save(deployedMission);
}
deployedMission.setSourcePlanet(origin);
deployedMission.setTargetPlanet(target);
var savedDeployedMission = missionRepository.save(deployedMission);
obtainedUnitRepository.save(unit);
return savedDeployedMission;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,11 @@ private ObtainedUnit configureObtainedUnit(
) {
var retVal = new ObtainedUnit();
retVal.setMission(mission);
var firstDeploymentMission = dbUnit.getFirstDeploymentMission();
retVal.setFirstDeploymentMission(firstDeploymentMission);
retVal.setCount(count);
retVal.setUser(user);
retVal.setUnit(unitRepository.getReferenceById(dbUnit.getUnit().getId()));
retVal.setExpirationId(dbUnit.getExpirationId());
retVal.setSourcePlanet(firstDeploymentMission == null ? mission.getSourcePlanet()
: firstDeploymentMission.getSourcePlanet());
retVal.setSourcePlanet(mission.getSourcePlanet());
retVal.setTargetPlanet(mission.getTargetPlanet());
retVal.setOwnerUnit(ownerUnit);
return retVal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
import com.kevinguanchedarias.owgejava.util.SpringRepositoryUtil;
import com.kevinguanchedarias.taggablecache.aspect.TaggableCacheEvictByTag;
import com.kevinguanchedarias.taggablecache.manager.TaggableCacheManager;
import jakarta.persistence.EntityManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import jakarta.persistence.EntityManager;

import java.io.Serial;
import java.util.List;

Expand Down Expand Up @@ -201,7 +200,6 @@ public ObtainedUnit moveUnit(ObtainedUnit unit, Integer userId, Long planetId) {
savedUnit = saveWithAdding(userId, unit, planetId);
unit.setMission(null);
unit.setTargetPlanet(null);
unit.setFirstDeploymentMission(null);
unit.setOwnerUnit(null);
} else if (unit.getMission() != null && MissionType.valueOf(unit.getMission().getType().getCode()) == MissionType.DEPLOYED) {
savedUnit = repository.save(unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.kevinguanchedarias.owgejava.entity.cache.EntityWithTaggableCacheByUser;
import com.kevinguanchedarias.owgejava.entity.listener.EntityWithByUserCacheTagListener;
import lombok.*;

import jakarta.persistence.*;
import lombok.*;

import java.io.Serial;
import java.util.List;
Expand Down Expand Up @@ -50,10 +49,6 @@ public class ObtainedUnit implements EntityWithTaggableCacheByUser<Long> {
@JoinColumn(name = "mission_id")
private Mission mission;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "first_deployment_mission")
private Mission firstDeploymentMission;

private boolean isFromCapture = false;

private Long expirationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.kevinguanchedarias.owgejava.repository.PlanetRepository;
import com.kevinguanchedarias.owgejava.test.answer.InvokeRunnableLambdaAnswer;
import com.kevinguanchedarias.taggablecache.manager.TaggableCacheManager;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -29,8 +30,6 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.system.OutputCaptureExtension;

import jakarta.persistence.EntityManager;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -223,7 +222,6 @@ void moveUnit_should_handle_save_to_user_owned_planet() {
ou.setSourcePlanet(null);
ou.setMission(givenExploreMission());
ou.setTargetPlanet(givenTargetPlanet());
ou.setFirstDeploymentMission(givenDeployMission());
ou.setOwnerUnit(givenObtainedUnit2());
var planet = givenSourcePlanet();
given(planetRepository.findById(SOURCE_PLANET_ID)).willReturn(Optional.of(planet));
Expand All @@ -240,7 +238,6 @@ void moveUnit_should_handle_save_to_user_owned_planet() {
assertThat(saved.getSourcePlanet()).isEqualTo(planet);
assertThat(saved.getTargetPlanet()).isNull();
assertThat(saved.getMission()).isNull();
assertThat(saved.getFirstDeploymentMission()).isNull();
assertThat(saved.getOwnerUnit()).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.kevinguanchedarias.owgejava.entity.Mission;
import com.kevinguanchedarias.owgejava.entity.MissionInformation;
import com.kevinguanchedarias.owgejava.entity.ObtainedUnit;
import com.kevinguanchedarias.owgejava.entity.Planet;
import com.kevinguanchedarias.owgejava.enumerations.MissionType;
import com.kevinguanchedarias.owgejava.mock.MissionMock;
import com.kevinguanchedarias.owgejava.repository.MissionRepository;
Expand Down Expand Up @@ -80,7 +79,6 @@ class MissionFinderBoTest {
@Test
void findDeployedMissionOrCreate_should_merge_new_deployment_with_existing_old_for_unit_group() {
var ou = givenObtainedUnit1();
ou.setFirstDeploymentMission(new Mission());
var existingMission = MissionMock.givenDeployedMission();
existingMission.setInvolvedUnits(new ArrayList<>());
when(missionRepository.findByUserIdAndTypeCodeAndTargetPlanetIdAndResolvedFalse(
Expand All @@ -96,7 +94,6 @@ void findDeployedMissionOrCreate_should_merge_new_deployment_with_existing_old_f
verify(this.obtainedUnitRepository, times(1)).save(captor.capture());
var saved = captor.getValue();
assertThat(saved).isEqualTo(ou);
assertThat(saved.getFirstDeploymentMission()).isNull();
assertThat(saved.getMission()).isEqualTo(existingMission);
verify(missionTypeBo, never()).find(any());
verify(missionRepository, never()).save(any());
Expand Down Expand Up @@ -126,35 +123,6 @@ void findDeployedMissionOrCreate_should_create_new_mission_when_not_exists_nor_h
assertThat(savedMission.getTargetPlanet()).isEqualTo(ou.getTargetPlanet());
var unitCaptor = ArgumentCaptor.forClass(ObtainedUnit.class);
verify(obtainedUnitRepository, times(1)).save(unitCaptor.capture());
var savedUnit = unitCaptor.getValue();
assertThat(savedUnit.getFirstDeploymentMission()).isEqualTo(savedMission);
assertThat(result).isEqualTo(savedMission);
}

@Test
void findDeployedMissionOrCreate_should_create_new_mission_and_use_first_deployment_coordinates() {
var ou = givenObtainedUnit1();
var firstDeploymentSourcePlanet = new Planet();
var firstDeploymentTargetPlanet = new Planet();
var firstDeploymentMission = givenRawMission(firstDeploymentSourcePlanet, firstDeploymentTargetPlanet);
var missionType = givenMissionType(MissionType.DEPLOYED);
ou.setFirstDeploymentMission(firstDeploymentMission);

when(missionTypeBo.find(MissionType.DEPLOYED)).thenReturn(missionType);
when(missionRepository.save(any())).thenAnswer(returnsFirstArg());
when(obtainedUnitRepository.save(any())).thenAnswer(returnsFirstArg());
when(missionRepository.findById(any())).thenReturn(Optional.of(firstDeploymentMission));

var result = missionFinderBo.findDeployedMissionOrCreate(ou);

var captor = ArgumentCaptor.forClass(Mission.class);
verify(missionRepository, times(1)).save(captor.capture());
var savedMission = captor.getValue();
assertThat(savedMission.getType()).isEqualTo(missionType);
assertThat(savedMission.getUser()).isEqualTo(ou.getUser());
assertThat(savedMission.getSourcePlanet()).isEqualTo(firstDeploymentSourcePlanet);
assertThat(savedMission.getTargetPlanet()).isEqualTo(firstDeploymentTargetPlanet);
verify(obtainedUnitRepository, never()).save(any());
assertThat(result).isEqualTo(savedMission);
}

Expand Down Expand Up @@ -208,7 +176,7 @@ void findBuildMissions_should_work(List<ObtainedUnit> ouList, Long expectedCount
var retVal = this.missionFinderBo.findBuildMissions(USER_ID_1);

assertThat(retVal).hasSize(1);
var entry = retVal.get(0);
var entry = retVal.getFirst();
assertThat(entry.getUnit().getId()).isEqualTo(UNIT_ID_1);
assertThat(entry.getMissionId()).isEqualTo(BUILD_MISSION_ID);
assertThat(entry.getSourcePlanet().getId()).isEqualTo(SOURCE_PLANET_ID);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.kevinguanchedarias.owgejava.business.mission.unit.registration;

import com.kevinguanchedarias.owgejava.entity.Mission;
import com.kevinguanchedarias.owgejava.entity.Planet;
import com.kevinguanchedarias.owgejava.enumerations.MissionType;
import com.kevinguanchedarias.owgejava.pojo.UnitInMap;
import com.kevinguanchedarias.owgejava.pojo.storedunit.UnitWithItsStoredUnits;
import com.kevinguanchedarias.owgejava.repository.UnitRepository;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -47,20 +45,16 @@ class MissionRegistrationUnitManagerTest {
this.unitRepository = unitRepository;
}

@ParameterizedTest
@MethodSource("manageUnitsRegistration_should_work_arguments")
void manageUnitsRegistration_should_work(
boolean isEnemyPlanet,
int expectedSizeAlteredMissions,
Mission firstDeploymentMission,
Planet expectedSourcePlanet
) {
@Test
void manageUnitsRegistration_should_work() {
boolean isEnemyPlanet = false;
int expectedSizeAlteredMissions = 0;
Planet expectedSourcePlanet = givenSourcePlanet();
var expirationId = 99282441L;
var information = givenUnitMissionInformation(MissionType.EXPLORE, expirationId);
var mission = givenGatherMission();
var user = givenUser1();
var ouDb = givenObtainedUnit1().toBuilder()
.firstDeploymentMission(firstDeploymentMission)
.expirationId(expirationId)
.build();
var dbUnits = Map.of(new UnitInMap(UNIT_ID_1, expirationId), new UnitWithItsStoredUnits(ouDb, null));
Expand All @@ -72,7 +66,6 @@ void manageUnitsRegistration_should_work(
assertThat(retVal.getAlteredVisibilityMissions()).hasSize(expectedSizeAlteredMissions);
assertThat(retVal.getUnits()).isNotEmpty();
var savedUnit = retVal.getUnits().get(0);
assertThat(savedUnit.getFirstDeploymentMission()).isEqualTo(firstDeploymentMission);
assertThat(savedUnit.getCount()).isEqualTo(SELECTED_UNIT_COUNT);
assertThat(savedUnit.getUser()).isEqualTo(user);
assertThat(savedUnit.getUnit()).isEqualTo(unit);
Expand Down
1 change: 1 addition & 0 deletions game-frontend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v0.11.5 (latest)
* __Improvememnt:__ [Allow to know that a mission is invisible to the mission sender user #532](https://github.com/KevinGuancheDarias/owge/issues/532)
* __Fix:__ [In Neon the gather reports don't show properly the resources, they have free width and height #523](https://github.com/KevinGuancheDarias/owge/issues/523)
* __Improvememnt:__ [Add button to mark all messages as read #514](https://github.com/KevinGuancheDarias/owge/issues/514)
* __Fix:__ [Deployment to non owned planet, and then EB mission to other planet, and them from that planet deploy to other non owned planet, would bug the deploy mission #541](https://github.com/KevinGuancheDarias/owge/issues/541)

v0.11.4 (2024-02-08 05:39)
==================
Expand Down

0 comments on commit 320df8d

Please sign in to comment.