awacke1 commited on
Commit
7f324ee
·
verified ·
1 Parent(s): ab455b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -229,7 +229,7 @@ async def websocket_handler(websocket, path):
229
  st.session_state.prairie_players[client_id]["animal"] = value
230
  elif action == "MOVE":
231
  target = PRAIRIE_LOCATIONS.get(value, PRAIRIE_LOCATIONS["Deadwood, SD"])
232
- st.session_state.prairie_players[client_id]["location"] = target
233
  action_msg = f"{username} ({st.session_state.prairie_players[client_id]['animal']}) moves to {value}"
234
  await save_chat_entry(username, action_msg, voice)
235
  else:
@@ -254,7 +254,7 @@ async def periodic_update():
254
  await broadcast_message(f"System|{message}", "quest")
255
  await broadcast_message(f"MAP_UPDATE:{player_data}", "quest")
256
 
257
- prairie_list = ", ".join([F"{p['username']} ({p['animal']})" for p in st.session_state.prairie_players.values()]) or "No animals yet!"
258
  prairie_message = f"🌾 Prairie Update: {prairie_list}"
259
  prairie_data = json.dumps(list(st.session_state.prairie_players.values()))
260
  await broadcast_message(f"System|{prairie_message}", "quest")
@@ -403,12 +403,12 @@ prairie_simulator_html = """
403
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
404
  <title>Prairie Simulator</title>
405
  <style>
406
- body { margin: 0; font-family: Arial, sans-serif; background: #f0f0f0; }
407
- #simContainer { width: 100%; height: 600px; position: relative; }
408
- #map { width: 100%; height: 400px; }
409
- #controls { padding: 10px; background: #fff; border-radius: 5px; }
410
- #status { color: #333; padding: 5px; }
411
- #chatBox { height: 100px; overflow-y: auto; background: #fff; border: 1px solid #ccc; padding: 5px; }
412
  </style>
413
  </head>
414
  <body>
@@ -437,65 +437,65 @@ prairie_simulator_html = """
437
  <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
438
  <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
439
  <script>
440
- const playerName = "%s";
441
  let ws = new WebSocket('ws://localhost:8765/prairie');
442
  const map = L.map('map').setView([44.0, -103.0], 7);
443
- L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
444
  attribution: '© OpenStreetMap contributors'
445
- }).addTo(map);
446
 
447
- const locations = {
448
  "Deadwood, SD": [44.3769, -103.7298],
449
  "Wind Cave National Park": [43.6047, -103.4798],
450
  "Wyoming Spring Creek": [41.6666, -106.6666]
451
- };
452
- for (let loc in locations) {
453
  L.marker(locations[loc]).addTo(map).bindPopup(loc);
454
- }
455
 
456
- let players = {};
457
- const markers = {};
458
 
459
- function updatePlayers(playerData) {
460
- playerData.forEach(player => {
461
- if (!markers[player.username]) {
462
- markers[player.username] = L.circleMarker(player.location, {
463
- color: `#${player.color.toString(16).padStart(6, '0')}`,
464
  radius: 5
465
- }).addTo(map).bindPopup(`${player.username} (${player.animal})`);
466
- } else {
467
  markers[player.username].setLatLng(player.location);
468
- }
469
- });
470
- document.getElementById('status').textContent = `Players: ${Object.keys(markers).length}`;
471
- }
472
 
473
- function updateAnimal() {
474
  const animal = document.getElementById('animal').value;
475
- ws.send(`${playerName}|PRAIRIE:ANIMAL:${animal}`);
476
- }
477
 
478
- function move() {
479
  const location = document.getElementById('location').value;
480
- ws.send(`${playerName}|PRAIRIE:MOVE:${location}`);
481
- }
482
 
483
- ws.onmessage = function(event) {
484
  const data = event.data;
485
- if (data.startsWith('PRAIRIE_UPDATE:')) {
486
  const playerData = JSON.parse(data.split('PRAIRIE_UPDATE:')[1]);
487
  updatePlayers(playerData);
488
- } else if (!data.startsWith('MAP_UPDATE:')) {
489
  const [sender, message] = data.split('|');
490
  const chatBox = document.getElementById('chatBox');
491
- chatBox.innerHTML += `<p>${sender}: ${message}</p>`;
492
  chatBox.scrollTop = chatBox.scrollHeight;
493
- }
494
- };
495
  </script>
496
  </body>
497
  </html>
