Skip to content

Commit

Permalink
Refactor story loading and removal
Browse files Browse the repository at this point in the history
  • Loading branch information
squarepear committed Mar 6, 2024
1 parent a1204de commit 208a9ec
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 91 deletions.
2 changes: 1 addition & 1 deletion project/common/story_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions project/story/drama_club/tech.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 1 addition & 3 deletions project/story/rpg/conflict.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
6 changes: 2 additions & 4 deletions project/story/starting_stories/schedule_conflict.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 1 addition & 3 deletions project/story/starting_stories/steven_dnd.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
48 changes: 10 additions & 38 deletions project/test/integration/simple_story_validity_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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():
Expand All @@ -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)


Expand Down Expand Up @@ -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


16 changes: 6 additions & 10 deletions project/test/integration/story_quantity_test.gd
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 3 additions & 5 deletions project/test/unit/world_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
11 changes: 4 additions & 7 deletions project/ui/main_scene.gd
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 18 additions & 18 deletions project/world/world.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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


Expand Down

0 comments on commit 208a9ec

Please sign in to comment.