Skip to content

Commit 8c9ec2d

Browse files
committed
mp refines
1 parent cb38eb0 commit 8c9ec2d

9 files changed

Lines changed: 563 additions & 137 deletions

File tree

‎game/Submods/MAICA_ChatSubmod/api.rpy‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ define _maica_selected_visuals = []
3434
# "mpostal_attachment_path":"",
3535
# "vista_image_info":{},
3636
# "responsed_content": "",
37-
# "responsed_status":"delaying|notupload|received|readed|failed|fatal",
37+
# "responsed_status":"delaying|notupload|received|readed|failed|newfatal|fatal",
38+
# "retry_after": None,
3839
# "failure_status": None,
3940
# "failure_protocol_status": None,
4041
# "failure_protocol_code": None,

‎game/Submods/MAICA_ChatSubmod/chat.rpy‎

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,20 @@ init 5 python:
491491

492492
def is_mail_waiting_reply():
493493
for i in persistent._maica_send_or_received_mpostals:
494-
if i["responsed_status"] in ("received", "failed"):
494+
if i["responsed_status"] in ("received", "failed", "newfatal"):
495495
return True
496496
return False
497497

498+
def maica_retry_mpostal(postal, reset_count=False):
499+
"""Delay the next attempt without changing the original letter time."""
500+
import time
501+
postal["retry_after"] = (
502+
time.time() + persistent.maica_setting_dict["mpostal_default_reply_time"] * 60
503+
)
504+
postal["responsed_status"] = "delaying"
505+
if reset_count:
506+
postal["failed_count"] = 0
507+
498508
@store.mas_submod_utils.functionplugin("ch30_loop", priority=-100)
499509
def push_mpostal_reply():
500510
if (
@@ -516,6 +526,12 @@ init 5 python:
516526
if wait_replying_count > 3:
517527
min_response_time *= 2
518528
if i["responsed_status"] == "delaying":
529+
# Retries wait a full interval after the failure is acknowledged.
530+
if i.get("retry_after") is not None:
531+
if time.time() >= i["retry_after"]:
532+
i["responsed_status"] = "notupload"
533+
i.pop("retry_after", None)
534+
continue
519535
# 时间计算
520536
last_sesh_ed = persistent.sessions.get("last_session_end", datetime.datetime.now())
521537

@@ -530,7 +546,7 @@ init 5 python:
530546
if time.time() - float(i['time']) > min_response_time:
531547
i["responsed_status"] = "notupload"
532548

533-
elif i["responsed_status"] in ("received", "failed"):
549+
elif i["responsed_status"] in ("received", "failed", "newfatal"):
534550
wait_replying_count += 1
535551

536552
return
@@ -1070,9 +1086,9 @@ label .talking_start:
10701086
call clear_all
10711087
return
10721088

1073-
label maica_connection_failure_dialogue(from_mspire = False, status_code = None):
1089+
label maica_connection_failure_dialogue(from_mspire = False, status_code = None, fallback_to_current = True):
10741090
$ ai = store.maica.maica_instance
1075-
$ failure_status = ai.status if status_code is None else status_code
1091+
$ failure_status = ai.status if (status_code is None and fallback_to_current) else status_code
10761092

10771093
if failure_status == ai.MaicaAiStatus.TOKEN_MISSING:
10781094
m 2rusdlb "...It seems you haven't got a token yet."
@@ -1467,35 +1483,33 @@ label maica_mpostal_received:
14671483
# 在重启后加入事件队列等待推送,随机对话频率设置为0将永远不推送
14681484
label maica_mpostal_replyed:
14691485
$ ev = mas_getEV("maica_mpostal_replyed")
1470-
python:
1471-
1472-
def _curr_count():
1473-
curr_queue_count = 0
1474-
for i in persistent._maica_send_or_received_mpostals:
1475-
if i["responsed_status"] == "received":
1476-
curr_queue_count += 1
1477-
return curr_queue_count
1478-
14791486
$ seq = 0
1487+
$ all_seq = 0
1488+
$ mpostal_shown_count = 0
14801489

14811490
# 这里是生成结果
1482-
label maica_mpostal_replyed.select_letter(is_repeat=False):
1491+
label maica_mpostal_replyed.select_letter:
1492+
$ is_repeat = False
14831493
$ current = None
14841494
python:
14851495
for letter in persistent._maica_send_or_received_mpostals:
1486-
if letter["responsed_status"] in ["received", "failed", "notupload"]:
1496+
all_seq += 1
1497+
# If there's "notupload" in queue, we generate them together to make game experience smoother
1498+
# Though those don't actively trigger mpostal_replyed
1499+
if letter["responsed_status"] in ["received", "failed", "newfatal", "notupload"]:
14871500
current = letter
14881501
break
14891502
if current is None:
14901503
jump maica_mpostal_replyed.end
1491-
1492-
label maica_mpostal_replyed.start:
14931504
$ seq += 1
14941505

1506+
label maica_mpostal_replyed.start:
14951507
# This method iters over ALL history letters.
14961508
# That means we cannot include "fatal"s in, because they'd come up every time before manually handled.
14971509
# So we only write reactions for non-stale status.
1498-
if current["responsed_status"] in ("failed"):
1510+
1511+
# newfatal is acknowledged once, then kept as fatal for manual handling.
1512+
if current["responsed_status"] in ("failed", "newfatal"):
14991513
if not is_repeat:
15001514
if seq <= 1:
15011515
m 2lksdlb "Uh, [player], {w=0.5}about your last letter."#担心
@@ -1506,12 +1520,17 @@ label maica_mpostal_replyed.start:
15061520
m 2lksdlb "Uh, [player], I'm really sorry but the Heaven Forest seems not working now."
15071521
m 2ekc "Let me see..."
15081522

1509-
call maica_connection_failure_dialogue(status_code = current.get("failure_status"))
1523+
$ failure_status = current.get("failure_status")
1524+
$ fallback_to_current = "failure_status" not in current
1525+
call maica_connection_failure_dialogue(status_code = failure_status, fallback_to_current = fallback_to_current)
15101526
if current["responsed_status"] == "failed":
15111527
m 1eua "It's okay, I'll remember to write you back as soon as you address that issue."
1512-
$ current["responsed_status"] = "notupload"
1528+
$ maica_retry_mpostal(current)
1529+
elif current["responsed_status"] == "newfatal":
1530+
m 1eksdla "I tried several times on this one but without success. But you can still use the 'Resend mail' button in 'Reread MPostal letters' menu, to let me try again."
1531+
$ current["responsed_status"] = "fatal"
15131532

1514-
elif current["responsed_status"] in ("received"):
1533+
elif current["responsed_status"] == "received":
15151534
if seq <= 1:
15161535
m 7hub "Oh, [player]! {w=0.5}I've finished writing you my reply!"
15171536
else:
@@ -1522,8 +1541,9 @@ label maica_mpostal_replyed.start:
15221541

15231542
call maica_mpostal_show(current["responsed_content"])
15241543
$ current["responsed_status"] = "readed"
1544+
$ mpostal_shown_count += 1
15251545

1526-
elif current["responsed_status"] in ("notupload"):
1546+
elif current["responsed_status"] == "notupload":
15271547
if seq <= 1:
15281548
m 3eksdlb "Oh, your letter [player]! I was kind of in a hurry so it's not completely ready yet."#尴尬
15291549
else:
@@ -1533,26 +1553,29 @@ label maica_mpostal_replyed.start:
15331553
show black with dissolve
15341554
call maica_mpostal_read
15351555

1536-
if _return == "failed":
1556+
if current["responsed_status"] != "received":
15371557
hide black with dissolve
1538-
# 直接重新开始, 失败的信会提示失败, 理论应与current一致
1539-
jump maica_mpostal_replyed.select_letter(is_repeat=True)
1558+
# The batch result belongs to all letters; inspect this letter only.
1559+
$ is_repeat = True
1560+
jump maica_mpostal_replyed.start
15401561

15411562
m "Okay, here it is!"
15421563
hide black with dissolve
15431564
call maica_mpostal_show(current["responsed_content"])
15441565
$ current["responsed_status"] = "readed"
1566+
$ mpostal_shown_count += 1
15451567

15461568
jump maica_mpostal_replyed.select_letter
15471569

15481570
label maica_mpostal_replyed.end:
1571+
if not mpostal_shown_count:
1572+
return "no_unlock"
15491573
if ev.shown_count <= 2:
15501574
m 2lksdlb "I have to admit that I'm not quite used to writing here, but I hope it's not too bad!"
15511575
elif ev.shown_count <= 4:
15521576
m 2lksdlb "May not as good as my former poems though, but I really tried. Hope you like it!"
15531577
else:
1554-
$ it = renpy.substitute(_("it") if seq <= 1 else _("these"))
1555-
m 2tublu "I have to assume you're loving {it} now, since you did write to me a lot!"
1578+
m 2tublu "I have to assume you're loving these now, since you did write to me a lot!"
15561579
m 5ekbsa "And welcome writing to me again anytime you like!"
15571580
return "no_unlock"
15581581

‎game/Submods/MAICA_ChatSubmod/main.rpy‎

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -382,39 +382,66 @@ label maica_connect_from_settings:
382382
return
383383

384384
label maica_mpostal_read:
385+
$ renpy.dynamic("mpostal_read_result")
386+
$ mpostal_read_result = "success"
385387
$ mas_HKBRaiseShield()
386388
if persistent.maica_setting_dict.get("show_console_when_reply", False):
387389
call maica_show_console
388390
else:
389391
window hide
390392
call maica_mpostal_load
391-
call maica_init_connect(use_pause_instand_wait = True)
392-
if _return == "disconnected":
393-
jump maica_mpostal_read.failed
394-
395393
python:
396394
ai = store.maica.maica_instance
397-
import time
395+
import bot_interface
398396
import traceback
399397

400-
def _save_mpostal_failure_snapshot(postal):
398+
def _save_mpostal_failure_snapshot(postal, error=None):
401399
"""Keep the request failure details with the postal that caused them."""
402-
postal["failure_status"] = getattr(ai, "status", None)
403-
postal["failure_protocol_status"] = getattr(ai, "error_protocol_status", None)
404-
postal["failure_protocol_code"] = getattr(ai, "error_protocol_code", None)
405-
failure_message = getattr(ai, "error_message", None)
400+
# Local setup errors do not update ai.status. Do not attribute them
401+
# to an earlier request or to the current healthy connection.
402+
postal["failure_status"] = getattr(ai, "status", None) if error is None else None
403+
postal["failure_protocol_status"] = (
404+
getattr(ai, "error_protocol_status", None) if error is None
405+
else "client_mpostal_failed"
406+
)
407+
postal["failure_protocol_code"] = getattr(ai, "error_protocol_code", None) if error is None else None
408+
failure_message = getattr(ai, "error_message", None) if error is None else error
406409
postal["failure_message"] = (
407-
None if failure_message is None else u"{}".format(failure_message)
410+
None if failure_message is None else bot_interface.to_unicode(failure_message)
408411
)
409412

413+
def _record_mpostal_failure(postal, error=None):
414+
_save_mpostal_failure_snapshot(postal, error)
415+
postal["responsed_status"] = "failed"
416+
postal["failed_count"] = postal.get("failed_count", 0) + 1
417+
if postal["failed_count"] >= 3:
418+
postal["responsed_status"] = "newfatal"
419+
postal["responsed_content"] = renpy.substitute(
420+
_("Failed replying mail. Not retrying because failure count limit reached")
421+
) + "\n" + (postal.get("responsed_content") or "")
422+
store.mas_submod_utils.submod_log.error(
423+
"label maica_mpostal_read: retry limit reached for '{}'".format(
424+
postal["raw_title"]
425+
)
426+
)
427+
410428
pending_postals = [
411429
postal
412430
for postal in persistent._maica_send_or_received_mpostals
413431
if postal["responsed_status"] == "notupload"
414432
]
433+
434+
call maica_init_connect(use_pause_instand_wait = True)
435+
if _return != "success":
436+
python:
437+
mpostal_read_result = "failed"
438+
for cur_postal in pending_postals:
439+
_record_mpostal_failure(cur_postal)
440+
jump maica_mpostal_read.failed
441+
442+
python:
415443
total_pending = len(pending_postals)
416444
for current_index, cur_postal in enumerate(pending_postals, 1):
417-
start_time = time.time()
418445
try:
419446
vista_info = cur_postal.get("vista_image_info") or {}
420447
uuid = vista_info.get("uuid")
@@ -424,17 +451,10 @@ label maica_mpostal_read:
424451
uuid = store.maica.upload_vista_image(image_source)
425452
cur_postal['vista_image_info'] = ai.vista_manager.get_info(uuid)
426453
ai.start_MPostal(cur_postal["raw_content"], title=cur_postal["raw_title"], visions = [ai.generate_vista_url(uuid)] if uuid else None)
427-
except Exception:
428-
_save_mpostal_failure_snapshot(cur_postal)
429-
cur_postal["responsed_status"] = "failed"
430-
cur_postal["failed_count"] = cur_postal.get("failed_count", 0) + 1
431-
_return = "failed"
454+
except Exception as error:
455+
_record_mpostal_failure(cur_postal, error)
456+
mpostal_read_result = "failed"
432457
store.mas_submod_utils.submod_log.error("label maica_mpostal_read: request setup failed: {}".format(traceback.format_exc()))
433-
if cur_postal["failed_count"] >= 3:
434-
cur_postal["responsed_status"] = "fatal"
435-
cur_postal["responsed_content"] = renpy.substitute(_("Failed replying mail. Not retrying because failure count limit reached")) + "\n" + cur_postal["responsed_content"]
436-
store.mas_submod_utils.submod_log.error("label maica_mpostal_read: retry limit reached for '{}'".format(cur_postal["raw_title"]))
437-
break
438458
continue
439459

440460
ai.console_logger.info("<Function> Processing mpostal {} ({}/{})".format(cur_postal["raw_title"], current_index, total_pending))
@@ -456,35 +476,26 @@ label maica_mpostal_read:
456476
message = ai.get_message()
457477
cur_postal["responsed_content"] = message[1]
458478
cur_postal["responsed_status"] = "received"
459-
_return = "success"
460479

461480
if ai.is_failed():
462-
_save_mpostal_failure_snapshot(cur_postal)
463-
cur_postal["responsed_status"] = "failed"
464-
cur_postal["responsed_content"] += renpy.substitute(_("Failed replying mail, check submod_log.log for details\nError code: [ai.status] | [ai.MaicaAiStatus.get_description(ai.status)]"))
465-
_return = "failed"
481+
cur_postal["responsed_content"] = (cur_postal.get("responsed_content") or "") + renpy.substitute(_("Failed replying mail, check submod_log.log for details\nError code: [ai.status] | [ai.MaicaAiStatus.get_description(ai.status)]"))
482+
_record_mpostal_failure(cur_postal)
483+
elif cur_postal["responsed_status"] != "received":
484+
_record_mpostal_failure(
485+
cur_postal, "MPostal request ended without a reply"
486+
)
466487

467-
if _return == "success" and cur_postal["responsed_status"] == "received":
488+
if cur_postal["responsed_status"] == "received":
468489
store.maica.delete_mpostal_original(cur_postal)
469-
470-
if _return != 'success':
471-
if cur_postal.get("failed_count", 0) >= 3:
472-
cur_postal["responsed_status"] = "fatal"
473-
cur_postal["responsed_content"] = renpy.substitute(_("Failed replying mail. Not retrying because failure count limit reached")) + "\n" +cur_postal["responsed_content"]
474-
store.mas_submod_utils.submod_log.error("label maica_mpostal_read: retry limit reached for '{}'".format(cur_postal["raw_title"]))
475-
break
476-
else:
477-
if "failed_count" not in cur_postal:
478-
cur_postal["failed_count"] = 0
479-
cur_postal["failed_count"] += 1
480-
490+
else:
491+
mpostal_read_result = "failed"
481492

482493
label maica_mpostal_read.failed:
483494
call maica_hide_console
484495
if not persistent.maica_setting_dict.get("show_console_when_reply", False):
485496
window show
486497
$ mas_HKBRaiseShield()
487-
return _return
498+
return mpostal_read_result
488499

489500

490501
label maica_mpostal_show(content = "no content"):

‎game/Submods/MAICA_ChatSubmod/screen_subs.rpy‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,9 +1107,9 @@ screen maica_mpostals():
11071107
Function(_maica_call_in_new_context_preserve_layers, "maica_mpostal_show_backtoscreen", postal["responsed_content"])
11081108
]
11091109

1110-
if postal["responsed_status"] in ("fatal"):
1110+
if postal["responsed_status"] == "fatal":
11111111
textbutton _("Resend mail"):
1112-
action SetDict(postal, "responsed_status", "delaying")
1112+
action Function(maica_retry_mpostal, postal, reset_count=True)
11131113
hbox:
11141114
textbutton _("Delete"):
11151115
action Function(_delete_postal, postal)

0 commit comments

Comments
 (0)