-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·95 lines (83 loc) · 3.49 KB
/
index.html
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
<html>
<body>
<div>
<span id="elementA"></span> x <span id="elementB"></span>
</div>
<div>
<form id="attempt-form" action="#" method="POST">
<div class="form-group">
<label for="result-attempt">Result?</label>
<input type="text" name="result-attempt" id="result-attempt" class="form-control" autofocus>
</div>
<span class="input-group-append">
<button type="submit" class="btn btn-light" data-name="register" onclick="formSubmit(event)">Check!</button>
</span>
</form>
</div>
<div>
<h2> User Stats </h2>
<table id = "stats" style=width:100%">
<tr>
<th>Challenge</th>
<th>Attempt</th>
<th>Correct?</th>
</tr>
<tbody id="user-results"></tbody>
</table>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.2/jquery.min.js" integrity="sha512-tWHlutFnuG0C6nQRlpvrEhE4QpkG1nn2MOUMWmUeRePl4e3Aki0VB6W1v3oLjFtd0hVOtRQ9PHpSfN6u6/QXkQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
var name = "";
$("document").ready( function () {
name = prompt("Please, enter your name", "")
})
var SERVER_URL = "http://localhost:8080";
var elementA = 0;
var elementB = 0;
function getData() {
$.get(SERVER_URL + "/challenges/random", (data, status) => {
elementA = data['valueA'];
elementB = data['valueB'];
$("#elementA").text(elementA)
$("#elementB").text(elementB)
});
}
function formSubmit(e){
e.preventDefault();
var attempt = $('#result-attempt').val();
var data = { user: { alias: name}, challenge: {valueA: elementA, valueB: elementB}, resultAttempt: parseInt(attempt)};
$.ajax({
url: SERVER_URL + '/challenges/attempt',
type: 'POST',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(result){
if (result == true) {
}
}
});
document.getElementById("attempt-form").reset();
getData();
updateStats();
}
function updateStats() {
$.ajax({
url: SERVER_URL + '/challenges/users/'+name,
}).then(function(data) {
$('#user-results').empty();
data.forEach(function(row) {
$('#user-results').append(
'<tr>' +
'<td>' + row.challenge.valueA + ' X ' + row.challenge.valueB + '</td>' +
'<td>' + row.resultAttempt + '</td>' +
'<td>' + (row.check=== true ? 'Yes' : 'Nn') + '</td>' +
'</tr>')
});
});
}
getData();
</script>
</body>
</html>