498
- """ % st.session_state.username
499
 
500
  # Main Game Loop
501
  def main():
 
229
  st.session_state.prairie_players[client_id]["animal"] = value
230
  elif action == "MOVE":
231
  target = PRAIRIE_LOCATIONS.get(value, PRAIRIE_LOCATIONS["Deadwood, SD"])
232
+ .ADDRESSst.session_state.prairie_players[client_id]["location"] = target
233
  action_msg = f"{username} ({st.session_state.prairie_players[client_id]['animal']}) moves to {value}"
234
  await save_chat_entry(username, action_msg, voice)
235
  else:
 
254
  await broadcast_message(f"System|{message}", "quest")
255
  await broadcast_message(f"MAP_UPDATE:{player_data}", "quest")
256
 
257
+ prairie_list = ", ".join([f"{p['username']} ({p['animal']})" for p in st.session_state.prairie_players.values()]) or "No animals yet!"
258
  prairie_message = f"🌾 Prairie Update: {prairie_list}"
259
  prairie_data = json.dumps(list(st.session_state.prairie_players.values()))
260
  await broadcast_message(f"System|{prairie_message}", "quest")
 
403
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
404
  <title>Prairie Simulator</title>
405
  <style>
406
+ body {{ margin: 0; font-family: Arial, sans-serif; background: #f0f0f0; }}
407
+ #simContainer {{ width: 100%; height: 600px; position: relative; }}
408
+ #map {{ width: 100%; height: 400px; }}
409
+ #controls {{ padding: 10px; background: #fff; border-radius: 5px; }}
410
+ #status {{ color: #333; padding: 5px; }}
411
+ #chatBox {{ height: 100px; overflow-y: auto; background: #fff; border: 1px solid #ccc; padding: 5px; }}
412
  </style>
413
  </head>
414
  <body>
 
437
  <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
438
  <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
439
  <script>
440
+ const playerName = "{username}";
441
  let ws = new WebSocket('ws://localhost:8765/prairie');
442
  const map = L.map('map').setView([44.0, -103.0], 7);
443
+ L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
444
  attribution: '© OpenStreetMap contributors'
445
+ }}).addTo(map);
446
 
447
+ const locations = {{
448
  "Deadwood, SD": [44.3769, -103.7298],
449
  "Wind Cave National Park": [43.6047, -103.4798],
450
  "Wyoming Spring Creek": [41.6666, -106.6666]
451
+ }};
452
+ for (let loc in locations) {{
453
  L.marker(locations[loc]).addTo(map).bindPopup(loc);
454
+ }}
455
 
456
+ let players = {{}};
457
+ const markers = {{}};
458
 
459
+ function updatePlayers(playerData) {{
460
+ playerData.forEach(player => {{
461
+ if (!markers[player.username]) {{
462
+ markers[player.username] = L.circleMarker(player.location, {{
463
+ color: `#${{player.color.toString(16).padStart(6, '0')}}`,
464
  radius: 5
465
+ }}).addTo(map).bindPopup(`${{player.username}} (${{player.animal}})`);
466
+ }} else {{
467
  markers[player.username].setLatLng(player.location);
468
+ }}
469
+ }});
470
+ document.getElementById('status').textContent = `Players: ${{Object.keys(markers).length}}`;
471
+ }}
472
 
473
+ function updateAnimal() {{
474
  const animal = document.getElementById('animal').value;
475
+ ws.send(`${{playerName}}|PRAIRIE:ANIMAL:${{animal}}`);
476
+ }}
477
 
478
+ function move() {{
479
  const location = document.getElementById('location').value;
480
+ ws.send(`${{playerName}}|PRAIRIE:MOVE:${{location}}`);
481
+ }}
482
 
483
+ ws.onmessage = function(event) {{
484
  const data = event.data;
485
+ if (data.startsWith('PRAIRIE_UPDATE:')) {{
486
  const playerData = JSON.parse(data.split('PRAIRIE_UPDATE:')[1]);
487
  updatePlayers(playerData);
488
+ }} else if (!data.startsWith('MAP_UPDATE:')) {{
489
  const [sender, message] = data.split('|');
490
  const chatBox = document.getElementById('chatBox');
491
+ chatBox.innerHTML += `<p>${{sender}}: ${{message}}</p>`;
492
  chatBox.scrollTop = chatBox.scrollHeight;
493
+ }}
494
+ }};
495
  </script>
496
  </body>
497
  </html>
498
+ """.format(username=st.session_state.username)
499
 
500
  # Main Game Loop
501
  def main():