[Enhance] API for redeem

- Add API endpoints for redeem system
- Continue to fix the bug mentioned in 930faf508d
This commit is contained in:
Lost-MSth
2023-02-28 18:28:09 +08:00
parent 930faf508d
commit a84ec560dd
15 changed files with 283 additions and 50 deletions

View File

@@ -15,7 +15,7 @@ class Config:
--------------------------------------------------
'''
IS_DEBUG = False
DEBUG = False
TIME_LIMIT = 3600000

View File

@@ -30,9 +30,9 @@ class UDP_handler(socketserver.BaseRequestHandler):
logging.error(e)
return None
if Config.IS_DEBUG:
logging.info(
f'UDP-From-{self.client_address[0]}-{binascii.b2a_hex(plaintext)}')
# if Config.DEBUG:
# logging.info(
# f'UDP-From-{self.client_address[0]}-{binascii.b2a_hex(plaintext)}')
commands = CommandParser(
user['room'], user['player_index']).get_commands(plaintext)
@@ -46,9 +46,9 @@ class UDP_handler(socketserver.BaseRequestHandler):
for i in commands:
iv, ciphertext, tag = encrypt(user['key'], i, b'')
if Config.IS_DEBUG:
logging.info(
f'UDP-To-{self.client_address[0]}-{binascii.b2a_hex(i)}')
# if Config.DEBUG:
# logging.info(
# f'UDP-To-{self.client_address[0]}-{binascii.b2a_hex(i)}')
server.sendto(token + iv + tag[:12] +
ciphertext, self.client_address)
@@ -59,15 +59,18 @@ class TCP_handler(socketserver.StreamRequestHandler):
self.data = self.rfile.readline().strip()
message = self.data.decode('utf-8')
# print(message)
if Config.DEBUG:
logging.info(f'TCP-From-{self.client_address[0]}-{message}')
data = message.split('|')
if data[0] != Config.AUTHENTICATION:
self.wfile.write(b'No authentication')
logging.warning('TCP-%s-No authentication' %
self.client_address[0])
logging.warning(f'TCP-{self.client_address[0]}-No authentication')
return None
self.wfile.write(TCPRouter(data[1:]).handle().encode('utf-8'))
r = TCPRouter(data[1:]).handle()
if Config.DEBUG:
logging.info(f'TCP-To-{self.client_address[0]}-{r}')
self.wfile.write(r.encode('utf-8'))
def link_play(ip: str = Config.HOST, udp_port: int = Config.UDP_PORT, tcp_port: int = Config.TCP_PORT):

View File

@@ -94,7 +94,7 @@ class TCPRouter:
self.data = data # data: list[str] = [command, ...]
def debug(self):
if Config.IS_DEBUG:
if Config.DEBUG:
return eval(self.data[1])
return 'ok'
@@ -186,9 +186,13 @@ class TCPRouter:
return '1202'
room: Room = Store.room_code_dict[room_code]
if room.player_num == 4:
player_num = room.player_num
if player_num == 4:
# 满人
return '1201'
elif player_num == 0:
# 房间不存在
return '1202'
elif room.state != 2:
# 无法加入
return '1205'

View File

@@ -1,3 +1,5 @@
from time import time
from .config import Config
@@ -79,8 +81,27 @@ class Room:
@property
def player_num(self) -> int:
self.check_player_online()
return sum(i.player_id != 0 for i in self.players)
def check_player_online(self, now: int = None):
# 检测玩家是否被自动踢出房间 / 离线判断
now = round(time() * 1000000) if now is None else now
flag = False
player_index_list = []
for i, x in enumerate(self.players):
if x.player_id == 0 or x.last_timestamp == 0:
continue
if now - x.last_timestamp >= Config.PLAYER_TIMEOUT:
self.delete_player(i)
flag = True
player_index_list.append(i)
elif x.online == 1 and now - x.last_timestamp >= Config.PLAYER_PRE_TIMEOUT:
x.online = 0
player_index_list.append(i)
return flag, player_index_list
def get_players_info(self):
# 获取所有玩家信息
re = b''

View File

@@ -142,22 +142,11 @@ class CommandParser:
re.append(self.s.command_0c())
player.last_timestamp = self.s.timestamp
flag_13 = False
# 离线判断
for i in range(4):
if i != self.player_index:
t = self.room.players[i]
if t.player_id != 0:
if t.last_timestamp != 0:
if t.online == 1 and self.s.timestamp - t.last_timestamp >= Config.PLAYER_PRE_TIMEOUT:
t.online = 0
self.room.command_queue.append(
self.s.command_12(i))
elif t.online == 0 and self.s.timestamp - t.last_timestamp >= Config.PLAYER_TIMEOUT:
self.room.delete_player(i)
self.room.command_queue.append(
self.s.command_12(i))
flag_13 = True
flag_13, player_index_list = self.room.check_player_online(
self.s.timestamp)
for i in player_index_list:
self.room.command_queue.append(self.s.command_12(i))
flag_11 = False
flag_12 = False