From 208a9ec3ea0ee17f4eb3c2fa4113ae21903e1006 Mon Sep 17 00:00:00 2001 From: Jeffrey Harmon Date: Wed, 6 Mar 2024 17:05:12 -0500 Subject: [PATCH] Refactor story loading and removal --- project/common/story_loader.gd | 2 +- project/story/drama_club/tech.gd | 3 +- project/story/rpg/conflict.gd | 4 +- .../starting_stories/schedule_conflict.gd | 6 +-- project/story/starting_stories/steven_dnd.gd | 4 +- .../integration/simple_story_validity_test.gd | 48 ++++--------------- .../test/integration/story_quantity_test.gd | 16 +++---- project/test/unit/world_test.gd | 8 ++-- project/ui/main_scene.gd | 11 ++--- project/world/world.gd | 36 +++++++------- 10 files changed, 47 insertions(+), 91 deletions(-) diff --git a/project/common/story_loader.gd b/project/common/story_loader.gd index c5e87e8..839cdfa 100644 --- a/project/common/story_loader.gd +++ b/project/common/story_loader.gd @@ -28,7 +28,7 @@ func get_story_paths_in_directory(path: String) -> Array[String]: while file_name != "": var file_path := path.path_join(file_name) - print("Checking file: " + file_path) + if dir.current_is_dir(): paths.append_array(get_story_paths_in_directory(file_path)) elif _is_valid_story_path(file_path): diff --git a/project/story/drama_club/tech.gd b/project/story/drama_club/tech.gd index a675faf..fa79c5f 100644 --- a/project/story/drama_club/tech.gd +++ b/project/story/drama_club/tech.gd @@ -28,5 +28,4 @@ var options := { func on_option_selected(option: String, world : World) -> void: if option == _SWITCH: - var dir := DirAccess.open(_DRAMA_DIR) - world.remove_stories(dir) + world.remove_stories(_DRAMA_DIR) diff --git a/project/story/rpg/conflict.gd b/project/story/rpg/conflict.gd index 39b8fe9..31d93cc 100644 --- a/project/story/rpg/conflict.gd +++ b/project/story/rpg/conflict.gd @@ -32,6 +32,4 @@ var options := { func on_option_selected(option:String, world: World) -> void: if option == QUIT: - var dir := DirAccess.open("res://story/rpg") - - world.remove_stories(dir) + world.remove_stories("res://story/rpg") diff --git a/project/story/starting_stories/schedule_conflict.gd b/project/story/starting_stories/schedule_conflict.gd index e169580..4573a8f 100644 --- a/project/story/starting_stories/schedule_conflict.gd +++ b/project/story/starting_stories/schedule_conflict.gd @@ -32,8 +32,6 @@ var options := { func on_option_selected(option: String, world : World) -> void: match option: _DRAMA_CLUB: - var dir := DirAccess.open(_DRAMA_DIR) - world.add_stories(dir) + world.add_stories(_DRAMA_DIR) _ROBOTICS_CLUB: - var dir := DirAccess.open(_ROBOTICS_DIR) - world.add_stories(dir) + world.add_stories(_ROBOTICS_DIR) diff --git a/project/story/starting_stories/steven_dnd.gd b/project/story/starting_stories/steven_dnd.gd index af413c0..3c4b7a5 100644 --- a/project/story/starting_stories/steven_dnd.gd +++ b/project/story/starting_stories/steven_dnd.gd @@ -40,6 +40,4 @@ var options := { func on_option_selected(option:String, world: World) -> void: if POSITIVE.values().has(option): - var dir := DirAccess.open("res://story/rpg") - world.add_stories(dir) - + world.add_stories("res://story/rpg") diff --git a/project/test/integration/simple_story_validity_test.gd b/project/test/integration/simple_story_validity_test.gd index 2e5daab..944bae9 100644 --- a/project/test/integration/simple_story_validity_test.gd +++ b/project/test/integration/simple_story_validity_test.gd @@ -2,7 +2,6 @@ ## that can be found in the story directories. extends GutTest -const STORY_PATH := "res://story/" const END_STORY_PATH := "res://end/" const CAST_PATH := "res://cast/" @@ -12,14 +11,16 @@ const MAX_LINES_PER_STORY := 7 const MAX_LINES_PER_OPTION := 3 const MAX_EFFECTS := 2 +var story_loader: StoryLoader var story_paths: Array[String] = [] func before_all(): - story_paths = _get_story_paths_in_directory(STORY_PATH) + story_loader = StoryLoader.new() + story_paths = story_loader.get_story_paths_in_directory(StoryLoader.STORY_PATH) func test_stories_exist(): - assert_true(story_paths.size() > 0, "No stories found in %s" % STORY_PATH) + assert_true(story_paths.size() > 0, "No stories found in %s" % StoryLoader.STORY_PATH) func test_story_has_npc(): @@ -41,7 +42,12 @@ func test_npc_is_in_cast(): func _for_each_story(callable:Callable) -> void: for story_path in story_paths: - var story := _load_simple_story(story_path) + var story := story_loader.load_simple_story(story_path) + + # Skip the story if not a SimpleStory + if not story: + continue + await callable.call(story, story_path) @@ -220,37 +226,3 @@ func test_location_validity(): else: assert_true(true) ) - - -func _get_story_paths_in_directory(path: String) -> Array[String]: - var paths: Array[String] = [] - - var dir := DirAccess.open(path) - assert_not_null(dir, "Could not open directory " + path) - - dir.list_dir_begin() - var file_name := dir.get_next() - - while file_name != "": - var file_path := path + file_name - if dir.current_is_dir(): - paths.append_array(_get_story_paths_in_directory(file_path + "/")) - elif file_name.ends_with(".gd") and _is_path_simple_story(file_path): - paths.append(file_path) - - file_name = dir.get_next() - - return paths - - -func _load_simple_story(file_path: String) -> SimpleStory: - var resource := load(file_path) - var story: Object = autofree(resource.new()) - - return story - - -func _is_path_simple_story(file_path: String) -> bool: - return _load_simple_story(file_path) is SimpleStory - - diff --git a/project/test/integration/story_quantity_test.gd b/project/test/integration/story_quantity_test.gd index b45116e..7913aa0 100644 --- a/project/test/integration/story_quantity_test.gd +++ b/project/test/integration/story_quantity_test.gd @@ -1,18 +1,14 @@ extends GutTest -const STARTING_STORY_DIR := "res://story/starting_stories/" - func test_there_are_stories_enough_to_fill_a_playthrough() -> void: + var story_loader := StoryLoader.new() var world := World.new() - var dir := DirAccess.open(STARTING_STORY_DIR) - var files := dir.get_files() - var count := 0 - for file_name in files: - if file_name.ends_with(".gd"): - count += 1 - + + var story_paths := story_loader.get_starting_story_paths() + var count := story_paths.size() + var needed_stories := world.turns_per_year * 4 - + assert_true(count >= needed_stories, "Not enough stories. We need at least %d but there are %d." % [ needed_stories, count diff --git a/project/test/unit/world_test.gd b/project/test/unit/world_test.gd index 38b7721..9c99b18 100644 --- a/project/test/unit/world_test.gd +++ b/project/test/unit/world_test.gd @@ -13,8 +13,7 @@ func before_each() -> void: func test_add_stories() -> void: var initial_story_count := world.available_stories.size() - var dir := DirAccess.open(TEST_STORY_DIR) - var added_paths := world.add_stories(dir) + var added_paths := world.add_stories(TEST_STORY_DIR) var expected := initial_story_count + NUMBER_OF_TEST_STORIES assert_eq(world.available_stories.size(), expected) assert_eq(added_paths.size(), NUMBER_OF_TEST_STORIES) @@ -23,9 +22,8 @@ func test_add_stories() -> void: func test_remove_stories() -> void: var initial_story_count := world.available_stories.size() - var dir := DirAccess.open(TEST_STORY_DIR) - world.add_stories(dir) - var removed_paths := world.remove_stories(dir) + world.add_stories(TEST_STORY_DIR) + var removed_paths := world.remove_stories(TEST_STORY_DIR) assert_eq(world.available_stories.size(), initial_story_count) assert_eq(removed_paths.size(), NUMBER_OF_TEST_STORIES) assert_eq(removed_paths[0], "%s/test_story_1.gd" % TEST_STORY_DIR) diff --git a/project/ui/main_scene.gd b/project/ui/main_scene.gd index 5222a06..a4af246 100644 --- a/project/ui/main_scene.gd +++ b/project/ui/main_scene.gd @@ -1,11 +1,10 @@ extends Control -const _STARTING_STORY_PATH := "res://story/starting_stories/" +signal _option_selected(option: String) + const _CAST_PATH := "res://cast/" const _LOCATIONS_PATH := "res://locations/" -signal _option_selected(option: String) - @export_category("Animation") ## How long it takes a new "card" to slide in on top of the stack. @@ -37,10 +36,8 @@ func _ready(): world.cast.load_cast(_CAST_PATH) _game_screen.world = world - ## Load all the stories in _STARTING_STORY_PATH - var file_paths := DirAccess.get_files_at(_STARTING_STORY_PATH) - for file_path in file_paths: - world.available_stories.append(_STARTING_STORY_PATH + file_path) + ## Load all of the starting stories + world.add_stories(StoryLoader.STARTING_STORIES_PATH) # Start by telling the player this is the start of freshman year await _game_screen.show_year_advancement(Year.Name.FRESHMAN) diff --git a/project/world/world.gd b/project/world/world.gd index 8c72540..5ad3329 100644 --- a/project/world/world.gd +++ b/project/world/world.gd @@ -32,15 +32,15 @@ var _turns_this_year := 0 ## of available stories ## ## Returns an array of the stories (resource paths) that were added -func add_stories(dir:DirAccess) -> Array[String]: - var results : Array[String] = [] - for file_name in dir.get_files(): - var file_path := dir.get_current_dir() + "/" + file_name - if FileAccess.file_exists(file_path): - results.append(file_path) - available_stories.append(file_path) - else: - push_error("Tried to append non-existing story path: %s" % file_path) +func add_stories(path: String) -> Array[String]: + var story_loader := StoryLoader.new() + + var results: Array[String] = [] + + for story_path in story_loader.get_story_paths_in_directory(path): + results.append(story_path) + available_stories.append(story_path) + return results @@ -63,15 +63,15 @@ func end_turn() -> bool: ## of available stories ## ## Returns an array of the stories (resource paths) that were removed -func remove_stories(dir:DirAccess) -> Array[String]: - var results : Array[String] = [] - for file_name in dir.get_files(): - var file_path := dir.get_current_dir() + "/" + file_name - if FileAccess.file_exists(file_path): - results.append(file_path) - available_stories.erase(file_path) - else: - push_error("Tried to remove non-existing story path: %s" % file_path) +func remove_stories(path: String) -> Array[String]: + var story_loader := StoryLoader.new() + + var results: Array[String] = [] + + for story_path in story_loader.get_story_paths_in_directory(path): + results.append(story_path) + available_stories.erase(story_path) + return results