fix: make sudoku comfortable as mobile web app

This commit is contained in:
nyan404 committed 2026-08-25 21:30:45 +03:00
1 parent a26fdcc878
commit f7b3213717
5 files changed
+35 -3

No files matched your search

+10 -3
View File
@@ -4,6 +4,10 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#202522">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="manifest" href="/sudoku.webmanifest">
<link rel="apple-touch-icon" href="/favicon.png">
<title>Судоку</title>
<style>
:root { --ink:#202522; --paper:#f2f4ef; --surface:#fffefa; --line:#9ca39d; --soft:#d9ded7; --accent:#df8d28; --accent-ink:#55320e; --danger:#9c3d32; --muted:#58615a; --radius:14px; font-family:"Avenir Next", "Segoe UI", sans-serif; color:var(--ink); background:var(--paper); }
@@ -19,7 +23,9 @@
.board { display:grid; grid-template-columns:repeat(9,1fr); aspect-ratio:1; border:2px solid var(--ink); background:var(--ink); gap:1px; } .cell { min-width:0; border:0; background:var(--surface); color:var(--ink); font-size:clamp(1.1rem,4.5vw,2rem); font-weight:600; } .cell.given { background:#e6eae3; font-weight:800; } .cell.selected { background:#f8d9ab; } .cell.related { background:#eff1ec; } .cell.conflict { background:#f3c6bf; color:#6f251d; } .cell:nth-child(3n):not(:nth-child(9n)) { border-right:2px solid var(--ink); } .cell:nth-child(n+19):nth-child(-n+27),.cell:nth-child(n+46):nth-child(-n+54) { border-bottom:2px solid var(--ink); }
.numpad { display:grid; grid-template-columns:repeat(5,1fr); gap:8px; margin-top:16px; } .number { min-height:48px; border:1px solid var(--line); border-radius:8px; background:var(--surface); font-weight:800; font-size:1.08rem; } .number:hover { background:#e6eae3; } .number.erase { grid-column:span 2; font-size:.9rem; }
.side { display:grid; gap:12px; } .side .button { width:100%; } .status { min-height:24px; margin:12px 0 0; color:var(--muted); font-size:.92rem; } .status.error { color:var(--danger); font-weight:700; } .status.done { color:#176b69; font-weight:700; } .how { border-top:1px solid var(--soft); padding-top:16px; font-size:.92rem; }
.hidden { display:none; } @media (max-width:720px) { main { padding-top:16px; } .masthead { margin-bottom:20px; } .intro,.game { grid-template-columns:1fr; } .game { gap:16px; } .side { grid-template-columns:1fr 1fr; } .side .how { grid-column:1/-1; } .board { max-width:600px; margin:auto; } } @media (prefers-reduced-motion:reduce) { *,*:before,*:after { transition:none!important; } }
.hidden { display:none; } @media (max-width:720px) { main { padding-top:16px; } .masthead { margin-bottom:20px; } .intro,.game { grid-template-columns:1fr; } .game { gap:16px; } .side { grid-template-columns:1fr 1fr; } .side .how { grid-column:1/-1; } .board { max-width:600px; margin:auto; }
body.playing { height:100dvh; overflow:hidden; } body.playing main { height:100dvh; padding:8px 12px; display:grid; grid-template-rows:auto minmax(0,1fr); } body.playing .masthead { padding-bottom:8px; margin-bottom:8px; } body.playing h1 { font-size:clamp(2rem,10vw,2.7rem); } body.playing .masthead p { max-width:15ch; font-size:.78rem; text-align:right; } body.playing .game { display:block; min-height:0; margin:0; } body.playing .game > .panel { height:100%; padding:0; border:0; box-shadow:none; background:transparent; display:flex; flex-direction:column; } body.playing .game-top { flex:0 0 auto; margin:0 0 8px; } body.playing .board { width:min(100%, calc(100dvh - 430px)); max-width:none; flex:0 0 auto; margin:0 auto; } body.playing .numpad { flex:0 0 auto; margin-top:10px; } body.playing .number { min-height:44px; } body.playing .status { margin:8px 0 0; min-height:20px; font-size:.83rem; } body.playing .side { position:fixed; z-index:10; left:0; right:0; bottom:0; display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:8px; padding:8px 12px calc(8px + env(safe-area-inset-bottom)); background:var(--paper); border-top:1px solid var(--soft); } body.playing .side label,body.playing .side .how { display:none; } body.playing .side .button { min-height:48px; padding:8px; font-size:.82rem; } }
@media (prefers-reduced-motion:reduce) { *,*:before,*:after { transition:none!important; } }
</style>
</head>
<body>
@@ -49,12 +55,13 @@
function numberButton(text,action,kind=''){const button=document.createElement('button');button.type='button';button.className=`number ${kind}`;button.textContent=text;button.onclick=action;return button;}
function setValue(value){if(selected===null||game.puzzle[selected]||game.completedAt)return;game.board[selected]=value;gameStatus.textContent='';gameStatus.className='status';render();clearTimeout(saveTimer);saveTimer=setTimeout(save,650);if(game.board.every(Boolean)&&!conflicts(game.board).size)save();}
function updateTimer(){document.querySelector('#timer').textContent=formatTime(elapsed());}
function openGame(next){game=next;selected=null;startedAt=Date.now();startView.classList.add('hidden');gameView.classList.remove('hidden');clearInterval(ticker);ticker=setInterval(updateTimer,1000);render();}
function openGame(next){game=next;selected=null;startedAt=Date.now();document.body.classList.add('playing');startView.classList.add('hidden');gameView.classList.remove('hidden');clearInterval(ticker);ticker=setInterval(updateTimer,1000);render();}
async function save(){if(!game||game.completedAt)return;const button=document.querySelector('#save-button');button.disabled=true;gameStatus.textContent='Сохранение…';try{const data=await request(`/api/sudoku/games/${game.id}`,{method:'PUT',body:JSON.stringify({nickname:game.nickname,board:game.board,elapsedSeconds:elapsed()})});game=data.game;startedAt=Date.now();gameStatus.textContent=game.completedAt?'Готово. Партия сохранена как завершённая.':'Сохранено. Можно продолжить на другом устройстве.';gameStatus.className=`status ${game.completedAt?'done':''}`;render();}catch(error){gameStatus.textContent=error.message;gameStatus.className='status error';}finally{button.disabled=false;}}
async function startNew(){const button=document.querySelector('#new-button');if(game&&!confirm('Создать новое поле? Текущая партия останется сохранённой.'))return;button.disabled=true;try{const data=await request(game?`/api/sudoku/games/${game.id}/regenerate`:'/api/sudoku/games',{method:'POST',body:JSON.stringify({nickname:game.nickname,difficulty:gameDifficultyInput.value})});openGame(data.game);gameStatus.textContent='Новое поле готово.';}catch(error){gameStatus.textContent=error.message;gameStatus.className='status error';}finally{button.disabled=false;}}
form.onsubmit=async event=>{event.preventDefault();const nickname=nicknameInput.value.trim();startStatus.textContent='Ищем сохранённую игру…';try{localStorage.setItem('sudokuNickname',nickname);const data=await request(`/api/sudoku/games?nickname=${encodeURIComponent(nickname)}`);if(data.game){openGame(data.game);gameStatus.textContent=data.game.completedAt?'Последняя партия уже завершена. Можно создать новую.':'Незавершённая партия восстановлена.';}else{const created=await request('/api/sudoku/games',{method:'POST',body:JSON.stringify({nickname,difficulty:difficultyInput.value})});openGame(created.game);}}catch(error){startStatus.textContent=error.message;startStatus.className='status error';}};
document.querySelector('#save-button').onclick=save;document.querySelector('#new-button').onclick=startNew;document.querySelector('#change-user').onclick=()=>{if(game&&!game.completedAt)save();clearInterval(ticker);game=null;gameView.classList.add('hidden');startView.classList.remove('hidden');nicknameInput.focus();};
document.querySelector('#save-button').onclick=save;document.querySelector('#new-button').onclick=startNew;document.querySelector('#change-user').onclick=()=>{if(game&&!game.completedAt)save();clearInterval(ticker);game=null;document.body.classList.remove('playing');gameView.classList.add('hidden');startView.classList.remove('hidden');nicknameInput.focus();};
document.addEventListener('keydown',event=>{if(!game||selected===null||event.metaKey||event.ctrlKey||event.altKey)return;if(/^[1-9]$/.test(event.key)){setValue(Number(event.key));event.preventDefault();}if(event.key==='Backspace'||event.key==='Delete'){setValue(0);event.preventDefault();}});
if('serviceWorker' in navigator)navigator.serviceWorker.register('/sudoku-sw.js').catch(()=>{});
})();
</script>
</body>