Spaces:
Sleeping
Sleeping
File size: 22,502 Bytes
faff0b3 75db435 faff0b3 2fe5d08 4b6073f 52c3f76 6fa452e 75db435 a6dadd9 6fa452e 75db435 a408963 75db435 da8aa4f 0b3d989 a408963 1d690f7 f756f2c 54a6ad1 60ab832 1d690f7 faff0b3 a408963 faff0b3 a408963 faff0b3 1d690f7 0b3d989 faff0b3 a408963 faff0b3 0fc62a6 0b3d989 1a9dc52 0b3d989 0fc62a6 58bc76a 51cd910 0fc62a6 a408963 2fe5d08 cd0c391 2fe5d08 a408963 2fe5d08 51cd910 cd0c391 58bc76a cd0c391 2fe5d08 51cd910 0fc62a6 c8b08cf a408963 0fc62a6 60ab832 2fe5d08 60ab832 a408963 60ab832 0fc62a6 60ab832 9d2fb75 a408963 f756f2c a408963 9d2fb75 a408963 60ab832 a408963 6ebe79c 60ab832 2fe5d08 c8b08cf 6ebe79c faff0b3 c8b08cf a408963 2fe5d08 6ebe79c 9d2fb75 2fe5d08 a408963 f756f2c a408963 f756f2c a408963 1a7591a 1d690f7 a408963 f756f2c 2fe5d08 1d690f7 2fe5d08 a408963 75db435 0fc62a6 113e947 a408963 faff0b3 60ab832 a408963 906bdfd 54a6ad1 a408963 2fe5d08 a408963 a6dadd9 a408963 a6dadd9 113e947 51cd910 60ab832 a6dadd9 0fc62a6 113e947 0fc62a6 51cd910 0fc62a6 a408963 0fc62a6 113e947 39c8c16 0fc62a6 faff0b3 a408963 faff0b3 2fe5d08 faff0b3 2fe5d08 faff0b3 c8b08cf 6ebe79c 0fc62a6 51cd910 a408963 51cd910 a408963 0fc62a6 a408963 0fc62a6 b7ccbc9 51cd910 2fe5d08 a408963 2fe5d08 a408963 faff0b3 a408963 1a7591a 60ab832 2fe5d08 a408963 2fe5d08 a408963 2fe5d08 6ebe79c 1d690f7 04cd2e8 51cd910 a408963 0fc62a6 60ab832 04cd2e8 a408963 04cd2e8 6fa452e 0fc62a6 f756f2c 0fc62a6 a408963 0fc62a6 60ab832 c8b08cf 60ab832 a408963 60ab832 a408963 60ab832 a408963 60ab832 0fc62a6 2fe5d08 0fc62a6 a408963 f756f2c a408963 2fe5d08 a408963 f756f2c a408963 f756f2c a408963 0fc62a6 b7ccbc9 a408963 0fc62a6 2fe5d08 0fc62a6 f756f2c 51cd910 f756f2c 58bc76a 2fe5d08 0fc62a6 51cd910 a408963 f756f2c 51cd910 0fc62a6 51cd910 a408963 b7ccbc9 0fc62a6 2fe5d08 0fc62a6 b7ccbc9 2fe5d08 b7ccbc9 60ab832 0fc62a6 b7ccbc9 2fe5d08 a408963 b7ccbc9 cd0c391 2fe5d08 a408963 faff0b3 a408963 2fe5d08 faff0b3 2fe5d08 faff0b3 0fc62a6 a408963 0fc62a6 2fe5d08 a408963 1d690f7 da8aa4f 6fa452e 75db435 113e947 75db435 0b3d989 113e947 1d690f7 a408963 51cd910 a408963 51cd910 75db435 a408963 75db435 51cd910 9d2fb75 a408963 75db435 0b3d989 51cd910 a408963 277d24b fe269d1 113e947 | 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | from flask import Flask, Response, request, jsonify
import json
import os
import hmac
import hashlib
from urllib.parse import unquote
app = Flask(__name__)
HOTSPOTS_FILE = 'hotspots.json'
BOT_TOKEN = '6909363967:AAGl58czIt7Vra_V8wWR7MyXkN_ayS27Soo'
def get_all_hotspots():
if not os.path.exists(HOTSPOTS_FILE):
return []
try:
with open(HOTSPOTS_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, FileNotFoundError):
return []
def save_hotspot(new_hotspot):
hotspots = get_all_hotspots()
hotspots.append(new_hotspot)
with open(HOTSPOTS_FILE, 'w', encoding='utf-8') as f:
json.dump(hotspots, f, ensure_ascii=False, indent=4)
def is_data_safe(init_data: str) -> (bool, dict):
try:
encoded_data = unquote(init_data)
data_check_string = []
recieved_hash = ''
for pair in encoded_data.split('&'):
key, value = pair.split('=', 1)
if key == 'hash':
recieved_hash = value
else:
data_check_string.append(f"{key}={value}")
data_check_string.sort()
data_check_string = "\n".join(data_check_string)
secret_key = hmac.new("WebAppData".encode(), BOT_TOKEN.encode(), hashlib.sha256).digest()
calculated_hash = hmac.new(secret_key, data_check_string.encode(), hashlib.sha256).hexdigest()
if calculated_hash == recieved_hash:
data = {k: v for k, v in [pair.split('=', 1) for pair in encoded_data.split('&')]}
user_data = json.loads(unquote(data['user']))
return True, user_data
return False, None
except Exception:
return False, None
@app.route('/')
def index():
html_content = '''
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>TON AR Hotspots</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover">
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<style>
:root {
--tg-bg-color: var(--tg-theme-bg-color, #000);
--tg-text-color: var(--tg-theme-text-color, #fff);
--tg-hint-color: var(--tg-theme-hint-color, #aaa);
--tg-button-color: var(--tg-theme-button-color, #007aff);
--tg-button-text-color: var(--tg-theme-button-text-color, #fff);
}
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: var(--tg-bg-color);
color: var(--tg-text-color);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}
#ar-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
#camera-view {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
z-index: 0;
}
.hotspot {
position: absolute;
background-color: rgba(0, 122, 255, 0.85);
color: white;
padding: 10px 15px;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.5);
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
transform: translate(-50%, -50%);
transition: opacity 0.3s, transform 0.1s linear;
white-space: nowrap;
font-size: 16px;
font-weight: 500;
will-change: transform, left, top, opacity;
z-index: 10;
text-align: center;
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
}
.hotspot small {
font-size: 0.75em;
opacity: 0.85;
display: block;
margin-top: 5px;
font-weight: 400;
}
.hotspot.hidden {
opacity: 0;
pointer-events: none;
}
#map-container {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 220px;
height: 150px;
border-radius: 15px;
overflow: hidden;
transition: all 0.3s ease-in-out;
z-index: 50;
border: 1px solid var(--tg-hint-color);
box-shadow: 0 5px 25px rgba(0,0,0,0.5);
}
#map-container.fullscreen {
width: 100vw;
height: 100vh;
top: 0;
left: 0;
bottom: 0;
border-radius: 0;
transform: none;
border: none;
}
#map {
width: 100%;
height: 100%;
}
.leaflet-control-container .leaflet-control-attribution {
display: none;
}
#welcome-message {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background-color: rgba(0,0,0,0.6);
padding: 8px 15px;
border-radius: 20px;
z-index: 51;
font-size: 14px;
backdrop-filter: blur(5px);
}
</style>
</head>
<body>
<div id="welcome-message"></div>
<video id="camera-view" playsinline autoplay muted></video>
<div id="ar-container"></div>
<div id="map-container">
<div id="map"></div>
</div>
<script>
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
const state = {
hotspots: [],
currentUserPosition: null,
deviceOrientation: { alpha: 0, beta: 0, gamma: 0 },
smoothedAlpha: 0,
smoothingFactor: 0.05,
cameraFov: 60,
map: null,
userMarker: null,
hotspotMarkers: [],
initialMapSet: false,
userInfo: tg.initDataUnsafe.user || {}
};
const MAX_VISIBLE_DISTANCE = 1000;
function formatUserName(user) {
if (!user) return "Аноним";
let name = user.first_name || '';
if (user.last_name) name += ` ${user.last_name}`;
if (user.username) name += ` (@${user.username})`;
return name.trim();
}
document.getElementById('welcome-message').innerText = `Привет, ${state.userInfo.first_name || 'пользователь'}!`;
async function initArApp() {
await setupCamera();
setupGPS();
setupOrientationListener();
initMap();
await loadHotspots();
setupAddHotspotListener();
requestAnimationFrame(update);
}
async function setupCamera() {
const video = document.getElementById('camera-view');
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: 'environment' }
});
video.srcObject = stream;
await video.play();
} catch (err) {
tg.showAlert('Не удалось получить доступ к камере: ' + err.message);
}
}
}
function setupGPS() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(
(position) => {
state.currentUserPosition = {
lat: position.coords.latitude,
lon: position.coords.longitude
};
if (state.userMarker) {
state.userMarker.setLatLng([state.currentUserPosition.lat, state.currentUserPosition.lon]);
if (!state.initialMapSet) {
state.map.setView([state.currentUserPosition.lat, state.currentUserPosition.lon], 16);
state.initialMapSet = true;
}
}
},
(error) => {
tg.showAlert('Не удалось получить доступ к GPS: ' + error.message);
},
{ enableHighAccuracy: true, maximumAge: 0, timeout: 5000 }
);
} else {
tg.showAlert('GPS не поддерживается вашим устройством.');
}
}
function setupOrientationListener() {
const handleOrientation = (event) => {
if (event.alpha !== null) {
state.deviceOrientation.alpha = event.alpha;
state.deviceOrientation.beta = event.beta;
state.deviceOrientation.gamma = event.gamma;
let alpha = event.webkitCompassHeading || event.alpha;
let diff = alpha - state.smoothedAlpha;
if (diff > 180) diff -= 360;
if (diff < -180) diff += 360;
state.smoothedAlpha += state.smoothingFactor * diff;
state.smoothedAlpha = (state.smoothedAlpha + 360) % 360;
}
};
if (window.DeviceOrientationEvent) {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
window.addEventListener('deviceorientation', handleOrientation, true);
} else {
tg.showAlert('Доступ к ориентации устройства отклонен.');
}
})
.catch(console.error);
} else {
window.addEventListener('deviceorientation', handleOrientation, true);
}
} else {
tg.showAlert('Отслеживание ориентации устройства не поддерживается.');
}
}
function initMap() {
state.map = L.map('map', { zoomControl: false }).setView([0, 0], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(state.map);
var userIcon = L.divIcon({
className: 'user-location-dot',
html: '<div style="background-color: #007aff; width: 12px; height: 12px; border-radius: 50%; border: 2px solid white;"></div>',
iconSize: [16, 16],
iconAnchor: [8, 8]
});
state.userMarker = L.marker([0, 0], { icon: userIcon }).addTo(state.map);
document.getElementById('map-container').addEventListener('click', () => {
const mapContainer = document.getElementById('map-container');
mapContainer.classList.toggle('fullscreen');
setTimeout(() => {
state.map.invalidateSize();
if (state.currentUserPosition) {
state.map.setView([state.currentUserPosition.lat, state.currentUserPosition.lon], state.map.getZoom());
}
}, 310);
});
}
async function loadHotspots() {
try {
const response = await fetch('/hotspots');
const data = await response.json();
state.hotspots = data;
renderHotspots();
renderHotspotsOnMap();
} catch (error) {
tg.showAlert('Ошибка загрузки хотспотов: ' + error.message);
}
}
function renderHotspots() {
const container = document.getElementById('ar-container');
container.innerHTML = '';
state.hotspots.forEach((hotspot, index) => {
const el = document.createElement('div');
el.className = 'hotspot';
el.id = `hotspot-${index}`;
el.innerHTML = `${hotspot.text}<br><small>by ${hotspot.creator_info || 'Unknown'}</small>`;
container.appendChild(el);
});
}
function renderHotspotsOnMap() {
if (!state.map) return;
state.hotspotMarkers.forEach(marker => state.map.removeLayer(marker));
state.hotspotMarkers = [];
state.hotspots.forEach(hotspot => {
const marker = L.marker([hotspot.lat, hotspot.lon])
.addTo(state.map)
.bindPopup(`<b>${hotspot.text}</b><br>by ${hotspot.creator_info || 'Unknown'}`);
state.hotspotMarkers.push(marker);
});
}
function setupAddHotspotListener() {
document.body.addEventListener('dblclick', (event) => {
if (event.target.closest('#map-container')) return;
tg.showPopup({
title: 'Новый хотспот',
message: 'Введите текст для новой AR-метки. Она будет создана в вашем текущем местоположении.',
buttons: [
{id: 'create', type: 'default', text: 'Создать'},
{type: 'cancel'},
]
}, async (buttonId) => {
if (buttonId === 'create') {
const text = prompt("Введите текст для хотспота:");
if (text && text.trim() !== '') {
await createHotspot(text.trim());
}
}
});
});
}
async function createHotspot(text) {
if (!state.currentUserPosition) {
tg.showAlert('GPS-координаты еще не определены. Подождите и попробуйте снова.');
return;
}
const newHotspotData = {
text: text,
lat: state.currentUserPosition.lat,
lon: state.currentUserPosition.lon,
initData: tg.initData
};
try {
const response = await fetch('/hotspots', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newHotspotData)
});
if (response.ok) {
const savedHotspot = await response.json();
state.hotspots.push(savedHotspot.hotspot);
renderHotspots();
renderHotspotsOnMap();
tg.HapticFeedback.notificationOccurred('success');
} else {
const error = await response.json();
tg.showAlert(`Не удалось сохранить хотспот: ${error.error}`);
}
} catch (error) {
tg.showAlert('Сетевая ошибка при сохранении хотспота.');
}
}
function haversineDistance(coords1, coords2) {
function toRad(x) { return x * Math.PI / 180; }
const R = 6371000;
const dLat = toRad(coords2.lat - coords1.lat);
const dLon = toRad(coords2.lon - coords1.lon);
const lat1 = toRad(coords1.lat);
const lat2 = toRad(coords2.lat);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function calculateBearing(start, end) {
function toRad(x) { return x * Math.PI / 180; }
function toDeg(x) { return x * 180 / Math.PI; }
const y = Math.sin(toRad(end.lon - start.lon)) * Math.cos(toRad(end.lat));
const x = Math.cos(toRad(start.lat)) * Math.sin(toRad(end.lat)) -
Math.sin(toRad(start.lat)) * Math.cos(toRad(end.lat)) * Math.cos(toRad(end.lon - start.lon));
let brng = toDeg(Math.atan2(y, x));
return (brng + 360) % 360;
}
function update() {
if (!state.currentUserPosition) {
requestAnimationFrame(update);
return;
}
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
state.hotspots.forEach((hotspot, index) => {
const el = document.getElementById(`hotspot-${index}`);
if (!el) return;
const distance = haversineDistance(state.currentUserPosition, hotspot);
if (distance > MAX_VISIBLE_DISTANCE) {
el.classList.add('hidden');
return;
}
const bearing = calculateBearing(state.currentUserPosition, hotspot);
let angleDiff = bearing - state.smoothedAlpha;
if (angleDiff > 180) angleDiff -= 360;
if (angleDiff < -180) angleDiff += 360;
if (Math.abs(angleDiff) > state.cameraFov / 2) {
el.classList.add('hidden');
} else {
el.classList.remove('hidden');
const x = screenWidth / 2 + (angleDiff / (state.cameraFov / 2)) * (screenWidth / 2);
const verticalAngle = -state.deviceOrientation.beta;
const y = screenHeight / 2 - (Math.tan(verticalAngle * Math.PI / 180) * (screenHeight / 2));
const scale = Math.max(0.4, 1.2 - distance / MAX_VISIBLE_DISTANCE);
el.style.left = `${x}px`;
el.style.top = `${y}px`;
el.style.transform = `translate(-50%, -50%) scale(${scale})`;
el.style.zIndex = Math.round(10000 - distance);
}
});
requestAnimationFrame(update);
}
initArApp();
</script>
</body>
</html>
'''
return Response(html_content, mimetype='text/html')
@app.route('/hotspots', methods=['GET', 'POST'])
def handle_hotspots():
if request.method == 'GET':
return jsonify(get_all_hotspots())
if request.method == 'POST':
if not request.is_json:
return jsonify({"error": "Missing JSON in request"}), 400
data = request.get_json()
init_data = data.get('initData')
if not init_data:
return jsonify({"error": "Missing initData"}), 401
is_safe, user_data = is_data_safe(init_data)
if not is_safe:
return jsonify({"error": "Validation failed"}), 403
text = data.get('text')
lat = data.get('lat')
lon = data.get('lon')
if not all([text, lat, lon]):
return jsonify({"error": "Missing data: text, lat, or lon"}), 400
try:
creator_info = user_data.get('first_name', 'User')
if user_data.get('username'):
creator_info = f"{creator_info} (@{user_data.get('username')})"
new_hotspot = {
"text": str(text),
"lat": float(lat),
"lon": float(lon),
"creator_info": creator_info,
"creator_id": user_data.get('id')
}
save_hotspot(new_hotspot)
return jsonify({"success": True, "hotspot": new_hotspot}), 201
except (ValueError, TypeError):
return jsonify({"error": "Invalid data types"}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=False) |