Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
-
import math
|
| 4 |
from collections import deque, defaultdict
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
-
MAX_RECENT_EVENTS = 1000 #
|
| 8 |
-
GRID_PRECISION = 4 #
|
| 9 |
-
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
# 1.
|
| 13 |
-
# 格式: {'t': time, 'u': user_id, 'l': [lat, lng], 's': speed}
|
| 14 |
recent_events = deque()
|
| 15 |
|
| 16 |
-
# 2.
|
| 17 |
-
# Key: (
|
| 18 |
-
archived_grid = defaultdict(
|
| 19 |
|
| 20 |
-
# 3.
|
| 21 |
snapshot_version = 0
|
| 22 |
last_snapshot_time = time.time()
|
| 23 |
|
| 24 |
-
def
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
-
<
|
| 28 |
-
|
| 29 |
-
> 20 km/h (車輛): 強度 0.1
|
| 30 |
"""
|
| 31 |
-
if speed_kmh is None: return
|
| 32 |
-
if speed_kmh <
|
| 33 |
-
if speed_kmh <
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
def compress_events_to_grid(events_list):
|
| 37 |
-
"""將事件列表壓縮進網格"""
|
| 38 |
-
global archived_grid
|
| 39 |
-
for event in events_list:
|
| 40 |
-
lat, lng = event['l']
|
| 41 |
-
speed = event['s']
|
| 42 |
-
|
| 43 |
-
# 網格化座標
|
| 44 |
-
grid_key = (round(lat, GRID_PRECISION), round(lng, GRID_PRECISION))
|
| 45 |
-
|
| 46 |
-
# 累加亮度
|
| 47 |
-
intensity = get_intensity_by_speed(speed)
|
| 48 |
-
archived_grid[grid_key] += intensity
|
| 49 |
-
|
| 50 |
-
# 限制最大亮度 (防止單點無限疊加)
|
| 51 |
-
if archived_grid[grid_key] > MAX_GRID_INTENSITY:
|
| 52 |
-
archived_grid[grid_key] = MAX_GRID_INTENSITY
|
| 53 |
|
| 54 |
def trigger_snapshot():
|
| 55 |
-
"""
|
| 56 |
global recent_events, snapshot_version, last_snapshot_time
|
| 57 |
|
| 58 |
-
# 如果暫存區超過閾值,將最舊的一半資料壓縮進歷史網格
|
| 59 |
if len(recent_events) > MAX_RECENT_EVENTS:
|
| 60 |
events_to_archive = []
|
| 61 |
count_to_remove = int(MAX_RECENT_EVENTS / 2)
|
|
@@ -63,34 +41,43 @@ def trigger_snapshot():
|
|
| 63 |
for _ in range(count_to_remove):
|
| 64 |
events_to_archive.append(recent_events.popleft())
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
snapshot_version += 1
|
| 69 |
last_snapshot_time = time.time()
|
| 70 |
-
print(f"[Server] Snapshot
|
| 71 |
|
| 72 |
def sync_game_state(client_last_time, client_ver, user_id, lat, lng, speed):
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
"""
|
| 76 |
current_time = time.time()
|
| 77 |
|
| 78 |
-
# 1.
|
| 79 |
if lat is not None and lng is not None:
|
| 80 |
-
#
|
| 81 |
if speed is None or speed < 150:
|
|
|
|
| 82 |
new_event = {
|
| 83 |
"t": current_time,
|
| 84 |
"u": user_id,
|
| 85 |
"l": [lat, lng],
|
| 86 |
-
"
|
| 87 |
}
|
| 88 |
recent_events.append(new_event)
|
| 89 |
|
| 90 |
-
# 2.
|
| 91 |
trigger_snapshot()
|
| 92 |
|
| 93 |
-
# 3.
|
| 94 |
response = {
|
| 95 |
"server_time": current_time,
|
| 96 |
"snapshot_version": snapshot_version,
|
|
@@ -98,49 +85,39 @@ def sync_game_state(client_last_time, client_ver, user_id, lat, lng, speed):
|
|
| 98 |
"payload": []
|
| 99 |
}
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
# if client_ver < snapshot_version:
|
| 104 |
-
if client_ver < 0:
|
| 105 |
response["sync_type"] = "snapshot"
|
| 106 |
-
#
|
| 107 |
response["payload"] = [[k[0], k[1], v] for k, v in archived_grid.items()]
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
response["recent_events"] = list(recent_events)
|
| 111 |
else:
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
delta = [e for e in recent_events if e['t'] > client_last_time]
|
| 115 |
response["payload"] = delta
|
| 116 |
|
| 117 |
return response
|
| 118 |
|
| 119 |
-
# --- Gradio
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
-
gr.Markdown("##
|
| 122 |
-
gr.Markdown("此 Space 為遊戲後端,請使用前端 HTML 連接。")
|
| 123 |
|
| 124 |
-
with gr.Row(visible=False):
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# Outputs
|
| 134 |
-
out_json = gr.JSON(label="Sync Response")
|
| 135 |
|
| 136 |
-
# API 按鈕
|
| 137 |
btn_sync = gr.Button("Sync")
|
| 138 |
btn_sync.click(
|
| 139 |
fn=sync_game_state,
|
| 140 |
inputs=[in_time, in_ver, in_uid, in_lat, in_lng, in_speed],
|
| 141 |
outputs=out_json,
|
| 142 |
-
api_name="sync"
|
| 143 |
)
|
| 144 |
|
| 145 |
-
# 啟動
|
| 146 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
|
|
|
| 3 |
from collections import deque, defaultdict
|
| 4 |
|
| 5 |
+
# --- Configuration ---
|
| 6 |
+
MAX_RECENT_EVENTS = 1000 # Trigger snapshot compression at this count
|
| 7 |
+
GRID_PRECISION = 4 # ~11 meters precision
|
| 8 |
+
RADIUS_BASE = 20 # Visual radius for the fog reveal
|
| 9 |
|
| 10 |
+
# --- Data Structures ---
|
| 11 |
+
# 1. Recent Events (Delta): For fast, smooth updates
|
|
|
|
| 12 |
recent_events = deque()
|
| 13 |
|
| 14 |
+
# 2. Historical Grid (Snapshot): Compressed long-term memory
|
| 15 |
+
# Key: (lat, lng), Value: max_radius (based on speed)
|
| 16 |
+
archived_grid = defaultdict(int)
|
| 17 |
|
| 18 |
+
# 3. Version Control
|
| 19 |
snapshot_version = 0
|
| 20 |
last_snapshot_time = time.time()
|
| 21 |
|
| 22 |
+
def get_radius_by_speed(speed_kmh):
|
| 23 |
"""
|
| 24 |
+
Determine radius of fog revealed based on speed.
|
| 25 |
+
Walking (< 5km/h) = Large radius (High clear)
|
| 26 |
+
Driving (> 20km/h) = Small radius (Low clear)
|
|
|
|
| 27 |
"""
|
| 28 |
+
if speed_kmh is None: return 10
|
| 29 |
+
if speed_kmh < 6: return 35 # Walking: Big reveal
|
| 30 |
+
if speed_kmh < 25: return 15 # Biking: Medium
|
| 31 |
+
return 8 # Driving: Tiny trace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def trigger_snapshot():
|
| 34 |
+
"""Compress recent events into the grid to save bandwidth."""
|
| 35 |
global recent_events, snapshot_version, last_snapshot_time
|
| 36 |
|
|
|
|
| 37 |
if len(recent_events) > MAX_RECENT_EVENTS:
|
| 38 |
events_to_archive = []
|
| 39 |
count_to_remove = int(MAX_RECENT_EVENTS / 2)
|
|
|
|
| 41 |
for _ in range(count_to_remove):
|
| 42 |
events_to_archive.append(recent_events.popleft())
|
| 43 |
|
| 44 |
+
# Bake into grid
|
| 45 |
+
for event in events_to_archive:
|
| 46 |
+
lat, lng = event['l']
|
| 47 |
+
radius = event['r'] # Radius stored in event
|
| 48 |
+
|
| 49 |
+
grid_key = (round(lat, GRID_PRECISION), round(lng, GRID_PRECISION))
|
| 50 |
+
# Keep the largest radius ever recorded for this spot
|
| 51 |
+
if radius > archived_grid[grid_key]:
|
| 52 |
+
archived_grid[grid_key] = radius
|
| 53 |
|
| 54 |
snapshot_version += 1
|
| 55 |
last_snapshot_time = time.time()
|
| 56 |
+
print(f"[Server] Snapshot v{snapshot_version} | Points: {len(archived_grid)}")
|
| 57 |
|
| 58 |
def sync_game_state(client_last_time, client_ver, user_id, lat, lng, speed):
|
| 59 |
"""
|
| 60 |
+
Main API Endpoint
|
| 61 |
"""
|
| 62 |
current_time = time.time()
|
| 63 |
|
| 64 |
+
# 1. Record User Position
|
| 65 |
if lat is not None and lng is not None:
|
| 66 |
+
# Basic filter for bad GPS data
|
| 67 |
if speed is None or speed < 150:
|
| 68 |
+
radius = get_radius_by_speed(speed)
|
| 69 |
new_event = {
|
| 70 |
"t": current_time,
|
| 71 |
"u": user_id,
|
| 72 |
"l": [lat, lng],
|
| 73 |
+
"r": radius
|
| 74 |
}
|
| 75 |
recent_events.append(new_event)
|
| 76 |
|
| 77 |
+
# 2. Check Snapshot Trigger
|
| 78 |
trigger_snapshot()
|
| 79 |
|
| 80 |
+
# 3. Prepare Response
|
| 81 |
response = {
|
| 82 |
"server_time": current_time,
|
| 83 |
"snapshot_version": snapshot_version,
|
|
|
|
| 85 |
"payload": []
|
| 86 |
}
|
| 87 |
|
| 88 |
+
# Logic: Full Snapshot vs Delta
|
| 89 |
+
if client_ver < snapshot_version:
|
|
|
|
|
|
|
| 90 |
response["sync_type"] = "snapshot"
|
| 91 |
+
# Return: [[lat, lng, radius], ...]
|
| 92 |
response["payload"] = [[k[0], k[1], v] for k, v in archived_grid.items()]
|
| 93 |
+
# Also include recent events to prevent gaps
|
| 94 |
+
response["recent_events"] = [[e['l'][0], e['l'][1], e['r']] for e in recent_events]
|
|
|
|
| 95 |
else:
|
| 96 |
+
# Delta: Return only new events [[lat, lng, radius], ...]
|
| 97 |
+
delta = [[e['l'][0], e['l'][1], e['r']] for e in recent_events if e['t'] > client_last_time]
|
|
|
|
| 98 |
response["payload"] = delta
|
| 99 |
|
| 100 |
return response
|
| 101 |
|
| 102 |
+
# --- Gradio App ---
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
+
gr.Markdown("## Fog of War Server")
|
|
|
|
| 105 |
|
| 106 |
+
with gr.Row(visible=False):
|
| 107 |
+
in_time = gr.Number()
|
| 108 |
+
in_ver = gr.Number()
|
| 109 |
+
in_uid = gr.Textbox()
|
| 110 |
+
in_lat = gr.Number()
|
| 111 |
+
in_lng = gr.Number()
|
| 112 |
+
in_speed = gr.Number()
|
| 113 |
+
out_json = gr.JSON()
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
| 115 |
btn_sync = gr.Button("Sync")
|
| 116 |
btn_sync.click(
|
| 117 |
fn=sync_game_state,
|
| 118 |
inputs=[in_time, in_ver, in_uid, in_lat, in_lng, in_speed],
|
| 119 |
outputs=out_json,
|
| 120 |
+
api_name="sync"
|
| 121 |
)
|
| 122 |
|
|
|
|
| 123 |
demo.queue().launch()
|