Skip to content

Commit

Permalink
remove load character and save button conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
neph1 committed Jan 3, 2024
1 parent 288ed13 commit fed1a3e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
7 changes: 5 additions & 2 deletions tale/llm/llm_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions tale/tio/if_browser_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input type="file" id="fileInput" accept=".json, .png, .jpeg, .jpg" value="">', '')
html_content = html_content.replace('<input type="file" id="loadCharacterInput" accept=".json, .png, .jpeg, .jpg">', '')
return html_content


Expand Down
5 changes: 5 additions & 0 deletions tale/tio/mud_browser_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input type="button" id="saveButton" value="Save story" onclick="showSaveDialog()" readonly/>', '')
return html_content


class CustomRequestHandler(WSGIRequestHandler):
Expand Down
5 changes: 0 additions & 5 deletions tale/web/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions tale/web/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ <h2>Your browser doesn't have Javascript or it is disabled. You can't use this w
<input type="submit" value="&#128682" name="quit" id="button-quit" onclick="quit_clicked(); return false;" readonly/>

</form>
<input type="file" id="fileInput" accept=".json, .png, .jpeg, .jpg" value="">
<input type="file" id="loadCharacterInput" accept=".json, .png, .jpeg, .jpg">
<input type="button" id="saveButton" value="Save story" onclick="showSaveDialog()" readonly/>
</div>
</div>
</body>
<script>
document.getElementById('fileInput').addEventListener('change', handleFile);
document.getElementById('loadCharacterInput').addEventListener('change', handleFile);
</script>

</html>
35 changes: 33 additions & 2 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -41,4 +44,32 @@ def test_create_chat_container(self):

assert "chat-container" in result
assert '<div class="user-name" content="Bloated Murklin"></div>' in result
assert '<div class="text-field" type="text">Hello World!</div>' in result
assert '<div class="text-field" type="text">Hello World!</div>' 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 = '<input type="file" id="loadCharacterInput" accept=".json, .png, .jpeg, .jpg">'
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 = '<input type="button" id="saveButton" value="Save story" onclick="showSaveDialog()" readonly/>'
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


9 changes: 5 additions & 4 deletions tests/test_json_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand Down

0 comments on commit fed1a3e

Please sign in to comment.