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