#CLASS {database} {kill};
#CLASS {database} {open};

#VARIABLE {map_id_names} {
  {1}{Ankh-Morpork}
  {2}{AM Assassins}
  {3}{AM Buildings}
  {4}{AM Cruets}
  {5}{AM Docks}
  {6}{AM Guilds}
  {7}{AM Isle of Gods}
  {8}{Shades Maze}
  {9}{Temple of Small Gods}
  {10}{AM Temples}
  {11}{AM Thieves}
  {12}{Unseen University}
  {13}{AM Warriors}
  {14}{Pseudopolis Watch House}
  {15}{Magpyr's Castle}
  {16}{Bois}
  {17}{Bes Pelargic}
  {18}{BP Buildings}
  {19}{BP Estates}
  {20}{BP Wizards}
  {21}{Brown Islands}
  {22}{Death's Domain}
  {23}{Djelibeybi}
  {24}{IIL - DJB Wizards}
  {25}{Ephebe}
  {26}{Ephebe Underdocks}
  {27}{Genua}
  {28}{Genua Sewers}
  {29}{GRFLX Caves}
  {30}{Hashishim Caves}
  {31}{Klatch Region}
  {32}{Lancre Region}
  {33}{Mano Rossa}
  {34}{Monks of Cool}
  {35}{Netherworld}
  {37}{Pumpkin Town}
  {38}{Ramtops Regions}
  {39}{Sto-Lat}
  {40}{Academy of Artificers}
  {41}{Cabbage Warehouse}
  {42}{AoA Library}
  {43}{Sto-Lat Sewers}
  {44}{Sprite Caves}
  {45}{Sto Plains Region}
  {46}{Uberwald Region}
  {47}{UU Library}
  {48}{Klatchian Farmsteads}
  {49}{CTF Arena}
  {50}{PK Arena}
  {51}{AM Post Office}
  {52}{Ninja Guild}
  {53}{The Travelling Shop}
  {54}{Slippery Hollow}
  {55}{House of Magic - Creel}
  {56}{Special Areas}
  {57}{Skund Wolf Trail}
}

#FUNCTION {map_parse} {
    #unvar {map_term} {search_map};
    #format {search_map} {%l} {%1};

    #regex {$search_map} {^am} { #var {map_term} {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 51} };
    #regex {$search_map} {^ankh-morpork$} { #var {map_term} {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 51} };
    #regex {$search_map} {^bp$} { #var {map_term} {17, 18, 19, 20, 21} };
    #regex {$search_map} {^bes pelargic$} { #var {map_term} {17, 18, 19, 20, 21} };
    #regex {$search_map} {^djb$} { #var {map_term} {23, 24} };
    #regex {$search_map} {^djelibeybi} { #var {map_term} {23, 24} };
    #regex {$search_map} {^ephebe$} { #var {map_term} {25, 26} };
    #regex {$search_map} {^genua$} { #var {map_term} {27, 28, 16} };
    #regex {$search_map} {^ramtops$} { #var {map_term} {38, 32, 54, 55} };
    #regex {$search_map} {^sto$} { #var {map_term} {29, 45} };
    #regex {$search_map} {^sto plains$} { #var {map_term} {29, 45} };
    #regex {$search_map} {^sto-lat$} { #var {map_term} {39, 40, 41, 42, 43} };

    #return {$search_map};
};

#LIST {last_query_list} {create};

