Spaces:
Runtime error
Runtime error
kwabs22
commited on
Commit
·
cbac46a
1
Parent(s):
1ad18bf
Load game supports media now?
Browse files
app.py
CHANGED
|
@@ -518,68 +518,129 @@ def make_choice(choice, game_session): #Calls the nested make choice function in
|
|
| 518 |
|
| 519 |
return result[0], gr.update(choices=result[1]), result[2], game_session
|
| 520 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
def load_game(custom_config=None, with_media=False):
|
| 522 |
global all_states
|
| 523 |
-
if custom_config:
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
description, choices, game_log = game_session.get_current_state_info()
|
| 582 |
-
return description, gr.update(choices=choices), game_log, game_session, gr.update()
|
| 583 |
|
| 584 |
initgameinfo = start_game()
|
| 585 |
|
|
|
|
| 518 |
|
| 519 |
return result[0], gr.update(choices=result[1]), result[2], game_session
|
| 520 |
|
| 521 |
+
# def load_game(custom_config=None, with_media=False):
|
| 522 |
+
# global all_states
|
| 523 |
+
# if custom_config:
|
| 524 |
+
# try:
|
| 525 |
+
# new_config = json.loads(custom_config)
|
| 526 |
+
|
| 527 |
+
# all_states = new_config
|
| 528 |
+
|
| 529 |
+
# # Determine the starting location and state
|
| 530 |
+
# starting_location = next(iter(all_states.keys()))
|
| 531 |
+
# starting_state = next(iter(all_states[starting_location].keys()))
|
| 532 |
+
# print(starting_location, starting_state)
|
| 533 |
+
|
| 534 |
+
# game_session = GameSession(starting_location, starting_state)
|
| 535 |
+
# description, choices, game_log = game_session.get_current_state_info()
|
| 536 |
+
# new_path_errors = validate_transitions(all_states)
|
| 537 |
+
# media = None
|
| 538 |
+
# output_media = None
|
| 539 |
+
|
| 540 |
+
# print(all_states[starting_location][starting_state][media])
|
| 541 |
+
# print(with_media)
|
| 542 |
+
# print("everything fine till here")
|
| 543 |
+
# if with_media:
|
| 544 |
+
# print("everything fine till here 5")
|
| 545 |
+
# if all_states[starting_location][starting_state][media] != []:
|
| 546 |
+
# print(f"everything fine till here 4 - {all_states[starting_location][starting_state][media]}")
|
| 547 |
+
# media = all_states[starting_location][starting_state][media]
|
| 548 |
+
# print("everything fine till here 2")
|
| 549 |
+
# for media_path in media:
|
| 550 |
+
# media_component = create_media_component(media_path)
|
| 551 |
+
# output_media.append(media_component)
|
| 552 |
+
# print("everything fine till here 3")
|
| 553 |
+
# return gr.update(value=f"Custom configuration loaded successfully! \n{new_path_errors}"), game_log, description, gr.update(choices=choices), game_session, gr.update(value=custom_config), output_media
|
| 554 |
+
|
| 555 |
+
# return gr.update(value=f"Custom configuration loaded successfully! \n{new_path_errors}"), game_log, description, gr.update(choices=choices), game_session, gr.update(value=custom_config)
|
| 556 |
+
# except json.JSONDecodeError as e:
|
| 557 |
+
# # Get the line number and column of the error
|
| 558 |
+
# lineno, colno = e.lineno, e.colno
|
| 559 |
+
|
| 560 |
+
# # Get the problematic line
|
| 561 |
+
# lines = custom_config.split('\n')
|
| 562 |
+
# error_line = lines[lineno - 1] if lineno <= len(lines) else ""
|
| 563 |
+
|
| 564 |
+
# # Create a pointer to the error location
|
| 565 |
+
# pointer = ' ' * (colno - 1) + '^'
|
| 566 |
+
|
| 567 |
+
# error_message = f"Invalid JSON format in custom configuration:\n"
|
| 568 |
+
# error_message += f"Error at line {lineno}, column {colno}:\n"
|
| 569 |
+
# error_message += f"{error_line}\n"
|
| 570 |
+
# error_message += f"{pointer}\n"
|
| 571 |
+
# error_message += f"Error details: {str(e)}"
|
| 572 |
+
|
| 573 |
+
# return gr.update(value=error_message), gr.update(), gr.update(), None, gr.update(value=custom_config), game_session, gr.update(value=custom_config)
|
| 574 |
+
# except Exception as e:
|
| 575 |
+
# return gr.update(value=f"Error loading custom configuration: {str(e)}"), gr.update(), gr.update(), None, gr.update(value=custom_config), game_session, gr.update(value=custom_config)
|
| 576 |
+
|
| 577 |
+
# # If no custom config, start with the default configuration
|
| 578 |
+
# starting_location = next(iter(all_states.keys()))
|
| 579 |
+
# starting_state = next(iter(all_states[starting_location].keys()))
|
| 580 |
+
# game_session = GameSession(starting_location, starting_state)
|
| 581 |
+
# description, choices, game_log = game_session.get_current_state_info()
|
| 582 |
+
# return description, gr.update(choices=choices), game_log, game_session, gr.update()
|
| 583 |
+
|
| 584 |
def load_game(custom_config=None, with_media=False):
|
| 585 |
global all_states
|
| 586 |
+
if not custom_config:
|
| 587 |
+
return gr.update(value="No custom configuration provided."), None, None, None, None, None, None
|
| 588 |
+
|
| 589 |
+
try:
|
| 590 |
+
new_config = json.loads(custom_config)
|
| 591 |
+
all_states = new_config
|
| 592 |
+
|
| 593 |
+
# Determine the starting location and state
|
| 594 |
+
starting_location = next(iter(all_states.keys()))
|
| 595 |
+
starting_state = next(iter(all_states[starting_location].keys()))
|
| 596 |
+
print(f"Starting location: {starting_location}, Starting state: {starting_state}")
|
| 597 |
+
|
| 598 |
+
game_session = GameSession(starting_location, starting_state)
|
| 599 |
+
description, choices, game_log = game_session.get_current_state_info()
|
| 600 |
+
new_path_errors = validate_transitions(all_states)
|
| 601 |
+
|
| 602 |
+
output_media = []
|
| 603 |
+
|
| 604 |
+
if with_media:
|
| 605 |
+
media_list = all_states[starting_location][starting_state].get('media', [])
|
| 606 |
+
print(f"Media list: {media_list}")
|
| 607 |
+
|
| 608 |
+
if media_list:
|
| 609 |
+
for media_path in media_list:
|
| 610 |
+
media_component = create_media_component(media_path)
|
| 611 |
+
output_media.append(media_component)
|
| 612 |
+
print(f"Created {len(output_media)} media components")
|
| 613 |
+
|
| 614 |
+
success_message = f"Custom configuration loaded successfully!\n{new_path_errors}"
|
| 615 |
+
return (
|
| 616 |
+
gr.update(value=success_message),
|
| 617 |
+
game_log,
|
| 618 |
+
description,
|
| 619 |
+
gr.update(choices=choices),
|
| 620 |
+
game_session,
|
| 621 |
+
gr.update(value=custom_config),
|
| 622 |
+
output_media if with_media else None
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
except json.JSONDecodeError as e:
|
| 626 |
+
error_message = format_json_error(custom_config, e)
|
| 627 |
+
return gr.update(value=error_message), None, None, None, None, gr.update(value=custom_config), None
|
| 628 |
+
|
| 629 |
+
except Exception as e:
|
| 630 |
+
error_message = f"Error loading custom configuration: {str(e)}"
|
| 631 |
+
return gr.update(value=error_message), None, None, None, None, gr.update(value=custom_config), None
|
| 632 |
+
|
| 633 |
+
def format_json_error(config, error):
|
| 634 |
+
lineno, colno = error.lineno, error.colno
|
| 635 |
+
lines = config.split('\n')
|
| 636 |
+
error_line = lines[lineno - 1] if lineno <= len(lines) else ""
|
| 637 |
+
pointer = ' ' * (colno - 1) + '^'
|
| 638 |
+
|
| 639 |
+
return f"""Invalid JSON format in custom configuration:
|
| 640 |
+
Error at line {lineno}, column {colno}:
|
| 641 |
+
{error_line}
|
| 642 |
+
{pointer}
|
| 643 |
+
Error details: {str(error)}"""
|
|
|
|
|
|
|
| 644 |
|
| 645 |
initgameinfo = start_game()
|
| 646 |
|