Fix indentation

This commit is contained in:
Andrea Cavalli 2024-05-24 12:10:34 +02:00
parent ac474aacbd
commit 7b920dd6a9
5 changed files with 144 additions and 140 deletions

View File

@ -4,8 +4,12 @@ root = true
[*]
charset = utf-8
indent_style = space
indent_style = tab
indent_size = 2
trim_trailing_whitespace = true
end_of_line = lf
insert_final_newline = true
[*.css]
indent_style = space
indent_size = 4

View File

@ -7,9 +7,9 @@ import io.vertx.core.VertxOptions;
public class App {
public static void main(String[] args) throws InterruptedException {
var vertx = Vertx.builder().with(new VertxOptions().setPreferNativeTransport(true)).build();
vertx.deployVerticle(new MainVerticle(), new DeploymentOptions().setThreadingModel(ThreadingModel.VIRTUAL_THREAD));
Thread.currentThread().join();
}
public static void main(String[] args) throws InterruptedException {
var vertx = Vertx.builder().with(new VertxOptions().setPreferNativeTransport(true)).build();
vertx.deployVerticle(new MainVerticle(), new DeploymentOptions().setThreadingModel(ThreadingModel.VIRTUAL_THREAD));
Thread.currentThread().join();
}
}

View File

@ -14,42 +14,42 @@ import java.time.Duration;
public class MainVerticle extends AbstractVerticle {
private Single<HttpServer> httpServer;
private Single<HttpServer> httpServer;
@Override
public Completable rxStart() {
@Override
public Completable rxStart() {
vertx.eventBus().consumer("connect2x.move")
.handler(event -> System.out.println("Received: " + event.body()));
vertx.eventBus().consumer("connect2x.move")
.handler(event -> System.out.println("Received: " + event.body()));
var server = vertx.createHttpServer();
var server = vertx.createHttpServer();
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
SockJSBridgeOptions sockJSBridgeOptions = new SockJSBridgeOptions()
.addInboundPermitted(new PermittedOptions().setAddress("connect2x.move"))
.addOutboundPermitted(new PermittedOptions().setAddress("connect2x.moved"));
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
SockJSBridgeOptions sockJSBridgeOptions = new SockJSBridgeOptions()
.addInboundPermitted(new PermittedOptions().setAddress("connect2x.move"))
.addOutboundPermitted(new PermittedOptions().setAddress("connect2x.moved"));
Router router = Router.router(vertx);
router
.get("/*")
.handler(StaticHandler
.create(FileSystemAccess.RELATIVE,"webroot")
//.setDirectoryListing(true)
.setCachingEnabled(true)
.setAlwaysAsyncFS(true)
.setIndexPage("static/index.html")
.setSendVaryHeader(true)
.setEnableRangeSupport(true)
.setMaxAgeSeconds(Duration.ofSeconds(1).toSeconds()));
Router router = Router.router(vertx);
router
.get("/*")
.handler(StaticHandler
.create(FileSystemAccess.RELATIVE,"webroot")
//.setDirectoryListing(true)
.setCachingEnabled(true)
.setAlwaysAsyncFS(true)
.setIndexPage("static/index.html")
.setSendVaryHeader(true)
.setEnableRangeSupport(true)
.setMaxAgeSeconds(Duration.ofSeconds(1).toSeconds()));
router.get("/eventbus/*").subRouter(sockJSHandler.bridge(sockJSBridgeOptions));
router.get("/eventbus/*").subRouter(sockJSHandler.bridge(sockJSBridgeOptions));
this.httpServer = server.requestHandler(router).listen(8081).cache();
return httpServer.ignoreElement();
}
this.httpServer = server.requestHandler(router).listen(8081).cache();
return httpServer.ignoreElement();
}
@Override
public Completable rxStop() {
return httpServer.flatMapCompletable(HttpServer::close);
}
@Override
public Completable rxStop() {
return httpServer.flatMapCompletable(HttpServer::close);
}
}

View File

