import os
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# 초기 상태
state = {
"scene": "mysterious_room",
"foundLantern": False,
"foundTalisman": False,
"ghostDefeated": False
}
# 시나리오
scenes = {
"mysterious_room": {
"description": "나라를 어지럽히는 악귀에 의해 미지의 공간에 갇힌 당신. 주위를 살펴보거나 발자국을 옮길 수 있습니다.",
"actions": {
"주위를 살펴본다": {
"choices": {
"연다": {"stateChanges": {"foundLantern": True}, "nextScene": "lantern_scene"},
"무시하자": {"stateChanges": {}, "nextScene": "mysterious_room"}
}
},
"발자국을 옮긴다": {"nextScene": "ghost_appear"}
}
},
"lantern_scene": {
"description": "청사초롱을 발견했다. 위에는 노란 부적이 붙어 있다.",
"actions": {
"부적을 조사한다": {"stateChanges": {"foundTalisman": True}, "nextScene": "ghost_appear"}
}
},
"ghost_appear": {
"description": "악귀가 나타났다! 숨을 참거나 귀신의 특성을 확인할 수 있다.",
"actions": {"숨을 참다": {"nextScene": "check_ghost"}}
},
"check_ghost": {
"description": "악귀는 목(木) 속성을 가졌다. 화(火) 속성을 사용하면 제압 가능.",
"actions": {
"처리한다": {
"choices": {
"왼쪽으로 날린다": {"stateChanges": {"ghostDefeated": True}, "ending": "bestEnding"},
"오른쪽으로 날린다": {"stateChanges": {}, "ending": "failEnding"}
}
}
}
}
}
# 현재 상태 조회
@app.route("/tools/getCurrentStatus", methods=["GET", "POST"])
def get_status():
current_scene = state["scene"]
scene_data = scenes[current_scene]
available_actions = list(scene_data["actions"].keys())
return jsonify({
"scene": current_scene,
"description": scene_data["description"],
"availableActions": available_actions
})
# 행동 처리
@app.route("/tools/processAction", methods=["POST"])
def process_action():
data = request.get_json()
action = data.get("action")
choice = data.get("choice", None)
current_scene = state["scene"]
scene_data = scenes.get(current_scene, {})
action_data = scene_data.get("actions", {}).get(action)
response = {"result": "fail", "stateChanges": {}, "nextScene": current_scene, "ending": None}
if not action_data:
return jsonify(response)
# 선택지
if "choices" in action_data:
if choice in action_data["choices"]:
result = action_data["choices"][choice]
for k, v in result.get("stateChanges", {}).items():
state[k] = v
response["nextScene"] = result.get("nextScene", current_scene)
response["ending"] = result.get("ending")
response["result"] = "success"
else:
for k, v in action_data.get("stateChanges", {}).items():
state[k] = v
response["nextScene"] = action_data.get("nextScene", current_scene)
response["result"] = "success"
state["scene"] = response["nextScene"]
return jsonify(response)
# 플레이어 상태 조회
@app.route("/tools/getPlayerStatus", methods=["GET", "POST"])
def get_player_status():
return jsonify(state)
if __name__ == "__main__":
# Render 같은 PaaS에서 PORT 환경변수를 전달하므로 이를 우선 사용
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))