Skip to content

Commit

Permalink
refactor: replace tuple with [_; 3]
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed Aug 3, 2024
1 parent f780813 commit 04ce0b0
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 96 deletions.
7 changes: 1 addition & 6 deletions src-tauri/src/dataset/game_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,7 @@ impl GameStructure {
.collect::<Result<_, ParseError>>()?;
let name = SpotName::new(key);
let any_of_all_requirements = to_any_of_all_requirements(value)?;
let mut items = items.into_iter();
let items = (
items.next().unwrap(),
items.next().unwrap(),
items.next().unwrap(),
);
let items = [items[0], items[1], items[2]];
let spot = ShopSpot::new(field_number, name, items, any_of_all_requirements);
shops.push(spot)
}
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/dataset/spot/shop_spot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::script::enums::{FieldNumber, ShopItem};
use super::params::{AnyOfAllRequirements, SpotName, SpotParams};

#[derive(Clone, Debug)]
pub struct ShopSpot(SpotParams<(Option<ShopItem>, Option<ShopItem>, Option<ShopItem>)>);
pub struct ShopSpot(SpotParams<[Option<ShopItem>; 3]>);

impl ShopSpot {
pub fn new(
field_number: FieldNumber,
name: SpotName,
content: (Option<ShopItem>, Option<ShopItem>, Option<ShopItem>),
content: [Option<ShopItem>; 3],
requirements: Option<AnyOfAllRequirements>,
) -> Self {
if cfg!(debug_assertions) {
Expand All @@ -27,7 +27,7 @@ impl ShopSpot {
pub fn name(&self) -> &SpotName {
&self.0.name
}
pub fn items(&self) -> (Option<ShopItem>, Option<ShopItem>, Option<ShopItem>) {
pub fn items(&self) -> [Option<ShopItem>; 3] {
self.0.content
}
pub fn requirements(&self) -> Option<&AnyOfAllRequirements> {
Expand Down
10 changes: 5 additions & 5 deletions src-tauri/src/randomizer/randomize_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ fn create_shuffled_storage(source: &Storage, spoiler_log: &SpoilerLogRef) -> Sto
.find(|x| x.spot.items() == shop.spot.items())
.unwrap()
.items;
items.0 = shop.items.0.cloned();
items.1 = shop.items.1.cloned();
items.2 = shop.items.2.cloned();
items.iter_mut().zip(&shop.items).for_each(|(old, new)| {
*old = new.cloned();
});
}
CheckpointRef::Rom(rom) => {
let content = rom.spot.rom();
Expand Down Expand Up @@ -150,7 +150,7 @@ fn assert_unique(storage: &Storage) {
storage
.shops
.iter()
.flat_map(|x| [&x.items.0, &x.items.1, &x.items.2])
.flat_map(|x| &x.items)
.filter_map(|item| item.as_ref())
.map(|item| ("shop", item)),
)
Expand Down Expand Up @@ -197,7 +197,7 @@ mod tests {

let shuffled_str = format!("{:?}", shuffled);
let shuffled_hash = hex::encode(sha3::Sha3_512::digest(shuffled_str));
const EXPECTED_SHUFFLED_HASH: &str = "6f06676c37439ee786c9a384d00dbac3f615980daeb07263b58001fa781049891334464fe18ee688cdc0357251a7e76459c039a4d7245755c1545d7b030e7ec2";
const EXPECTED_SHUFFLED_HASH: &str = "7d416fe875f11d80cc00ced83f63b7563bd145c4d8cc8003c00439f43349e17ab82e92c28391bddce262a6681dca19c8fac5c1605aa7f9f86389ad7d6b5ed5c2";
assert_eq!(shuffled_hash, EXPECTED_SHUFFLED_HASH);

let spoiler_log_str = format!("{}", spoiler_log.to_owned());
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/randomizer/spoiler/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> Items<'a> {
source
.shops
.iter()
.flat_map(|x| [&x.items.0, &x.items.1, &x.items.2])
.flat_map(|x| &x.items)
.filter_map(|x| x.as_ref()),
)
.chain(source.roms.values().map(|x| &x.item));
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<'a> Items<'a> {
item.as_ref()
.map_or(0, |x| !x.name.is_consumable() as usize)
};
count(&shop.items.0) + count(&shop.items.1) + count(&shop.items.2)
shop.items.iter().map(count).sum::<usize>()
})
.sum::<usize>()
+ source.roms.len(),
Expand Down
12 changes: 3 additions & 9 deletions src-tauri/src/randomizer/spoiler/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ fn place_items<'a>(
}
})
});
let items = (
let items = [
items.next().unwrap(),
items.next().unwrap(),
items.next().unwrap(),
);
];
let spot = shops[0].spot;
sphere.push(CheckpointRef::Shop(ShopRef { spot, items }));
}
Expand All @@ -142,13 +142,7 @@ fn append_flags<'a>(strategy_flags: &mut HashSet<&'a StrategyFlag>, sphere: &Sph
strategy_flags.insert(&checkpoint.item.name);
}
CheckpointRef::Shop(checkpoint) => {
if let Some(item) = &checkpoint.items.0 {
strategy_flags.insert(&item.name);
}
if let Some(item) = &checkpoint.items.1 {
strategy_flags.insert(&item.name);
}
if let Some(item) = &checkpoint.items.2 {
for item in checkpoint.items.iter().flatten() {
strategy_flags.insert(&item.name);
}
}
Expand Down
15 changes: 6 additions & 9 deletions src-tauri/src/randomizer/spoiler/spots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ impl<'a> Spots<'a> {
.shops
.iter()
.flat_map(|shop| {
[&shop.items.0, &shop.items.1, &shop.items.2]
.into_iter()
.enumerate()
.filter_map(|(idx, item)| {
item.as_ref().map(|item| ShopItemDisplay {
spot: &shop.spot,
idx,
name: &item.name,
})
shop.items.iter().enumerate().filter_map(|(idx, item)| {
item.as_ref().map(|item| ShopItemDisplay {
spot: &shop.spot,
idx,
name: &item.name,
})
})
})
.collect(),
events: source.events.iter().collect(),
Expand Down
16 changes: 5 additions & 11 deletions src-tauri/src/randomizer/spoiler_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ impl fmt::Display for Checkpoint {
Self::Shop(checkpoint) => {
let spot = &checkpoint.spot;
let items = &checkpoint.items;
let items = (
items.0.as_ref().map_or("_", |x| x.name.get()),
items.1.as_ref().map_or("_", |x| x.name.get()),
items.2.as_ref().map_or("_", |x| x.name.get()),
);
write!(f, "{} = {}, {}, {}", spot, items.0, items.1, items.2)
let item0 = items[0].as_ref().map_or("_", |x| x.name.get());
let item1 = items[1].as_ref().map_or("_", |x| x.name.get());
let item2 = items[2].as_ref().map_or("_", |x| x.name.get());
write!(f, "{} = {}, {}, {}", spot, item0, item1, item2)
}
Self::Rom(checkpoint) => {
write!(f, "{} = {}", checkpoint.spot, checkpoint.item.name.get())
Expand Down Expand Up @@ -105,11 +103,7 @@ impl<'a> CheckpointRef<'a> {
}),
Self::Shop(checkpoint) => Checkpoint::Shop(Shop {
spot: checkpoint.spot.to_owned(),
items: (
checkpoint.items.0.cloned(),
checkpoint.items.1.cloned(),
checkpoint.items.2.cloned(),
),
items: checkpoint.items.map(|x| x.cloned()),
}),
Self::Rom(checkpoint) => Checkpoint::Rom(Rom {
spot: checkpoint.spot.to_owned(),
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/randomizer/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Seal {
#[derive(Clone, Debug)]
pub struct Shop {
pub spot: ShopSpot,
pub items: (Option<Item>, Option<Item>, Option<Item>),
pub items: [Option<Item>; 3],
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct SealRef<'a> {

pub struct ShopRef<'a> {
pub spot: &'a ShopSpot,
pub items: (Option<&'a Item>, Option<&'a Item>, Option<&'a Item>),
pub items: [Option<&'a Item>; 3],
}

pub struct RomRef<'a> {
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Storage {
.chain(
self.shops
.iter()
.flat_map(|x| [&x.items.0, &x.items.1, &x.items.2])
.flat_map(|x| &x.items)
.filter_map(|x| x.as_ref()),
)
.chain(self.roms.values().map(|x| &x.item))
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/randomizer/storage/create_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ pub fn create_source(
})
.enumerate()
.map(|(idx, name)| name.map(|name| Item::shop_item(spot.items(), idx, name)));
let items = (
let items = [
names.next().unwrap(),
names.next().unwrap(),
names.next().unwrap(),
);
];
shops.push(Shop { spot, items });
}
let mut events: Vec<_> = game_structure
Expand Down
11 changes: 2 additions & 9 deletions src-tauri/src/randomizer/storage/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ pub enum ItemSource {
SubWeapon((FieldNumber, SubWeapon)),
Chest((FieldNumber, ChestItem)),
Seal(Seal),
Shop(
(Option<ShopItem>, Option<ShopItem>, Option<ShopItem>),
usize,
),
Shop([Option<ShopItem>; 3], usize),
Rom(Rom),
}

Expand Down Expand Up @@ -103,11 +100,7 @@ impl Item {
let src = ItemSource::Seal(seal);
Self { src, name }
}
pub fn shop_item(
items: (Option<ShopItem>, Option<ShopItem>, Option<ShopItem>),
item_idx: usize,
name: StrategyFlag,
) -> Self {
pub fn shop_item(items: [Option<ShopItem>; 3], item_idx: usize, name: StrategyFlag) -> Self {
let src = ItemSource::Shop(items, item_idx);
Self { src, name }
}
Expand Down
10 changes: 1 addition & 9 deletions src-tauri/src/script/data/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,7 @@ impl Item {
};
Ok(Self::Rom(rom.rom().clone()))
}
fn shop(
script: &Script,
items: (
Option<enums::ShopItem>,
Option<enums::ShopItem>,
Option<enums::ShopItem>,
),
item_idx: usize,
) -> Result<Self> {
fn shop(script: &Script, items: [Option<enums::ShopItem>; 3], item_idx: usize) -> Result<Self> {
let Some(shop) = script
.shops()
.filter_map(|x| Shop::try_from_shop_object(x, &script.talks).transpose())
Expand Down
8 changes: 3 additions & 5 deletions src-tauri/src/script/data/shop_items_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ pub enum ShopItem {
}

impl ShopItem {
pub fn to_spot_shop_items(
selfs: &(ShopItem, ShopItem, ShopItem),
) -> (enums::ShopItem, enums::ShopItem, enums::ShopItem) {
(
pub fn to_spot_shop_items(selfs: &(ShopItem, ShopItem, ShopItem)) -> [enums::ShopItem; 3] {
[
selfs.0.to_spot_shop_item(),
selfs.1.to_spot_shop_item(),
selfs.2.to_spot_shop_item(),
)
]
}

pub fn from_item(item: Item, price: u16) -> Self {
Expand Down
30 changes: 14 additions & 16 deletions src-tauri/src/script/editor/talks_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,16 @@ fn replace_items(
fn create_shop_item_talks(
talks: &[Talk],
base_talk_number: u16,
old: (enums::ShopItem, enums::ShopItem, enums::ShopItem),
new: (
Option<enums::ShopItem>,
Option<enums::ShopItem>,
Option<enums::ShopItem>,
),
old: [enums::ShopItem; 3],
new: [Option<enums::ShopItem>; 3],
) -> Result<Vec<(usize, Talk)>> {
[(1, old.0, new.0), (2, old.1, new.1), (3, old.2, new.2)]
.into_iter()
.flat_map(|(idx, old, new)| new.map(|new| (idx, old, new)))
old.into_iter()
.enumerate()
.zip(new)
.flat_map(|((idx, old), new)| new.map(|new| (idx, old, new)))
.filter(|(_, old, new)| old != new)
.map(|(idx, old, new)| {
let talk_number = base_talk_number as usize + idx;
let talk_number = base_talk_number as usize + 1 + idx;
let new_talk = replace_shop_item_talk(talks, talk_number, old, new)?;
Ok((talk_number, new_talk))
})
Expand All @@ -195,19 +192,20 @@ pub fn replace_shops(
item.as_ref().map(|x| Item::new(&x.src, script)).transpose()
};
let new_items = (
create_item(&dataset_shop.items.0)?,
create_item(&dataset_shop.items.1)?,
create_item(&dataset_shop.items.2)?,
create_item(&dataset_shop.items[0])?,
create_item(&dataset_shop.items[1])?,
create_item(&dataset_shop.items[2])?,
);
let new_dataset_shop_items = (
let new_dataset_shop_items = [
to_dataset_shop_item_from_item(new_items.0.as_ref()),
to_dataset_shop_item_from_item(new_items.1.as_ref()),
to_dataset_shop_item_from_item(new_items.2.as_ref()),
);
];

let Some(script_shop) = script_shops.iter().find(|script_shop| {
let old = ShopItem::to_spot_shop_items(&script_shop.items);
enums::ShopItem::matches_items(old, dataset_shop.spot.items())
let items = dataset_shop.spot.items();
enums::ShopItem::matches_items(old, items)
}) else {
bail!("shop not found: {:?}", dataset_shop.spot.items())
};
Expand Down
11 changes: 4 additions & 7 deletions src-tauri/src/script/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ pub enum ShopItem {
}

impl ShopItem {
pub fn matches_items(
left: (Self, Self, Self),
right: (Option<Self>, Option<Self>, Option<Self>),
) -> bool {
left.0.matches(right.0.as_ref())
&& left.1.matches(right.1.as_ref())
&& left.2.matches(right.2.as_ref())
pub fn matches_items(left: [Self; 3], right: [Option<Self>; 3]) -> bool {
left.iter()
.zip(right)
.all(|(left, right)| left.matches(right.as_ref()))
}

pub fn matches(&self, right: Option<&Self>) -> bool {
Expand Down

0 comments on commit 04ce0b0

Please sign in to comment.