aboutsummaryrefslogtreecommitdiffstats
path: root/ui-desktop/src/main/java/market/guess/ui/desktop/controllers/UsersTabController.java
blob: 62113373c38698ed59c3a5887c2cfc15c19719a6 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package market.guess.ui.desktop.controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.function.Consumer;
import javafx.collections.ListChangeListener;
import javafx.css.PseudoClass;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import market.guess.model.event.EventStatus;
import market.guess.model.event.MechanismType;
import market.guess.ui.desktop.AppState;
import market.guess.ui.desktop.components.TradeSide;
import market.guess.ui.desktop.components.UserEventItemView;
import market.guess.ui.desktop.components.UserListCell;
import market.guess.ui.desktop.components.graphic.Graphic;
import market.guess.ui.desktop.components.graphic.SparklineGraphic;
import market.guess.ui.desktop.model.UserData;
import market.guess.ui.desktop.model.UserEventRow;
import market.guess.ui.desktop.util.Charts;
import market.guess.ui.desktop.util.Format;
import market.guess.ui.desktop.util.Views;

public class UsersTabController {
  private static final PseudoClass BLOCKED = PseudoClass.getPseudoClass("blocked");
  private static final PseudoClass MM = PseudoClass.getPseudoClass("mm");
  private static final PseudoClass TRADER = PseudoClass.getPseudoClass("trader");
  private static final PseudoClass NEGATIVE = PseudoClass.getPseudoClass("negative");
  private static final PseudoClass ERROR = PseudoClass.getPseudoClass("error");

  @FunctionalInterface
  public interface OrderPlacer {
    void placeOrder(
        String eventKey, String optionKey, String side, BigDecimal price, long quantity);
  }

  private AppState state;
  private Runnable onCreateNewEvent;
  private Consumer<String> onToast;
  private OrderPlacer onPlaceOrder;
  private Graphic sparkline;
  private boolean updatingSelection = false;

  @FXML private StackPane sparklineBox;
  @FXML private ListView<UserData> userListContainer;
  @FXML private VBox userDetailCard;
  @FXML private Label userNameLabel;
  @FXML private Label userRolePill;
  @FXML private Label userSubLabel;
  @FXML private Label userBalanceLabel;

  @FXML private HBox blockedBanner;
  @FXML private LineChart<Number, Number> balanceChart;
  @FXML private VBox participationRowsContainer;

  @FXML private VBox tradePanel;
  @FXML private Label noTradeEventLabel;
  @FXML private VBox tradeContentBox;
  @FXML private Label tradeEventTitle;
  @FXML private Label tradeEventSub;
  @FXML private VBox tradeYesOptCard;
  @FXML private Label tradeYesPriceLabel;
  @FXML private Label tradeYesHeldLabel;
  @FXML private VBox tradeNoOptCard;
  @FXML private Label tradeNoPriceLabel;
  @FXML private Label tradeNoHeldLabel;
  @FXML private Button tradeBuyBtn;
  @FXML private Button tradeSellBtn;
  @FXML private TextField tradeQtyField;
  @FXML private TextField tradePriceField;
  @FXML private Label tradeCostLabel;
  @FXML private Button tradeSubmitBtn;
  @FXML private Label tradeHintLabel;

