summaryrefslogtreecommitdiff
path: root/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain
add: added projectsHEADmain
Diffstat (limited to 'jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain')
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/GameEntity.java4
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemEntity.java13
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemsEntity.java13
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/MoveEntity.java11
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/PlayerEntity.java18
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/StartEntity.java11
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/UpgradeCostEntity.java12
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ErwenService.java4
-rw-r--r--jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ItemProducerService.java199
9 files changed, 285 insertions, 0 deletions
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/GameEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/GameEntity.java
new file mode 100644
index 0000000..e248cb5
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/GameEntity.java
@@ -0,0 +1,4 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+public class GameEntity {
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemEntity.java
new file mode 100644
index 0000000..a8b666d
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemEntity.java
@@ -0,0 +1,13 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+import fr.epita.assistants.common.aggregate.ItemAggregate;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class ItemEntity {
+ long id;
+ float quantity;
+ ItemAggregate.ResourceType type;
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemsEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemsEntity.java
new file mode 100644
index 0000000..b03b373
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/ItemsEntity.java
@@ -0,0 +1,13 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+import fr.epita.assistants.common.api.response.ItemResponse;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+import java.util.List;
+
+@Getter
+@AllArgsConstructor
+public class ItemsEntity {
+ List<ItemEntity> items;
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/MoveEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/MoveEntity.java
new file mode 100644
index 0000000..72ea511
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/MoveEntity.java
@@ -0,0 +1,11 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class MoveEntity {
+ int posX;
+ int posY;
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/PlayerEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/PlayerEntity.java
new file mode 100644
index 0000000..0913c3f
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/PlayerEntity.java
@@ -0,0 +1,18 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+import java.time.LocalDateTime;
+
+@AllArgsConstructor
+@Getter
+public class PlayerEntity {
+ int posX;
+ int posY;
+ LocalDateTime lastMove;
+ LocalDateTime lastCollect;
+ float moveSpeedMultiplier;
+ float staminaMultiplier;
+ float collectRateMultiplier;
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/StartEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/StartEntity.java
new file mode 100644
index 0000000..44a4961
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/StartEntity.java
@@ -0,0 +1,11 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class StartEntity {
+ String mapPath;
+ String rle;
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/UpgradeCostEntity.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/UpgradeCostEntity.java
new file mode 100644
index 0000000..fac79e0
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/entity/UpgradeCostEntity.java
@@ -0,0 +1,12 @@
+package fr.epita.assistants.item_producer.domain.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class UpgradeCostEntity {
+ float upgradeCollectCost;
+ float upgradeMoveCost;
+ float upgradeStaminaCost;
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ErwenService.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ErwenService.java
new file mode 100644
index 0000000..fb3a507
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ErwenService.java
@@ -0,0 +1,4 @@
+package fr.epita.assistants.item_producer.domain.service;
+
+public class ErwenService {
+}
diff --git a/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ItemProducerService.java b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ItemProducerService.java
new file mode 100644
index 0000000..2a224c7
--- /dev/null
+++ b/jws/epibazaar/item-producer/src/main/java/fr/epita/assistants/item_producer/domain/service/ItemProducerService.java
@@ -0,0 +1,199 @@
+package fr.epita.assistants.item_producer.domain.service;
+
+import fr.epita.assistants.common.aggregate.ItemAggregate;
+import fr.epita.assistants.common.aggregate.ResetInventoryAggregate;
+import fr.epita.assistants.common.api.response.*;
+import fr.epita.assistants.common.command.*;
+import fr.epita.assistants.common.utils.Direction;
+import fr.epita.assistants.item_producer.converter.ItemProducerConverter;
+import fr.epita.assistants.item_producer.data.model.ItemModel;
+import fr.epita.assistants.item_producer.data.model.PlayerModel;
+import fr.epita.assistants.item_producer.data.repository.GameRepository;
+import fr.epita.assistants.item_producer.data.repository.ItemRepository;
+import fr.epita.assistants.item_producer.data.repository.PlayerRepository;
+import fr.epita.assistants.item_producer.domain.entity.*;
+import io.smallrye.reactive.messaging.annotations.Broadcast;
+import jakarta.enterprise.context.ApplicationScoped;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.eclipse.microprofile.reactive.messaging.Channel;
+import org.eclipse.microprofile.reactive.messaging.Emitter;
+
+import jakarta.inject.Inject;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
+import java.time.temporal.ChronoUnit;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+@ApplicationScoped
+public class ItemProducerService {
+ @ConfigProperty(name = "JWS_TICK_DURATION")
+ String tps;
+ @ConfigProperty(name = "JWS_DELAY_MOVEMENT")
+ String movementDelay;
+ @ConfigProperty(name = "JWS_DELAY_COLLECT")
+ String collectDelay;
+ @ConfigProperty(name = "JWS_UPGRADE_COLLECT_COST")
+ String upgradeCollectCost;
+ @ConfigProperty(name = "JWS_UPGRADE_MOVE_COST")
+ String upgradeMoveCost;
+ @ConfigProperty(name = "JWS_UPGRADE_STAMINA_COST")
+ String upgradeStaminaCost;
+ @ConfigProperty(name = "JWS_UPGRADE_MULTIPLIER")
+ String upgradeMultiplier;
+ @Inject
+ PlayerRepository playerRepository;
+ @Inject
+ GameRepository gameRepository;
+ @Inject
+ ItemRepository itemRepository;
+
+ @Inject
+ @Channel("reset-inventory-command")
+ @Broadcast
+ Emitter<ResetInventoryCommand> resetInventoryCommandEmitter;
+ @Inject
+ @Channel("collect-item-command")
+ @Broadcast
+ Emitter<CollectItemCommand> collectItemCommandEmitter;
+ @Inject
+ @Channel("upgrade-movement-speed-command")
+ @Broadcast
+ Emitter<UpgradeMovementSpeedCommand> upgradeMovementSpeedCommandEmitter;
+ @Inject
+ @Channel("upgrade-stamina-command")
+ @Broadcast
+ Emitter<UpgradeStaminaCommand> upgradeStaminaCommandEmitter;
+ @Inject
+ @Channel("upgrade-collect-rate-command")
+ @Broadcast
+ Emitter<UpgradeCollectRateCommand> upgradeCollectRateCommandEmitter;
+
+ public StartEntity startGame(StartEntity startEntity) {
+ if (startEntity == null)
+ return null;
+ gameRepository.clearDB();
+ playerRepository.clearDB();
+ try (Stream<String> stream = Files.lines(Paths.get(startEntity.getMapPath()))) {
+ if (new File(startEntity.getMapPath()).isDirectory())
+ return null;
+ String rle = stream.collect(Collectors.joining(";"));
+ gameRepository.addEntry(rle);
+ playerRepository.addEntry();
+ resetInventoryCommandEmitter.send(new ResetInventoryCommand());
+ return new StartEntity(null, rle);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ public void clearItems(ResetInventoryAggregate items) {
+ itemRepository.deleteItems(ItemProducerConverter.DeleteItems(items));
+ }
+
+ public ItemsEntity getAllItems() {
+ return new ItemsEntity(itemRepository.getItems().stream().map(m -> new ItemEntity(m.getId(),
+ m.getQuantity(), m.getType())).toList());
+ }
+
+ public boolean isGameRunning() {
+ return !gameRepository.isEmpty();
+ }
+
+ public boolean canMove() {
+ long tickRate = Long.parseLong(tps);
+ long moveDelay = Long.parseLong(movementDelay);
+ PlayerEntity player = getCurrentPlayer();
+ return player.getLastMove() == null || player.getLastMove().plus((long) (moveDelay * tickRate * player.getMoveSpeedMultiplier()),
+ ChronoUnit.MILLIS).isBefore(LocalDateTime.now());
+ }
+
+ public boolean canCollect() {
+ long tickRate = Long.parseLong(tps);
+ long collectDelayLong = Long.parseLong(collectDelay);
+ PlayerEntity player = getCurrentPlayer();
+ return player.getLastCollect() == null || player.getLastCollect().plus((long) (collectDelayLong * tickRate * player.getMoveSpeedMultiplier()),
+ ChronoUnit.MILLIS).isBefore(LocalDateTime.now());
+ }
+
+ public PlayerEntity getCurrentPlayer() {
+ PlayerModel p = playerRepository.getFirstPlayer();
+ return new PlayerEntity(p.getPosX(), p.getPosY(), p.getLastMove(), p.getLastCollect(),
+ p.getMoveSpeedMultiplier(), p.getStaminaMultiplier(), p.getCollectRateMultiplier());
+ }
+
+ public UpgradeCostEntity getUpgradeCosts() {
+ return new UpgradeCostEntity(Float.parseFloat(upgradeCollectCost),
+ Float.parseFloat(upgradeMoveCost), Float.parseFloat(upgradeStaminaCost));
+ }
+
+ public MoveEntity move(Direction direction) {
+ if (direction == null)
+ return null;
+ if (!playerRepository.isValidMove(direction, gameRepository.getMap()))
+ return null;
+ playerRepository.movePlayer(direction);
+ return ItemProducerConverter.Move(playerRepository.getFirstPlayer());
+ }
+
+ public StartEntity collect() {
+ PlayerEntity player = getCurrentPlayer();
+ if (!gameRepository.isValidCollect(player.getPosX(), player.getPosY()))
+ return null;
+ String item = gameRepository.collectResource(player.getPosX(), player.getPosY());
+ gameRepository.setToGround(player.getPosX(), player.getPosY());
+ collectItemCommandEmitter.send(new CollectItemCommand(ItemAggregate.ResourceType.valueOf(item),
+ player.getCollectRateMultiplier()));
+ return ItemProducerConverter.Collect(item);
+ }
+
+ public void collectItem(ItemAggregate agr) {
+ Optional<ItemModel> item = itemRepository.exists(agr.getType());
+ if (item.isPresent()) {
+ itemRepository.fillInventory(agr.getType(), agr.getQuantity());
+ } else {
+ itemRepository.addItem(agr.getType(), agr.getQuantity());
+ }
+ }
+
+ public Double getMoney() {
+ List<ItemModel> entries = itemRepository.getMoney();
+ if (entries.isEmpty())
+ return null;
+ return entries.stream().mapToDouble(m -> m.getQuantity().doubleValue()).sum();
+ }
+
+ public void upgradeMove() {
+ upgradeMovementSpeedCommandEmitter.send(new UpgradeMovementSpeedCommand(Float.parseFloat(upgradeMoveCost)));
+ }
+
+ public void updateMove() {
+ playerRepository.updateMoveSpeed(getCurrentPlayer().getMoveSpeedMultiplier() - Float.parseFloat(upgradeMultiplier));
+ }
+
+ public void upgradeCollect() {
+ upgradeCollectRateCommandEmitter.send(new UpgradeCollectRateCommand(Float.parseFloat(upgradeMoveCost)));
+ }
+
+ public void updateCollect() {
+ playerRepository.updateCollectRate(getCurrentPlayer().getCollectRateMultiplier() - Float.parseFloat(upgradeMultiplier));
+ }
+
+ public void upgradeStamina() {
+ upgradeStaminaCommandEmitter.send(new UpgradeStaminaCommand(Float.parseFloat(upgradeMoveCost)));
+ }
+
+ public void updateStamina() {
+ playerRepository.updateStamina(getCurrentPlayer().getStaminaMultiplier() - Float.parseFloat(upgradeMultiplier));
+ }
+
+ public void pay(Float newMoney) {
+ itemRepository.fillInventory(ItemAggregate.ResourceType.MONEY, newMoney);
+ }
+}