commit bad1f2570ba6d4d0626614ea9ab2b3af0a0640ca Author: Merlin Date: Sat Jul 11 03:08:25 2026 +0800 init: cs player_status CDK demo diff --git a/logic/__pycache__/event_detector.cpython-314.pyc b/logic/__pycache__/event_detector.cpython-314.pyc new file mode 100644 index 0000000..987844b Binary files /dev/null and b/logic/__pycache__/event_detector.cpython-314.pyc differ diff --git a/logic/__pycache__/events.cpython-314.pyc b/logic/__pycache__/events.cpython-314.pyc new file mode 100644 index 0000000..f284867 Binary files /dev/null and b/logic/__pycache__/events.cpython-314.pyc differ diff --git a/logic/__pycache__/parser.cpython-314.pyc b/logic/__pycache__/parser.cpython-314.pyc new file mode 100644 index 0000000..4923ce7 Binary files /dev/null and b/logic/__pycache__/parser.cpython-314.pyc differ diff --git a/logic/__pycache__/state_manager.cpython-314.pyc b/logic/__pycache__/state_manager.cpython-314.pyc new file mode 100644 index 0000000..19d3a63 Binary files /dev/null and b/logic/__pycache__/state_manager.cpython-314.pyc differ diff --git a/logic/event_detector.py b/logic/event_detector.py new file mode 100644 index 0000000..9a41c72 --- /dev/null +++ b/logic/event_detector.py @@ -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 \ No newline at end of file diff --git a/logic/events.py b/logic/events.py new file mode 100644 index 0000000..b14dfa4 --- /dev/null +++ b/logic/events.py @@ -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 \ No newline at end of file diff --git a/logic/parser.py b/logic/parser.py new file mode 100644 index 0000000..91e260c --- /dev/null +++ b/logic/parser.py @@ -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") + } + } \ No newline at end of file diff --git a/logic/state_manager.py b/logic/state_manager.py new file mode 100644 index 0000000..4c5e65a --- /dev/null +++ b/logic/state_manager.py @@ -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 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e4cc527 --- /dev/null +++ b/main.py @@ -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 +) \ No newline at end of file