#ALIAS {db gatherable %1} {
    #unvariable {gatherable_display};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #format {query} {SELECT item_name, room_short, shop_items.room_id, map_id, INSTR(item_name, '%s') pos FROM shop_items INNER JOIN rooms on rooms.room_id = shop_items.room_id WHERE pos > 0 AND sale_price = 'gather' LIMIT 10;} {$search_term};
    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};

    #script {gatherable_query_result} {$db_command};
    #list {gatherable_query_result} {size} {gatherable_result_size};

    #list {last_query_list} {clear};

    #if {$gatherable_result_size > 0} {
        #format {gatherable_display} {%cResults for gatherables matching "%c%s%c":} {bold blue}{yellow}{%1}{bold blue};
        #echo {$gatherable_display};

        #var {gather_count} {0};

        #foreach {$gatherable_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list gatherable_results create {${inner_query_result}};

            #variable {gatherable_info} {
                {item_name}{$gatherable_results[1]}
                {room_short}{$gatherable_results[2]}
                {room_id}{$gatherable_results[3]}
                {map_name}{$map_id_names[$gatherable_results[4]]}
            };

            #format {gatherable_display} {%c  [%c%d%c]: %c%s%c found in %c%s%c,%c %s%c} {bold blue}{cyan}{$gather_count}{bold blue}{green}{$gatherable_info[item_name]}{bold blue}{orange}{$gatherable_info[room_short]}{bold blue}{orange}{$gatherable_info[map_name]}{bold blue};
            #echo {$gatherable_display};

            #variable {last_query_list[$gather_count]} {$gatherable_info};
            #math {gather_count} {$gather_count + 1};
        }
    } {
        #format {gatherable_display} {%cQuery Result: %c%s%c was not found.} {bold blue}{green}{%1}{bold blue};
        #echo {$gatherable_display};
    }
}

#ALIAS {db npc {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {npc_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&map_term} {
        #format {query} {SELECT npc_name, rooms.map_id, rooms.room_id, rooms.room_short, INSTR(npc_name, '%s') pos FROM npc_info INNER JOIN rooms on rooms.room_id = npc_info.room_id WHERE pos > 0 AND rooms.map_id IN (%s) LIMIT 10} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT npc_name, rooms.map_id, rooms.room_id, rooms.room_short, INSTR(npc_name, '%s') pos FROM npc_info INNER JOIN rooms on rooms.room_id = npc_info.room_id WHERE pos > 0 LIMIT 10} {$search_term};
    }

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {npc_query_result} {$db_command};
    #list {npc_query_result} {size} {npc_result_size};

    #list {last_query_list} {clear};

    #if {$npc_result_size > 0} {
        #format {npc_display} {%cResults for NPCs matching "%c%s%c":} {bold blue}{yellow}{%1}{bold blue};
        #echo {$npc_display};

        #var {npc_count} {0};

        #foreach {$npc_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list npc_results create {${inner_query_result}};

            #variable {npc_info} {
                {npc_name}{$npc_results[1]}
                {map_name}{$map_id_names[$npc_results[2]]}
                {room_id}{$npc_results[3]}
                {room_short}{$npc_results[4]}
            };

            #format {npc_display} {%c  [%c%d%c]: %c%s%c found in %c%s%c,%c %s%c} {bold blue}{cyan}{$npc_count}{bold blue}{green}{$npc_info[npc_name]}{bold blue}{orange}{$npc_info[room_short]}{bold blue}{orange}{$npc_info[map_name]}{bold blue};
            #echo {$npc_display};

            #variable {last_query_list[$npc_count]} {$npc_info};
            #math {npc_count} {$npc_count + 1};
        }
    } {
        #format {npc_display} {%cQuery Result: %c%s%c was not found.} {bold blue}{green}{%1}{bold blue};
        #echo {$npc_display};
    }
};

