#CLASS {missiontimers_letters} {kill};
#CLASS {missiontimers_letters} {open};

#VARIABLE {letter_types} {
    {DLA}{DLA (close by\; smudged)}
    {DLB}{DLB (close by\; handwriting)}
    {DLC}{DLC (close by\; vague)}
    {DLD}{DLD (close by\; city)}
    {DLE}{DLE (far away\; smudged)}
    {DLF}{DLF (far away\; handwriting)}
    {DLG}{DLG (far away\; vague)}
    {DLH}{DLH (far away\; city)}
    {DLI}{DLI (difficult customer)}
};
#VARIABLE {missions[letter]} {
    {last_difficulty}{0}
}

#nop Capture offering to deliver a letter;
#ALIAS {^offer to deliver {(d|D)[A-z]{2}}$} {
    #send {%0};
    #format {missions[letter][last_difficulty]} {%u} {%1};
};

#nop Capture offering to return a letter;
#ALIAS {return letter} {
    #send {return letter};
    #format {missions[letter][retry_list][$GMCP[charinfo][capname]]} {%T};
    /letter_returned;
};
#ACTION {^You offer to give an? {.+?} letter to Frank.$} {
    #format {missions[letter][retry_list][$GMCP[charinfo][capname]]} {%T};
    #nop [missions[letter][last_difficulty]];
    /letter_returned;
};

#ALIAS {/letter_returned} {
    #format {missions[letter][retry_list][$GMCP[charinfo][capname]]} {%T};

    #line sub variable {
        #format {delayname} {letter_retry_%s} {$GMCP[charinfo][capname]};
        #delay {$delayname} {
            #format {letter_timer_line} {<acf>[<fff>missions<acf>] <138>*<900><cfa> %s <acf>can now request another letter from Frank.<099>} {$GMCP[charinfo][capname]};
            #echo {$letter_timer_line};
            #unvar {letter_timer_line};

            #unvar {missions[letter][retry_list][$GMCP[charinfo][capname]]};
        } {600};
    };
    #unvar {delayname};
};

#nop Capture being rewarded for completing delivery of the letter;
#ACTION {^You have been awarded {[0-9]{1,7}} experience points for delivering this letter.$} {
    #local {last_difficulty} {$missions[letter][last_difficulty]};

    #nop If we don't have a difficulty match, we can't act.;
    #if {"$last_difficulty" == "0"} {
        #return;
    };


    #nop You have been awarded 60407 experience points and given 2 Agatean Empire ten-rhinu and 5 Agatean Empire rhinu.;
    #format {missions[letter][active_list][$GMCP[charinfo][capname]][$last_difficulty]} {%T};

    #line sub variable {
        #format {delayname} {letter_%s_%s} {$last_difficulty} {$GMCP[charinfo][capname]};
        #delay {$delayname} {
            #format {letter_timer_line} {<acf>[<fff>missions<acf>] <138>*<900><cfa> %s <acf>can now do another "%c%s%c" letter.<099>} {$GMCP[charinfo][capname]} {Jade} {$letter_types[$last_difficulty]} {<acf>};
            #echo {$letter_timer_line};
            #unvar {letter_timer_line};

            #unvar {missions[letter][active_list][$GMCP[charinfo][capname]][$last_difficulty]};
        } {3600};
    };
    #unvar {delayname};
    #variable {missions[letter][last_difficulty]} {0};
};

#ALIAS {/letters_recreate_delays} {
    #format {current_time} {%T};

    #nop == start with retry list;
    #foreach {*missions[letter][retry_list][]} {player} {
        #local {delay_time} {$missions[letter][retry_list][$player]};
        #math {delay_time} {600 - ($current_time - $delay_time)};

        #if {$delay_time < 1} {
            #unvar {missions[letter][retry_list][$player]};
            #continue;
        };

        #line sub variable {
            #delay {$delay_time} {
                #format {letter_timer_line} {<acf>[<fff>missions<acf>] <138>*<900><cfa> %s <acf>can now request another letter from Frank.<099>} {$player};
                #echo {$letter_timer_line};
                #unvar {letter_timer_line};

                #unvar {missions[letter][retry_list][$player]};
            };
        };
    };
    #nop == do all the difficulties;
    #foreach {*missions[letter][active_list][]} {player} {
        #foreach {*missions[letter][active_list][$player][]} {$difficulty} {
            #local {delay_time} {$missions[letter][active_list][$player][$difficulty]};
            #math {delay_time} {3600 - ($current_time - $delay_time)};

            #if {$delay_time < 1} {
                #unvar {missions[letter][active_list][$player][$difficulty]};
                #continue;
            };

            #line sub variable {
                #delay {$delay_time} {
                    #format {letter_timer_line} {<acf>[<fff>missions<acf>] <138>*<900><cfa> %s <acf>can now do another "%c%s%c" letter.<099>} {$player} {Jade} {$letter_types[$difficulty]} {<acf>};
                    #echo {$letter_timer_line};
                    #unvar {letter_timer_line};

                    #unvar {missions[letter][active_list][$player][$difficulty]};
                };
            };
        };
    };
};

#CLASS {missiontimers_letters} {close};
