diff options
| author | Kostya <mail@sartin.in> | 2026-08-15 16:25:40 +0000 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-08-15 16:25:40 +0000 |
| commit | 0247f3f5068b96df52a4f9424578d4c66ca6975c (patch) | |
| tree | 0cad643715e5f77920781318ecbb720fa1dec6e6 /ui-console/src/main/java/market/guess/ui/console | |
| parent | d77a70c48865592be9029aeb0c83b36c162ea31a (diff) | |
| download | guess-market-0247f3f5068b96df52a4f9424578d4c66ca6975c.tar.gz guess-market-0247f3f5068b96df52a4f9424578d4c66ca6975c.tar.xz guess-market-0247f3f5068b96df52a4f9424578d4c66ca6975c.zip | |
Implement LMSR and order-book trading logic, wire MarketContext facade and new menu commands
Fill in the previously stubbed cost/pricing math for both trading
mechanisms, and give OrderBookTradingMechanism an actual matching engine
with resting bid/ask books. Fold EventRepository/UserRepository access
into a single MarketContext used by LocalGuessMarketContext, and move the
repository package under infrastructure. Add Buy/EventDetails/LoadState/
SaveState/SettleEvent console commands and wire them into App/Menu.
Diffstat (limited to 'ui-console/src/main/java/market/guess/ui/console')
8 files changed, 242 insertions, 19 deletions
diff --git a/ui-console/src/main/java/market/guess/ui/console/App.java b/ui-console/src/main/java/market/guess/ui/console/App.java index 3d42e9b..330498c 100644 --- a/ui-console/src/main/java/market/guess/ui/console/App.java +++ b/ui-console/src/main/java/market/guess/ui/console/App.java | |||
| @@ -4,16 +4,22 @@ import market.guess.api.GuessMarketContext; | |||
| 4 | import market.guess.service.LocalGuessMarketContext; | 4 | import market.guess.service.LocalGuessMarketContext; |
| 5 | import market.guess.service.TradingOperation; | 5 | import market.guess.service.TradingOperation; |
| 6 | import market.guess.service.TradingOperationImpl; | 6 | import market.guess.service.TradingOperationImpl; |
| 7 | import market.guess.service.infrastructure.MarketContext; | ||
| 7 | import market.guess.service.infrastructure.mapper.v1.EventMapperV1; | 8 | import market.guess.service.infrastructure.mapper.v1.EventMapperV1; |
| 8 | import market.guess.service.infrastructure.provider.Loader; | 9 | import market.guess.service.infrastructure.provider.Loader; |
| 9 | import market.guess.service.infrastructure.provider.v1.XMLLoaderV1; | 10 | import market.guess.service.infrastructure.provider.v1.XMLLoaderV1; |
| 10 | import market.guess.service.repository.EventRepository; | 11 | import market.guess.service.infrastructure.repository.EventRepository; |
| 11 | import market.guess.service.repository.InMemoryEventRepository; | 12 | import market.guess.service.infrastructure.repository.InMemoryEventRepository; |
| 12 | import market.guess.service.repository.InMemoryUserRepository; | 13 | import market.guess.service.infrastructure.repository.InMemoryUserRepository; |
| 13 | import market.guess.service.repository.UserRepository; | 14 | import market.guess.service.infrastructure.repository.UserRepository; |
| 15 | import market.guess.ui.console.command.BuyMenuCommand; | ||
| 16 | import market.guess.ui.console.command.EventDetailsMenuCommand; | ||
| 14 | import market.guess.ui.console.command.ExitMenuCommand; | 17 | import market.guess.ui.console.command.ExitMenuCommand; |
| 15 | import market.guess.ui.console.command.ListEventsMenuCommand; | 18 | import market.guess.ui.console.command.ListEventsMenuCommand; |
| 16 | import market.guess.ui.console.command.LoadFileMenuCommand; | 19 | import market.guess.ui.console.command.LoadFileMenuCommand; |
| 20 | import market.guess.ui.console.command.LoadMarketStateMenuCommand; | ||
| 21 | import market.guess.ui.console.command.SaveMarketStateMenuCommand; | ||
| 22 | import market.guess.ui.console.command.SettleEventMenuCommand; | ||
| 17 | import market.guess.ui.console.io.InputProcessor; | 23 | import market.guess.ui.console.io.InputProcessor; |
| 18 | import org.picocontainer.DefaultPicoContainer; | 24 | import org.picocontainer.DefaultPicoContainer; |
| 19 | import org.picocontainer.behaviors.Caching; | 25 | import org.picocontainer.behaviors.Caching; |
| @@ -24,7 +30,7 @@ public class App { | |||
| 24 | 30 | ||
| 25 | pico.addComponent(EventRepository.class, InMemoryEventRepository.class); | 31 | pico.addComponent(EventRepository.class, InMemoryEventRepository.class); |
| 26 | pico.addComponent(UserRepository.class, InMemoryUserRepository.class); | 32 | pico.addComponent(UserRepository.class, InMemoryUserRepository.class); |
| 27 | 33 | pico.addComponent(MarketContext.class); | |
| 28 | pico.addComponent(TradingOperation.class, TradingOperationImpl.class); | 34 | pico.addComponent(TradingOperation.class, TradingOperationImpl.class); |
| 29 | pico.addComponent(EventMapperV1.class); | 35 | pico.addComponent(EventMapperV1.class); |
| 30 | pico.addComponent(Loader.class, XMLLoaderV1.class); | 36 | pico.addComponent(Loader.class, XMLLoaderV1.class); |
| @@ -33,6 +39,11 @@ public class App { | |||
| 33 | pico.addComponent(LoadFileMenuCommand.class); | 39 | pico.addComponent(LoadFileMenuCommand.class); |
| 34 | pico.addComponent(ListEventsMenuCommand.class); | 40 | pico.addComponent(ListEventsMenuCommand.class); |
| 35 | pico.addComponent(ExitMenuCommand.class); | 41 | pico.addComponent(ExitMenuCommand.class); |
| 42 | pico.addComponent(BuyMenuCommand.class); | ||
| 43 | pico.addComponent(SettleEventMenuCommand.class); | ||
| 44 | pico.addComponent(EventDetailsMenuCommand.class); | ||
| 45 | pico.addComponent(LoadMarketStateMenuCommand.class); | ||
| 46 | pico.addComponent(SaveMarketStateMenuCommand.class); | ||
| 36 | 47 | ||
| 37 | pico.addComponent(InputProcessor.class); | 48 | pico.addComponent(InputProcessor.class); |
| 38 | pico.addComponent(Menu.class); | 49 | pico.addComponent(Menu.class); |
diff --git a/ui-console/src/main/java/market/guess/ui/console/Menu.java b/ui-console/src/main/java/market/guess/ui/console/Menu.java index 18b72b7..90ddd38 100644 --- a/ui-console/src/main/java/market/guess/ui/console/Menu.java +++ b/ui-console/src/main/java/market/guess/ui/console/Menu.java | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | package market.guess.ui.console; | 1 | package market.guess.ui.console; |
| 2 | 2 | ||
| 3 | import java.util.Comparator; | ||
| 3 | import java.util.List; | 4 | import java.util.List; |
| 4 | import market.guess.ui.console.command.MenuCommand; | 5 | import market.guess.ui.console.command.MenuCommand; |
| 5 | import market.guess.ui.console.io.InputProcessor; | 6 | import market.guess.ui.console.io.InputProcessor; |
| @@ -10,7 +11,8 @@ public final class Menu implements Startable { | |||
| 10 | private final List<MenuCommand> commands; | 11 | private final List<MenuCommand> commands; |
| 11 | 12 | ||
| 12 | public Menu(List<MenuCommand> commands, InputProcessor io) { | 13 | public Menu(List<MenuCommand> commands, InputProcessor io) { |
| 13 | this.commands = commands; | 14 | this.commands = |
| 15 | commands.stream().sorted(Comparator.comparingInt(MenuCommand::getIndex)).toList(); | ||
| 14 | this.io = io; | 16 | this.io = io; |
| 15 | } | 17 | } |
| 16 | 18 | ||
| @@ -19,8 +21,11 @@ public final class Menu implements Startable { | |||
| 19 | var max = commands.stream().mapToInt(MenuCommand::getIndex).max().orElse(0); | 21 | var max = commands.stream().mapToInt(MenuCommand::getIndex).max().orElse(0); |
| 20 | io.splashScreen(); | 22 | io.splashScreen(); |
| 21 | while (true) { | 23 | while (true) { |
| 22 | printMenu(); | ||
| 23 | 24 | ||
| 25 | io.newLine(); | ||
| 26 | for (var command : commands) { | ||
| 27 | io.println(" [%d]->> %s".formatted(command.getIndex(), command.getName())); | ||
| 28 | } | ||
| 24 | var selection = io.readInt("Choose a command: ", 1, max); | 29 | var selection = io.readInt("Choose a command: ", 1, max); |
| 25 | var commandOptional = commands.stream().filter(c -> c.getIndex() == selection).findFirst(); | 30 | var commandOptional = commands.stream().filter(c -> c.getIndex() == selection).findFirst(); |
| 26 | 31 | ||
| @@ -32,7 +37,9 @@ public final class Menu implements Startable { | |||
| 32 | 37 | ||
| 33 | command.execute(); | 38 | command.execute(); |
| 34 | 39 | ||
| 35 | if (command.isExit()) return; | 40 | if (command.isExit()) { |
| 41 | break; | ||
| 42 | } | ||
| 36 | } | 43 | } |
| 37 | } | 44 | } |
| 38 | 45 | ||
| @@ -40,11 +47,4 @@ public final class Menu implements Startable { | |||
| 40 | public void stop() { | 47 | public void stop() { |
| 41 | // Do nothing. | 48 | // Do nothing. |
| 42 | } | 49 | } |
| 43 | |||
| 44 | private void printMenu() { | ||
| 45 | io.newLine(); | ||
| 46 | for (var command : commands) { | ||
| 47 | io.println(" [%d]->> %s".formatted(command.getIndex(), command.getName())); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | 50 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java new file mode 100644 index 0000000..b8e2523 --- /dev/null +++ b/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | package market.guess.ui.console.command; | ||
| 2 | |||
| 3 | import market.guess.api.EventStatus; | ||
| 4 | import market.guess.api.GuessMarketContext; | ||
| 5 | import market.guess.exception.GuessMarketException; | ||
| 6 | import market.guess.ui.console.io.InputProcessor; | ||
| 7 | |||
| 8 | public final class BuyMenuCommand implements MenuCommand { | ||
| 9 | private final GuessMarketContext context; | ||
| 10 | private final InputProcessor io; | ||
| 11 | |||
| 12 | public BuyMenuCommand(GuessMarketContext context, InputProcessor io) { | ||
| 13 | super(); | ||
| 14 | this.context = context; | ||
| 15 | this.io = io; | ||
| 16 | } | ||
| 17 | |||
| 18 | @Override | ||
| 19 | public int getIndex() { | ||
| 20 | return 4; | ||
| 21 | } | ||
| 22 | |||
| 23 | @Override | ||
| 24 | public String getName() { | ||
| 25 | return "Buy share(s) in an event"; | ||
| 26 | } | ||
| 27 | |||
| 28 | @Override | ||
| 29 | public boolean allowed() { | ||
| 30 | // TODO Auto-generated method stub | ||
| 31 | throw new UnsupportedOperationException("Unimplemented method 'allowed'"); | ||
| 32 | } | ||
| 33 | |||
| 34 | @Override | ||
| 35 | public void execute() { | ||
| 36 | var active = | ||
| 37 | context.listEvents().stream() | ||
| 38 | .filter(event -> event.status() == EventStatus.ACTIVE) | ||
| 39 | .toList(); | ||
| 40 | |||
| 41 | if (active.isEmpty()) { | ||
| 42 | io.println("There are currently no active events to trade on."); | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | var pick = | ||
| 47 | io.readSelect( | ||
| 48 | "Pick an event: ", | ||
| 49 | active, | ||
| 50 | event -> "[%d] %s".formatted(event.displayId(), event.name())); | ||
| 51 | |||
| 52 | var event = context.getEvent(pick.eventKey()); | ||
| 53 | |||
| 54 | if (event == null) { | ||
| 55 | io.println("That event is no longer available."); | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | // TODO: print state | ||
| 60 | |||
| 61 | var option = | ||
| 62 | io.readSelect( | ||
| 63 | "Choose an option: ", event.state().options(), o -> "%s [value %d, %d shares helds]"); | ||
| 64 | |||
| 65 | var amount = io.readInt("How many shares? ", 1, Integer.MAX_VALUE); | ||
| 66 | |||
| 67 | try { | ||
| 68 | var receipt = context.buyShares("", option.optionKey(), option.optionKey(), amount); | ||
| 69 | } catch (GuessMarketException e) { | ||
| 70 | io.newLine(); | ||
| 71 | io.println("Purchase declined: %s".formatted(e.getMessage())); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java new file mode 100644 index 0000000..e12caeb --- /dev/null +++ b/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | package market.guess.ui.console.command; | ||
| 2 | |||
| 3 | public final class EventDetailsMenuCommand implements MenuCommand { | ||
| 4 | |||
| 5 | @Override | ||
| 6 | public int getIndex() { | ||
| 7 | return 3; | ||
| 8 | } | ||
| 9 | |||
| 10 | @Override | ||
| 11 | public String getName() { | ||
| 12 | return "Show event details"; | ||
| 13 | } | ||
| 14 | |||
| 15 | @Override | ||
| 16 | public boolean allowed() { | ||
| 17 | // TODO Auto-generated method stub | ||
| 18 | throw new UnsupportedOperationException("Unimplemented method 'allowed'"); | ||
| 19 | } | ||
| 20 | |||
| 21 | @Override | ||
| 22 | public void execute() { | ||
| 23 | // TODO Auto-generated method stub | ||
| 24 | throw new UnsupportedOperationException("Unimplemented method 'execute'"); | ||
| 25 | } | ||
| 26 | } | ||
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java new file mode 100644 index 0000000..3111044 --- /dev/null +++ b/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | package market.guess.ui.console.command; | ||
| 2 | |||
| 3 | public final class LoadMarketStateMenuCommand implements MenuCommand { | ||
| 4 | |||
| 5 | @Override | ||
| 6 | public int getIndex() { | ||
| 7 | return 8; | ||
| 8 | } | ||
| 9 | |||
| 10 | @Override | ||
| 11 | public String getName() { | ||
| 12 | return "Load market state"; | ||
| 13 | } | ||
| 14 | |||
| 15 | @Override | ||
| 16 | public boolean allowed() { | ||
| 17 | // TODO Auto-generated method stub | ||
| 18 | throw new UnsupportedOperationException("Unimplemented method 'allowed'"); | ||
| 19 | } | ||
| 20 | |||
| 21 | @Override | ||
| 22 | public void execute() { | ||
| 23 | // TODO Auto-generated method stub | ||
| 24 | throw new UnsupportedOperationException("Unimplemented method 'execute'"); | ||
| 25 | } | ||
| 26 | } | ||
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java new file mode 100644 index 0000000..2f950f0 --- /dev/null +++ b/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | package market.guess.ui.console.command; | ||
| 2 | |||
| 3 | import java.nio.file.Path; | ||
| 4 | import market.guess.api.GuessMarketContext; | ||
| 5 | import market.guess.ui.console.io.InputProcessor; | ||
| 6 | |||
| 7 | public final class SaveMarketStateMenuCommand implements MenuCommand { | ||
| 8 | private final GuessMarketContext context; | ||
| 9 | private final InputProcessor io; | ||
| 10 | |||
| 11 | public SaveMarketStateMenuCommand(GuessMarketContext context, InputProcessor io) { | ||
| 12 | super(); | ||
| 13 | this.context = context; | ||
| 14 | this.io = io; | ||
| 15 | } | ||
| 16 | |||
| 17 | @Override | ||
| 18 | public int getIndex() { | ||
| 19 | return 7; | ||
| 20 | } | ||
| 21 | |||
| 22 | @Override | ||
| 23 | public String getName() { | ||
| 24 | return "Save current state"; | ||
| 25 | } | ||
| 26 | |||
| 27 | @Override | ||
| 28 | public boolean allowed() { | ||
| 29 | // TODO Auto-generated method stub | ||
| 30 | throw new UnsupportedOperationException("Unimplemented method 'allowed'"); | ||
| 31 | } | ||
| 32 | |||
| 33 | @Override | ||
| 34 | public void execute() { | ||
| 35 | var path = io.readPath("Save to: "); | ||
| 36 | |||
| 37 | try { | ||
| 38 | context.saveState(Path.of(path)); | ||
| 39 | io.println("Saved current state to %s.".formatted(path)); | ||
| 40 | } catch (Exception e) { | ||
| 41 | io.println("Failed to save: %s".formatted(e.getMessage())); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/SettleEventMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/SettleEventMenuCommand.java new file mode 100644 index 0000000..9b62be5 --- /dev/null +++ b/ui-console/src/main/java/market/guess/ui/console/command/SettleEventMenuCommand.java | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | package market.guess.ui.console.command; | ||
| 2 | |||
| 3 | import market.guess.api.GuessMarketContext; | ||
| 4 | import market.guess.ui.console.io.InputProcessor; | ||
| 5 | |||
| 6 | public final class SettleEventMenuCommand implements MenuCommand { | ||
| 7 | private final GuessMarketContext context; | ||
| 8 | private final InputProcessor io; | ||
| 9 | |||
| 10 | public SettleEventMenuCommand(GuessMarketContext context, InputProcessor io) { | ||
| 11 | this.context = context; | ||
| 12 | this.io = io; | ||
| 13 | } | ||
| 14 | |||
| 15 | @Override | ||
| 16 | public int getIndex() { | ||
| 17 | return 5; | ||
| 18 | } | ||
| 19 | |||
| 20 | @Override | ||
| 21 | public String getName() { | ||
| 22 | return "Settle an event"; | ||
| 23 | } | ||
| 24 | |||
| 25 | @Override | ||
| 26 | public boolean allowed() { | ||
| 27 | // TODO Auto-generated method stub | ||
| 28 | throw new UnsupportedOperationException("Unimplemented method 'allowed'"); | ||
| 29 | } | ||
| 30 | |||
| 31 | @Override | ||
| 32 | public void execute() { | ||
| 33 | // TODO Auto-generated method stub | ||
| 34 | throw new UnsupportedOperationException("Unimplemented method 'execute'"); | ||
| 35 | } | ||
| 36 | } | ||
diff --git a/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java b/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java index fc5422f..b6d5405 100644 --- a/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java +++ b/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java | |||
| @@ -20,14 +20,20 @@ public final class InputProcessor { | |||
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | public void splashScreen() { | 22 | public void splashScreen() { |
| 23 | IO.println("<<-- GUESS MARKET -->>"); | 23 | newLine(); |
| 24 | IO.println("<<- GUESS MARKET ->>"); | ||
| 25 | } | ||
| 26 | |||
| 27 | public void printState() { | ||
| 28 | nextLine(); | ||
| 29 | println(" Option | Value | Shares "); | ||
| 30 | |||
| 31 | newLine(); | ||
| 24 | } | 32 | } |
| 25 | 33 | ||
| 26 | public String readLine(String prompt) { | 34 | public String readLine(String prompt) { |
| 27 | print(prompt); | 35 | print(prompt); |
| 28 | var line = nextLine(); | 36 | return nextLine().trim(); |
| 29 | |||
| 30 | return line.trim(); | ||
| 31 | } | 37 | } |
| 32 | 38 | ||
| 33 | public int readInt(String prompt, int min, int max) { | 39 | public int readInt(String prompt, int min, int max) { |