#ALIAS {db npcitem {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {npcitem_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&map_term} {
        #format {query} {SELECT npc_info.map_id, npc_name, npc_info.room_id, item_name, room_short, INSTR(item_name, '%s') pos FROM 'npc_info' INNER JOIN npc_items ON npc_items.npc_id = npc_info.npc_id INNER JOIN rooms on npc_info.room_id = rooms.room_id WHERE pos > 0 AND rooms.map_id IN (%s) LIMIT 10} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT npc_info.map_id, npc_name, npc_info.room_id, item_name, room_short, INSTR(item_name, '%s') pos FROM 'npc_info' INNER JOIN npc_items ON npc_items.npc_id = npc_info.npc_id INNER JOIN rooms on npc_info.room_id = rooms.room_id WHERE pos > 0 LIMIT 10} {$search_term};
    }

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {npcitem_query_result} {$db_command};
    #list {npcitem_query_result} {size} {npcitem_result_size};

    #list {last_query_list} {clear};

    #if {$npcitem_result_size > 0} {
        #format {npcitem_display} {%cResults for NPC items matching "%c%s%c":} {bold blue}{yellow}{%1}{bold blue};
        #echo {$npcitem_display};

        #var {npcitem_count} {0};

        #foreach {$npcitem_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list npcitem_results create {${inner_query_result}};

            #variable {npcitem_info} {
                {map_name}{$map_id_names[$npcitem_results[1]]}
                {npc_name}{$npcitem_results[2]}
                {room_id}{$npcitem_results[3]}
                {item_name}{$npcitem_results[4]}
                {room_short}{$npcitem_results[5]}
            };

            #format {npcitem_display} {%c  [%c%d%c]: %c%s%c found on %c%s%c in %c%s%c,%c %s%c} {bold blue}{cyan}{$npcitem_count}{bold blue}{green}{$npcitem_info[item_name]}{bold blue}{white}{$npcitem_info[npc_name]}{bold blue}{orange}{$npcitem_info[room_short]}{bold blue}{orange}{$npcitem_info[map_name]}{bold blue};
            #echo {$npcitem_display};

            #variable {last_query_list[$npcitem_count]} {$npcitem_info};
            #math {npcitem_count} {$npcitem_count + 1};
        }
    } {
        #format {npcitem_display} {%cQuery Result: %c%s%c was not found.} {bold blue}{green}{%1}{bold blue};
        #echo {$npcitem_display};
    }
};

#ALIAS {^db item {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {item_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&map_term} {
        #format {query} {SELECT item_name, sale_price, room_short, shop_items.room_id, map_id, INSTR(item_name, '%s') pos FROM shop_items INNER JOIN rooms on rooms.room_id = shop_items.room_id WHERE pos > 0 AND sale_price != 'gather' AND map_id IN (%s) LIMIT 10;} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT item_name, sale_price, room_short, shop_items.room_id, map_id, INSTR(item_name, '%s') pos FROM shop_items INNER JOIN rooms on rooms.room_id = shop_items.room_id WHERE pos > 0 AND sale_price != 'gather' LIMIT 10;} {$search_term};
    }

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {item_query_result} {$db_command};
    #list {item_query_result} {size} {item_result_size};

    #list {last_query_list} {clear};

    #if {$item_result_size > 0} {
        #format {item_display} {%cResults for items matching "%c%s%c":} {bold blue}{yellow}{%1}{bold blue};
        #echo {$item_display};

        #var {item_count} {0};

        #foreach {$item_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list item_results create {${inner_query_result}};

            #variable {item_info} {
                {item_name}{$item_results[1]}
                {sale_price}{$item_results[2]}
                {room_short}{$item_results[3]}
                {room_id}{$item_results[4]}
                {map_name}{$map_id_names[$item_results[5]]}
            };

            #format {item_display} {%c  [%c%d%c]: %c%s%c (%c%s%c) found in %c%s%c,%c %s%c} {bold blue}{cyan}{$item_count}{bold blue}{green}{$item_info[item_name]}{bold blue}{yellow}{$item_info[sale_price]}{bold blue}{orange}{$item_info[room_short]}{bold blue}{orange}{$item_info[map_name]}{bold blue};
            #echo {$item_display};

            #variable {last_query_list[$item_count]} {$item_info};
            #math {item_count} {$item_count + 1};
        };
    } {
        #format {item_display} {%cQuery Result: %c%s%c was not found.} {bold blue}{green}{%1}{bold blue};
        #echo {$item_display};
    };
};

