File size: 3,583 Bytes
fce2dad
 
 
 
ad7d9ec
fce2dad
ad7d9ec
 
 
 
 
 
 
 
 
fce2dad
 
 
ad7d9ec
 
fce2dad
ad7d9ec
 
fce2dad
 
ad7d9ec
 
 
fce2dad
 
ad7d9ec
 
fce2dad
ad7d9ec
 
 
 
fce2dad
 
ad7d9ec
 
fce2dad
ad7d9ec
 
fce2dad
ad7d9ec
fce2dad
 
ad7d9ec
 
 
 
 
 
 
 
fce2dad
 
 
 
 
ad7d9ec
 
 
 
 
fce2dad
ad7d9ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fce2dad
 
 
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
93
94
95
96
97
98
# Import necessary libraries
import streamlit as st
import random
import time
from datetime import datetime

# Constants
HISTORY_LENGTH = 5  # Number of previous actions to display

# Initialize game state
def initialize_game(player_name):
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    with open(f"{timestamp}_{player_name}_state.txt", "w") as f:
        f.write(f"{player_name}:100,None,Player 1")
    with open(f"{timestamp}_{player_name}_actions.txt", "w") as f:
        f.write("")

# Read game state
def read_game_state(player_name, timestamp):
    with open(f"{timestamp}_{player_name}_state.txt", "r") as f:
        state = f.readline().split(',')
        player_name, hp, last_action, turn = state
    return player_name, int(hp), last_action, turn

# Write player action
def write_action(player_name, action, timestamp):
    with open(f"{timestamp}_{player_name}_actions.txt", "a") as f:
        f.write(f"{action}\n")

# Update game state based on actions
def update_game_state(player_name, timestamp):
    _, hp, _, turn = read_game_state(player_name, timestamp)
    
    # Read Player's action
    with open(f"{timestamp}_{player_name}_actions.txt", "r") as f:
        actions = f.readlines()
        last_action = actions[-1].strip() if actions else None
    
    # Update game based on actions
    if turn == player_name and last_action:
        if last_action == "Attack":
            damage = random.randint(5, 15)
            hp -= damage
        elif last_action == "Heal":
            heal = random.randint(5, 10)
            hp += heal
    
    # Write updated game state
    with open(f"{timestamp}_{player_name}_state.txt", "w") as f:
        f.write(f"{player_name}:{hp},{last_action},Player 1")

# Get the last few actions
def get_recent_actions(player_name, timestamp):
    with open(f"{timestamp}_{player_name}_actions.txt", "r") as f:
        actions = f.readlines()
    return actions[-HISTORY_LENGTH:]

# Streamlit Interface
def app():
    st.title("Text Battle!")
    
    if "player_name" not in st.session_state:
        st.session_state.player_name = st.text_input("Enter your name:")
        if st.session_state.player_name:
            st.session_state.timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
            initialize_game(st.session_state.player_name)
    
    if "player_name" in st.session_state:
        player_name, hp, last_action, turn = read_game_state(st.session_state.player_name, st.session_state.timestamp)
        
        st.write(f"{player_name} HP: {hp}")
        st.write(f"Last Action: {last_action}")
        
        if hp <= 0:
            st.write(f"{player_name} has been defeated!")
            del st.session_state.player_name
            st.stop()
        
        # Display recent actions
        st.write("Recent Actions:")
        for action in get_recent_actions(st.session_state.player_name, st.session_state.timestamp):
            st.write(action.strip())
        
        # Player actions
        if turn == st.session_state.player_name:
            action = st.selectbox(f"{st.session_state.player_name}, choose your action:", ["", "Attack", "Heal", "Pass"])
            if st.button("Submit"):
                write_action(st.session_state.player_name, action, st.session_state.timestamp)
                update_game_state(st.session_state.player_name, st.session_state.timestamp)
        
        # Display timer and refresh game state every 10 seconds
        st.write("Refreshing in 10 seconds...")
        time.sleep(10)
        st.experimental_rerun()

# Run the Streamlit app
app()