@ -2,9 +2,8 @@ use std::collections::BinaryHeap;
use std ::collections ::HashMap ;
use std ::collections ::HashMap ;
use std ::ops ::Deref ;
use std ::ops ::Deref ;
use fuzzy_matcher ::FuzzyMatcher ;
use fuzzy_matcher ::skim ::SkimMatcherV2 ;
use fuzzy_matcher ::skim ::SkimMatcherV2 ;
use multi_map::MultiMap ;
use fuzzy_matcher::FuzzyMatcher ;
use serde_derive ::Deserialize ;
use serde_derive ::Deserialize ;
use serde_derive ::Serialize ;
use serde_derive ::Serialize ;
@ -52,6 +51,9 @@ pub struct WorldArea {
#[ serde(rename = " Name " ) ]
#[ serde(rename = " Name " ) ]
pub name : String ,
pub name : String ,
#[ serde(rename = " NamedId " ) ]
pub named_id : String ,
#[ serde(rename = " Act " ) ]
#[ serde(rename = " Act " ) ]
pub act : i64 ,
pub act : i64 ,
@ -69,23 +71,13 @@ pub struct WorldArea {
}
}
pub type AreaId = usize ;
pub type AreaId = usize ;
pub type WorldAreasMap = MultiMap< AreaName , AreaId , WorldArea > ;
pub type WorldAreasMap = HashMap< String , WorldArea > ;
#[ allow(dead_code) ]
#[ allow(dead_code) ]
pub fn load_world_areas_map ( content : & ' static str ) -> WorldAreasMap {
pub fn load_world_areas_map ( content : & ' static str ) -> WorldAreasMap {
let mut world_area_map = WorldAreasMap ::new ( ) ;
serde_json ::from_str ::< WorldAreasMap > ( content )
serde_json ::from_str ::< HashMap < AreaName , WorldArea > > ( content )
. expect ( "Could not load world areas json" )
. expect ( "Could not load world areas json" )
. into_iter ( )
. for_each ( | ( area_name , world_area ) |
{
world_area_map . insert ( area_name , world_area . key_id , world_area ) ;
} ) ;
world_area_map
}
}
#[ macro_export ]
#[ macro_export ]
macro_rules! load_world_areas {
macro_rules! load_world_areas {
( $e :expr ) = > {
( $e :expr ) = > {
@ -93,7 +85,6 @@ macro_rules! load_world_areas {
} ;
} ;
}
}
#[ allow(dead_code) ]
#[ allow(dead_code) ]
pub struct WorldAreas {
pub struct WorldAreas {
pub areas_map : WorldAreasMap ,
pub areas_map : WorldAreasMap ,
@ -113,18 +104,24 @@ impl WorldAreas {
pub fn fuzzy_area ( & self , count : usize , area : & str ) -> Vec < WorldArea > {
pub fn fuzzy_area ( & self , count : usize , area : & str ) -> Vec < WorldArea > {
let mut matches = Vec ::new ( ) ;
let mut matches = Vec ::new ( ) ;
for ( _k , ( _key_id , v ) ) in & self . areas_map {
for ( _key_id , v ) in & self . areas_map {
if let Some ( mut score ) = self . matcher . fuzzy_match ( & v . name , area ) {
if let Some ( mut score ) = self . matcher . fuzzy_match ( & v . name , area ) {
if v . name . contains ( area ) {
if v . name . contains ( area ) {
score = i64 ::MAX ;
score = i64 ::MAX ;
}
}
let f_entry = FEntry { area : v . clone ( ) , score } ;
let f_entry = FEntry {
area : v . clone ( ) ,
score ,
} ;
matches . push ( f_entry ) ;
matches . push ( f_entry ) ;
}
}
}
}
matches . sort_by ( | entry1 , entry2 | {
matches . sort_by ( | entry1 , entry2 | {
entry1 . score . cmp ( & entry2 . score ) . reverse ( )
entry1
. score
. cmp ( & entry2 . score )
. reverse ( )
. then ( entry1 . area . key_id . cmp ( & entry2 . area . key_id ) )
. then ( entry1 . area . key_id . cmp ( & entry2 . area . key_id ) )
} ) ;
} ) ;
@ -138,21 +135,11 @@ impl WorldAreas {
}
}
#[ allow(dead_code) ]
#[ allow(dead_code) ]
pub fn by_area_id ( & self , key : AreaId ) -> Option < & WorldArea > {
pub fn by_area_id ( & self , key : String ) -> Option < & WorldArea > {
self . areas_map . get_alt ( & key )
}
#[ allow(dead_code) ]
pub fn by_area_name ( & self , key : AreaName ) -> Option < & WorldArea > {
self . areas_map . get ( & key )
self . areas_map . get ( & key )
}
}
}
}
#[ derive(PartialEq, Eq, Clone, Debug) ]
#[ derive(PartialEq, Eq, Clone, Debug) ]
struct FEntry {
struct FEntry {
area : WorldArea ,
area : WorldArea ,
@ -176,9 +163,7 @@ pub struct Matcher(SkimMatcherV2);
impl Matcher {
impl Matcher {
#[ allow(dead_code) ]
#[ allow(dead_code) ]
pub fn new ( ) -> Self {
pub fn new ( ) -> Self {
let matcher = SkimMatcherV2 ::default ( )
let matcher = SkimMatcherV2 ::default ( ) . ignore_case ( ) . use_cache ( true ) ;
. ignore_case ( )
. use_cache ( true ) ;
Matcher ( matcher )
Matcher ( matcher )
}
}
@ -195,13 +180,14 @@ impl Deref for Matcher {
impl Matcher {
impl Matcher {
#[ allow(dead_code) ]
#[ allow(dead_code) ]
pub fn fuzzy_area ( & self , areas : & WorldAreasMap , area : & str ) -> Vec < WorldArea > {
pub fn fuzzy_area ( & self , areas : & WorldAreasMap , area : & str ) -> Vec < WorldArea > {
let mut matches : BinaryHeap < FEntry > = BinaryHeap ::new ( ) ;
let mut matches : BinaryHeap < FEntry > = BinaryHeap ::new ( ) ;
for ( _k , ( _key_id , v ) ) in areas {
for ( _key_id , v ) in areas {
//println!("k: {:?}\nv: {:?}\n\n", k, v);
if let Some ( score ) = self . fuzzy_match ( & v . name , area ) {
if let Some ( score ) = self . fuzzy_match ( & v . name , area ) {
let f_entry = FEntry { area : v . clone ( ) , score } ;
let f_entry = FEntry {
area : v . clone ( ) ,
score ,
} ;
matches . push ( f_entry ) ;
matches . push ( f_entry ) ;
}
}
}
}
@ -222,38 +208,41 @@ impl Matcher {
}
}
}
}
//TODO: Multimap
//TODO: Multimap
#[ allow(dead_code) ]
#[ allow(dead_code) ]
pub fn repack ( content : & str , out_path : & str ) {
pub fn repack ( content : & str , out_path : & str ) {
let areas_json = serde_json ::from_str ::< UnprocessedAreas > ( content ) ;
let areas_json = serde_json ::from_str ::< UnprocessedAreas > ( content ) ;
let area_map = areas_json . expect ( "Could not read world areas" ) . into_iter ( ) . filter_map ( | w_a | {
let area_map = areas_json
if
. expect ( "Could not read world areas" )
w_a . act < 11 & &
. into_iter ( )
! w_a . is_vaal_area & &
. filter_map ( | w_a | {
! w_a . connections_world_areas_keys . is_empty ( ) & &
if w_a . act < 11
! w_a . unknown64 & &
& & ! w_a . is_vaal_area
! w_a . named_id . starts_with ( "Map" ) & &
& & ! w_a . connections_world_areas_keys . is_empty ( )
! w_a . named_id . starts_with ( "Descent" ) & &
& & ! w_a . unknown64
! w_a . named_id . starts_with ( "EndlessLedge" ) & &
& & ! w_a . named_id . starts_with ( "Map" )
w_a . unknown9 ! = 20
& & ! w_a . named_id . starts_with ( "Descent" )
& & ! w_a . named_id . starts_with ( "EndlessLedge" )
& & w_a . unknown9 ! = 20
{
{
Some ( (
Some ( (
w_a . named_id ,
w_a . named_id . clone ( ) ,
WorldArea {
WorldArea {
name : w_a . name ,
name : w_a . name ,
named_id : w_a . named_id ,
act : w_a . act ,
act : w_a . act ,
is_town : w_a . is_town ,
is_town : w_a . is_town ,
has_waypoint : w_a . has_waypoint ,
has_waypoint : w_a . has_waypoint ,
connections_world_areas_keys : w_a . connections_world_areas_keys ,
connections_world_areas_keys : w_a . connections_world_areas_keys ,
key_id : w_a . key_id ,
key_id : w_a . key_id ,
}
} ,
) )
) )
} else {
} else {
None
None
}
}
} ) . collect ::< HashMap < AreaName , WorldArea > > ( ) ;
} )
. collect ::< WorldAreasMap > ( ) ;
let new_json = serde_json ::to_string ( & area_map ) . unwrap ( ) ;
let new_json = serde_json ::to_string ( & area_map ) . unwrap ( ) ;