@ -1,118 +1,118 @@
const chars = ["AN","AR","AT","CA","CH","CO","DE","DI","EL","EN","ER","ES","HE","IA","IL","IN","IO","LA","LE","LI","LL","MA","ME","NA","NE","NO","NT","OL","ON","OR","PE","RA","RE","RI","RO","SE","SI","SO","ST","TA","TE","TI","TO","TR","TT","UN","TO","RE","ER","ON","CO","DI","TA","EN","IN","TE","AT","RA","AN","NO","NT","ST","LA","AR","AL","OR","CH","RI","TI","IO","LE","DE","ES","NE","ME","TT","EL","PE","IL","UN","IA","LI","SE","SO","LL","SI","OL","RO","MA","CA","NA","TR","HE","ALE","ALL","ANC","AND","ANT","ARE","ATO","ATT","CHE","CHI","COM","CON","DEL","ELL","ENT","ERA","ERE","ESS","EST","ETT","GLI","ION","LLA","MEN","NON","NTE","NTI","NTO","OLO","ONE","ONO","PER","QUE","SON","STA","STO","TAT","TRA","TTO","UNA","VER","ZIO","ENT","CHE","ATO","PER","NTE","CON","ELL","STA","ARE","MEN","ION","DEL","LLA","TTO","TAT","ESS","ERE","ETT","EST","ONE","ONO","ZIO","NON","ERA","CHI","GLI","COM","TRA","STO","NTI","SON","VER","ATT","UNA","QUE","NTO","AND","ALL","OLO","ANC","ANT","ALE"];
function randomID() {
var u8 = crypto.getRandomValues(new Uint8Array(3));
return Array.from(u8).map(n => chars[n % chars.length]).reduce((a, b) => a + b);
var u8 = crypto.getRandomValues(new Uint8Array(3));
return Array.from(u8).map(n => chars[n % chars.length]).reduce((a, b) => a + b);
}
document.addEventListener("DOMContentLoaded", e => {
const gameCode = document.getElementById("game-code");
const gameCodeLink = document.getElementById("game-code-link");
const newGameLink = document.getElementById("new-game-link");
const mainBody = document.getElementById("main-body");
const gameCode = document.getElementById("game-code");
const gameCodeLink = document.getElementById("game-code-link");
const newGameLink = document.getElementById("new-game-link");
const mainBody = document.getElementById("main-body");
const eb = new EventBus(`${document.location.origin}/eventbus`);
eb.enableReconnect(true);
eb.onopen = function() {}; // Set up handlers here, will be called on initial connection and all reconnections
eb.onreconnect = function() {}; // Optional, will only be called on reconnections
const eb = new EventBus(`${document.location.origin}/eventbus`);
eb.enableReconnect(true);
eb.onopen = function() {}; // Set up handlers here, will be called on initial connection and all reconnections
eb.onreconnect = function() {}; // Optional, will only be called on reconnections
// Alternatively, pass in an options object
var options = {
vertxbus_reconnect_attempts_max: Infinity, // Max reconnect attempts
vertxbus_reconnect_delay_min: 1000, // Initial delay (in ms) before first reconnect attempt
vertxbus_reconnect_delay_max: 5000, // Max delay (in ms) between reconnect attempts
vertxbus_reconnect_exponent: 2, // Exponential backoff factor
vertxbus_randomization_factor: 0.5 // Randomization factor between 0 and 1
};
// Alternatively, pass in an options object
var options = {
vertxbus_reconnect_attempts_max: Infinity, // Max reconnect attempts
vertxbus_reconnect_delay_min: 1000, // Initial delay (in ms) before first reconnect attempt
vertxbus_reconnect_delay_max: 5000, // Max delay (in ms) between reconnect attempts
vertxbus_reconnect_exponent: 2, // Exponential backoff factor
vertxbus_randomization_factor: 0.5 // Randomization factor between 0 and 1
};
const connect2x = document.connect2x = {
clientId: randomID(),
gameId: null
}
const connect2x = document.connect2x = {
clientId: randomID(),
gameId: null
}
window.addEventListener("hashchange", e=> {
onHashChange(e.target.location.hash);
})
window.addEventListener("hashchange", e=> {
onHashChange(e.target.location.hash);
})
onHashChange(document.location.hash);
onHashChange(document.location.hash);
function onHashChange(hash) {
if (hash != null && hash.length > 0) {
hash = hash.substring(1);
} else {
hash = "";
}
if (hash.length === 0) {
if (connect2x.gameId == null) {
onGameIdChange(randomID());
} else {
console.warn("Game hash unchanged.");
}
} else if (hash.length > 32) {
console.error("Invalid page hash length", hash);
document.body.innerHTML = `<h1>Invalid page hash length</h1><p><pre><code id="hash-text"></code></pre></p><p><a id="redirect-link">Go back</a></p>`;
document.getElementById("hash-text").innerText = hash;
const redirectLink = document.location.origin;
document.getElementById("redirect-link").href = redirectLink;
setTimeout(() => {
document.location.href = redirectLink;
}, 5000);
} else if (connect2x.gameId !== hash) {
if (connect2x.gameId != null) {
console.log("Game id changed, reloading...");
document.location.reload();
} else {
onGameIdChange(hash);
}
} else {
console.warn("Game hash unchanged.");
}
}
function onHashChange(hash) {
if (hash != null && hash.length > 0) {
hash = hash.substring(1);
} else {
hash = "";
}
if (hash.length === 0) {
if (connect2x.gameId == null) {
onGameIdChange(randomID());
} else {
console.warn("Game hash unchanged.");
}
} else if (hash.length > 32) {
console.error("Invalid page hash length", hash);
document.body.innerHTML = `<h1>Invalid page hash length</h1><p><pre><code id="hash-text"></code></pre></p><p><a id="redirect-link">Go back</a></p>`;
document.getElementById("hash-text").innerText = hash;
const redirectLink = document.location.origin;
document.getElementById("redirect-link").href = redirectLink;
setTimeout(() => {
document.location.href = redirectLink;
}, 5000);
} else if (connect2x.gameId !== hash) {
if (connect2x.gameId != null) {
console.log("Game id changed, reloading...");
document.location.reload();
} else {
onGameIdChange(hash);
}
} else {
console.warn("Game hash unchanged.");
}
}
function onGameIdChange(gameId) {
connect2x.gameId = gameId;
console.info("Game ID", gameId);
gameCode.innerText = gameId;
const gameLink = `${document.location.origin}#${gameId}`;
document.location.hash = gameId;
gameCodeLink.href = gameLink;
gameCodeLink.onclick = async e => {
if (navigator.share !== undefined) {
e.preventDefault();
await navigator.share({
title: "Connect 2x",
text: "Play Connect 2x game with me",
url: gameLink
}).then(() => console.log("Shared!")).catch(err => {
console.error("Share failed", err);
navigateToGame(gameId);
});
} else {
navigateToGame(gameId);
}
}
onGameLoad();
}
function onGameIdChange(gameId) {
connect2x.gameId = gameId;
console.info("Game ID", gameId);
gameCode.innerText = gameId;
const gameLink = `${document.location.origin}#${gameId}`;
document.location.hash = gameId;
gameCodeLink.href = gameLink;
gameCodeLink.onclick = async e => {
if (navigator.share !== undefined) {
e.preventDefault();
await navigator.share({
title: "Connect 2x",
text: "Play Connect 2x game with me",
url: gameLink
}).then(() => console.log("Shared!")).catch(err => {
console.error("Share failed", err);
navigateToGame(gameId);
});
} else {
navigateToGame(gameId);
}
}
onGameLoad();
}
function navigateToGame(gameId) {
document.location.hash = gameId;
}
function navigateToGame(gameId) {
document.location.hash = gameId;
}
function onGameLoad() {
mainBody.classList.add("js-loaded");
}
function onGameLoad() {
mainBody.classList.add("js-loaded");
}
eb.onopen = () => {
eb.onopen = () => {
// set a handler to receive a message
eb.registerHandler('connect2x.moved', (error, message) => {
console.log('received a message: ' + JSON.stringify(message));
});
eb.registerHandler('connect2x.moved', (error, message) => {
console.log('received a message: ' + JSON.stringify(message));
});
// send a message
eb.send('connect2x.move', {name: 'tim', age: 587});
eb.send('connect2x.move', {name: 'tim', age: 587});
}
}
})

View File

@ -1,20 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Connect 2x</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/static/style.css">
<script src="https://unpkg.io/sockjs-client@1.5.0/dist/sockjs.min.js"></script>
<script src='https://unpkg.io/@vertx/eventbus-bridge-client.js@1.0.0-1/vertx-eventbus.js'></script>
<script src='/static/connect2x.js'></script>
<title>Connect 2x</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/static/style.css">
<script src="https://unpkg.io/sockjs-client@1.5.0/dist/sockjs.min.js"></script>
<script src='https://unpkg.io/@vertx/eventbus-bridge-client.js@1.0.0-1/vertx-eventbus.js'></script>
<script src='/static/connect2x.js'></script>
</head>
<body>
<div id="main-body">
<h1>Connect 2x</h1>
<p>Game code: <span id="game-code"></span> <a id="game-code-link" href="#">[share]</a> <a id="new-game" href="/">[new game]</a></p>
<main id="board">
<h1>Connect 2x</h1>
<p>Game code: <span id="game-code"></span> <a id="game-code-link" href="#">[share]</a> <a id="new-game" href="/">[new game]</a></p>
<main id="board">
</main>
</main>
</div>
</body>
</html>