diff options
Diffstat (limited to 'ui-client/src/main/java/market/guess')
6 files changed, 406 insertions, 0 deletions
diff --git a/ui-client/src/main/java/market/guess/ui/client/App.java b/ui-client/src/main/java/market/guess/ui/client/App.java new file mode 100644 index 0000000..d24eda1 --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/App.java | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import javafx.application.Application; | ||
| 4 | import javafx.application.Platform; | ||
| 5 | import javafx.stage.Stage; | ||
| 6 | import market.guess.ui.desktop.AppView; | ||
| 7 | |||
| 8 | public final class App extends Application { | ||
| 9 | private ServerClient client; | ||
| 10 | |||
| 11 | @Override | ||
| 12 | public void start(Stage stage) { | ||
| 13 | var args = getParameters().getNamed(); | ||
| 14 | client = | ||
| 15 | new ServerClient( | ||
| 16 | args.getOrDefault("server", "http://localhost:8080/guess-market") + "/api"); | ||
| 17 | var login = client.login(args.getOrDefault("user", System.getProperty("user.name"))); | ||
| 18 | if (!login.isSuccess()) { | ||
| 19 | System.err.println(login.getMessage()); | ||
| 20 | Platform.exit(); | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | client.pull(); | ||
| 24 | |||
| 25 | var view = | ||
| 26 | new AppView( | ||
| 27 | stage, | ||
| 28 | new RemoteCatalogContext(client), | ||
| 29 | new RemoteGuessMarketContext(client), | ||
| 30 | new RemoteAccountContext(client)); | ||
| 31 | // refreshData reselects the event and user by id, so a refresh keeps the selection | ||
| 32 | client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); | ||
| 33 | stage.show(); | ||
| 34 | } | ||
| 35 | |||
| 36 | @Override | ||
| 37 | public void stop() { | ||
| 38 | client.logout(); | ||
| 39 | client.close(); | ||
| 40 | } | ||
| 41 | } | ||
diff --git a/ui-client/src/main/java/market/guess/ui/client/Launcher.java b/ui-client/src/main/java/market/guess/ui/client/Launcher.java new file mode 100644 index 0000000..78709d0 --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/Launcher.java | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import javafx.application.Application; | ||
| 4 | |||
| 5 | public final class Launcher { | ||
| 6 | public static void main(String[] args) { | ||
| 7 | Application.launch(App.class, args); | ||
| 8 | } | ||
| 9 | } | ||
diff --git a/ui-client/src/main/java/market/guess/ui/client/RemoteAccountContext.java b/ui-client/src/main/java/market/guess/ui/client/RemoteAccountContext.java new file mode 100644 index 0000000..fc90bc0 --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/RemoteAccountContext.java | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import java.util.ArrayList; | ||
| 4 | import java.util.List; | ||
| 5 | import market.guess.api.AccountContext; | ||
| 6 | import market.guess.api.Result; | ||
| 7 | import market.guess.model.ledger.LedgerDTO; | ||
| 8 | import okhttp3.FormBody; | ||
| 9 | |||
| 10 | /** | ||
| 11 | * Accounts over HTTP. Only our own ledger is ours to read; the server shows other users as name | ||
| 12 | * and balance, so they appear here as accounts without entries, after our own. | ||
| 13 | */ | ||
| 14 | public final class RemoteAccountContext implements AccountContext { | ||
| 15 | private final ServerClient client; | ||
| 16 | |||
| 17 | public RemoteAccountContext(ServerClient client) { | ||
| 18 | this.client = client; | ||
| 19 | } | ||
| 20 | |||
| 21 | @Override | ||
| 22 | public Result<List<LedgerDTO>> getAllAccounts() { | ||
| 23 | var snapshot = client.snapshot(); | ||
| 24 | var own = snapshot.account(); | ||
| 25 | var all = new ArrayList<LedgerDTO>(); | ||
| 26 | if (own != null) { | ||
| 27 | all.add(own); | ||
| 28 | } | ||
| 29 | for (var u : snapshot.users()) { | ||
| 30 | if (own == null || !u.username().equalsIgnoreCase(own.owner())) { | ||
| 31 | all.add(new LedgerDTO(u.username(), u.balance(), false, List.of())); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | return Result.ok(all); | ||
| 35 | } | ||
| 36 | |||
| 37 | @Override | ||
| 38 | public Result<LedgerDTO> getAccount(String owner) { | ||
| 39 | return getAllAccounts().getData().stream() | ||
| 40 | .filter(a -> a.owner().equalsIgnoreCase(owner)) | ||
| 41 | .findFirst() | ||
| 42 | .map(Result::ok) | ||
| 43 | .orElseGet(() -> Result.error("Account not found")); | ||
| 44 | } | ||
| 45 | |||
| 46 | @Override | ||
| 47 | public Result<LedgerDTO> deposit(String owner, String amount) { | ||
| 48 | Result<LedgerDTO> res = | ||
| 49 | client.post( | ||
| 50 | LedgerDTO.class, new FormBody.Builder().add("amount", amount).build(), "user", "deposit"); | ||
| 51 | client.pull(); | ||
| 52 | return res; | ||
| 53 | } | ||
| 54 | } | ||
diff --git a/ui-client/src/main/java/market/guess/ui/client/RemoteCatalogContext.java b/ui-client/src/main/java/market/guess/ui/client/RemoteCatalogContext.java new file mode 100644 index 0000000..cc0b03e --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/RemoteCatalogContext.java | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import java.io.IOException; | ||
| 4 | import java.nio.file.Files; | ||
| 5 | import java.nio.file.Path; | ||
| 6 | import java.util.List; | ||
| 7 | import market.guess.api.CatalogContext; | ||
| 8 | import market.guess.api.LoadResult; | ||
| 9 | import market.guess.api.Result; | ||
| 10 | import market.guess.model.event.EventDetailDTO; | ||
| 11 | import market.guess.model.event.EventSummaryDTO; | ||
| 12 | import okhttp3.FormBody; | ||
| 13 | import okhttp3.MultipartBody; | ||
| 14 | import okhttp3.RequestBody; | ||
| 15 | |||
| 16 | public final class RemoteCatalogContext implements CatalogContext { | ||
| 17 | private final ServerClient client; | ||
| 18 | |||
| 19 | public RemoteCatalogContext(ServerClient client) { | ||
| 20 | this.client = client; | ||
| 21 | } | ||
| 22 | |||
| 23 | @Override | ||
| 24 | public Result<LoadResult> loadEvents(Path path) { | ||
| 25 | byte[] bytes; | ||
| 26 | try { | ||
| 27 | bytes = Files.readAllBytes(path); | ||
| 28 | } catch (IOException e) { | ||
| 29 | var message = "Cannot read " + path + "."; | ||
| 30 | return Result.error(message, message); | ||
| 31 | } | ||
| 32 | var body = | ||
| 33 | new MultipartBody.Builder() | ||
| 34 | .setType(MultipartBody.FORM) | ||
| 35 | .addFormDataPart("file", path.getFileName().toString(), RequestBody.create(bytes)) | ||
| 36 | .build(); | ||
| 37 | Result<LoadResult> res = client.post(LoadResult.class, body, "upload"); | ||
| 38 | client.pull(); | ||
| 39 | return res; | ||
| 40 | } | ||
| 41 | |||
| 42 | @Override | ||
| 43 | public Result<List<EventSummaryDTO>> getAllEvents() { | ||
| 44 | return Result.ok(client.snapshot().events()); | ||
| 45 | } | ||
| 46 | |||
| 47 | @Override | ||
| 48 | public Result<EventDetailDTO> getEvent(String eventKey) { | ||
| 49 | var detail = client.snapshot().details().get(eventKey); | ||
| 50 | return detail != null ? Result.ok(detail) : Result.error("Event not found"); | ||
| 51 | } | ||
| 52 | |||
| 53 | @Override | ||
| 54 | public Result<Void> saveState(Path path) { | ||
| 55 | return Result.error("The server keeps the market; there is nothing to save here."); | ||
| 56 | } | ||
| 57 | |||
| 58 | @Override | ||
| 59 | public Result<LoadResult> restoreState(Path path) { | ||
| 60 | return Result.error("The server keeps the market; there is nothing to restore here."); | ||
| 61 | } | ||
| 62 | |||
| 63 | @Override | ||
| 64 | public Result<EventDetailDTO> openEvent(String eventKey) { | ||
| 65 | var name = client.eventName(eventKey); | ||
| 66 | if (name == null) { | ||
| 67 | return Result.error("Event not found"); | ||
| 68 | } | ||
| 69 | Result<EventDetailDTO> res = | ||
| 70 | client.post(EventDetailDTO.class, new FormBody.Builder().build(), "events", name, "open"); | ||
| 71 | client.pull(); | ||
| 72 | return res; | ||
| 73 | } | ||
| 74 | } | ||
diff --git a/ui-client/src/main/java/market/guess/ui/client/RemoteGuessMarketContext.java b/ui-client/src/main/java/market/guess/ui/client/RemoteGuessMarketContext.java new file mode 100644 index 0000000..6bc704b --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/RemoteGuessMarketContext.java | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import java.math.BigDecimal; | ||
| 4 | import market.guess.api.GuessMarketContext; | ||
| 5 | import market.guess.api.Result; | ||
| 6 | import market.guess.model.event.EventDetailDTO; | ||
| 7 | import market.guess.model.market.PurchaseReceiptDTO; | ||
| 8 | import okhttp3.FormBody; | ||
| 9 | |||
| 10 | public final class RemoteGuessMarketContext implements GuessMarketContext { | ||
| 11 | private final ServerClient client; | ||
| 12 | |||
| 13 | public RemoteGuessMarketContext(ServerClient client) { | ||
| 14 | this.client = client; | ||
| 15 | } | ||
| 16 | |||
| 17 | @Override | ||
| 18 | public Result<PurchaseReceiptDTO> placeOrder( | ||
| 19 | String userName, | ||
| 20 | String eventKey, | ||
| 21 | String optionKey, | ||
| 22 | String side, | ||
| 23 | BigDecimal price, | ||
| 24 | long quantity) { | ||
| 25 | var name = client.eventName(eventKey); | ||
| 26 | if (name == null) { | ||
| 27 | return Result.error("Event not found"); | ||
| 28 | } | ||
| 29 | var form = | ||
| 30 | new FormBody.Builder() | ||
| 31 | .add("event", name) | ||
| 32 | .add("option", optionKey) | ||
| 33 | .add("side", side) | ||
| 34 | .add("price", price.toPlainString()) | ||
| 35 | .add("quantity", Long.toString(quantity)) | ||
| 36 | .build(); | ||
| 37 | Result<PurchaseReceiptDTO> res = client.post(PurchaseReceiptDTO.class, form, "orders"); | ||
| 38 | client.pull(); | ||
| 39 | return res; | ||
| 40 | } | ||
| 41 | |||
| 42 | @Override | ||
| 43 | public Result<EventDetailDTO> settleEvent( | ||
| 44 | String userName, String eventKey, String winningOptionKey) { | ||
| 45 | var name = client.eventName(eventKey); | ||
| 46 | if (name == null) { | ||
| 47 | return Result.error("Event not found"); | ||
| 48 | } | ||
| 49 | var form = new FormBody.Builder().add("option", winningOptionKey).build(); | ||
| 50 | Result<EventDetailDTO> res = | ||
| 51 | client.post(EventDetailDTO.class, form, "events", name, "resolve"); | ||
| 52 | client.pull(); | ||
| 53 | return res; | ||
| 54 | } | ||
| 55 | } | ||
diff --git a/ui-client/src/main/java/market/guess/ui/client/ServerClient.java b/ui-client/src/main/java/market/guess/ui/client/ServerClient.java new file mode 100644 index 0000000..01962a2 --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/ServerClient.java | |||
| @@ -0,0 +1,173 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import com.google.gson.Gson; | ||
| 4 | import com.google.gson.JsonObject; | ||
| 5 | import java.io.IOException; | ||
| 6 | import java.lang.reflect.Type; | ||
| 7 | import java.util.HashMap; | ||
| 8 | import java.util.List; | ||
| 9 | import java.util.Map; | ||
| 10 | import java.util.concurrent.ConcurrentHashMap; | ||
| 11 | import java.util.concurrent.Executors; | ||
| 12 | import java.util.concurrent.ScheduledExecutorService; | ||
| 13 | import java.util.concurrent.TimeUnit; | ||
| 14 | import market.guess.api.Result; | ||
| 15 | import market.guess.model.event.EventDetailDTO; | ||
| 16 | import market.guess.model.event.EventSummaryDTO; | ||
| 17 | import market.guess.model.ledger.LedgerDTO; | ||
| 18 | import market.guess.model.ledger.UserSummaryDTO; | ||
| 19 | import okhttp3.Cookie; | ||
| 20 | import okhttp3.CookieJar; | ||
| 21 | import okhttp3.FormBody; | ||
| 22 | import okhttp3.HttpUrl; | ||
| 23 | import okhttp3.OkHttpClient; | ||
| 24 | import okhttp3.Request; | ||
| 25 | import okhttp3.RequestBody; | ||
| 26 | |||
| 27 | public final class ServerClient implements AutoCloseable { | ||
| 28 | private static final Gson GSON = new Gson(); | ||
| 29 | |||
| 30 | record Snapshot( | ||
| 31 | long version, | ||
| 32 | List<EventSummaryDTO> events, | ||
| 33 | Map<String, EventDetailDTO> details, | ||
| 34 | List<UserSummaryDTO> users, | ||
| 35 | LedgerDTO account) {} | ||
| 36 | |||
| 37 | private record SyncReply( | ||
| 38 | boolean changed, | ||
| 39 | long version, | ||
| 40 | List<EventSummaryDTO> events, | ||
| 41 | List<UserSummaryDTO> users, | ||
| 42 | LedgerDTO account) {} | ||
| 43 | |||
| 44 | private final HttpUrl api; | ||
| 45 | private final OkHttpClient http; | ||
| 46 | private final ScheduledExecutorService poller = | ||
| 47 | Executors.newSingleThreadScheduledExecutor( | ||
| 48 | r -> { | ||
| 49 | var t = new Thread(r, "gm-poller"); | ||
| 50 | t.setDaemon(true); | ||
| 51 | return t; | ||
| 52 | }); | ||
| 53 | private volatile Snapshot snapshot = new Snapshot(-1, List.of(), Map.of(), List.of(), null); | ||
| 54 | |||
| 55 | public ServerClient(String apiUrl) { | ||
| 56 | this.api = HttpUrl.get(apiUrl); | ||
| 57 | var cookies = new ConcurrentHashMap<String, List<Cookie>>(); | ||
| 58 | this.http = | ||
| 59 | new OkHttpClient.Builder() | ||
| 60 | .cookieJar( | ||
| 61 | new CookieJar() { | ||
| 62 | @Override | ||
| 63 | public void saveFromResponse(HttpUrl url, List<Cookie> list) { | ||
| 64 | cookies.put(url.host(), list); | ||
| 65 | } | ||
| 66 | |||
| 67 | @Override | ||
| 68 | public List<Cookie> loadForRequest(HttpUrl url) { | ||
| 69 | return cookies.getOrDefault(url.host(), List.of()); | ||
| 70 | } | ||
| 71 | }) | ||
| 72 | .build(); | ||
| 73 | } | ||
| 74 | |||
| 75 | public Result<String> login(String username) { | ||
| 76 | Result<JsonObject> res = | ||
| 77 | post(JsonObject.class, new FormBody.Builder().add("username", username).build(), "login"); | ||
| 78 | return res.isSuccess() | ||
| 79 | ? Result.ok(res.getData().get("username").getAsString()) | ||
| 80 | : Result.error(res.getMessage(), res.getDetails()); | ||
| 81 | } | ||
| 82 | |||
| 83 | public void logout() { | ||
| 84 | post(JsonObject.class, new FormBody.Builder().build(), "logout"); | ||
| 85 | } | ||
| 86 | |||
| 87 | Snapshot snapshot() { | ||
| 88 | return snapshot; | ||
| 89 | } | ||
| 90 | |||
| 91 | String eventName(String eventKey) { | ||
| 92 | return snapshot.events().stream() | ||
| 93 | .filter(e -> e.key().equals(eventKey)) | ||
| 94 | .map(EventSummaryDTO::name) | ||
| 95 | .findFirst() | ||
| 96 | .orElse(null); | ||
| 97 | } | ||
| 98 | |||
| 99 | public synchronized boolean pull() { | ||
| 100 | var url = url("sync").newBuilder().addQueryParameter("since", "" + snapshot.version()).build(); | ||
| 101 | Result<SyncReply> res = call(new Request.Builder().url(url).build(), SyncReply.class); | ||
| 102 | if (!res.isSuccess() || !res.getData().changed()) { | ||
| 103 | return false; | ||
| 104 | } | ||
| 105 | var reply = res.getData(); | ||
| 106 | var details = new HashMap<String, EventDetailDTO>(); | ||
| 107 | for (var e : reply.events()) { | ||
| 108 | Result<EventDetailDTO> detail = get(EventDetailDTO.class, "events", e.name()); | ||
| 109 | if (detail.isSuccess()) { | ||
| 110 | details.put(e.key(), detail.getData()); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | snapshot = | ||
| 114 | new Snapshot(reply.version(), reply.events(), details, reply.users(), reply.account()); | ||
| 115 | return true; | ||
| 116 | } | ||
| 117 | |||
| 118 | public void startPolling(Runnable onChange) { | ||
| 119 | poller.scheduleWithFixedDelay( | ||
| 120 | () -> { | ||
| 121 | try { | ||
| 122 | if (pull()) { | ||
| 123 | onChange.run(); | ||
| 124 | } | ||
| 125 | } catch (RuntimeException e) { | ||
| 126 | } | ||
| 127 | }, | ||
| 128 | 1, | ||
| 129 | 1, | ||
| 130 | TimeUnit.SECONDS); | ||
| 131 | } | ||
| 132 | |||
| 133 | <T> Result<T> get(Type type, String... path) { | ||
| 134 | return call(new Request.Builder().url(url(path)).build(), type); | ||
| 135 | } | ||
| 136 | |||
| 137 | <T> Result<T> post(Type type, RequestBody body, String... path) { | ||
| 138 | return call(new Request.Builder().url(url(path)).post(body).build(), type); | ||
| 139 | } | ||
| 140 | |||
| 141 | private HttpUrl url(String... path) { | ||
| 142 | var b = api.newBuilder(); | ||
| 143 | for (var segment : path) { | ||
| 144 | b.addPathSegment(segment); // percent-encodes, so event names may hold spaces and '?' | ||
| 145 | } | ||
| 146 | return b.build(); | ||
| 147 | } | ||
| 148 | |||
| 149 | private <T> Result<T> call(Request request, Type type) { | ||
| 150 | try (var res = http.newCall(request).execute()) { | ||
| 151 | var body = res.body().string(); | ||
| 152 | if (res.isSuccessful()) { | ||
| 153 | return Result.ok(GSON.fromJson(body, type)); | ||
| 154 | } | ||
| 155 | var message = "The server answered " + res.code() + "."; | ||
| 156 | try { | ||
| 157 | message = GSON.fromJson(body, JsonObject.class).get("message").getAsString(); | ||
| 158 | } catch (RuntimeException notOurJson) { | ||
| 159 | } | ||
| 160 | return Result.error(message, message); | ||
| 161 | } catch (IOException | RuntimeException e) { | ||
| 162 | var message = "Cannot reach the server: " + e.getMessage(); | ||
| 163 | return Result.error(message, message); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | @Override | ||
| 168 | public void close() { | ||
| 169 | poller.shutdownNow(); | ||
| 170 | http.dispatcher().executorService().shutdown(); | ||
| 171 | http.connectionPool().evictAll(); | ||
| 172 | } | ||
| 173 | } | ||