In this directory, you will see a REST API developed by Java Spring boot and a react web application.
this is a game and single player has been already developed. However, multiplayer is missing.
In the same logic, I would like to develope multiplayer version with you.
Requirements:
- There is no db connection so everybody would be create a new game.
- when multiplayer page lists all available games.
- each game has two status (waiting, playing, closed)
- each game has start with two player (not more and not less)
- multiplayer page has also create new game button.
- when a player click to new play, a modal appears and start with the following fields
Game Name: Unique
Number Limit: 3-10 digits
Game Type: auto guesstures, player guesstures, time challenge
- game waits while two players are online
- when one of player leaves the game, the game is over.
auto guesstures: two different guess numbers are randomly created by the game for the players. each turns run together (first guess waits the other). after the turn each youser see his own guess resut and rival's resutl.
both scors will show, when a player find the number
Number selection rule will be the same as single player (each number will be used one time)
player guesstures: before start this game each players set a rival's guesstures number suitable for number selection rule. the rest of the game is the same with auto guesstures type.
time challenge: the game creates a number for the both players. turns go forward independently. players cannot see the rival's guess.
There is a time counter in this game. the game continue until both user find the number. the best score win the game.
Please create a score calculation rule depening on time and turn count.
Finally please update the summary note in the current directory
---
Projeyi inceledim. Kısaca: Node.js'e geçmenize gerek yok. Sorunların ikisi de nginx ayarından kaynaklanıyor ve mevcut nginx'i proxy olarak kullanmanız doğru yol.
1. Reload'da 403 Forbidden hatası
next.config.mjs içinde output: 'export' var. Build sonucu web/out altında şu dosyalar oluşuyor:
multiplayer/ ← klasör (içinde index.html yok, sadece play.html var)
single-player.html
Sayfa reload edildiğinde nginx try_files $uri $uri/ =404; kuralını uyguluyor. /mastermind/multiplayer isteğinde $uri/ kısmı multiplayer/ klasörüyle eşleşiyor. Klasörde index.html olmadığı için nginx klasörü listelemeyi reddediyor ve 403 dönüyor. Uygulama içinde link ile gezerken sorun çıkmıyor, çünkü yönlendirmeyi Next tarayıcıda yapıyor.
Çözüm (ikisinden biri yeterli):
nginx tarafında: try_files $uri $uri.html $uri/ =404;, yani .html denemesini klasörden önce yapmak.
Next tarafında: next.config.mjs içine trailingSlash: true eklemek. Bu durumda her sayfa multiplayer/index.html şeklinde üretilir.
2. Güncellemeden sonra tarayıcı cache sorunları
Next'in _next/static/... altındaki dosyalarının adları hash içeriyor, yani her build'de değişiyor. Sorun HTML dosyalarında:
Tarayıcı ya da Cloudflare eski index.html'i cache'ten veriyor. Eski HTML eski chunk'ları istiyor, ama siz klasörü silip yeni out'u kopyaladığınız için o chunk'lar artık yok. Sonuç: bozuk sayfa veya ChunkLoadError.
nginx'te hiç cache header'ı yok, bu yüzden tarayıcı tahmini (heuristic) cache uyguluyor.
Çözüm:
HTML dosyaları için Cache-Control: no-cache, _next/static için max-age=31536000, immutable gönderin (aşağıdaki config'te var).
Cloudflare'de HTML'i cache'leyen bir "Cache Everything" kuralı varsa /mastermind/*.html ve /mastermind/ için bypass ekleyin, ya da her deploy sonrası "Purge Cache" yapın.
Deploy'u klasörü silip kopyalayarak yapmak yerine container image ile yapın (aşağıda). Böylece geçiş tek adımda olur.
3. Node.js mi, nginx mi?
Proje tamamen statik export. next start bu modda çalışmıyor, SSR kullanmıyorsunuz ve Node'un sağlayacağı ek bir şey yok. Statik dosya sunmakta nginx Node'dan daha hızlı ve daha hafif.
└─ /mastermind/api/ → mastermind-api container (Spring, port dışarı kapalı)
web/Dockerfile zaten bu amaçla yazılmış, ancak şu an çalışmıyor: npm run export diye bir script yok (Next 15'te kaldırıldı). Ayrıca basePath=/mastermind olduğu için dosyaların container içinde /usr/share/nginx/html/mastermind altına kopyalanması gerekiyor.
4. Backend dışarı açık mı kalmalı, içeride mi?
İçeride kalmasını öneririm. Şu an web https://api.mmkilic.com/api-mastermind adresine gidiyor ve API 8005 portundan dışarıya açık. Aynı origin üzerinden proxy'lemenin faydaları:
CORS ayarına gerek kalmaz (SecurityConfig içindeki origin listesiyle uğraşmazsınız).
API portu host'a publish edilmez, doğrudan erişilemez.
Tek domain ve tek SSL olur, Cloudflare kuralları da tek yerde toplanır.
Multiplayer polling ile çalışıyor (WebSocket veya SSE yok), bu yüzden proxy tarafında ek bir ayar gerekmiyor. Başka projeler de api.mmkilic.com adresini kullanıyorsa o domain açık kalabilir. Mastermind'ı içeri almak onları etkilemez.
Örnek yapılandırma
Ortak docker network (bir kez çalıştırın; ana nginx container'ını da bu ağa bağlayın):
Mimariyi şimdilik değiştirmek istemiyorsanız, ana nginx'teki /mastermind bloğuna yukarıdaki try_files ... $uri.html satırını ve iki Cache-Control kuralını eklemeniz bile hem 403 hatasını hem cache sorununun büyük kısmını çözer.
İsterseniz bu değişiklikleri (Dockerfile, nginx.conf, env, compose ve gerekiyorsa SecurityConfig) projeye uygulayabilirim.