forked from bcgame-project/verify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower.html
228 lines (222 loc) · 7.45 KB
/
tower.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tower Verify</title>
<link rel="stylesheet" href="./lib/main.css">
<link rel="stylesheet" href="./lib/bootstrap/css/bootstrap.min.css">
<style>
.mode-select-input {
margin-bottom: 1rem;
display: block;
width: 100%;
height: calc(1.5em + 0.75rem + 2px);
padding: 0.375rem 0.75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
transition: border-color .15s
}
.result-table {
display: flex;
flex-direction: column-reverse;
width: 40%;
height: 100%;
}
.round-item {
display: flex;
flex: 1;
margin-bottom: 4px;
display: flex;
align-items: center;
justify-content: space-between;
}
.round-item:first-child {
margin-bottom: 0;
}
.field-item {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
height: 100%;
margin-right: 4px;
background: #f5f6f7;
}
.result-wrap {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
height: 300px;
margin-bottom: 48px;
}
.result-list {
display: flex;
flex-direction: column-reverse;
width: 50%;
height: 100%;
}
.field-item:last-child {
margin-right: 0;
}
</style>
<script src="./lib/react.production.min.js"></script>
<script src="./lib/react-dom.production.min.js"></script>
<script src="./lib/crypto-js.js"></script>
<script src="./lib/tools.js"></script>
<script src="./lib/hooks.js"></script>
<script src="./lib/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
let qs = tools.queryString();
let { useEffect, useMemo } = React;
const { useSetState } = hooks;
function useInputControl (initState) {
const [state, setState] = useSetState(initState);
const bind = key => ({
value: state[key],
onChange: v => setState({[key]: v})
});
return [
state,
setState,
bind
];
}
function Input ({value, onChange, ...others}) {
return (
<div class="form-group">
<input value={value}
onChange={e => onChange(e.target.value)}
class="form-control"
{...others}
/>
</div>
);
}
const difficulityOptions = [
{label: "Easy", value: 0, totalChose: 4, errorChose: 1, level: 9},
{label: "Medium", value: 1, totalChose: 3, errorChose: 1, level: 9},
{label: "Hard", value: 2, totalChose: 2, errorChose: 1, level: 9},
{label: "Extreme", valuehmac_sha256: 3, totalChose: 3, errorChose: 2, level: 6},
{label: "Nightmare", value: 4, totalChose: 4, errorChose: 3, level: 6},
];
function getHashResultNumber (hashResult) {
const result = [];
for (let i = 0; i < hashResult.length; i += 2) {
let dext = hashResult[i] + hashResult[i + 1]
let hext = parseInt(dext, 16)
result.push(hext)
}
return result;
}
function createNums (hash, index, events) {
const bytes = hash.substring(index*8, (index+1)*8);
const resultList = getHashResultNumber(bytes);
const firstStep = resultList[0]/(Math.pow(256, 1));
const secondStep = resultList[1]/(Math.pow(256, 2));
const thirdStep = resultList[2]/(Math.pow(256, 3));
const fourthStep = resultList[3]/(Math.pow(256, 4));
const totalStepsNum = firstStep + secondStep + thirdStep + fourthStep;
return Math.floor(totalStepsNum * events);
}
function getResult (serverSeed, clientSeed, nonce, mode) {
const modeInfo = difficulityOptions[mode];
const result = [];
for(let i = 0; i < modeInfo.level; i++) {
const winList = [];
const resultArr = [clientSeed, nonce, i];
const hmacSha256Result = CryptoJS.HmacSHA256(resultArr.join(":"), serverSeed).toString();
const tower = [];
for(let t = 1; t <= modeInfo.totalChose; t++) tower.push(t);
for(let j = 0; j < (modeInfo.totalChose - modeInfo.errorChose); j++) {
const event = createNums(hmacSha256Result, j, tower.length);
winList.push(tower[event]);
tower.splice(event, 1);
}
result.push(winList);
}
return result;
}
function Tower () {
const [state, setState, bind] = useInputControl({
clientSeed: qs.c||'',
serverSeed: qs.s||'',
nonce: qs.n||'',
mode: qs.m||0
});
const resultList = getResult(state.serverSeed, state.clientSeed, state.nonce, state.mode);
const modeInfo = difficulityOptions[state.mode];
return (
<div className="main">
<h1 className="text-center pb-5">Tower verify</h1>
<hr />
<h2 class="text-center">Input</h2>
<form className="py-5">
<Input placeholder="Server Seed" {...bind('serverSeed')} />
<Input placeholder="Client Seed (Hashed)" {...bind('clientSeed')} />
<Input placeholder="Nonce" {...bind('nonce')} />
<select className="mode-select-input" placeholder="Mode" value={state.mode} onChange={e => {
setState({mode: e.target.value});
}}>
<option value={0}>Easy</option>
<option value={1}>Medium</option>
<option value={2}>Hard</option>
<option value={3}>Exterme</option>
<option value={4}>Nightmare</option>
</select>
</form>
<h2 class="text-center">Output</h2>
<form className="py-5">
<div className="result-wrap">
<div className="result-table">
{resultList.map((row, rowIndex) => {
return (
<div className="round-item" key={"row-" + rowIndex}>
{row.sort().map((item, index) => {
return (
<div className="field-item" key={"item-" + index}>
{item}
</div>
)
})}
</div>
)
})}
</div>
<div className="result-line">➡</div>
<div className="result-list">
{Array(modeInfo.level).fill(1).map((round, roundIndex) => {
return (
<div className="round-item" key={"round-" + roundIndex}>
{Array(modeInfo.totalChose).fill(1).map((field, fieldIndex) => {
const isRight = resultList[roundIndex].includes(fieldIndex + 1);
return (
<div className={"field-item " + (isRight ? "right" : "error")} key={"field-" + fieldIndex}>
{isRight ? "✅" : "❎"}
</div>
)
})}
</div>
)
})}
</div>
</div>
</form>
</div>
);
}
ReactDOM.render(<Tower />, document.getElementById("app"));
</script>
</body>
</html>