diff --git a/src/main/java/cz/kamma/darts/App.java b/src/main/java/cz/kamma/darts/App.java index c2cac8c..45d509a 100644 --- a/src/main/java/cz/kamma/darts/App.java +++ b/src/main/java/cz/kamma/darts/App.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; public class App { private static final Gson GSON = new Gson(); - private static final List GAMES_CACHE = Collections.synchronizedList(new ArrayList<>()); + private static final Map> GAMES_CACHE = Collections.synchronizedMap(new HashMap<>()); private static final long MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5MB public static void main(String[] args) throws IOException { @@ -104,21 +104,30 @@ public class App { List rawGames = GSON.fromJson(body, new TypeToken>(){}.getType()); List convertedGames = convertRawToGames(rawGames); - GAMES_CACHE.clear(); - GAMES_CACHE.addAll(convertedGames); + String code = generateCode(); + GAMES_CACHE.put(code, convertedGames); - String response = "Upload successful"; - byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8); - exchange.sendResponseHeaders(200, responseBytes.length); - try (OutputStream os = exchange.getResponseBody()) { - os.write(responseBytes); - } + exchange.getResponseHeaders().set("Location", "/?code=" + code); + exchange.sendResponseHeaders(303, -1); } catch (Exception e) { e.printStackTrace(); sendError(exchange, 400, "Error processing upload: " + e.getMessage()); } } + private String generateCode() { + String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random rnd = new Random(); + StringBuilder sb; + do { + sb = new StringBuilder(5); + for (int i = 0; i < 5; i++) { + sb.append(chars.charAt(rnd.nextInt(chars.length()))); + } + } while (GAMES_CACHE.containsKey(sb.toString())); + return sb.toString(); + } + private List convertRawToGames(List rawGames) { List result = new ArrayList<>(); for (RawGame rg : rawGames) { @@ -183,7 +192,15 @@ public class App { return; } - StatsResponse response = aggregateStats(new ArrayList<>(GAMES_CACHE)); + Map params = parseQuery(exchange.getRequestURI().getQuery()); + String code = params.get("code"); + + List games = (code != null) ? GAMES_CACHE.get(code.toUpperCase()) : null; + if (games == null) { + games = Collections.emptyList(); + } + + StatsResponse response = aggregateStats(new ArrayList<>(games)); String json = GSON.toJson(response); byte[] bytes = json.getBytes(StandardCharsets.UTF_8); @@ -195,6 +212,18 @@ public class App { os.close(); } + private Map parseQuery(String query) { + if (query == null) return Collections.emptyMap(); + Map result = new HashMap<>(); + for (String param : query.split("&")) { + String[] entry = param.split("="); + if (entry.length > 1) { + result.put(entry[0], entry[1]); + } + } + return result; + } + private StatsResponse aggregateStats(List games) { Map> byPlayer = games.stream() .collect(Collectors.groupingBy(DartsGame::getPlayer)); diff --git a/src/main/resources/web/index.html b/src/main/resources/web/index.html index f220926..239d973 100644 --- a/src/main/resources/web/index.html +++ b/src/main/resources/web/index.html @@ -58,6 +58,29 @@ transition: all 0.2s; } button:hover { background: var(--primary-hover); transform: translateY(-1px); } + + .icon-button:hover { + color: var(--primary) !important; + transform: scale(1.1); + } + + #notification { + position: fixed; + bottom: 20px; + right: 20px; + background: var(--primary); + color: #052e16; + padding: 12px 24px; + border-radius: 8px; + font-weight: 700; + box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3); + transform: translateY(100px); + transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); + z-index: 1000; + } + #notification.show { + transform: translateY(0); + } .stats-grid { display: grid; @@ -164,11 +187,21 @@

Upload Stats JSON

- - - +
+ + + +
+
+
+
Current Code
+
+
NONE
+ + 📋 + +
-
@@ -201,12 +234,24 @@ +
+