-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcypher.html
146 lines (121 loc) · 4.75 KB
/
cypher.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
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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/general.css">
</head>
<body>
<div id="Character">
<p id="rpg"></p>
<input type="submit" value="Rules" id="show_rules" class="click" onclick="showRules()">
<p id="rules" class="text"></p>
<p id="form" class="text">Difficulty and Effort (1 - 6): </p>
<input type="text" id="difficulty" value="5" class="num">
<input type="text" id="effort" value="1" class="num">
<input type="submit" value="Roll" id="update" onclick="drawData()" class="click">
</div>
<br>
<h3 id="error"></h3>
<div class="chart-container">
<canvas id="chart"></canvas>
</div>
<script>
drawData();
async function drawData()
{
if(window.chartObject!= undefined)
window.chartObject.destroy();
document.getElementById('error').innerHTML = '';
let req = await fetch('api.php?cypher',
{
method: "post",
headers: new Headers({
"Content-Type": "application/json"
}),
body: JSON.stringify({
difficulty: document.getElementById('difficulty').value,
effort: document.getElementById('effort').value
})
});
json_data = await req.json();
if(json_data.error)
{
document.getElementById('error').innerHTML = json_data.error;
}
else
{
document.getElementById('rpg').innerHTML = json_data.rpg;
document.getElementById('rules').innerHTML = json_data.rules;
document.getElementById('rules').style.display = 'none';
var result = [];
for(var i in json_data.roll)
result.push(json_data.roll[i]);
var ctx = document.getElementById('chart').getContext('2d');
window.chartObject = new Chart(ctx, {
type: 'pie',
data: {
labels: ['Success', 'Failure'],
datasets: [
{
borderColor: 'black',
data: [],
backgroundColor: ['rgb(0, 70, 0)', 'rgba(100, 0, 0, 0.9)']
}]
},
options: {
scales: {
yAxes: [{
ticks: {
suggestedMin: 0,
suggestedMax: json_data.yaxes,
},
gridLines: {
display: false
},
ticks: {
display: false
}
}],
xAxes: [{
gridLines: {
display: false
},
ticks: {
display: false
}
}]
}
}
});
let success = [];
let failure = [];
for(var i in json_data.roll)
{
if(json_data.roll[i] >= json_data.difficulty)
{
success.push(json_data.roll[i]);
}
else
{
failure.push(json_data.roll[i]);
}
}
window.chartObject.data.datasets[0].data.push(success.length, failure.length);
window.chartObject.update();
}
}
function showRules()
{
if(document.getElementById('rules').style.display === 'none')
{
document.getElementById('rules').style.display = 'block';
}
else
{
document.getElementById('rules').style.display = 'none';
}
}
</script>
</body>
</html>