fix: state_reader game.player → game.players[1]

This commit is contained in:
2026-03-25 15:09:48 +09:00
parent ec418195ca
commit ea4be446bb

View File

@@ -1,11 +1,18 @@
""" """
state_reader.py state_reader.py
RCON을 통해 팩토리오 게임 상태를 읽어오는 모듈 RCON을 통해 팩토리오 게임 상태를 읽어오는 모듈
핵심 변경: game.player → game.players[1] (RCON 호환)
""" """
import json import json
from factorio_rcon import FactorioRCON from factorio_rcon import FactorioRCON
# 플레이어 참조 헬퍼 (RCON에서 game.player는 nil)
P = """local p = game.players[1]
if not p then rcon.print("{}") return end
"""
class StateReader: class StateReader:
def __init__(self, rcon: FactorioRCON): def __init__(self, rcon: FactorioRCON):
self.rcon = rcon self.rcon = rcon
@@ -20,10 +27,9 @@ class StateReader:
"tech": self.get_research_status(), "tech": self.get_research_status(),
} }
# ── 플레이어 ──────────────────────────────────────────────── # ── 플레이어 ────────────────────────────────────────────────────
def get_player_info(self) -> dict: def get_player_info(self) -> dict:
lua = """ lua = P + """
local p = game.player
rcon.print(game.table_to_json({ rcon.print(game.table_to_json({
x = math.floor(p.position.x), x = math.floor(p.position.x),
y = math.floor(p.position.y), y = math.floor(p.position.y),
@@ -33,10 +39,11 @@ rcon.print(game.table_to_json({
raw = self.rcon.lua(lua) raw = self.rcon.lua(lua)
return json.loads(raw) if raw else {} return json.loads(raw) if raw else {}
# ── 인벤토리 ──────────────────────────────────────────────── # ── 인벤토리 ────────────────────────────────────────────────────
def get_inventory(self) -> dict: def get_inventory(self) -> dict:
lua = """ lua = P + """
local inv = game.player.get_main_inventory() local inv = p.get_main_inventory()
if not inv then rcon.print("{}") return end
local result = {} local result = {}
local contents = inv.get_contents() local contents = inv.get_contents()
for name, count in pairs(contents) do for name, count in pairs(contents) do
@@ -47,11 +54,10 @@ rcon.print(game.table_to_json(result))
raw = self.rcon.lua(lua) raw = self.rcon.lua(lua)
return json.loads(raw) if raw else {} return json.loads(raw) if raw else {}
# ── 자원 패치 스캔 ────────────────────────────────────────── # ── 자원 패치 스캔 ──────────────────────────────────────────────
def scan_resources(self, radius: int = 200) -> dict: def scan_resources(self, radius: int = 200) -> dict:
"""플레이어 주변 자원 패치 위치 탐색""" """플레이어 주변 자원 패치 위치 탐색"""
lua = f""" lua = P + f"""
local p = game.player
local surface = p.surface local surface = p.surface
local center = p.position local center = p.position
local resources = {{}} local resources = {{}}
@@ -66,7 +72,6 @@ for _, ore in ipairs(ore_types) do
name = ore name = ore
}} }}
if #entities > 0 then if #entities > 0 then
-- 패치 중심점 계산
local sx, sy = 0, 0 local sx, sy = 0, 0
for _, e in ipairs(entities) do for _, e in ipairs(entities) do
sx = sx + e.position.x sx = sx + e.position.x
@@ -84,10 +89,9 @@ rcon.print(game.table_to_json(resources))
raw = self.rcon.lua(lua) raw = self.rcon.lua(lua)
return json.loads(raw) if raw else {} return json.loads(raw) if raw else {}
# ── 건물 목록 ─────────────────────────────────────────────── # ── 건물 목록 ───────────────────────────────────────────────────
def get_buildings(self, radius: int = 300) -> list: def get_buildings(self, radius: int = 300) -> dict:
lua = f""" lua = P + f"""
local p = game.player
local surface = p.surface local surface = p.surface
local center = p.position local center = p.position
@@ -120,10 +124,10 @@ rcon.print(game.table_to_json(result))
raw = self.rcon.lua(lua) raw = self.rcon.lua(lua)
return json.loads(raw) if raw else {} return json.loads(raw) if raw else {}
# ── 연구 현황 ─────────────────────────────────────────────── # ── 연구 현황 ───────────────────────────────────────────────────
def get_research_status(self) -> dict: def get_research_status(self) -> dict:
lua = """ lua = P + """
local force = game.player.force local force = p.force
local completed = {} local completed = {}
local k = 0 local k = 0
for name, tech in pairs(force.technologies) do for name, tech in pairs(force.technologies) do
@@ -168,6 +172,9 @@ rcon.print(game.table_to_json({
if count > 0: if count > 0:
lines.append(f"- {item}: {count}") lines.append(f"- {item}: {count}")
if not any(inv.get(item, 0) > 0 for item in key_items):
lines.append("- 비어 있음")
lines += ["", "### 주변 자원 패치"] lines += ["", "### 주변 자원 패치"]
if res: if res:
for ore, info in res.items(): for ore, info in res.items():