File size: 2,615 Bytes
fdcbf65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from game.dungeon import Dungeon
from game.player import Player
from game.npc import NPC
from game.combat import combat, adjust_difficulty
from game.items import Item, use_item
from assets.ascii_art import display_dungeon
from helper import (
    get_game_state,
    run_action,
    start_game,
    is_safe,
)

import pdb
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def main_loop(message, history, game_state):
    """Main game loop that processes commands and returns responses"""

    try:
        print(f"\nProcessing command: {message}")  # Debug print

        # Get AI response
        output = run_action(message, history, game_state)
        logger.info(f"Generated response: {output}")
        print(f"\nGenerated output: {output}")  # Debug print

        # Safety check
        safe = is_safe(output)
        print(f"\nSafety Check Result: {'SAFE' if safe else 'UNSAFE'}")
        logger.info(f"Safety check result: {'SAFE' if safe else 'UNSAFE'}")

        if not safe:
            logging.warning("Unsafe output detected")
            logger.warning("Unsafe content detected - blocking response")
            print("Unsafe content detected - Response blocked")
            return "This response was blocked for safety reasons.", history

        # Update history with safe response
        if not history:
            history = []
        history.append((message, output))

        return output, history

    except Exception as e:
        logger.error(f"Error in main_loop: {str(e)}")
        return "Error processing command", history


def main():
    """Initialize game and start interface"""
    try:
        logger.info("Starting main function")
        print("\nInitializing game...")
        game_state = get_game_state(
            inventory={
                "cloth pants": 1,
                "cloth shirt": 1,
                "goggles": 1,
                "leather bound journal": 1,
                "gold": 5,
            }
        )
        logger.debug(f"Initialized game state: {game_state}")

        # Create and add game objects
        dungeon = Dungeon(10, 10)
        player = Player("Hero")

        # Update game state with objects
        game_state["dungeon"] = dungeon
        game_state["player"] = player

        # logger.info(f"Game state in main(): {game_state}")

        # Start game interface
        print("Starting game interface...")
        start_game(main_loop, game_state, True)

    except Exception as e:
        logger.error(f"Error in main: {str(e)}")
        raise


if __name__ == "__main__":
    main()