init: cs player_status CDK demo
This commit is contained in:
BIN
logic/__pycache__/event_detector.cpython-314.pyc
Normal file
BIN
logic/__pycache__/event_detector.cpython-314.pyc
Normal file
Binary file not shown.
BIN
logic/__pycache__/events.cpython-314.pyc
Normal file
BIN
logic/__pycache__/events.cpython-314.pyc
Normal file
Binary file not shown.
BIN
logic/__pycache__/parser.cpython-314.pyc
Normal file
BIN
logic/__pycache__/parser.cpython-314.pyc
Normal file
Binary file not shown.
BIN
logic/__pycache__/state_manager.cpython-314.pyc
Normal file
BIN
logic/__pycache__/state_manager.cpython-314.pyc
Normal file
Binary file not shown.
106
logic/event_detector.py
Normal file
106
logic/event_detector.py
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
from hashlib import new
|
||||||
|
|
||||||
|
from logic import events
|
||||||
|
from logic.events import *
|
||||||
|
|
||||||
|
|
||||||
|
class EventDetector:
|
||||||
|
|
||||||
|
|
||||||
|
def detect(self, old, new):
|
||||||
|
|
||||||
|
events=[]
|
||||||
|
|
||||||
|
old_hp = old.get("health")
|
||||||
|
new_hp = new.get("health")
|
||||||
|
old_flashes = old.get("flashed")
|
||||||
|
new_flashes = new.get("flashed")
|
||||||
|
old_burning = old.get("burning")
|
||||||
|
new_burning = new.get("burning")
|
||||||
|
old_kills = old.get("kills")
|
||||||
|
new_kills = new.get("kills")
|
||||||
|
old_headshots = old.get("headshots")
|
||||||
|
new_headshots = new.get("headshots")
|
||||||
|
|
||||||
|
|
||||||
|
# 死亡
|
||||||
|
if (
|
||||||
|
old_hp > 0
|
||||||
|
and new_hp == 0
|
||||||
|
):
|
||||||
|
events.append(
|
||||||
|
DeathEvent()
|
||||||
|
)
|
||||||
|
|
||||||
|
# 受伤
|
||||||
|
if new_hp < old_hp:
|
||||||
|
|
||||||
|
damage = old_hp-new_hp
|
||||||
|
|
||||||
|
if damage >= 50:
|
||||||
|
events.append(
|
||||||
|
DamageEvent(
|
||||||
|
damage
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
events.append(
|
||||||
|
DamageEvent(
|
||||||
|
old_hp - new_hp
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 闪光
|
||||||
|
if new_flashes > old_flashes:
|
||||||
|
events.append(
|
||||||
|
FlashEvent(
|
||||||
|
new_flashes
|
||||||
|
)
|
||||||
|
)
|
||||||
|
elif new_flashes < old_flashes:
|
||||||
|
events.append(
|
||||||
|
FlashEndEvent()
|
||||||
|
)
|
||||||
|
|
||||||
|
# 燃烧
|
||||||
|
if old_burning == 0 and new_burning > 0:
|
||||||
|
|
||||||
|
events.append(
|
||||||
|
BurnEvent(
|
||||||
|
new_burning
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# 燃烧结束
|
||||||
|
elif old_burning > 0 and new_burning == 0:
|
||||||
|
|
||||||
|
events.append(
|
||||||
|
BurnEndEvent()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# 击杀
|
||||||
|
if new_kills > old_kills:
|
||||||
|
|
||||||
|
events.append(
|
||||||
|
KillEvent(
|
||||||
|
new_kills
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# 爆头
|
||||||
|
if new_headshots > old_headshots:
|
||||||
|
|
||||||
|
events.append(
|
||||||
|
HeadshotEvent(
|
||||||
|
new_headshots
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
return events
|
||||||
47
logic/events.py
Normal file
47
logic/events.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class GameNotReadyEvent:
|
||||||
|
reason:str
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class GameReadyEvent:
|
||||||
|
reason:str
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DamageEvent:
|
||||||
|
damage: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DeathEvent:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FlashEvent:
|
||||||
|
value: int
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FlashEndEvent:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BurnEvent:
|
||||||
|
value: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BurnEndEvent:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class KillEvent:
|
||||||
|
kills: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class HeadshotEvent:
|
||||||
|
headshots: int
|
||||||
44
logic/parser.py
Normal file
44
logic/parser.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
def parse_game_state(data):
|
||||||
|
|
||||||
|
player = data.get("player", {})
|
||||||
|
state = player.get("state", {})
|
||||||
|
|
||||||
|
return {
|
||||||
|
|
||||||
|
"timestamp":
|
||||||
|
data.get("provider", {})
|
||||||
|
.get("timestamp"),
|
||||||
|
|
||||||
|
|
||||||
|
"player":{
|
||||||
|
"name":
|
||||||
|
player.get("name"),
|
||||||
|
|
||||||
|
"team":
|
||||||
|
player.get("team"),
|
||||||
|
|
||||||
|
"health":
|
||||||
|
state.get("health"),
|
||||||
|
|
||||||
|
"armor":
|
||||||
|
state.get("armor"),
|
||||||
|
|
||||||
|
"helmet":
|
||||||
|
state.get("helmet"),
|
||||||
|
|
||||||
|
"flashed":
|
||||||
|
state.get("flashed"),
|
||||||
|
|
||||||
|
"smoked":
|
||||||
|
state.get("smoked"),
|
||||||
|
|
||||||
|
"burning":
|
||||||
|
state.get("burning"),
|
||||||
|
|
||||||
|
"kills":
|
||||||
|
state.get("round_kills"),
|
||||||
|
|
||||||
|
"headshots":
|
||||||
|
state.get("round_killhs")
|
||||||
|
}
|
||||||
|
}
|
||||||
21
logic/state_manager.py
Normal file
21
logic/state_manager.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
class StateManager:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.previous = None
|
||||||
|
|
||||||
|
|
||||||
|
def update(self, current):
|
||||||
|
|
||||||
|
if current is None or current.get("health") is None:
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
# 第一次收到有效数据,只记录,不触发事件
|
||||||
|
if self.previous is None:
|
||||||
|
self.previous = current
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
old = self.previous
|
||||||
|
|
||||||
|
self.previous = current
|
||||||
|
|
||||||
|
return old, current
|
||||||
54
main.py
Normal file
54
main.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from flask import Flask,request
|
||||||
|
|
||||||
|
from logic.parser import parse_game_state
|
||||||
|
from logic.state_manager import StateManager
|
||||||
|
from logic.event_detector import EventDetector
|
||||||
|
|
||||||
|
|
||||||
|
app=Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
manager=StateManager()
|
||||||
|
|
||||||
|
detector=EventDetector()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/")
|
||||||
|
def receive():
|
||||||
|
|
||||||
|
|
||||||
|
data=request.json
|
||||||
|
|
||||||
|
|
||||||
|
state=parse_game_state(data)
|
||||||
|
|
||||||
|
|
||||||
|
old,new=manager.update(
|
||||||
|
state["player"]
|
||||||
|
)
|
||||||
|
|
||||||
|
if old is None or new is None:
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
events=detector.detect(
|
||||||
|
old,
|
||||||
|
new
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
for event in events:
|
||||||
|
|
||||||
|
print(event)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
app.run(
|
||||||
|
host="127.0.0.1",
|
||||||
|
port=3000
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user