summaryrefslogtreecommitdiff
path: root/index.html
blob: f12c5856f847774c43bfa655e623074de68ebf1c (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
<!doctype html>
<html>
    <head>
        <title>Vainsocial backend status</title>
    </head>
    <body>
        <p>
            <form id="update-form">
                <input type="text" id="update-name">
                <input type="checkbox" id="update-force">
                <label for="update-force">Force refresh</label>
                <input type="submit" value="Update">
            </form>
        </p>
        <p>
            <form id="update-random-form">
                <input type="submit" value="Update random player">
            </form>
        </p>
        <p>
            <form id="crunch-form">
                <div>
                    <input type="text" id="crunch-on">
                    <label for="crunch-on">Table</label>
                </div>
                <div>
                    <input type="text" id="crunch-filter">
                    <label for="crunch-filter">Filter</label>
                </div>
                <div>
                    <input type="text" id="crunch-value">
                    <label for="crunch-value">Filter value</label>
                </div>
                <input type="submit" value="Crunch stats">
            </form>
        </p>

        <ul id="updates"></ul>

        <script src="https://cdn.jsdelivr.net/sockjs/1/sockjs.min.js"></script>
        <script type="text/javascript" src="/js/webstomp.min.js"></script>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script>
            let client;
            // set up the client
            if ('WebSocket' in window && (typeof WebSocket === 'function' || typeof WebSocket === 'object')) {
                client = webstomp.client("ws://localhost:15674/ws");
            } else {
                // http fallback
                client = webstomp.over(SockJS("http://localhost:15674/stomp"));
            }

            let notif = (text, color) => {
                $("#updates").append(
                    $("<li>")
                    .text(text)
                    .css("color", color)
                );
            };

            // connect
            client.connect("web", "web", () => notif("connected to socket", "purple"),
            (err) => notif("error connecting to socket: " + JSON.stringify(err), "red"));

            let subscribe = (name) => {
                // subscribe
                client.subscribe("/topic/player." + name, (msg) => {
                    notif(msg.body, "grey");
                    switch (msg.body) {
                        case "search_fail":
                            notif(name + ": not found", "red"); break;
                        case "search_success":
                            notif(name + ": found", "green"); break;
                        case "matches_update":
                            notif(name + ": new match(es) available", "orange"); break;
                        case "stats_update":
                            notif(name + ": player profile stats updated", "orange"); break;
                    }
                    msg.ack();
                }, {"ack": "client"});
            }

            $("#update-form").submit((e) => {
                e.preventDefault();
                let ign = $("#update-name").val(),
                    force = $("#update-force").is(":checked");

                subscribe(ign);
                if (force) {
                    $.post("/api/player/" + ign + "/search");
                } else {
                    $.post("/api/player/" + ign + "/update");
                }
            });
            $("#update-random-form").submit((e) => {
                e.preventDefault();
                $.post("/api/player").done((resp) => {
                    notif(resp.name + ": random user", "green");
                    subscribe(resp.name);
                });
            });

            $("#crunch-form").submit((e) => {
                e.preventDefault();
                let dimension_on = $("#crunch-on").val(),
                    data = {};
                data[$("#crunch-filter").val()] = $("#crunch-value").val();
                $.ajax("/api/stats/" + dimension_on + "/update", {
                    data: JSON.stringify(data),
                    contentType: "application/json",
                    type: "POST"
                });
            });
        </script>
    </body>
</html>