-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathinstance.html
More file actions
415 lines (395 loc) · 14.1 KB
/
instance.html
File metadata and controls
415 lines (395 loc) · 14.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instance manager | rplace</title>
<link rel="stylesheet" href="/shared.css">
<link rel="stylesheet" href="/instance.css">
<link rel="apple-touch-icon" href="favicon.ico">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script>
let accountData = null;
const ws = new WebSocket("ws://localhost:1234")
ws.binaryType = "arraybuffer"
ws.onmessage = async function ({ data }) {
data = new DataView(data)
switch (data.getUint8(0)) {
//string Username, string Password, string Email, int AccountTier, List<int> Instances
case ServerPackets.AccountInfo: {
accountData = JSON.parse(decoder.decode(data.buffer.slice(1)))
accountName.textContent = accountData.Username
let censoredSection = accountData.Email.slice(4, accountData.Email.indexOf("@"))
accountEmail.textContent = accountData.Email.replace(censoredSection, "*".repeat(censoredSection.length))
accountInfo.style.display = 'block'
// Add to servers select
for (let instance of accountData.Instances) {
let el = document.createElement("option")
el.textContent = instance
viewServerOptions.appendChild(el)
}
// Create canvas if originated from builder
if (builderData) {
createCanvas(builderData)
}
break
}
case ServerPackets.Fail: {
console.log(decoder.decode(data.buffer.slice(1)))
break
}
case ServerPackets.WorkerLocations: {
workerLocations = JSON.parse(decoder.decode(data.buffer.slice(1)))
for (let i = 0; i < workerLocations.length; i++) {
workerLocations[i].workerSocket = new WebSocket(workerLocations[i].SocketAddress)
workerLocations[i].workerSocket.binaryType = "arraybuffer"
for (let instance in accountData.Instances) {
if (instance >= workerLocations[i].Range.Start && instance < workerLocations[i].Range.End) {
// Then this worker carries one of our instances, this is the first step to building up what we know about
// our "instances" before we can finally combine it all on the instances HTML table. Is like an odd form of bootstrapping.
if (!instances[instance]) instances[instance] = { }
instances[instance].workerSocket = workerLocations[i].workerSocket
instances[instance].workerSocket.onmessage = async function ({ data }) {
data = new DataView(data)
switch (data.getUint8(0)) {
case WorkerPackets.InstanceQuery: {
instances[instance].info = JSON.parse(data.buffer.slice(1))
break
}
case WorkerPackets.InstanceCreated: {
console.log("Callback - Created instance " + data.getInt32(5))
requestQueue[data.getInt32(1)]?.resolve(data.getInt32(5))
break
}
}
}
let buffer = new DataView(new ArrayBuffer(5))
buffer.setUint8(0, ClientPackets.QueryInstance)
buffer.setInt32(1, instance)
instances[instance].workerSocket.send(buffer)
}
}
}
break
}
case ServerPackets.AvailableVanity: {
vanity.style.border = "1px solid " + (data.getUint8(1) ? "green" : "red")
vanity.setAttribute("available", !!data.getUint8(1))
break
}
}
}
ws.onclose = function() {
overlay.style.display = 'flex'
}
</script>
</head>
<body>
<div id="overlay" class="page-cover">
<h1 style="margin: 0px;">Bruh.. 💀</h1>
<h3 style="text-align: center;">The server has disconnected and something isn't working right</h3>
<div style="display: flex; column-gap: 8px;">
<button type="button" onclick="window.location.reload(true)">Reload page</button>
<button type="button" onclick="window.location = window.location.origin">Go back</button>
</div>
</div>
<dialog class="modal" id="createModal">
<div class="header"><h4 style="margin: 0px;">Create new<br>rplace canvas</h4></div>
<div class="body">
<r-close-icon tabindex="0" class="close-icon" onclick="createModal.close()"></r-close-icon>
<input id="vanity" type="text" placeholder="Vanity name (unique)" oninput="
this.value = this.value.toLowerCase().replaceAll(' ', '-')
let encoded = encoder.encode('X' + this.value)
encoded[0] = ClientPackets.VanityAvailable
ws.send(encoded)">
<input id="cooldown" type="number" placeholder="Canvas cooldown (seconds)">
<input id="width" type="number" placeholder="Canvas width (pixels)">
<input id="height" type="number" placeholder="Canvas height (pixels)">
<p style="margin: 0px;">Import canvas from image file:</p>
<input type="file">
<div style="flex-grow: 1"></div>
<button class="create-button" onclick="
if (vanity.getAttribute('available') != 'true') {
alert('The provided canvas vanity name is not available, try another.')
return
}
createCanvas({
name: vanity.value,
width: Math.max(parseInt(width.value), 1),
height: Math.max(parseInt(height.value), 1),
cooldown: Math.max(cooldown.value, 1)
})
">Create canvas!</button>
</div>
</dialog>
<header>
<nav>
<img src="favicon.png" style="height: 64px;border-radius: 8px;">
<h1 style="align-self: center;margin: 0px;">Rplace instance manager</h1>
<ul>
<li><a href="#dashboard">Dashboard</a></li>
<li><a href="#servers">Servers</a></li>
<li><a href="/discord">Discord</a></li>
</ul>
</nav>
</header>
<div id="dashboard">
<h1>Dashboard</h1>
<p>Welcome to the rplace server hosting control panel. Here you can
manage your servers, view your server statistics, and get
support.</p>
<br>
<div>
<h2>Your Servers</h2>
<table id="instanceTable">
<thead>
<tr>
<th>Name</th>
<th>IP Address</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
<button onclick="createModal.showModal()">+ Create instance</button> <button>- Delete instance</button>
</div>
<br>
<div>
<h2 id="accountHeader">Your Account</h2>
<div id="loginPanel" style="display: block;">
<input id="loginUsername" type="text" placeholder="Username" style="display: block; width: 256px;"
maxlength="10">
<input id="loginEmail" type="text" placeholder="Email" style="display: block; width: 256px;"
maxlength="32">
</div>
<div id="preLoginOptions">
<a href="#dashboard" onclick="
if (loginPanel.style.display == 'block') {
let buffer = encoder.encode('X' + loginUsername.value.padEnd(10) + loginEmail.value.padEnd(32))
buffer[0] = ClientPackets.Authenticate
ws.send(buffer)
loginPanel.style.display = 'none'
preLoginOptions.style.display = 'none'
signoutOptions.style.display = 'block'
localStorage.username = btoa(loginUsername.value)
localStorage.password = btoa(loginEmail.value)
setTimeout(() => {
ws.send(new Uint8Array([ClientPackets.AccountInfo]))
ws.send(new Uint8Array([ClientPackets.LocateWorkers]))
}, 100) // TODO: Unjank
}
else {
loginPanel.style.display = 'block'
signupPanel.style.display = 'none'
}
">Login</a>
<span style="color: gray;">•</span>
<a href="./index.hml">Signup</a>
</div>
<div id="signoutOptions" style="display: none;">
<a href="#dashboard" onclick="window.location.reload()">Log out</a>
</div>
<p id="loginSignupMessage" style="color: red; font-size: small;"></p>
<br>
<div id="accountInfo" style="display: none;">
<h4>Account info:</h4>
<div>Account name: <span id="accountName"></span></div>
<div>Account email: <span id="accountEmail"></span></div>
<div>Billing info: <span id="accountBilling"></span></div>
<a href="#dashboard">Delete account</a>
</div>
</div>
</div>
<div id="servers">
<h1>Servers</h1>
<p>Manage one of your servers, view it's activity and change its settings.</p>
<select id="viewServerOptions">
<option>currently viewing</option>
</select>
<br><br>
<h4>Canvas info:</h4>
<div style="display: flex; flex-direction: column; margin-bottom: 24px;">
<div><a href="">https://rplace.live?board=...</a> <button>copy direct link</button></div>
<span style="color: #0079d3;">https://rplace.live/<span></span><button>edit vanity link</button></span>
</div>
<h4>Canvas settings:</h4>
<div style="display: flex; flex-direction: column; margin-bottom: 24px;">
<div id="serverSettings"></div>
<div>
<button class="button">Save changes</button>
<button class="button">Undo changes</button>
</div>
</div>
<h4>Server activity:</h4>
<div id="serverActivity">
<div class="player-list">
<div class="player-list-header">
<h4 style="flex-grow: 1;">Players</h4>
<span style="opacity: 0.5;">16 online, 4 VIPs</span>
</div>
<div class="player-list-body">
<div class="player-card">
<div class="player-card-ip">192.168.1.1 | #1234</div>
<div class="player-card-vip">Permissions: VIP</div>
<div class="player-card-detail">Join date: Mon Jan 01 1970 00:00:00</div>
<div class="player-card-detail">Pixels placed: 0</div>
<div style="position: absolute;top: 0px;right: 0px;"></div>
<div class="player-card-actions">
<button>Kick</button>
<button>Ban</button>
<button>Message</button>
</div>
</div>
</div>
</div>
<br>
<button class="button">Restart server</button>
<button class="button">Delete server</button>
<br><br>
</div>
</div>
<div id="instancePlayer">
<div style="position: relative;aspect-ratio: 1/1;"><canvas id="instanceCanvas" width="400" height="400"></canvas></div>
<div id="instanceLiveChat" style="flex-grow: 1; min-height: 150px; max-height: 200px; overflow-y: scroll;">
<span>[en][zekiahepic] Hello world</span>
</div>
</div>
<footer>
<p>© 2023 Zekiah-A. Powered by <a href="https://github.com/Zekiah-A/RplaceServer">RplaceServer</a>.</p>
</footer>
</body>
<script>
const params = new URLSearchParams(location.search)
const encoder = new TextEncoder()
const decoder = new TextDecoder()
const instances = {}
const ClientPackets = {
// To auth server
DeleteAccount: 0,
UpdateAccount: 1,
CreateAccount: 2,
AuthenticateCreate: 3,
AccountInfo: 4,
Authenticate: 5,
LocateVanity: 6,
LocateWorkers: 7,
VanityAvailable: 8,
// To worker
CreateInstance: 0,
DeleteInstance: 1,
RestartInstance: 2,
Subscribe: 3,
QueryInstance: 4,
CreateVanity: 5,
}
const ServerPackets = {
Fail: 0,
AccountInfo: 1,
VanityLocation: 2,
WorkerLocations: 3,
AvailableVanity: 4
}
const WorkerPackets = {
Logger: 0,
PlayerConnected: 1,
PlayerDisconnected: 2,
BackupCreated: 3,
InstanceQuery: 4,
InstanceCreated: 5
}
const gameData = {
Cooldown: 10,
ChatCooldown: 10,
CaptchaEnabled: false,
CreateBackups: false,
Vips: [],
Bans: [],
BoardWidth: 1000,
BoardHeight: 1000,
BackupFrequency: 100000,
PostLimitPeriod: 100000,
TimelapseLimitPeriod: 100000,
CensorChatMessages: false,
ChatHistoryLength: 100,
WebhookUrl: "\"\"",
Palette: []
}
let builderData = null
let requestQueue = { }
let requestId = 0
let workerLocations = null
// Equivalent of TaskCompletionSource
class PublicPromise {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
}
}
serverSettings.innerHTML = objectToHtml(gameData, true)
window.location.hash = "#dashboard"
loginUsername.value = atob(localStorage.username || "")
loginEmail.value = atob(localStorage.password || "")
if (params.get("builder")) {
accountHeader.textContent = "Sign in to finish creating your canvas"
builderData = {
name: params.get("name"),
width: +params.get("width"),
height: +params.get("height"),
cooldown: +params.get("cooldown")
}
}
async function createCanvas(builder) {
let encoded = encoder.encode('X' + atob(localStorage.username).padEnd(10) + atob(localStorage.password).padEnd(32) + 'XXXX')
encoded[0] = ClientPackets.CreateInstance
let view = new DataView(encoded.buffer)
let request = requestId++
view.setInt32(42, request)
let responseSource = new PublicPromise()
requestQueue[request] = responseSource
workerLocations[0].workerSocket.send(view.buffer)
let instanceId = await responseSource.promise
delete requestQueue[request]
if (builder.name) {
let vanityEncode = endoded.slice(0, encoded.length - 4) + encoder.encode(builder.name)
vanityEncode[0] = ClientPackets.CreateVanity
workerLocations[0].workerSocket.send(vanityEncode)
}
}
async function renderBoard(url) {
const ctx = instanceCanvas.getContext("2d")
ctx.clearRect(0, 0, +instanceCanvas.getAttribute("width") || 0, +instanceCanvas.getAttribute("height") || 0)
instanceCanvas.style.visibility = 'visible'
let response = await fetch(url)
if (!response.ok) return
let placeView = new DataView(await response.arrayBuffer())
let metadataLength = placeView.getInt16(placeView.byteLength - 2)
let boardLength = placeView.byteLength - metadataLength
let boardWidth = placeView.getUint32(boardLength)
let boardHeight = boardLength / boardWidth
instanceCanvas.setAttribute("width", boardWidth)
instanceCanvas.setAttribute("height", boardHeight)
let palette = []
for (let i = boardLength + 4; i < boardLength - 2; i += 4) {
palette.push(placeView.getUint32(i))
}
if (!palette.length) {
palette = [0xff1a006d, 0xff3900be, 0xff0045ff, 0xff00a8ff, 0xff35d6ff, 0xffb8f8ff, 0xff68a300, 0xff78cc00,
0xff56ed7e, 0xff6f7500, 0xffaa9e00, 0xffc0cc00, 0xffa45024, 0xffea9036, 0xfff4e951, 0xffc13a49, 0xffff5c6a,
0xffffb394, 0xff9f1e81, 0xffc04ab4, 0xffffabe4, 0xff7f10de, 0xff8138ff, 0xffaa99ff, 0xff2f486d, 0xff26699c,
0xff70b4ff, 0xff000000, 0xff525251, 0xff908d89, 0xffd9d7d4, 0xffffffff] // argb
}
const pixelData = ctx.createImageData(1, 1)
for (let i = 0; i < boardLength; i++) {
let colour = palette[placeView.getUint8(i)]
pixelData.data[0] = colour & 0xFF
pixelData.data[1] = (colour >> 8) & 0xFF
pixelData.data[2] = (colour >> 16) & 0xFF
pixelData.data[3] = (colour >> 24) & 0xFF
ctx.putImageData(pixelData, i % boardWidth, Math.floor(i / boardWidth));
}
}
</script>
</html>