  public void init(AppState state) {
    this.state = state;
    sparkline = SparklineGraphic.create();
    sparklineBox.getChildren().setAll(sparkline.node);

    userListContainer.setCellFactory(lv -> new UserListCell());
    userListContainer
        .getSelectionModel()
        .selectedItemProperty()
        .addListener(
            (obs, oldVal, newVal) -> {
              if (updatingSelection) return;
              if (newVal != null && state != null) {
                state.setActingUser(newVal);
              }
            });

    state
        .actingUserProperty()
        .addListener(
            (obs, oldVal, newVal) -> {
              if (newVal != null) {
                if (!newVal.equals(userListContainer.getSelectionModel().getSelectedItem())) {
                  updatingSelection = true;
                  try {
                    userListContainer.getSelectionModel().select(newVal);
                  } finally {
                    updatingSelection = false;
                  }
                }
              }
              refreshUserDetail(newVal);
            });

    state
        .selectedEventProperty()
        .addListener(
            (obs, oldVal, newVal) -> {
              refreshTradePanel(state.actingUser());
            });

    state
        .tradeOptionYesProperty()
        .addListener((obs, oldVal, newVal) -> refreshTradePanel(state.actingUser()));

    state
        .tradeSideProperty()
        .addListener((obs, oldVal, newVal) -> refreshTradePanel(state.actingUser()));

    state
        .getUsers()
        .addListener(
            (ListChangeListener<UserData>)
                c -> {
                  refreshUserList();
                  refreshUserDetail(state.actingUser());
                });
    state.animationsOnProperty().addListener((obs, oldVal, newVal) -> sparkline.setPlaying(newVal));

    tradeQtyField
        .textProperty()
        .addListener(
            (obs, o, n) -> {
              if (state != null) state.setTradeQty(n);
              validateTradeInputs();
            });

    tradePriceField
        .textProperty()
        .addListener(
            (obs, o, n) -> {
              if (state != null) state.setTradePrice(n);
              validateTradeInputs();
            });
  }

  public void setOnCreateNewEvent(Runnable onCreateNewEvent) {
    this.onCreateNewEvent = onCreateNewEvent;
  }

  public void setOnToast(Consumer<String> onToast) {
    this.onToast = onToast;
  }

  public void setOnPlaceOrder(OrderPlacer onPlaceOrder) {
    this.onPlaceOrder = onPlaceOrder;
  }

  public void refresh() {
    if (state == null) return;
    refreshUserList();
    refreshUserDetail(state.actingUser());
  }

  private void refreshUserList() {
    if (state == null) return;
    updatingSelection = true;
    try {
      userListContainer.getItems().setAll(state.getUsers());
      var actor = state.actingUser();
      UserData match = null;
      if (actor != null) {
        for (var u : state.getUsers()) {
          if (u.name.equals(actor.name)) {
            match = u;
            break;
          }
        }
      }
      if (match != null) {
        userListContainer.getSelectionModel().select(match);
      } else if (!state.getUsers().isEmpty()) {
        userListContainer.getSelectionModel().select(0);
      }
    } finally {
      updatingSelection = false;
    }
  }

  private void refreshUserDetail(UserData actor) {
    if (actor == null) {
      userDetailCard.setVisible(false);
      return;
    }
    userDetailCard.setVisible(true);

    userNameLabel.setText(actor.name);
    userRolePill.setText(actor.blocked ? "BLOCKED" : actor.isMm ? "MARKET MAKER" : "TRADER");
    userRolePill.pseudoClassStateChanged(BLOCKED, actor.blocked);
    userRolePill.pseudoClassStateChanged(MM, !actor.blocked && actor.isMm);
    userRolePill.pseudoClassStateChanged(TRADER, !actor.blocked && !actor.isMm);

    userSubLabel.setText("Active in " + actor.events.size() + " events");
    userBalanceLabel.setText(Format.money(actor.balance));
    userBalanceLabel.pseudoClassStateChanged(NEGATIVE, actor.balance.signum() < 0);
    sparkline.node.pseudoClassStateChanged(NEGATIVE, actor.balance.signum() < 0);
    sparkline.setPlaying(state.isAnimationsOn());

    // Blocked Banner
    blockedBanner.setVisible(actor.blocked);
    blockedBanner.setManaged(actor.blocked);

    // Balance Chart
    Charts.setSingleSeries(balanceChart, "balance", actor.balanceHistory);
    if (balanceChart.getYAxis() instanceof NumberAxis yAxis) {
      Charts.scaleYAxis(yAxis, actor.balanceHistory, actor.balance.doubleValue());
    }

    // Participation Table
    var evRows = new ArrayList<Node>(actor.events.size());
    for (var ev : actor.events) {
      evRows.add(buildParticipationRow(ev));
    }
    participationRowsContainer.getChildren().setAll(evRows);

    // Trade Panel
    refreshTradePanel(actor);
  }

