Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
from flask_socketio import SocketIO, emit
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
app.config['SECRET_KEY'] = 'marauders_map_secret!'
|
| 8 |
+
socketio = SocketIO(app, cors_allowed_origins="*")
|
| 9 |
+
|
| 10 |
+
# 存储用户位置信息的字典
|
| 11 |
+
# 结构: {
|
| 12 |
+
# 'username': {
|
| 13 |
+
# 'location': {'lat': float, 'lng': float},
|
| 14 |
+
# 'sid': string,
|
| 15 |
+
# 'last_update': datetime string,
|
| 16 |
+
# 'footprints': list # 存储最近的脚印位置
|
| 17 |
+
# }
|
| 18 |
+
# }
|
| 19 |
+
users = {}
|
| 20 |
+
|
| 21 |
+
@app.route('/')
|
| 22 |
+
def index():
|
| 23 |
+
return render_template('index.html')
|
| 24 |
+
|
| 25 |
+
@socketio.on('connect')
|
| 26 |
+
def handle_connect():
|
| 27 |
+
print(f'Client connected: {request.sid}')
|
| 28 |
+
|
| 29 |
+
@socketio.on('disconnect')
|
| 30 |
+
def handle_disconnect():
|
| 31 |
+
user_sid = request.sid
|
| 32 |
+
disconnected_user = None
|
| 33 |
+
|
| 34 |
+
# 查找断开连接的用户
|
| 35 |
+
for username, data in users.items():
|
| 36 |
+
if data['sid'] == user_sid:
|
| 37 |
+
disconnected_user = username
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
# 如果找到断开连接的用户,从用户字典中移除
|
| 41 |
+
if disconnected_user:
|
| 42 |
+
del users[disconnected_user]
|
| 43 |
+
print(f'User disconnected: {disconnected_user}')
|
| 44 |
+
# 广播用户断开连接的消息
|
| 45 |
+
emit('user_disconnected', {'username': disconnected_user}, broadcast=True)
|
| 46 |
+
|
| 47 |
+
@socketio.on('update_location')
|
| 48 |
+
def handle_location_update(data):
|
| 49 |
+
username = data['username']
|
| 50 |
+
location = data['location']
|
| 51 |
+
|
| 52 |
+
# 更新用户位置信息
|
| 53 |
+
if username not in users:
|
| 54 |
+
users[username] = {
|
| 55 |
+
'location': location,
|
| 56 |
+
'sid': request.sid,
|
| 57 |
+
'last_update': datetime.now().isoformat(),
|
| 58 |
+
'footprints': []
|
| 59 |
+
}
|
| 60 |
+
else:
|
| 61 |
+
users[username].update({
|
| 62 |
+
'location': location,
|
| 63 |
+
'last_update': datetime.now().isoformat()
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
# 添加新的脚印到用户的脚印列表
|
| 67 |
+
users[username]['footprints'].append(location)
|
| 68 |
+
# 只保留最近的20个脚印
|
| 69 |
+
if len(users[username]['footprints']) > 20:
|
| 70 |
+
users[username]['footprints'] = users[username]['footprints'][-20:]
|
| 71 |
+
|
| 72 |
+
# 广播更新后的用户信息
|
| 73 |
+
emit('users_update', users, broadcast=True)
|
| 74 |
+
|
| 75 |
+
@socketio.on('update_username')
|
| 76 |
+
def handle_username_update(data):
|
| 77 |
+
old_username = data['old_username']
|
| 78 |
+
new_username = data['new_username']
|
| 79 |
+
|
| 80 |
+
# 如果旧用户名存在,更新为新用户名
|
| 81 |
+
if old_username in users:
|
| 82 |
+
users[new_username] = users.pop(old_username)
|
| 83 |
+
print(f'Username updated: {old_username} -> {new_username}')
|
| 84 |
+
# 广播用户名更新信息
|
| 85 |
+
emit('users_update', users, broadcast=True)
|
| 86 |
+
|
| 87 |
+
@socketio.on('error')
|
| 88 |
+
def handle_error(error):
|
| 89 |
+
print(f'Error occurred: {error}')
|
| 90 |
+
|
| 91 |
+
if __name__ == '__main__':
|
| 92 |
+
# 确保static和templates目录存在
|
| 93 |
+
for dir_name in ['static', 'templates']:
|
| 94 |
+
if not os.path.exists(dir_name):
|
| 95 |
+
os.makedirs(dir_name)
|
| 96 |
+
print(f'Created directory: {dir_name}')
|
| 97 |
+
|
| 98 |
+
# 启动服务器
|
| 99 |
+
print('Starting Marauder\'s Map server...')
|
| 100 |
+
socketio.run(app, debug=True, host='0.0.0.0', port=5000)
|