From fed1a3eba5f6949df95cd23b53daddd5f914a521 Mon Sep 17 00:00:00 2001 From: rickard Date: Wed, 3 Jan 2024 21:16:38 +0100 Subject: [PATCH] remove load character and save button conditionally --- tale/llm/llm_ext.py | 7 +++++-- tale/tio/if_browser_io.py | 3 +-- tale/tio/mud_browser_io.py | 5 +++++ tale/web/script.js | 5 ----- tale/web/story.html | 4 ++-- tests/test_browser.py | 35 +++++++++++++++++++++++++++++++++-- tests/test_json_story.py | 9 +++++---- 7 files changed, 51 insertions(+), 17 deletions(-) diff --git a/tale/llm/llm_ext.py b/tale/llm/llm_ext.py index 62b4d125..b5adcc54 100644 --- a/tale/llm/llm_ext.py +++ b/tale/llm/llm_ext.py @@ -127,9 +127,12 @@ def save(self, save_name: str = '') -> None: json.dump(llm_cache.json_dump(), fp, indent=4) if save_name: - os.mkdir(os.path.join(save_path, 'resources')) + resource_path = os.path.join(save_path, 'resources') + if not os.path.exists(resource_path): + os.mkdir(resource_path) shutil.copy(os.path.join(os.getcwd(), 'story.py'), os.path.join(save_path, 'story.py')) - shutil.copytree(os.path.join(os.getcwd(), 'resources'), os.path.join(save_path, 'resources'), dirs_exist_ok=True) + if os.path.exists(os.path.join(os.getcwd(), 'resources')): + shutil.copytree(os.path.join(os.getcwd(), 'resources'), resource_path, dirs_exist_ok=True) def generate_quest(self, npc: LivingNpc, type: QuestType = QuestType.GIVE) -> Quest: diff --git a/tale/tio/if_browser_io.py b/tale/tio/if_browser_io.py index 51d57bdc..3cfe94c2 100644 --- a/tale/tio/if_browser_io.py +++ b/tale/tio/if_browser_io.py @@ -470,9 +470,8 @@ def wsgi_serve_static(self, path: str, environ: Dict[str, Any], start_response: def modify_web_page(self, player_connection: PlayerConnection, html_content: str) -> None: """Modify the html before it is sent to the browser.""" - # Conditionally modify the content based on your conditions if not "wizard" in player_connection.player.privileges: - html_content = html_content.replace('', '') + html_content = html_content.replace('', '') return html_content diff --git a/tale/tio/mud_browser_io.py b/tale/tio/mud_browser_io.py index 622d413a..bf5256e3 100644 --- a/tale/tio/mud_browser_io.py +++ b/tale/tio/mud_browser_io.py @@ -141,6 +141,11 @@ def wsgi_handle_about(self, environ: Dict[str, Any], parameters: Dict[str, str], num_players=len(self.driver.all_players), player_table=player_table_txt) return [txt.encode("utf-8")] + + def modify_web_page(self, player_connection: PlayerConnection, html_content: str) -> None: + html_content = super().modify_web_page(player_connection, html_content) + html_content = html_content.replace('', '') + return html_content class CustomRequestHandler(WSGIRequestHandler): diff --git a/tale/web/script.js b/tale/web/script.js index 56a22e4c..47bb77f7 100644 --- a/tale/web/script.js +++ b/tale/web/script.js @@ -76,11 +76,6 @@ function process_text(json) populateNpcDropdown(json["npcs"]); } setLocationImage(json["location"].toLowerCase().replace(/ /g, '_') + '.jpg'); - // if (json.hasOwnProperty("location_image") && json["location_image"] !== '') { - // setLocationImage(json["location_image"]); - // } else { - // setLocationImage(null); - // } txtdiv.innerHTML += json["text"]; if(!document.smoothscrolling_busy) smoothscroll(txtdiv, 0); } diff --git a/tale/web/story.html b/tale/web/story.html index 6b905023..5733b602 100644 --- a/tale/web/story.html +++ b/tale/web/story.html @@ -46,13 +46,13 @@

Your browser doesn't have Javascript or it is disabled. You can't use this w - + diff --git a/tests/test_browser.py b/tests/test_browser.py index 3b97160f..7c8e9db3 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -1,9 +1,12 @@ +from os import getcwd from wsgiref.simple_server import WSGIServer from tale.player import PlayerConnection -from tale.tio.if_browser_io import HttpIo +from tale.tio.if_browser_io import HttpIo, TaleWsgiApp +from tale.tio.mud_browser_io import TaleMudWsgiApp from tale.web.web_utils import create_chat_container +from tests.supportstuff import FakeDriver class TestHttpIo: @@ -41,4 +44,32 @@ def test_create_chat_container(self): assert "chat-container" in result assert '
' in result - assert '
Hello World!
' in result \ No newline at end of file + assert '
Hello World!
' in result + + def test_remove_load_character_button(self): + connection = PlayerConnection() + driver = FakeDriver() + wsgi_app = TaleWsgiApp(driver=driver, player_connection=connection, use_ssl=False, ssl_certs=None) + + load_button = '' + with open('tale/web/story.html', 'r') as file: + contents = file.read() + assert load_button in contents + result = wsgi_app.modify_web_page(connection, contents) + + assert load_button not in result + + def test_remove_save_button(self): + connection = PlayerConnection() + driver = FakeDriver() + wsgi_app = TaleMudWsgiApp(driver=driver, use_ssl=False, ssl_certs=None) + + save_button = '' + with open('tale/web/story.html', 'r') as file: + contents = file.read() + assert save_button in contents + result = wsgi_app.modify_web_page(connection, contents) + + assert save_button not in result + + diff --git a/tests/test_json_story.py b/tests/test_json_story.py index baf3f298..b6caf32c 100644 --- a/tests/test_json_story.py +++ b/tests/test_json_story.py @@ -58,10 +58,11 @@ def test_save_story(self): self.story.save() def test_save_story_as(self): - self.story.save('./LlamaTale/stories/test_story2') - assert os.path.exists('../LlamaTale/stories/test_story2') - shutil.rmtree('./stories/test_story2', ignore_errors=True) - assert not os.path.exists('./stories/test_story2') + os.chdir(os.getcwd() + '/stories/test_story/') + self.story.save('test_story2') + assert os.path.exists('../test_story2') + shutil.rmtree('../test_story2', ignore_errors=True) + assert not os.path.exists('../test_story2') class TestAnythingStory():