Skip to content

Commit

Permalink
Send partial listings to the website
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLamoureux committed Dec 5, 2024
1 parent 144f836 commit 93827df
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ plugins {
}

group = 'tools.sctrade'
version = '1.0.4'
version = '1.0.5'

java {
sourceCompatibility = '17'
sourceCompatibility = '21'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
import java.time.Instant;

public record CommodityListing(String location, TransactionType transactionType, String commodity,
double price, int inventory, InventoryLevel inventoryLevel, String batchId, Instant timestamp) {
Double price, Integer inventory, InventoryLevel inventoryLevel, Integer maxBoxSize,
String batchId, Instant timestamp) {
public CommodityListing(String location, TransactionType transactionType, String commodity,
double price, int inventory, InventoryLevel inventoryLevel, String batchId,
Instant timestamp) {
this(location, transactionType, commodity, price, inventory, inventoryLevel, null, batchId,
timestamp);
}

public CommodityListing(String location, TransactionType transactionType, String commodity,
Integer maxBoxSize, String batchId, Instant timestamp) {
this(location, transactionType, commodity, null, null, null, maxBoxSize, batchId, timestamp);
}

public CommodityListing withLocation(String location) {
return new CommodityListing(location, transactionType(), commodity(), price(), inventory(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.TreeMap;
import org.slf4j.Logger;
Expand Down Expand Up @@ -40,6 +41,16 @@ public CommodityListingFactory(CommodityRepository commodityRepository, ImageWri
.withInitial(() -> new CommodityListingsTesseractOcr(preprocessingManipulations));
}

public CommodityListing build(String shopId, String shopName, String commodity,
int maxBoxSizeInScu) {
Instant now = TimeUtil.getNow();
String location = String.format(Locale.ROOT, "%s#%s", shopName, shopId);
String batchId = HashUtil.hash(String.format(Locale.ROOT, "%s%s%d%s", location, commodity,
maxBoxSizeInScu, now.toString()));
return new CommodityListing(location, TransactionType.SELLS, commodity, maxBoxSizeInScu,
batchId, now);
}

public Collection<CommodityListing> build(BufferedImage screenCapture, String location) {
try {
logger.debug("Reading listings...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down Expand Up @@ -32,11 +33,21 @@ public CommodityService(CommoditySubmissionFactory submissionFactory,
this.pendingSubmission = Optional.empty();
}

public void process(CommodityListing commodityListing) throws InterruptedException {
CommoditySubmission submission = submissionFactory.build(commodityListing);

process(submission);
}

@Override
public void process(BufferedImage screenCapture) throws InterruptedException {
CommoditySubmission submission = submissionFactory.build(screenCapture);
notificationService.info(LocalizationUtil.get("infoCommodityListingsRead"));

process(submission);
}

private void process(CommoditySubmission submission) throws InterruptedException {
try {
logger.debug("Acquiring mutex...");
mutex.acquire();
Expand All @@ -52,7 +63,7 @@ public void process(BufferedImage screenCapture) throws InterruptedException {
}
}

@Scheduled(fixedDelay = 600)
@Scheduled(fixedDelay = 30, timeUnit = TimeUnit.SECONDS)
public void flush() throws InterruptedException {
try {
logger.debug("Acquiring mutex...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.sctrade.companion.domain.commodity;

import java.awt.image.BufferedImage;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.sctrade.companion.domain.notification.NotificationService;
Expand Down Expand Up @@ -40,4 +41,8 @@ CommoditySubmission build(BufferedImage screenCapture) {

return new CommoditySubmission(userService.get(), listings);
}

CommoditySubmission build(CommodityListing commodityListing) {
return new CommoditySubmission(userService.get(), List.of(commodityListing));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package tools.sctrade.companion.domain.gamelog.lineprocessors;

import java.util.Locale;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.sctrade.companion.domain.commodity.CommodityListingFactory;
import tools.sctrade.companion.domain.commodity.CommodityService;
import tools.sctrade.companion.domain.gamelog.GameLogLineProcessor;
import tools.sctrade.companion.domain.notification.NotificationService;
import tools.sctrade.companion.utils.LocalizationUtil;

public class LoadShopInventoryDataLogLineProcessor extends GameLogLineProcessor {
private final Logger logger =
LoggerFactory.getLogger(LoadShopInventoryDataLogLineProcessor.class);

public LoadShopInventoryDataLogLineProcessor() {
private NotificationService notificationService;
private CommodityListingFactory commodityListingFactory;
private CommodityService commodityService;

public LoadShopInventoryDataLogLineProcessor(CommodityListingFactory commodityListingFactory,
CommodityService commodityService, NotificationService notificationService) {
this.regex =
".+LoadShopInventoryData.+shopName\\[(?<shopName>\\w+)\\] commodityName\\[ResourceType\\.(?<commodityName>\\w+)\\].+boxSize\\[(?<maxBoxSize>\\d+)\\] \\[Team_NAPU\\]\\[Shops\\]\\[UI\\]";
".+LoadShopInventoryData.+shopId\\[(?<shopId>\\d+)\\] shopName\\[(?<shopName>[\\w-]+)\\] commodityName\\[ResourceType\\.(?<commodityName>[\\w-]+)\\].+boxSize\\[(?<maxBoxSize>\\d+)\\] \\[Team_NAPU\\]\\[Shops\\]\\[UI\\]";
this.commodityListingFactory = commodityListingFactory;
this.commodityService = commodityService;
this.notificationService = notificationService;
}

@Override
Expand All @@ -20,11 +33,24 @@ protected void handle(String value) {
var matcher = pattern.matcher(value);
matcher.matches();

var shopId = matcher.group("shopId");
var shopName = matcher.group("shopName");
var commodityName = matcher.group("commodityName");
var maxBoxSize = Integer.valueOf(matcher.group("maxBoxSize"));

logger.info("Shop {} sells {} in boxes of up to {} SCU", shopName, commodityName, maxBoxSize);
}
logger.info("Shop {}#{} sells {} in boxes of up to {} SCU", shopName, shopId, commodityName,
maxBoxSize);
notificationService
.info(String.format(Locale.ROOT, LocalizationUtil.get("infoLocationDetectedFromLogs"),
String.format(Locale.ROOT, "%s#%s", shopName, shopId)));

var commodityListing =
commodityListingFactory.build(shopId, shopName, commodityName, maxBoxSize);

try {
commodityService.process(commodityListing);
} catch (InterruptedException e) {
logger.error("Could not send {} for processing", commodityListing, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ private CommoditySubmissionDto buildDto(CommoditySubmission submission) {
UserDto userDto = new UserDto(submission.getUser().id(), submission.getUser().label());
List<CommodityListingDto> listings = submission.getListings().parallelStream()
.map(n -> new CommodityListingDto(n.location(), n.transactionType().toString(),
n.commodity(), n.price(), n.inventory(), n.inventoryLevel().getSaturation(),
n.batchId(), new Timestamp(n.timestamp().toEpochMilli())))
n.commodity(), n.price(), n.inventory(),
(n.inventoryLevel() == null ? null : n.inventoryLevel().getSaturation()),
n.maxBoxSize(), n.batchId(), new Timestamp(n.timestamp().toEpochMilli())))
.toList();

return new CommoditySubmissionDto(userDto, listings);
Expand All @@ -113,7 +114,8 @@ private record UserDto(String id, String label) {
}

private record CommodityListingDto(String location, String transaction, String commodity,
double price, Integer quantity, double saturation, String batchId, Timestamp timestamp) {
Double price, Integer quantity, Double saturation, Integer maxBoxSizeInScu, String batchId,
Timestamp timestamp) {
}

private record LocationDto(String name, String type) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/tools/sctrade/companion/spring/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public GameLogPathSubject buildGameLogSubject(SettingRepository settings) {
}

@Bean("TailerListener")
public TailerListener buildTailerListener() {
public TailerListener buildTailerListener(CommodityListingFactory commodityListingFactory,
CommodityService commodityService, NotificationService notificationService) {
var oldLogLineProcessor = new OldLogLineProcessor();
var loadShopInventoryDataLogLineProcessor = new LoadShopInventoryDataLogLineProcessor();
var loadShopInventoryDataLogLineProcessor = new LoadShopInventoryDataLogLineProcessor(
commodityListingFactory, commodityService, notificationService);
var fallbackLogLineProcessor = new FallbackLogLineProcessor();

oldLogLineProcessor.setNext(loadShopInventoryDataLogLineProcessor);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/bundles/localization.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ infoCommodityListingsCsvOutput = Wrote %d commodity listings to '%s'
infoCommodityListingsScTradeToolsOutput = Sent %d commodity listings to SC Trade Tools
infoCommodityListingsRead = Read commodity listings
infoTailingGameLogs = Watching game logs...
infoLocationDetectedFromLogs = Location detected: %s
warnNoLocation = Select the current shop/city name in the "Your inventories" dropdown
errorNotEnoughColumns = [Code 1] Could not make out %d or more column(s) of text in:%n%s
errorRectangleOutOfBounds = [Code 2] Rectangle '%s' is not within '%s'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tools.sctrade.companion.domain.gamelog.lineprocessors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import tools.sctrade.companion.domain.commodity.CommodityListingFactory;
import tools.sctrade.companion.domain.commodity.CommodityService;
import tools.sctrade.companion.domain.notification.NotificationService;

class LoadShopInventoryDataLogLineProcessorTest {
@Mock
private CommodityListingFactory commodityListingFactory;
@Mock
private CommodityService commodityService;
@Mock
private NotificationService notificationService;

private LoadShopInventoryDataLogLineProcessor processor;

@BeforeEach()
void setUp() {
processor = new LoadShopInventoryDataLogLineProcessor(commodityListingFactory, commodityService,
notificationService);
}

@ParameterizedTest
@ValueSource(strings = {
"<2024-12-04T19:23:39.548Z> [Notice] <CEntityComponentCommodityUIProvider::LoadShopInventoryData::<lambda_1>::operator ()> AddingCommodityBox - playerId[123456789012] shopId[5643020808917] shopName[SCShop_CommEx_TDD_Orison] commodityName[ResourceType.Scrap] Available Box Sizes: boxSize[1] boxSize[2] boxSize[4] boxSize[8] boxSize[16] boxSize[24] boxSize[32] [Team_NAPU][Shops][UI]",
"<2024-12-04T19:31:12.471Z> [Notice] <CEntityComponentCommodityUIProvider::LoadShopInventoryData::<lambda_1>::operator ()> AddingCommodityBox - playerId[123456789012] shopId[5643020760642] shopName[SCShop_AdminOffice_NewBabbage-002] commodityName[ResourceType.Scrap] Available Box Sizes: boxSize[1] boxSize[2] boxSize[4] boxSize[8] boxSize[16] boxSize[24] boxSize[32] [Team_NAPU][Shops][UI]"})
void givenValidLogLinesWhenCheckingIfCanHandleThenReturnTrue(String line) {
// TODO
}
}

0 comments on commit 93827df

Please sign in to comment.