Spaces:
Running
Running
Update index.html
Browse files- index.html +195 -147
index.html
CHANGED
@@ -1,159 +1,207 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
const [tick, setTick] = useState(0);
|
11 |
-
const [researchProgress, setResearchProgress] = useState({ automation: 0, speed: 0 });
|
12 |
-
const [message, setMessage] = useState('');
|
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 |
-
grid.forEach(row => {
|
42 |
-
row.forEach(cell => {
|
43 |
-
if (cell.type && tools[cell.type]) {
|
44 |
-
const produces = tools[cell.type].produces;
|
45 |
-
newResources[produces] = (newResources[produces] || 0) + 1;
|
46 |
-
}
|
47 |
-
});
|
48 |
-
});
|
49 |
-
return newResources;
|
50 |
-
});
|
51 |
-
}, 1000);
|
52 |
-
|
53 |
-
return () => clearInterval(timer);
|
54 |
-
}, [grid]);
|
55 |
-
|
56 |
-
const handleCellClick = (rowIndex, colIndex) => {
|
57 |
-
if (!selectedTool) {
|
58 |
-
setMessage('Select a building first!');
|
59 |
-
return;
|
60 |
-
}
|
61 |
-
|
62 |
-
const tool = tools[selectedTool];
|
63 |
-
const canAfford = Object.entries(tool.cost).every(
|
64 |
-
([resource, cost]) => resources[resource] >= cost
|
65 |
-
);
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
return newResources;
|
79 |
-
});
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
newGrid[rowIndex][colIndex] = {
|
86 |
-
type: selectedTool,
|
87 |
-
color: tool.color
|
88 |
-
};
|
89 |
-
return newGrid;
|
90 |
-
});
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
};
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
</div>
|
105 |
-
<div className="p-3 bg-gray-100 rounded-lg flex items-center gap-2">
|
106 |
-
<Box className="w-4 h-4" />
|
107 |
-
<span>Minerals: {resources.minerals}</span>
|
108 |
-
</div>
|
109 |
-
<div className="p-3 bg-green-100 rounded-lg flex items-center gap-2">
|
110 |
-
<Cpu className="w-4 h-4" />
|
111 |
-
<span>Circuits: {resources.circuits}</span>
|
112 |
-
</div>
|
113 |
-
</div>
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
132 |
</div>
|
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 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from dataclasses import dataclass, asdict
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
@dataclass
|
7 |
+
class Building:
|
8 |
+
type: str
|
9 |
+
color: str
|
10 |
+
cost: dict
|
11 |
+
produces: str
|
|
|
|
|
|
|
12 |
|
13 |
+
class FactoryGame:
|
14 |
+
def __init__(self):
|
15 |
+
self.grid_size = 8
|
16 |
+
self.grid = [[None for _ in range(self.grid_size)] for _ in range(self.grid_size)]
|
17 |
+
self.resources = {"energy": 50, "minerals": 50, "circuits": 0}
|
18 |
+
|
19 |
+
self.buildings = {
|
20 |
+
"solarPanel": Building(
|
21 |
+
type="solarPanel",
|
22 |
+
color="#FEF08A", # yellow-200
|
23 |
+
cost={"minerals": 10},
|
24 |
+
produces="energy"
|
25 |
+
),
|
26 |
+
"mineralExtractor": Building(
|
27 |
+
type="mineralExtractor",
|
28 |
+
color="#D1D5DB", # gray-300
|
29 |
+
cost={"energy": 10},
|
30 |
+
produces="minerals"
|
31 |
+
),
|
32 |
+
"circuitFactory": Building(
|
33 |
+
type="circuitFactory",
|
34 |
+
color="#BBF7D0", # green-200
|
35 |
+
cost={"energy": 15, "minerals": 15},
|
36 |
+
produces="circuits"
|
37 |
+
)
|
38 |
+
}
|
39 |
|
40 |
+
def to_json(self):
|
41 |
+
return json.dumps({
|
42 |
+
"grid": self.grid,
|
43 |
+
"resources": self.resources,
|
44 |
+
"buildings": {k: asdict(v) for k, v in self.buildings.items()}
|
45 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
def can_afford(self, building_type):
|
48 |
+
building = self.buildings[building_type]
|
49 |
+
return all(self.resources.get(resource, 0) >= cost
|
50 |
+
for resource, cost in building.cost.items())
|
51 |
|
52 |
+
def place_building(self, row, col, building_type):
|
53 |
+
if not self.can_afford(building_type):
|
54 |
+
return False, "Not enough resources!"
|
55 |
+
|
56 |
+
if self.grid[row][col] is not None:
|
57 |
+
return False, "Cell already occupied!"
|
|
|
|
|
58 |
|
59 |
+
# Deduct costs
|
60 |
+
building = self.buildings[building_type]
|
61 |
+
for resource, cost in building.cost.items():
|
62 |
+
self.resources[resource] -= cost
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# Place building
|
65 |
+
self.grid[row][col] = building_type
|
66 |
+
return True, "Building placed successfully!"
|
|
|
67 |
|
68 |
+
def update_resources(self):
|
69 |
+
for row in range(self.grid_size):
|
70 |
+
for col in range(self.grid_size):
|
71 |
+
building_type = self.grid[row][col]
|
72 |
+
if building_type:
|
73 |
+
building = self.buildings[building_type]
|
74 |
+
self.resources[building.produces] = self.resources.get(building.produces, 0) + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
def create_game_html():
|
77 |
+
return """
|
78 |
+
<html>
|
79 |
+
<head>
|
80 |
+
<style>
|
81 |
+
.game-container { max-width: 800px; margin: 0 auto; padding: 1rem; }
|
82 |
+
.resources { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1rem; }
|
83 |
+
.resource { padding: 0.75rem; border-radius: 0.5rem; }
|
84 |
+
.buildings { display: flex; gap: 1rem; margin-bottom: 1rem; }
|
85 |
+
.building { padding: 1rem; border-radius: 0.5rem; cursor: pointer; text-align: center; }
|
86 |
+
.building.selected { outline: 2px solid #3B82F6; }
|
87 |
+
.grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 0.25rem; background: #F3F4F6; padding: 0.5rem; border-radius: 0.5rem; }
|
88 |
+
.cell { aspect-ratio: 1; border-radius: 0.5rem; border: none; cursor: pointer; }
|
89 |
+
.message { text-align: center; color: #2563EB; font-weight: 500; margin: 0.5rem 0; }
|
90 |
+
</style>
|
91 |
+
</head>
|
92 |
+
<body>
|
93 |
+
<div class="game-container">
|
94 |
+
<div class="resources" id="resources"></div>
|
95 |
+
<div class="buildings" id="buildings"></div>
|
96 |
+
<div class="message" id="message"></div>
|
97 |
+
<div class="grid" id="grid"></div>
|
98 |
</div>
|
99 |
+
<script>
|
100 |
+
let game = null;
|
101 |
+
let selectedBuilding = null;
|
102 |
+
|
103 |
+
function updateUI() {
|
104 |
+
if (!game) return;
|
105 |
+
|
106 |
+
// Update resources
|
107 |
+
const resourcesDiv = document.getElementById('resources');
|
108 |
+
resourcesDiv.innerHTML = Object.entries(game.resources)
|
109 |
+
.map(([resource, amount]) => `
|
110 |
+
<div class="resource" style="background: ${getResourceColor(resource)}">
|
111 |
+
${resource}: ${amount}
|
112 |
+
</div>
|
113 |
+
`).join('');
|
114 |
+
|
115 |
+
// Update buildings selection
|
116 |
+
const buildingsDiv = document.getElementById('buildings');
|
117 |
+
buildingsDiv.innerHTML = Object.entries(game.buildings)
|
118 |
+
.map(([type, data]) => `
|
119 |
+
<button class="building ${type === selectedBuilding ? 'selected' : ''}"
|
120 |
+
style="background: ${data.color}"
|
121 |
+
onclick="selectBuilding('${type}')">
|
122 |
+
${type}<br>
|
123 |
+
Cost: ${Object.entries(data.cost).map(([r, c]) => `${c} ${r}`).join(', ')}
|
124 |
+
</button>
|
125 |
+
`).join('');
|
126 |
+
|
127 |
+
// Update grid
|
128 |
+
const gridDiv = document.getElementById('grid');
|
129 |
+
gridDiv.innerHTML = game.grid.map((row, i) =>
|
130 |
+
row.map((cell, j) => `
|
131 |
+
<button class="cell"
|
132 |
+
style="background: ${cell ? game.buildings[cell].color : 'white'}"
|
133 |
+
onclick="placeBuilding(${i}, ${j})">
|
134 |
+
</button>
|
135 |
+
`).join('')
|
136 |
+
).join('');
|
137 |
+
}
|
138 |
+
|
139 |
+
function getResourceColor(resource) {
|
140 |
+
switch(resource) {
|
141 |
+
case 'energy': return '#FEF9C3';
|
142 |
+
case 'minerals': return '#F3F4F6';
|
143 |
+
case 'circuits': return '#DCFCE7';
|
144 |
+
default: return 'white';
|
145 |
+
}
|
146 |
+
}
|
147 |
+
|
148 |
+
function selectBuilding(type) {
|
149 |
+
selectedBuilding = type;
|
150 |
+
updateUI();
|
151 |
+
}
|
152 |
+
|
153 |
+
function placeBuilding(row, col) {
|
154 |
+
if (!selectedBuilding) {
|
155 |
+
document.getElementById('message').textContent = 'Select a building first!';
|
156 |
+
return;
|
157 |
+
}
|
158 |
+
|
159 |
+
gradioCallback([row, col, selectedBuilding]).then(response => {
|
160 |
+
game = JSON.parse(response);
|
161 |
+
updateUI();
|
162 |
+
});
|
163 |
+
}
|
164 |
+
|
165 |
+
function initialize(gameState) {
|
166 |
+
game = JSON.parse(gameState);
|
167 |
+
updateUI();
|
168 |
+
}
|
169 |
+
|
170 |
+
// Resource production tick
|
171 |
+
setInterval(() => {
|
172 |
+
if (game) {
|
173 |
+
gradioCallback(['tick']).then(response => {
|
174 |
+
game = JSON.parse(response);
|
175 |
+
updateUI();
|
176 |
+
});
|
177 |
+
}
|
178 |
+
}, 1000);
|
179 |
+
</script>
|
180 |
+
</body>
|
181 |
+
</html>
|
182 |
+
"""
|
183 |
|
184 |
+
def create_interface():
|
185 |
+
game = FactoryGame()
|
186 |
+
|
187 |
+
def handle_action(action):
|
188 |
+
if isinstance(action, list) and len(action) == 3:
|
189 |
+
row, col, building_type = action
|
190 |
+
game.place_building(row, col, building_type)
|
191 |
+
elif action == 'tick':
|
192 |
+
game.update_resources()
|
193 |
+
return game.to_json()
|
194 |
+
|
195 |
+
with gr.Blocks() as demo:
|
196 |
+
gr.HTML(create_game_html())
|
197 |
+
game_state = gr.State(game.to_json())
|
198 |
+
demo.load(lambda: game.to_json(), None, game_state)
|
199 |
+
demo.load(None, js="initialize(arguments[0])", inputs=[game_state])
|
200 |
+
callback = demo.load(handle_action, inputs=gr.State([]), outputs=game_state)
|
201 |
+
demo.load(None, js=f"window.gradioCallback = {callback}")
|
202 |
+
|
203 |
+
return demo
|
204 |
|
205 |
+
if __name__ == "__main__":
|
206 |
+
interface = create_interface()
|
207 |
+
interface.launch()
|