io.mstream.trader.simulation.simulation.ContinueSimulationService Maven / Gradle / Ivy
package io.mstream.trader.simulation.simulation;
import io.mstream.trader.simulation.handlers.api.simulation.continuation.data
.ContinueRequestPayload;
import io.mstream.trader.simulation.handlers.api.simulation.data.Simulation;
import io.mstream.trader.simulation.handlers.api.simulation.data
.SimulationFactory;
import io.mstream.trader.simulation.security.SimulationTokenCipher;
import io.mstream.trader.simulation.stocks.Stock;
import io.mstream.trader.simulation.stocks.StockFactory;
import io.mstream.trader.simulation.stocks.StocksPriceRepository;
import io.mstream.trader.simulation.stocks.datafeed.api.DataFeed;
import io.mstream.trader.simulation.stocks.datafeed.data.StockPrice;
import rx.Single;
import javax.inject.Inject;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
public class ContinueSimulationService {
private final StocksPriceRepository stocksPriceRepository;
private final SimulationFactory simulationFactory;
private final SimulationTokenFactory simulationTokenFactory;
private final SimulationTokenCipher simulationTokenCipher;
private final DateTimeFormatter dateFormatter;
private final StockFactory stockFactory;
@Inject
public ContinueSimulationService(
@DataFeed
StocksPriceRepository stocksPriceRepository,
SimulationFactory simulationFactory,
SimulationTokenFactory simulationTokenFactory,
SimulationTokenCipher simulationTokenCipher,
DateTimeFormatter dateFormatter,
StockFactory stockFactory
) {
this.stocksPriceRepository = stocksPriceRepository;
this.simulationFactory = simulationFactory;
this.simulationTokenFactory = simulationTokenFactory;
this.simulationTokenCipher = simulationTokenCipher;
this.dateFormatter = dateFormatter;
this.stockFactory = stockFactory;
}
public Single continuation(ContinueRequestPayload payload) {
SimulationToken payloadToken =
decryptToken(
payload.getToken()
);
Stock stock =
stockFactory.create(
payloadToken.getStockName()
);
int simulationDay = parseInt(payload.getSimulationDay());
double requestStockPrice =
parseDouble(
payload.getStockPrice()
);
long balance = parseLong(payload.getBalance());
LocalDate nextDay =
calculateNextDay(
payloadToken.getStartDate(),
simulationDay
);
SimulationToken responseToken =
simulationTokenFactory
.create(
stock.getName(),
payloadToken.getStartDate()
);
String encryptedResponseToken =
simulationTokenCipher.encrypt(
responseToken
);
return stocksPriceRepository
.get(
new StocksPriceRepository.Key(
stock,
nextDay
)
)
.map(
StockPrice::getValue
)
.map(valueText ->
simulationFactory.create(
encryptedResponseToken,
valueText,
String.valueOf(
simulationDay + 1
),
payload.getSimulationEndDay(),
Integer.toString(0),
Integer.toString(10_000)
)
);
}
private SimulationToken decryptToken(String token) {
return simulationTokenCipher
.decrypt(token)
.get();
}
private LocalDate calculateNextDay(
String startDateText,
int simulationDay
) {
return LocalDate
.from(
dateFormatter.parse(
startDateText
)
)
.plus(
simulationDay,
ChronoUnit.DAYS
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy