Spaces:
Running
Running
File size: 6,813 Bytes
c4e4866 aef21d2 c4e4866 aef21d2 de0b2f1 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 de0b2f1 c4e4866 de0b2f1 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 de0b2f1 c4e4866 aef21d2 c4e4866 aef21d2 c4e4866 de0b2f1 c4e4866 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Factory Game</title>
<script type="module" crossorigin src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
</style>
</head>
<body>
<gradio-lite>
<gradio-file name="app.py" entrypoint>
import gradio as gr
import json
from dataclasses import dataclass
import numpy as np
class GameState:
def __init__(self):
self.resources = {
"energy": 50,
"minerals": 50,
"circuits": 0
}
self.grid = [[None for _ in range(8)] for _ in range(8)]
self.buildings = {
"solarPanel": {
"cost": {"minerals": 10},
"produces": "energy",
"color": "#FEF08A"
},
"mineralExtractor": {
"cost": {"energy": 10},
"produces": "minerals",
"color": "#D1D5DB"
},
"circuitFactory": {
"cost": {"energy": 15, "minerals": 15},
"produces": "circuits",
"color": "#BBF7D0"
}
}
game_state = GameState()
def update_game(action_type, data):
if action_type == "tick":
# Update resources based on buildings
for row in range(8):
for col in range(8):
building_type = game_state.grid[row][col]
if building_type:
produces = game_state.buildings[building_type]["produces"]
game_state.resources[produces] += 1
elif action_type == "build":
row, col, building_type = data
if game_state.grid[row][col] is not None:
return {"error": "Cell already occupied"}
building = game_state.buildings[building_type]
# Check if can afford
for resource, cost in building["cost"].items():
if game_state.resources[resource] < cost:
return {"error": "Not enough resources"}
# Pay costs
for resource, cost in building["cost"].items():
game_state.resources[resource] -= cost
# Place building
game_state.grid[row][col] = building_type
return {
"resources": game_state.resources,
"grid": game_state.grid,
"buildings": game_state.buildings
}
def create_ui():
html = """
<div style="max-width: 800px; margin: 0 auto; padding: 1rem;">
<div id="resources" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1rem;"></div>
<div id="buildings" style="display: flex; gap: 1rem; margin-bottom: 1rem;"></div>
<div id="message" style="text-align: center; color: #2563EB; font-weight: 500; margin: 0.5rem 0;"></div>
<div id="grid" style="display: grid; grid-template-columns: repeat(8, 1fr); gap: 0.25rem; background: #F3F4F6; padding: 0.5rem; border-radius: 0.5rem;"></div>
</div>
<script>
let selectedBuilding = null;
function updateUI(gameState) {
// Update resources
document.getElementById('resources').innerHTML = Object.entries(gameState.resources)
.map(([resource, amount]) => `
<div style="padding: 0.75rem; border-radius: 0.5rem; background: ${getResourceColor(resource)}">
${resource}: ${amount}
</div>
`).join('');
// Update buildings
document.getElementById('buildings').innerHTML = Object.entries(gameState.buildings)
.map(([type, data]) => `
<button onclick="selectBuilding('${type}')"
style="padding: 1rem; border-radius: 0.5rem; cursor: pointer; background: ${data.color};
${type === selectedBuilding ? 'outline: 2px solid #3B82F6;' : ''}">
${type}<br>
Cost: ${Object.entries(data.cost).map(([r, c]) => `${c} ${r}`).join(', ')}
</button>
`).join('');
// Update grid
document.getElementById('grid').innerHTML = gameState.grid.map((row, i) =>
row.map((cell, j) => `
<button onclick="placeBuilding(${i}, ${j})"
style="aspect-ratio: 1; border-radius: 0.5rem; border: none; cursor: pointer;
background: ${cell ? gameState.buildings[cell].color : 'white'}">
</button>
`).join('')
).join('');
}
function getResourceColor(resource) {
switch(resource) {
case 'energy': return '#FEF9C3';
case 'minerals': return '#F3F4F6';
case 'circuits': return '#DCFCE7';
default: return 'white';
}
}
function selectBuilding(type) {
selectedBuilding = type;
syncGameState("tick", null);
}
function placeBuilding(row, col) {
if (!selectedBuilding) {
document.getElementById('message').textContent = 'Select a building first!';
return;
}
syncGameState("build", [row, col, selectedBuilding]);
}
// Resource production tick
setInterval(() => syncGameState("tick", null), 1000);
// Initial state
syncGameState("tick", null);
</script>
"""
return gr.HTML(html)
def handle_action(action_type, data):
return json.dumps(update_game(action_type, data))
with gr.Blocks() as demo:
ui = create_ui()
action = gr.State("tick")
data = gr.State(None)
demo.load(lambda: handle_action("tick", None), None, _js="(o) => { updateUI(JSON.parse(o)) }")
demo.load(None, None, _js="""
function(arg) {
window.syncGameState = function(actionType, actionData) {
gradioApp().querySelector("gradio-app").props.action_type = actionType;
gradioApp().querySelector("gradio-app").props.action_data = actionData;
updateUI(JSON.parse(handle_action(actionType, actionData)));
}
}
""")
demo.launch()
</gradio-file>
<gradio-requirements>
# No additional requirements needed
</gradio-requirements>
</gradio-lite>
</body>
</html> |