first commit

This commit is contained in:
Midas Wollinger
2026-07-29 12:37:38 +02:00
commit db8642080c
5 changed files with 973 additions and 0 deletions

249
static/css/style.css Normal file
View File

@ -0,0 +1,249 @@
:root {
--bg: #1b2735;
--panel: #24344a;
--water: #2f6f9e;
--water-dark: #24597f;
--grid-line: #16232f;
--fog: #3a536b;
--text: #eef3f8;
--accent: #e8b23a;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: "Segoe UI", Verdana, sans-serif;
background: linear-gradient(180deg, var(--bg), #0e1620);
color: var(--text);
min-height: 100vh;
}
.app {
max-width: 1100px;
margin: 0 auto;
padding: 24px 16px 60px;
}
header { text-align: center; margin-bottom: 8px; }
header h1 { margin: 0 0 4px; font-size: 2.1rem; }
.subtitle { margin: 0; opacity: 0.75; }
.message {
text-align: center;
min-height: 1.4em;
margin: 14px auto 20px;
padding: 10px 16px;
background: var(--panel);
border-radius: 8px;
max-width: 640px;
font-weight: 600;
}
.screen.hidden { display: none; }
h2, h3 { text-align: center; }
.placement-controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
align-items: center;
margin-bottom: 18px;
}
button {
background: var(--accent);
color: #2a1c00;
border: none;
border-radius: 6px;
padding: 10px 16px;
font-size: 0.95rem;
font-weight: 700;
cursor: pointer;
transition: transform 0.08s ease, opacity 0.15s ease;
}
button:hover:not(:disabled) { transform: translateY(-1px); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
.fleet-picker {
display: flex;
flex-wrap: wrap;
gap: 8px;
justify-content: center;
}
.fleet-picker button {
background: var(--panel);
color: var(--text);
border: 2px solid transparent;
display: flex;
align-items: center;
gap: 6px;
}
.fleet-picker button.selected { border-color: var(--accent); }
.fleet-picker button.placed { opacity: 0.35; cursor: default; }
.boards {
display: flex;
flex-wrap: wrap;
gap: 32px;
justify-content: center;
}
.board-wrap { margin-bottom: 20px; }
.board {
display: grid;
grid-template-columns: repeat(10, 34px);
grid-template-rows: repeat(10, 34px);
gap: 2px;
background: var(--grid-line);
padding: 4px;
border-radius: 8px;
width: max-content;
margin: 0 auto;
}
.cell {
width: 34px;
height: 34px;
background: var(--water);
position: relative;
border-radius: 3px;
cursor: default;
display: flex;
align-items: center;
justify-content: center;
overflow: visible;
}
.cell.own { background: var(--water-dark); }
#enemy-board .cell {
cursor: crosshair;
background: var(--fog);
}
#enemy-board .cell:hover:not(.hit):not(.miss) {
background: #4a6a87;
}
.cell.preview-ok { outline: 2px solid #2ecc71; }
.cell.preview-bad { outline: 2px solid #e74c3c; }
.cell.miss::after {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: rgba(255,255,255,0.6);
}
.cell.hit {
background: #7a2323 !important;
}
.cell.hit::after {
content: "💥";
font-size: 18px;
}
/* --- Dwarf ship sprite, built purely from CSS shapes --- */
.dwarf {
position: relative;
width: 24px;
height: 26px;
}
.dwarf .hat {
position: absolute;
top: -2px;
left: 2px;
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 12px solid var(--hat, #c0392b);
filter: drop-shadow(0 1px 0 rgba(0,0,0,0.4));
}
.dwarf .hat::after {
content: "";
position: absolute;
top: 11px;
left: -6px;
width: 12px;
height: 4px;
background: var(--hat, #c0392b);
border-radius: 2px;
}
.dwarf .head {
position: absolute;
top: 9px;
left: 4px;
width: 16px;
height: 14px;
background: #f0c090;
border-radius: 50% 50% 45% 45%;
}
.dwarf .beard {
position: absolute;
top: 15px;
left: 3px;
width: 18px;
height: 12px;
background: #d9d9d9;
border-radius: 0 0 9px 9px;
}
.dwarf .eyes {
position: absolute;
top: 13px;
left: 7px;
width: 10px;
height: 2px;
background: transparent;
box-shadow: 0 0 0 1px #2a1c00, 8px 0 0 -1px #2a1c00;
}
.cell.hit .dwarf { filter: grayscale(1) brightness(0.6); transform: rotate(90deg); }
.cell.hit .dwarf .eyes { box-shadow: none; }
.cell.hit .dwarf::before {
content: "✕";
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-weight: bold;
font-size: 14px;
transform: rotate(-90deg);
}
.fleet-status {
display: flex;
flex-wrap: wrap;
gap: 6px;
justify-content: center;
margin-top: 10px;
max-width: 340px;
}
.fleet-status .badge {
font-size: 0.78rem;
padding: 4px 8px;
border-radius: 12px;
background: var(--panel);
}
.fleet-status .badge.sunk {
text-decoration: line-through;
opacity: 0.5;
background: #5a1a1a;
}
#new-game-btn { display: block; margin: 24px auto 0; }
@media (max-width: 560px) {
.board {
grid-template-columns: repeat(10, 28px);
grid-template-rows: repeat(10, 28px);
}
.cell { width: 28px; height: 28px; }
}

232
static/js/game.js Normal file
View File

@ -0,0 +1,232 @@
const BOARD_SIZE = 10;
const state = {
fleetDef: [],
selectedShip: null,
orientation: "h",
status: "placing",
};
const el = (id) => document.getElementById(id);
async function api(path, options) {
const res = await fetch(path, {
method: options?.method || "GET",
headers: { "Content-Type": "application/json" },
body: options?.body ? JSON.stringify(options.body) : undefined,
});
const data = await res.json();
if (!res.ok) {
setMessage(data.error || "Fehler", true);
throw new Error(data.error || "Request failed");
}
return data;
}
function setMessage(text, isError = false) {
const m = el("message");
m.textContent = text;
m.style.color = isError ? "#ff8b8b" : "";
}
function dwarfSprite(hat) {
const wrap = document.createElement("div");
wrap.className = "dwarf";
wrap.style.setProperty("--hat", hat);
wrap.innerHTML = '<div class="hat"></div><div class="head"></div><div class="eyes"></div><div class="beard"></div>';
return wrap;
}
function buildBoard(container, size, onClick) {
container.innerHTML = "";
const cells = [];
for (let r = 0; r < size; r++) {
const rowCells = [];
for (let c = 0; c < size; c++) {
const cell = document.createElement("div");
cell.className = "cell";
cell.dataset.row = r;
cell.dataset.col = c;
if (onClick) cell.addEventListener("click", () => onClick(r, c, cell));
container.appendChild(cell);
rowCells.push(cell);
}
cells.push(rowCells);
}
return cells;
}
// ----- Placement screen -----
let placementCells = null;
function renderFleetPicker(data) {
const picker = el("fleet-picker");
picker.innerHTML = "";
const placedIds = new Set();
for (const row of data.player_board) {
for (const cell of row) if (cell) placedIds.add(cell);
}
for (const ship of data.fleet_def) {
const btn = document.createElement("button");
btn.type = "button";
const isPlaced = placedIds.has(ship.id);
btn.textContent = `${ship.name} (${ship.size})`;
btn.style.setProperty("--hat", ship.hat);
btn.classList.toggle("placed", isPlaced);
btn.classList.toggle("selected", state.selectedShip === ship.id);
btn.disabled = isPlaced;
btn.addEventListener("click", () => {
state.selectedShip = ship.id;
renderFleetPicker(data);
});
picker.appendChild(btn);
}
}
function renderPlacementBoard(data) {
if (!placementCells) {
placementCells = buildBoard(el("placement-board"), BOARD_SIZE, handlePlacementClick);
}
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
const cell = placementCells[r][c];
cell.className = "cell own";
cell.innerHTML = "";
const shipId = data.player_board[r][c];
if (shipId) {
const ship = data.fleet_def.find((s) => s.id === shipId);
cell.appendChild(dwarfSprite(ship.hat));
}
}
}
const placedCount = new Set(data.player_board.flat().filter(Boolean)).size;
el("start-btn").disabled = placedCount !== data.fleet_def.length;
}
async function handlePlacementClick(r, c) {
if (!state.selectedShip) {
setMessage("Wähle zuerst einen Zwergenclan aus.", true);
return;
}
try {
const data = await api("/api/place_ship", {
method: "POST",
body: { ship_id: state.selectedShip, row: r, col: c, orientation: state.orientation },
});
applyState(data);
state.selectedShip = null;
} catch (e) {
/* message already shown */
}
}
// ----- Battle screen -----
let ownCells = null;
let enemyCells = null;
function renderBattle(data) {
if (!ownCells) ownCells = buildBoard(el("own-board"), BOARD_SIZE, null);
if (!enemyCells) enemyCells = buildBoard(el("enemy-board"), BOARD_SIZE, handleFireClick);
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
const own = ownCells[r][c];
own.className = "cell own";
own.innerHTML = "";
const shipId = data.player_board[r][c];
const receivedShot = data.player_shots_received[r][c];
if (shipId) own.appendChild(dwarfSprite(data.fleet_def.find((s) => s.id === shipId).hat));
if (receivedShot) own.classList.add(receivedShot);
const enemy = enemyCells[r][c];
enemy.className = "cell";
enemy.innerHTML = "";
const madeShot = data.player_shots_made[r][c];
if (madeShot) enemy.classList.add(madeShot);
}
}
renderFleetStatus(el("own-fleet"), data.player_fleet);
renderFleetStatus(el("enemy-fleet"), data.computer_fleet);
}
function renderFleetStatus(container, fleet) {
container.innerHTML = "";
for (const ship of fleet) {
const badge = document.createElement("span");
badge.className = "badge" + (ship.sunk ? " sunk" : "");
badge.textContent = `${ship.name}`;
container.appendChild(badge);
}
}
async function handleFireClick(r, c) {
if (state.status !== "playing") return;
try {
const data = await api("/api/fire", { method: "POST", body: { row: r, col: c } });
applyState(data);
} catch (e) {
/* message already shown */
}
}
// ----- Shared -----
function applyState(data) {
state.fleetDef = data.fleet_def;
state.status = data.status;
setMessage(data.message);
const placementScreen = el("placement-screen");
const battleScreen = el("battle-screen");
if (data.status === "placing") {
placementScreen.classList.remove("hidden");
battleScreen.classList.add("hidden");
renderFleetPicker(data);
renderPlacementBoard(data);
} else {
placementScreen.classList.add("hidden");
battleScreen.classList.remove("hidden");
renderBattle(data);
el("new-game-btn").classList.toggle(
"hidden",
!(data.status === "player_win" || data.status === "computer_win")
);
}
}
function init() {
el("rotate-btn").addEventListener("click", () => {
state.orientation = state.orientation === "h" ? "v" : "h";
el("rotate-btn").textContent =
"🔄 Ausrichtung: " + (state.orientation === "h" ? "horizontal" : "vertikal");
});
el("random-btn").addEventListener("click", async () => {
const data = await api("/api/random_place", { method: "POST" });
applyState(data);
});
el("reset-btn").addEventListener("click", async () => {
const data = await api("/api/reset_placement", { method: "POST" });
applyState(data);
});
el("start-btn").addEventListener("click", async () => {
try {
const data = await api("/api/start", { method: "POST" });
applyState(data);
} catch (e) {
/* message already shown */
}
});
el("new-game-btn").addEventListener("click", async () => {
const data = await api("/api/new_game", { method: "POST" });
applyState(data);
});
api("/api/state").then(applyState);
}
init();