From a9005c6539a4ad666bf10c2513fcae1aab66f012 Mon Sep 17 00:00:00 2001 From: Mo Date: Thu, 22 Oct 2015 15:11:25 +0300 Subject: [PATCH] add copy mission to route entry --- .../java/ru/trader/model/MissionModel.java | 59 ++++++++++++++----- .../main/java/ru/trader/model/RouteModel.java | 2 + 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/client/src/main/java/ru/trader/model/MissionModel.java b/client/src/main/java/ru/trader/model/MissionModel.java index c7cf710..e28803e 100644 --- a/client/src/main/java/ru/trader/model/MissionModel.java +++ b/client/src/main/java/ru/trader/model/MissionModel.java @@ -6,6 +6,7 @@ import ru.trader.core.Offer; import ru.trader.store.simple.SimpleOffer; import java.util.Collection; +import java.util.Objects; public class MissionModel { private final StationModel target; @@ -17,21 +18,11 @@ public class MissionModel { private Collection reserves; public MissionModel(StationModel target, double profit) { - this.target = target; - this.profit = profit; - item = null; - count = 0; - offer = null; - need = 0; + this(target, null, 0, profit); } public MissionModel(StationModel target, long count, double profit) { - this.target = target; - this.count = count; - this.profit = profit; - this.item = null; - offer = null; - need = 0; + this(target, null, count, profit); } @@ -40,8 +31,13 @@ public class MissionModel { this.item = item; this.count = count; this.profit = profit; - offer = SimpleOffer.fakeBuy(target.getStation(), item.getItem(), profit/count, count); - need = count; + if (item != null) { + offer = SimpleOffer.fakeBuy(target.getStation(), item.getItem(), profit / count, count); + need = count; + } else { + need = 0; + offer = null; + } } public StationModel getTarget() { @@ -65,15 +61,24 @@ public class MissionModel { } public boolean isDelivery(){ - return count > 0; + return item == null && count > 0; } public boolean isCourier(){ - return count == 0; + return item == null && count == 0; } @Override public String toString() { + if (isDelivery()){ + return String.format("Deliver %d items to %s", count, target.getName()); + } + if (isCourier()){ + return String.format("Deliver message to %s", target.getName()); + } + if (isSupply()){ + return String.format("Supply %d %s to %s", count, item.getName(), target.getName()); + } return "MissionModel{" + "target=" + target + ", item=" + item + @@ -121,4 +126,26 @@ public class MissionModel { public boolean isCompleted(){ return need <= 0; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MissionModel that = (MissionModel) o; + return Objects.equals(count, that.count) && + Objects.equals(profit, that.profit) && + Objects.equals(target, that.target) && + Objects.equals(item, that.item); + } + + @Override + public int hashCode() { + return Objects.hash(target, item, count); + } + + + + public MissionModel getCopy(){ + return new MissionModel(target, item, count, profit); + } } diff --git a/client/src/main/java/ru/trader/model/RouteModel.java b/client/src/main/java/ru/trader/model/RouteModel.java index f8cd8dc..bfa244c 100644 --- a/client/src/main/java/ru/trader/model/RouteModel.java +++ b/client/src/main/java/ru/trader/model/RouteModel.java @@ -140,6 +140,7 @@ public class RouteModel { } public void add(int offset, MissionModel mission){ + mission = mission.getCopy(); int completeIndex = -1; Offer offer = mission.getOffer(); if (offer != null){ @@ -176,6 +177,7 @@ public class RouteModel { public void addAll(int offset, Collection missions){ for (MissionModel mission : missions) { + mission = mission.getCopy(); Offer offer = mission.getOffer(); int completeIndex = -1; if (offer != null){