aboutsummaryrefslogtreecommitdiffstats
path: root/ui-console/src/main/java/market/guess/ui/console/command/SettleEventMenuCommand.java
blob: bec25a7fe4e5c916f987acf6a79df296d2dfd372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package market.guess.ui.console.command;

import market.guess.api.CatalogContext;
import market.guess.api.GuessMarketContext;
import market.guess.model.event.EventStatus;
import market.guess.ui.console.io.InputProcessor;

public final class SettleEventMenuCommand implements MenuCommand {
  private final GuessMarketContext context;
  private final CatalogContext catalog;
  private final InputProcessor io;

  public SettleEventMenuCommand(
      GuessMarketContext context, CatalogContext catalog, InputProcessor io) {
    this.context = context;
    this.catalog = catalog;
    this.io = io;
  }

  @Override
  public int getIndex() {
    return 5;
  }

  @Override
  public String getName() {
    return "Settle an event";
  }

  @Override
  public void execute() {
    var result = catalog.getAllEvents();

    if (!result.isSuccess()) {
      io.println("Failed to list events: %s.".formatted(result.getMessage()));
      return;
    }

    var events = result.getData();
    if (events.isEmpty()) {
      io.println("No events are currently loaded. Please load a valid XML file first.");
      return;
    }

    var active = events.stream().filter(event -> event.status() == EventStatus.ACTIVE).toList();

    if (active.isEmpty()) {
      io.println("There are currently no active events to settle.");
      return;
    }

    io.newLine();
    io.println("Active events to settle:");
    var pick = io.readSelect("Pick an event to settle: ", active, event -> "[ID: %d] %s".formatted(event.displayId(), event.name()));

    var result2 = catalog.getEvent(pick.key());
    var event = result2.getData();
    if (event == null) {
      io.println("That event is no longer available.");
      return;
    }

    io.printState(event.state());

    var winner =
        io.readSelect("Which option won? ", event.state().markets(), o -> "%s".formatted(o.name()));

    var settleResult = context.settleEvent("Tester", event.summary().key(), winner.key());
    if (settleResult.isSuccess()) {
      var settledEvent = settleResult.getData();
      io.newLine();
      io.println("Event closed successfully! Winning option: %s".formatted(settledEvent.settledMarket()));
      io.printState(settledEvent.state());
      io.printHistory(settledEvent.history());
      io.newLine();
      io.println("Winning option: %s".formatted(settledEvent.settledMarket()));
      return;
    }
    io.newLine();
    io.println("Settlement failed: %s".formatted(settleResult.getMessage()));
  }
}