Update.
This commit is contained in:
parent
052e63bdea
commit
9383cd45dc
68
app.py
68
app.py
@ -15,7 +15,7 @@ DB_FILE = Path("tycoon.db")
|
||||
|
||||
# --- Database Management ---
|
||||
def init_db():
|
||||
"""Initializes the database and creates/alters tables if they don't exist."""
|
||||
"""Initializes the database and creates tables if they don't exist."""
|
||||
try:
|
||||
conn = sqlite3.connect(DB_FILE)
|
||||
cursor = conn.cursor()
|
||||
@ -23,17 +23,9 @@ def init_db():
|
||||
CREATE TABLE IF NOT EXISTS players (
|
||||
nickname TEXT PRIMARY KEY,
|
||||
money INTEGER NOT NULL,
|
||||
population INTEGER NOT NULL,
|
||||
happiness REAL NOT NULL DEFAULT 0.5
|
||||
population INTEGER NOT NULL
|
||||
)
|
||||
""")
|
||||
# Add happiness column if it doesn't exist for migrations
|
||||
try:
|
||||
cursor.execute("ALTER TABLE players ADD COLUMN happiness REAL NOT NULL DEFAULT 0.5")
|
||||
logger.info("Added 'happiness' column to players table.")
|
||||
except sqlite3.OperationalError:
|
||||
pass # Column already exists
|
||||
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS buildings (
|
||||
key TEXT PRIMARY KEY,
|
||||
@ -119,12 +111,10 @@ game_state: Dict[str, Any] = {
|
||||
building_data = {
|
||||
'residential': { 'cost': 100, 'population': 10 },
|
||||
'commercial': { 'cost': 250, 'income': 5 },
|
||||
'industrial': { 'cost': 500, 'income': 20, 'happiness_impact': -0.02 },
|
||||
'park': { 'cost': 80, 'population_bonus': 5, 'happiness_impact': 0.01 },
|
||||
'industrial': { 'cost': 500, 'income': 20 },
|
||||
'park': { 'cost': 80, 'population_bonus': 5 },
|
||||
'powerplant': { 'cost': 1000, 'income': 50 },
|
||||
'road': { 'cost': 20 },
|
||||
'police': { 'cost': 600, 'happiness_bonus': 0.1 },
|
||||
'stadium': {'cost': 5000, 'income': 150, 'happiness_impact': 0.05 }
|
||||
'road': { 'cost': 20 }
|
||||
}
|
||||
|
||||
# --- Game Logic ---
|
||||
@ -150,52 +140,33 @@ async def game_loop():
|
||||
player = game_state["players"][nickname]
|
||||
income = 0
|
||||
population = 0
|
||||
base_happiness = 0.5 # Start with a neutral base happiness
|
||||
|
||||
player_buildings = {k: v for k, v in game_state["buildings"].items() if v["owner"] == nickname}
|
||||
|
||||
# Calculate base income, population, and happiness impacts
|
||||
# Calculate base income and population
|
||||
for building in player_buildings.values():
|
||||
b_type = building["type"]
|
||||
b_data = building_data.get(b_type, {})
|
||||
|
||||
if b_type == 'residential':
|
||||
population += b_data.get('population', 0)
|
||||
elif 'income' in b_data:
|
||||
if b_type == 'commercial':
|
||||
# Commercial income is modified by happiness
|
||||
happiness_multiplier = max(0.1, player.get('happiness', 0.5))
|
||||
income += b_data['income'] * (1 + happiness_multiplier)
|
||||
else:
|
||||
income += b_data.get('income', 0)
|
||||
population += building_data['residential']['population']
|
||||
elif b_type == 'commercial':
|
||||
income += building_data['commercial']['income']
|
||||
elif b_type == 'industrial':
|
||||
income += building_data['industrial']['income']
|
||||
elif b_type == 'powerplant':
|
||||
income += building_data['powerplant']['income']
|
||||
|
||||
if 'happiness_impact' in b_data:
|
||||
base_happiness += b_data['happiness_impact']
|
||||
|
||||
# Calculate adjacency bonuses
|
||||
# Calculate park adjacency bonuses
|
||||
parks = {k: v for k, v in player_buildings.items() if v["type"] == 'park'}
|
||||
police_stations = {k: v for k, v in player_buildings.items() if v["type"] == 'police'}
|
||||
|
||||
for park_key in parks:
|
||||
for neighbor_key in get_neighbors(park_key):
|
||||
neighbor = player_buildings.get(neighbor_key)
|
||||
if neighbor and neighbor["type"] == 'residential':
|
||||
population += building_data['park']['population_bonus']
|
||||
|
||||
for police_key in police_stations:
|
||||
for neighbor_key in get_neighbors(police_key):
|
||||
neighbor = player_buildings.get(neighbor_key)
|
||||
if neighbor and neighbor["type"] == 'residential':
|
||||
base_happiness += building_data['police']['happiness_bonus'] / 4 # Distribute bonus over 4 neighbors
|
||||
|
||||
# Finalize and clamp values
|
||||
final_happiness = max(0, min(1, base_happiness))
|
||||
|
||||
player["money"] += income
|
||||
player["population"] = population
|
||||
player["happiness"] = final_happiness
|
||||
|
||||
db_execute("UPDATE players SET money = ?, population = ?, happiness = ? WHERE nickname = ?", (player["money"], player["population"], player["happiness"], nickname))
|
||||
db_execute("UPDATE players SET money = ?, population = ? WHERE nickname = ?", (player["money"], player["population"], nickname))
|
||||
|
||||
if game_state["players"]:
|
||||
await manager.broadcast(json.dumps({
|
||||
@ -216,15 +187,14 @@ async def on_startup():
|
||||
async def websocket_endpoint(websocket: WebSocket, nickname: str):
|
||||
await manager.connect(websocket, nickname)
|
||||
|
||||
player_data = db_fetchone("SELECT money, population, happiness FROM players WHERE nickname = ?", (nickname,))
|
||||
player_data = db_fetchone("SELECT money, population FROM players WHERE nickname = ?", (nickname,))
|
||||
if player_data:
|
||||
game_state["players"][nickname] = { "money": player_data["money"], "population": player_data["population"], "happiness": player_data["happiness"] }
|
||||
game_state["players"][nickname] = { "money": player_data["money"], "population": player_data["population"] }
|
||||
else:
|
||||
initial_money = 1500
|
||||
initial_pop = 0
|
||||
initial_happiness = 0.5
|
||||
db_execute("INSERT INTO players (nickname, money, population, happiness) VALUES (?, ?, ?, ?)", (nickname, initial_money, initial_pop, initial_happiness))
|
||||
game_state["players"][nickname] = {"money": initial_money, "population": initial_pop, "happiness": initial_happiness}
|
||||
db_execute("INSERT INTO players (nickname, money, population) VALUES (?, ?, ?)", (nickname, initial_money, initial_pop))
|
||||
game_state["players"][nickname] = {"money": initial_money, "population": initial_pop}
|
||||
|
||||
await websocket.send_text(json.dumps({ "type": "full_state", "buildings": game_state["buildings"] }))
|
||||
await manager.broadcast(json.dumps({ "type": "status_update", "message": f"'{nickname}' has joined the game!" }))
|
||||
|
||||
42
index.html
42
index.html
@ -24,16 +24,16 @@
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
#title-card { top: 20px; left: 50%; transform: translateX(-50%); padding: 8px 16px; font-size: 1.25rem; font-weight: bold; }
|
||||
#info-card { top: 20px; right: 20px; max-width: 250px; font-size: 0.875rem; }
|
||||
#info-card h3, #stats-panel h3, #players-panel h3 { font-weight: bold; font-size: 1.125rem; margin-top: 0; margin-bottom: 8px; color: white; }
|
||||
#info-card strong { color: #63b3ed; }
|
||||
#stats-panel { top: 20px; left: 20px; font-size: 1rem; min-width: 240px; }
|
||||
#stats-panel { top: 80px; left: 20px; font-size: 1rem; min-width: 240px; }
|
||||
#stats-panel div { margin-bottom: 4px; }
|
||||
#stats-panel span { font-weight: bold; }
|
||||
#money-stat { color: #48bb78; }
|
||||
#population-stat { color: #63b3ed; }
|
||||
#happiness-stat { color: #f6e05e; }
|
||||
#players-panel { top: 220px; left: 20px; max-width: 240px; }
|
||||
#players-panel { top: 200px; left: 20px; max-width: 240px; }
|
||||
#players-list .player-div { padding: 4px; font-size: 0.875rem; }
|
||||
#players-list .player-div.is-self { background-color: rgba(66, 153, 225, 0.5); border-radius: 4px; }
|
||||
#players-list .player-div strong { font-weight: bold; }
|
||||
@ -82,6 +82,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id="game-container" class="hidden">
|
||||
<div id="title-card" class="ui-panel">Tiny Tycoon 3D</div>
|
||||
<div id="info-card" class="ui-panel">
|
||||
<h3>Controls</h3>
|
||||
<p><strong>Left-Click + Drag:</strong> Rotate</p>
|
||||
@ -92,7 +93,6 @@
|
||||
<h3>My City</h3>
|
||||
<div>💰 Money: <span id="money-stat"></span></div>
|
||||
<div>👥 Population: <span id="population-stat"></span></div>
|
||||
<div>😊 Happiness: <span id="happiness-stat"></span></div>
|
||||
</div>
|
||||
<div id="players-panel" class="ui-panel">
|
||||
<h3>Players Online</h3>
|
||||
@ -105,8 +105,6 @@
|
||||
<button class="build-btn" data-type="industrial" title="Cost: $500"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 22h16"/><path d="M6 18V8.103a2 2 0 0 1 .91-1.657l6-3.79a2 2 0 0 1 2.18 0l6 3.79A2 2 0 0 1 22 8.103V18"/><path d="m14 14-2-1-2 1"/><path d="M18 18h-4v-2h4v2Z"/></svg>Factory</button>
|
||||
<button class="build-btn" data-type="park" title="Cost: $80"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22v-8"/><path d="m17 8-1.5-1.5c-.9-.9-2.5-.9-3.4 0L12 6.6c-.9-.9-2.5-.9-3.4 0L7 8"/><path d="M12 22h-1a2 2 0 0 1-2-2v-4a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v4a2 2 0 0 1-2 2h-1Z"/></svg>Park</button>
|
||||
<button class="build-btn" data-type="powerplant" title="Cost: $1000"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 12v3"/><path d="M16.24 7.76 14.12 9.88"/><path d="m7.76 7.76 2.12 2.12"/><path d="m12 2 3.5 3.5"/><path d="m12 2-3.5 3.5"/><path d="M22 12h-3"/><path d="M5 12H2"/><path d="M19.07 19.07 16.95 16.95"/><path d="m4.93 19.07 2.12-2.12"/><path d="M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20Z"/></svg>Power</button>
|
||||
<button class="build-btn" data-type="police" title="Cost: $600"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path></svg>Police</button>
|
||||
<button class="build-btn" data-type="stadium" title="Cost: $5000"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 8c-2.209 0-4 1.791-4 4s1.791 4 4 4 4-1.791 4-4-1.791-4-4-4z"/><path d="M22 12c0-5.523-4.477-10-10-10S2 6.477 2 12s4.477 10 10 10 10-4.477 10-10z"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="M22 12h-2"/><path d="M4 12H2"/><path d="M19.071 4.929l-1.414 1.414"/><path d="M6.343 17.657l-1.414 1.414"/><path d="M19.071 19.071l-1.414-1.414"/><path d="M6.343 6.343l-1.414-1.414"/></svg>Stadium</button>
|
||||
<button class="build-btn" data-type="road" title="Cost: $20"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 12h16"/><path d="M4 12v-2a4 4 0 0 1 4-4h8a4 4 0 0 1 4 4v2"/><path d="M4 12v2a4 4 0 0 0 4 4h8a4 4 0 0 0 4-4v-2"/></svg>Road</button>
|
||||
<button class="build-btn" data-type="remove" title="Refund: 50%"><svg viewBox="0 0 24 24" fill="none" stroke="#fca5a5" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 4H8l-7 8 7 8h13a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2Z"></path><line x1="18" x2="12" y1="9" y2="15"></line><line x1="12" x2="18" y1="9" y2="15"></line></svg>Remove</button>
|
||||
</div>
|
||||
@ -132,8 +130,7 @@
|
||||
const buildingData = {
|
||||
'residential': { cost: 100 }, 'commercial': { cost: 250 },
|
||||
'industrial': { cost: 500 }, 'park': { cost: 80 },
|
||||
'powerplant': { cost: 1000 }, 'road': { cost: 20 },
|
||||
'police': { cost: 600 }, 'stadium': { cost: 5000 }
|
||||
'powerplant': { cost: 1000 }, 'road': { cost: 20 }
|
||||
};
|
||||
const nicknameModal = document.getElementById('nickname-modal');
|
||||
const nicknameInput = document.getElementById('nickname-input');
|
||||
@ -195,11 +192,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
function animate() {
|
||||
requestAnimationFrame(animate);
|
||||
controls.update();
|
||||
renderer.render(scene, camera);
|
||||
}
|
||||
function animate() { requestAnimationFrame(animate); controls.update(); renderer.render(scene, camera); }
|
||||
|
||||
// --- WebSocket Logic ---
|
||||
function connectWebSocket() {
|
||||
@ -254,8 +247,6 @@
|
||||
case 'industrial': mainMaterial.color.set(0xa0aec0); break;
|
||||
case 'park': mainMaterial.color.set(0x48bb78); break;
|
||||
case 'powerplant': mainMaterial.color.set(0xf56565); break;
|
||||
case 'police': mainMaterial.color.set(0x4299e1); break;
|
||||
case 'stadium': mainMaterial.color.set(0xed8936); break;
|
||||
default: mainMaterial.color.set(0xffffff); break;
|
||||
}
|
||||
}
|
||||
@ -307,22 +298,6 @@
|
||||
roadSurface.position.y = 0.05; buildingGroup.add(roadSurface);
|
||||
break;
|
||||
}
|
||||
case 'police': {
|
||||
const base = new THREE.Mesh(new THREE.BoxGeometry(gridCellSize * 0.8, 2, gridCellSize * 0.8), new THREE.MeshLambertMaterial({color: 0xedf2f7}));
|
||||
const roof = new THREE.Mesh(new THREE.BoxGeometry(gridCellSize * 0.9, 0.3, gridCellSize * 0.9), mainMaterial);
|
||||
base.position.y = 1;
|
||||
roof.position.y = 2.15;
|
||||
buildingGroup.add(base, roof);
|
||||
break;
|
||||
}
|
||||
case 'stadium': {
|
||||
const base = new THREE.Mesh(new THREE.CylinderGeometry(gridCellSize * 0.8, gridCellSize, 3, 32), mainMaterial);
|
||||
const field = new THREE.Mesh(new THREE.CylinderGeometry(gridCellSize * 0.5, gridCellSize * 0.6, 0.1, 32), new THREE.MeshLambertMaterial({color: 0x48bb78}));
|
||||
base.position.y = 1.5;
|
||||
field.position.y = 3.05;
|
||||
buildingGroup.add(base, field);
|
||||
break;
|
||||
}
|
||||
}
|
||||
buildingGroup.position.set(position.x, 0, position.z);
|
||||
buildingGroup.traverse(c => { if(c.isMesh) c.castShadow = true; });
|
||||
@ -344,7 +319,6 @@
|
||||
document.querySelectorAll('.build-btn').forEach(button => {
|
||||
button.addEventListener('click', () => {
|
||||
const type = button.dataset.type;
|
||||
|
||||
currentBuildType = (currentBuildType === type) ? null : type;
|
||||
document.querySelectorAll('.build-btn').forEach(btn => btn.classList.toggle('active', btn === button && currentBuildType !== null));
|
||||
});
|
||||
@ -367,13 +341,12 @@
|
||||
const playerDiv = document.createElement('div');
|
||||
playerDiv.className = 'player-div';
|
||||
if(name === nickname) playerDiv.classList.add('is-self');
|
||||
playerDiv.innerHTML = `<strong style="color: #${getPlayerColor(name).toString(16)}">${name}</strong>: $${Math.floor(p.money)} | Pop: ${p.population} | Hap: ${Math.round(p.happiness * 100)}%`;
|
||||
playerDiv.innerHTML = `<strong style="color: #${getPlayerColor(name).toString(16)}">${name}</strong>: $${Math.floor(p.money)} | Pop: ${p.population}`;
|
||||
listDiv.appendChild(playerDiv);
|
||||
|
||||
if (name === nickname) {
|
||||
document.getElementById('money-stat').textContent = `$${Math.floor(p.money)}`;
|
||||
document.getElementById('population-stat').textContent = p.population;
|
||||
document.getElementById('happiness-stat').textContent = `${Math.round(p.happiness * 100)}%`;
|
||||
document.querySelectorAll('.build-btn[data-type]').forEach(btn => {
|
||||
const type = btn.dataset.type;
|
||||
if (type !== 'remove' && buildingData[type]) {
|
||||
@ -424,4 +397,3 @@
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user