#ALIAS {^db room {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {room_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&map_term} {
        #format {query} {SELECT room_id, map_id, room_short, room_type, INSTR(room_short, '%s') pos FROM rooms WHERE pos > 0 AND map_id IN (%s) LIMIT 10;} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT room_id, map_id, room_short, room_type, INSTR(room_short, '%s') pos FROM rooms WHERE pos > 0 LIMIT 10;} {$search_term};
    }

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {room_query_result} {$db_command};
    #list {room_query_result} {size} {room_result_size};

    #list {last_query_list} {clear};

    #if {$room_result_size > 0} {
        #format {room_display} {%cResults for items items matching "%c%s%c":} {bold blue}{yellow}{%1}{bold blue};
        #echo {$room_display};

        #var {room_count} {0};

        #foreach {$room_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list room_results create {${inner_query_result}};

            #variable {room_info} {
                {room_id}{$room_results[1]}
                {map_name}{$map_id_names[$room_results[2]]}
                {room_short}{$room_results[3]}
                {room_type}{$room_results[4]}
            };

            #format {room_display} {%c  [%c%d%c]: %c%s%c (%c%s%c) found in %c%s%c} {bold blue}{cyan}{$room_count}{bold blue}{green}{$room_info[room_short]}{bold blue}{yellow}{$room_info[room_type]}{bold blue}{orange}{$room_info[map_name]}{bold blue};
            #echo {$room_display};

            #variable {last_query_list[$room_count]} {$room_info};
            #math {room_count} {$room_count + 1};
        }
    } {
        #format {room_display} {%cQuery Result: %c%s%c was not found.} {bold blue}{green}{%1}{bold blue};
        #echo {$room_display};
    };
};

#ALIAS {db route %1} {
    #if {&last_query_list[%1]} {
        #var {target_room} {$last_query_list[%1]};
        #format {route_display} {%c[speedwalk] Generating speedwalk from current location to %c%s%c, %c%s%c...} {bold blue}{green}{$target_room[room_short]}{bold blue}{orange}{$target_room[map_name]}{bold blue};
        
        #echo {$route_display};

        #var {route_current_room_id} {$GMCP[room][info][identifier]};
        #var {route_target_room_id} {$target_room[room_id]};

        #format {route_command} {python3 src/maproute.py %s %s} {$route_current_room_id} {$route_target_room_id};
        #script {speedwalk_result} {$route_command};

        #if {"$speedwalk_result[1]" == "0"} {
            #format {route_error} {%c[speedwalk] Unable to find route to %c%s%c or unknown current room.} {bold blue}{green}{$target_room[room_short]}{bold blue};
            #echo {$route_error};
            #return;
        }
        #send {$speedwalk_result[1]};
        
        #variable {route_length} {$speedwalk_result[1]};
        #replace {route_length} {\\\;} {;};
        #replace {route_length} { } {};
        #list {route_length} {create} {$route_length};
        #list {route_length} {size} {route_length};

        #format {route_confirm} {%c[speedwalk] A route was found, type "%cspeedwalk%c" to fast travel. [%g steps]} {bold blue}{green}{bold blue}{$route_length};
        #echo {$route_confirm};
    } {
        #format {route_display} {%c[speedwalk] No previous query data found, try searching first.} {bold blue};
        #echo {$route_display};
    }
};

#ALIAS {speedwalk} {
    #send {RuhsSpeedRun};
}

#ALIAS {db routeto %1} {
    #var {target_room} {%1};
    #format {route_display} {[speedwalk] Generating speedwalk from current location to specified id %s.} {%1};

    #echo {$route_display};

    #var {route_current_room_id} {$GMCP[room][info][identifier]};
    #var {route_target_room_id} {%1};

    #format {route_command} {python3 src/maproute.py %s %s} {$route_current_room_id} {$route_target_room_id};
    #script {speedwalk_result} {$route_command};

    #if {"$speedwalk_result[1]" == "0"} {
        #format {route_error} {%c[speedwalk] Unable to find route to %c%s%c or unknown current room.} {bold blue}{green}{$target_room[room_short]}{bold blue};
        #echo {$route_error};
        #return;
    }
    #send {$speedwalk_result[1]};

    #variable {route_length} {$speedwalk_result[1]};
    #replace {route_length} {\\\;} {;};
    #replace {route_length} { } {};
    #list {route_length} {create} {$route_length};
    #list {route_length} {size} {route_length};

    #format {route_confirm} {%c[speedwalk] A route was found, type "%cspeedwalk%c" to fast travel. [%g steps]} {bold blue}{green}{bold blue}{$route_length};
    #echo {$route_confirm};
};


#CLASS {database} {close};
