Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -806,7 +806,8 @@ def create_ui():
|
|
| 806 |
character_select = gr.Dropdown(
|
| 807 |
choices=[(c['name'], c['id']) for c in CHARACTERS],
|
| 808 |
label="Select Character",
|
| 809 |
-
value=CHARACTERS[0]['id']
|
|
|
|
| 810 |
)
|
| 811 |
|
| 812 |
with gr.Row():
|
|
@@ -915,46 +916,86 @@ def create_ui():
|
|
| 915 |
# ===== EVENT HANDLERS =====
|
| 916 |
|
| 917 |
def create_room_handler(your_name, character_id, session):
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 921 |
|
| 922 |
-
|
| 923 |
-
|
|
|
|
| 924 |
|
| 925 |
-
|
|
|
|
| 926 |
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
|
|
|
|
|
|
| 940 |
|
| 941 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 942 |
|
| 943 |
def join_room_handler(room_id, your_name, character_id, session):
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 958 |
|
| 959 |
def refresh_game_state(session):
|
| 960 |
empty_return = ("*No game joined*", "", "", {}, "", "", {}, [], [], "*No spectators*", "")
|
|
@@ -1116,13 +1157,15 @@ def create_ui():
|
|
| 1116 |
create_btn.click(
|
| 1117 |
fn=create_room_handler,
|
| 1118 |
inputs=[your_name_input, character_select, session_state],
|
| 1119 |
-
outputs=[session_state, join_result]
|
|
|
|
| 1120 |
)
|
| 1121 |
|
| 1122 |
join_btn.click(
|
| 1123 |
fn=join_room_handler,
|
| 1124 |
inputs=[room_id_input, your_name_input, character_select, session_state],
|
| 1125 |
-
outputs=[session_state, join_result]
|
|
|
|
| 1126 |
)
|
| 1127 |
|
| 1128 |
refresh_btn.click(
|
|
|
|
| 806 |
character_select = gr.Dropdown(
|
| 807 |
choices=[(c['name'], c['id']) for c in CHARACTERS],
|
| 808 |
label="Select Character",
|
| 809 |
+
value=CHARACTERS[0]['id'],
|
| 810 |
+
interactive=True
|
| 811 |
)
|
| 812 |
|
| 813 |
with gr.Row():
|
|
|
|
| 916 |
# ===== EVENT HANDLERS =====
|
| 917 |
|
| 918 |
def create_room_handler(your_name, character_id, session):
|
| 919 |
+
try:
|
| 920 |
+
import uuid
|
| 921 |
+
print(f"DEBUG: create_room_handler called with name={your_name}, char={character_id}")
|
| 922 |
+
|
| 923 |
+
# Validate inputs
|
| 924 |
+
if not your_name or not your_name.strip():
|
| 925 |
+
return session, "β Please enter your name"
|
| 926 |
+
|
| 927 |
+
if not character_id:
|
| 928 |
+
return session, "β Please select a character"
|
| 929 |
+
|
| 930 |
+
room_id = str(uuid.uuid4())[:8].upper()
|
| 931 |
+
player_id = str(uuid.uuid4())[:8]
|
| 932 |
+
|
| 933 |
+
print(f"DEBUG: Creating room {room_id} for player {player_id}")
|
| 934 |
|
| 935 |
+
if not db.create_room(room_id):
|
| 936 |
+
print("DEBUG: Failed to create room")
|
| 937 |
+
return session, "β Failed to create room. Please try again."
|
| 938 |
|
| 939 |
+
success, slot, msg = db.join_room(room_id, player_id, your_name, character_id)
|
| 940 |
+
print(f"DEBUG: Join result - success={success}, slot={slot}, msg={msg}")
|
| 941 |
|
| 942 |
+
if success:
|
| 943 |
+
new_session = {
|
| 944 |
+
'room_id': room_id,
|
| 945 |
+
'player_id': player_id,
|
| 946 |
+
'player_slot': slot,
|
| 947 |
+
'is_spectator': slot is None
|
| 948 |
+
}
|
| 949 |
+
result_msg = f"β
Room created: **{room_id}**\n\n{msg}\n\nShare this Room ID with your partner!"
|
| 950 |
+
print(f"DEBUG: Returning success - {result_msg}")
|
| 951 |
+
return new_session, result_msg
|
| 952 |
+
|
| 953 |
+
# If join fails, clean up the created room
|
| 954 |
+
with db.get_connection() as conn:
|
| 955 |
+
conn.execute("DELETE FROM rooms WHERE room_id = ?", (room_id,))
|
| 956 |
+
conn.commit()
|
| 957 |
|
| 958 |
+
return session, f"β Failed to join room after creation: {msg}"
|
| 959 |
+
except Exception as e:
|
| 960 |
+
print(f"ERROR in create_room_handler: {e}")
|
| 961 |
+
import traceback
|
| 962 |
+
traceback.print_exc()
|
| 963 |
+
return session, f"β Error: {str(e)}"
|
| 964 |
|
| 965 |
def join_room_handler(room_id, your_name, character_id, session):
|
| 966 |
+
try:
|
| 967 |
+
import uuid
|
| 968 |
+
print(f"DEBUG: join_room_handler called - room={room_id}, name={your_name}, char={character_id}")
|
| 969 |
+
|
| 970 |
+
# Validate inputs
|
| 971 |
+
if not room_id or not room_id.strip():
|
| 972 |
+
return session, "β Please enter a Room ID"
|
| 973 |
+
|
| 974 |
+
if not your_name or not your_name.strip():
|
| 975 |
+
return session, "β Please enter your name"
|
| 976 |
+
|
| 977 |
+
if not character_id:
|
| 978 |
+
return session, "β Please select a character"
|
| 979 |
+
|
| 980 |
+
player_id = str(uuid.uuid4())[:8]
|
| 981 |
+
|
| 982 |
+
success, slot, msg = db.join_room(room_id.strip().upper(), player_id, your_name, character_id)
|
| 983 |
+
print(f"DEBUG: Join result - success={success}, slot={slot}, msg={msg}")
|
| 984 |
+
|
| 985 |
+
if success:
|
| 986 |
+
new_session = {
|
| 987 |
+
'room_id': room_id.strip().upper(),
|
| 988 |
+
'player_id': player_id,
|
| 989 |
+
'player_slot': slot,
|
| 990 |
+
'is_spectator': slot is None
|
| 991 |
+
}
|
| 992 |
+
return new_session, f"β
{msg}"
|
| 993 |
+
return session, f"β {msg}"
|
| 994 |
+
except Exception as e:
|
| 995 |
+
print(f"ERROR in join_room_handler: {e}")
|
| 996 |
+
import traceback
|
| 997 |
+
traceback.print_exc()
|
| 998 |
+
return session, f"β Error: {str(e)}"
|
| 999 |
|
| 1000 |
def refresh_game_state(session):
|
| 1001 |
empty_return = ("*No game joined*", "", "", {}, "", "", {}, [], [], "*No spectators*", "")
|
|
|
|
| 1157 |
create_btn.click(
|
| 1158 |
fn=create_room_handler,
|
| 1159 |
inputs=[your_name_input, character_select, session_state],
|
| 1160 |
+
outputs=[session_state, join_result],
|
| 1161 |
+
show_progress=True
|
| 1162 |
)
|
| 1163 |
|
| 1164 |
join_btn.click(
|
| 1165 |
fn=join_room_handler,
|
| 1166 |
inputs=[room_id_input, your_name_input, character_select, session_state],
|
| 1167 |
+
outputs=[session_state, join_result],
|
| 1168 |
+
show_progress=True
|
| 1169 |
)
|
| 1170 |
|
| 1171 |
refresh_btn.click(
|