  private void refreshTradePanel(UserData actor) {
    if (state == null) return;
    var e = state.selectedEvent();

    if (e == null) {
      noTradeEventLabel.setVisible(true);
      noTradeEventLabel.setManaged(true);
      tradeContentBox.setVisible(false);
      tradeContentBox.setManaged(false);
      return;
    }

    noTradeEventLabel.setVisible(false);
    noTradeEventLabel.setManaged(false);
    tradeContentBox.setVisible(true);
    tradeContentBox.setManaged(true);

    tradeEventTitle.setText(e.num + ". " + e.name);
    tradeEventSub.setText(
        (e.type == MechanismType.LMSR ? "LMSR" : "Order Book")
            + "  Status: "
            + e.status
            + "  Fee: "
            + e.feeText());

    int yesHeld = 0, noHeld = 0;
    if (actor != null && actor.events != null) {
      for (var ev : actor.events) {
        if (ev.eventId().equals(e.id)) {
          yesHeld = ev.yes();
          noHeld = ev.no();
          break;
        }
      }
    }

    tradeYesPriceLabel.setText(Format.money(e.yesPrice));
    tradeYesHeldLabel.setText("You hold " + yesHeld + " shares");

    tradeNoPriceLabel.setText(Format.money(e.noPrice));
    tradeNoHeldLabel.setText("You hold " + noHeld + " shares");

    Views.setSelected(tradeYesOptCard, state.isTradeOptionYes());
    Views.setSelected(tradeNoOptCard, !state.isTradeOptionYes());

    Views.setSelected(tradeBuyBtn, state.getTradeSide() == TradeSide.BUY);
    Views.setSelected(tradeSellBtn, state.getTradeSide() == TradeSide.SELL);

    if (tradeQtyField.getText().isEmpty()) tradeQtyField.setText(state.getTradeQty());
    if (tradePriceField.getText().isEmpty()) tradePriceField.setText(state.getTradePrice());

    tradePriceField.setVisible(e.type == MechanismType.ORDER_BOOK);
    tradePriceField.setManaged(e.type == MechanismType.ORDER_BOOK);

    tradeSubmitBtn.setText(
        state.getTradeSide() == TradeSide.BUY ? "Place Buy Order" : "Place Sell Order");

    validateTradeInputs();
  }

