Skip to content

Commit ddb2597

Browse files
Ishigh1qwint
authored andcommitted
Core: Fix auto-fill in the text client when clicking on a hint suggestion (ArchipelagoMW#3267)
1 parent 353ec66 commit ddb2597

File tree

4 files changed

+64
-34
lines changed

4 files changed

+64
-34
lines changed

MultiServer.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import NetUtils
3939
import Utils
40-
from Utils import version_tuple, restricted_loads, Version, async_start
40+
from Utils import version_tuple, restricted_loads, Version, async_start, get_intended_text
4141
from NetUtils import Endpoint, ClientStatus, NetworkItem, decode, encode, NetworkPlayer, Permission, NetworkSlot, \
4242
SlotType, LocationStore
4343

@@ -1095,28 +1095,6 @@ def json_format_send_event(net_item: NetworkItem, receiving_player: int):
10951095
"item": net_item}
10961096

10971097

1098-
def get_intended_text(input_text: str, possible_answers) -> typing.Tuple[str, bool, str]:
1099-
picks = Utils.get_fuzzy_results(input_text, possible_answers, limit=2)
1100-
if len(picks) > 1:
1101-
dif = picks[0][1] - picks[1][1]
1102-
if picks[0][1] == 100:
1103-
return picks[0][0], True, "Perfect Match"
1104-
elif picks[0][1] < 75:
1105-
return picks[0][0], False, f"Didn't find something that closely matches '{input_text}', " \
1106-
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"
1107-
elif dif > 5:
1108-
return picks[0][0], True, "Close Match"
1109-
else:
1110-
return picks[0][0], False, f"Too many close matches for '{input_text}', " \
1111-
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"
1112-
else:
1113-
if picks[0][1] > 90:
1114-
return picks[0][0], True, "Only Option Match"
1115-
else:
1116-
return picks[0][0], False, f"Didn't find something that closely matches '{input_text}', " \
1117-
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"
1118-
1119-
11201098
class CommandMeta(type):
11211099
def __new__(cls, name, bases, attrs):
11221100
commands = attrs["commands"] = {}

Utils.py

+35
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,41 @@ def get_fuzzy_ratio(word1: str, word2: str) -> float:
622622
)
623623

624624

625+
def get_intended_text(input_text: str, possible_answers) -> typing.Tuple[str, bool, str]:
626+
picks = get_fuzzy_results(input_text, possible_answers, limit=2)
627+
if len(picks) > 1:
628+
dif = picks[0][1] - picks[1][1]
629+
if picks[0][1] == 100:
630+
return picks[0][0], True, "Perfect Match"
631+
elif picks[0][1] < 75:
632+
return picks[0][0], False, f"Didn't find something that closely matches '{input_text}', " \
633+
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"
634+
elif dif > 5:
635+
return picks[0][0], True, "Close Match"
636+
else:
637+
return picks[0][0], False, f"Too many close matches for '{input_text}', " \
638+
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"
639+
else:
640+
if picks[0][1] > 90:
641+
return picks[0][0], True, "Only Option Match"
642+
else:
643+
return picks[0][0], False, f"Didn't find something that closely matches '{input_text}', " \
644+
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"
645+
646+
647+
def get_input_text_from_response(text: str, command: str) -> typing.Optional[str]:
648+
if "did you mean " in text:
649+
for question in ("Didn't find something that closely matches",
650+
"Too many close matches"):
651+
if text.startswith(question):
652+
name = get_text_between(text, "did you mean '",
653+
"'? (")
654+
return f"!{command} {name}"
655+
elif text.startswith("Missing: "):
656+
return text.replace("Missing: ", "!hint_location ")
657+
return None
658+
659+
625660
def open_filename(title: str, filetypes: typing.Iterable[typing.Tuple[str, typing.Iterable[str]]], suggest: str = "") \
626661
-> typing.Optional[str]:
627662
logging.info(f"Opening file input dialog for {title}.")

kvui.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
fade_in_animation = Animation(opacity=0, duration=0) + Animation(opacity=1, duration=0.25)
6565

6666
from NetUtils import JSONtoTextParser, JSONMessagePart, SlotType
67-
from Utils import async_start
67+
from Utils import async_start, get_input_text_from_response
6868

6969
if typing.TYPE_CHECKING:
7070
import CommonClient
@@ -285,16 +285,10 @@ def on_touch_down(self, touch):
285285
temp = MarkupLabel(text=self.text).markup
286286
text = "".join(part for part in temp if not part.startswith(("[color", "[/color]", "[ref=", "[/ref]")))
287287
cmdinput = App.get_running_app().textinput
288-
if not cmdinput.text and " did you mean " in text:
289-
for question in ("Didn't find something that closely matches, did you mean ",
290-
"Too many close matches, did you mean "):
291-
if text.startswith(question):
292-
name = Utils.get_text_between(text, question,
293-
"? (")
294-
cmdinput.text = f"!{App.get_running_app().last_autofillable_command} {name}"
295-
break
296-
elif not cmdinput.text and text.startswith("Missing: "):
297-
cmdinput.text = text.replace("Missing: ", "!hint_location ")
288+
if not cmdinput.text:
289+
input_text = get_input_text_from_response(text, App.get_running_app().last_autofillable_command)
290+
if input_text is not None:
291+
cmdinput.text = input_text
298292

299293
Clipboard.copy(text.replace("&amp;", "&").replace("&bl;", "[").replace("&br;", "]"))
300294
return self.parent.select_with_touch(self.index, touch)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
3+
from Utils import get_intended_text, get_input_text_from_response
4+
5+
6+
class TestClient(unittest.TestCase):
7+
def test_autofill_hint_from_fuzzy_hint(self) -> None:
8+
tests = (
9+
("item", ["item1", "item2"]), # Multiple close matches
10+
("itm", ["item1", "item21"]), # No close match, multiple option
11+
("item", ["item1"]), # No close match, single option
12+
("item", ["\"item\" 'item' (item)"]), # Testing different special characters
13+
)
14+
15+
for input_text, possible_answers in tests:
16+
item_name, usable, response = get_intended_text(input_text, possible_answers)
17+
self.assertFalse(usable, "This test must be updated, it seems get_fuzzy_results behavior changed")
18+
19+
hint_command = get_input_text_from_response(response, "hint")
20+
self.assertIsNotNone(hint_command,
21+
"The response to fuzzy hints is no longer recognized by the hint autofill")
22+
self.assertEqual(hint_command, f"!hint {item_name}",
23+
"The hint command autofilled by the response is not correct")

0 commit comments

Comments
 (0)