  private boolean validateTradeInputs() {
    if (state == null) return false;
    var e = state.selectedEvent();
    var actor = state.actingUser();
    if (e == null || actor == null) return false;

    boolean valid = true;
    String qtyText = tradeQtyField.getText() != null ? tradeQtyField.getText().trim() : "";
    long qty = 0;
    if (qtyText.isEmpty()) {
      valid = false;
      tradeQtyField.pseudoClassStateChanged(ERROR, true);
      tradeHintLabel.setText("Please enter the number of shares.");
    } else {
      try {
        qty = Long.parseLong(qtyText);
        if (qty <= 0) {
          valid = false;
          tradeQtyField.pseudoClassStateChanged(ERROR, true);
          tradeHintLabel.setText("Shares must be a positive number.");
        } else {
          tradeQtyField.pseudoClassStateChanged(ERROR, false);
        }
      } catch (NumberFormatException nfe) {
        valid = false;
        tradeQtyField.pseudoClassStateChanged(ERROR, true);
        tradeHintLabel.setText("Shares must be a valid integer.");
      }
    }

    BigDecimal price = null;
    if (e.type == MechanismType.ORDER_BOOK) {
      String priceText = tradePriceField.getText() != null ? tradePriceField.getText().trim() : "";
      if (priceText.isEmpty()) {
        valid = false;
        tradePriceField.pseudoClassStateChanged(ERROR, true);
        if (valid) {
          tradeHintLabel.setText("Please enter a price per share.");
        }
      } else {
        try {
          price = new BigDecimal(priceText);
          if (price.compareTo(BigDecimal.ZERO) <= 0) {
            valid = false;
            tradePriceField.pseudoClassStateChanged(ERROR, true);
            tradeHintLabel.setText("Price per share must be positive.");
          } else {
            tradePriceField.pseudoClassStateChanged(ERROR, false);
          }
        } catch (Exception nfe) {
          valid = false;
          tradePriceField.pseudoClassStateChanged(ERROR, true);
          tradeHintLabel.setText("Price must be a valid decimal number.");
        }
      }
    } else {
      tradePriceField.pseudoClassStateChanged(ERROR, false);
      price = state.isTradeOptionYes() ? e.yesPrice : e.noPrice;
    }

    if (valid && qty > 0 && price != null) {
      BigDecimal cost = price.multiply(BigDecimal.valueOf(qty));
      tradeCostLabel.setText(Format.money(cost));
      if (actor.blocked) {
        tradeHintLabel.setText("Trading is disabled because this account is blocked.");
        tradeSubmitBtn.setDisable(true);
      } else if (e.status != EventStatus.ACTIVE) {
        tradeHintLabel.setText("Trading is only available for ACTIVE events.");
        tradeSubmitBtn.setDisable(true);
      } else if (state.getTradeSide() == TradeSide.SELL) {
        int held = 0;
        if (actor.events != null) {
          for (var evRow : actor.events) {
            if (evRow.eventId().equals(e.id)) {
              held = state.isTradeOptionYes() ? evRow.yes() : evRow.no();
              break;
            }
          }
        }
        if (!actor.isMm && qty > held) {
          tradeHintLabel.setText("Cannot sell " + qty + " shares; you only hold " + held + ".");
          tradeSubmitBtn.setDisable(true);
        } else {
          tradeHintLabel.setText("Sell order will match existing bids or rest as an ask.");
          tradeSubmitBtn.setDisable(false);
        }
      } else {
        if (actor.balance.compareTo(cost) < 0) {
          tradeHintLabel.setText(
              "Insufficient balance. Required: "
                  + Format.money(cost)
                  + ", available: "
                  + Format.money(actor.balance));
          tradeSubmitBtn.setDisable(true);
        } else {
          tradeHintLabel.setText(
              e.type == MechanismType.ORDER_BOOK
                  ? "Buy order will match existing asks or rest as a bid."
                  : "Orders execute immediately against the market maker.");
          tradeSubmitBtn.setDisable(false);
        }
      }
    } else {
      tradeCostLabel.setText("—");
      tradeSubmitBtn.setDisable(true);
    }

    return valid;
  }

  private Node buildParticipationRow(UserEventRow ev) {
    var row = new UserEventItemView(ev);
    row.setOnMouseClicked(
        e -> {
          if (state != null) {
            state.setSelectedEventId(ev.eventId());
          }
        });
    return row;
  }

  // ---- FXML Handlers ----------------------------------------------------

  @FXML
  private void handleCreateNewEvent() {
    if (onCreateNewEvent != null) {
      onCreateNewEvent.run();
    }
  }

  @FXML
  private void handleSelectTradeYes() {
    if (state != null) state.setTradeOptionYes(true);
  }

  @FXML
  private void handleSelectTradeNo() {
    if (state != null) state.setTradeOptionYes(false);
  }

  @FXML
  private void handleTradeBuy() {
    if (state != null) state.setTradeSide(TradeSide.BUY);
  }

  @FXML
  private void handleTradeSell() {
    if (state != null) state.setTradeSide(TradeSide.SELL);
  }

  @FXML
  private void handleTradeSubmit() {
    if (state == null) return;
    var e = state.selectedEvent();
    var u = state.actingUser();
    if (e == null || u == null) return;
    if (!validateTradeInputs()) return;

    long qty = Long.parseLong(tradeQtyField.getText().trim());
    BigDecimal price =
        (e.type == MechanismType.ORDER_BOOK)
            ? new BigDecimal(tradePriceField.getText().trim())
            : (state.isTradeOptionYes() ? e.yesPrice : e.noPrice);

    String optionKey = state.isTradeOptionYes() ? "YES" : "NO";
    String side = state.getTradeSide() == TradeSide.SELL ? "SELL" : "BUY";

    if (onPlaceOrder != null) {
      onPlaceOrder.placeOrder(e.id, optionKey, side, price, qty);
    } else if (onToast != null) {
      onToast.accept("Order placed: " + side + " " + qty + " " + optionKey + " on #" + e.num);
    }
  }
}