Skip to content

Commit

Permalink
Add palindrome charm (ordinals#4064)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 11, 2024
1 parent 9b88cd9 commit 8ab2aa0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion crates/ordinals/src/charm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ pub enum Charm {
Vindicated = 10,
Mythic = 11,
Burned = 12,
Palindrome = 13,
}

impl Charm {
pub const ALL: [Self; 13] = [
pub const ALL: [Self; 14] = [
Self::Coin,
Self::Uncommon,
Self::Rare,
Self::Epic,
Self::Legendary,
Self::Mythic,
Self::Nineball,
Self::Palindrome,
Self::Reinscription,
Self::Cursed,
Self::Unbound,
Expand Down Expand Up @@ -60,6 +62,7 @@ impl Charm {
Self::Lost => "🤔",
Self::Mythic => "🎃",
Self::Nineball => "\u{39}\u{fe0f}\u{20e3}",
Self::Palindrome => "🦋",
Self::Rare => "🧿",
Self::Reinscription => "♻️",
Self::Unbound => "🔓",
Expand Down Expand Up @@ -90,6 +93,7 @@ impl Display for Charm {
Self::Lost => "lost",
Self::Mythic => "mythic",
Self::Nineball => "nineball",
Self::Palindrome => "palindrome",
Self::Rare => "rare",
Self::Reinscription => "reinscription",
Self::Unbound => "unbound",
Expand Down
30 changes: 30 additions & 0 deletions crates/ordinals/src/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ impl Sat {
self.n() >= 50 * COIN_VALUE * 9 && self.n() < 50 * COIN_VALUE * 10
}

pub fn palindrome(self) -> bool {
let mut n = self.0;
let mut reversed = 0;

while n > 0 {
reversed = reversed * 10 + n % 10;
n /= 10;
}

self.0 == reversed
}

pub fn percentile(self) -> String {
format!("{}%", (self.0 as f64 / Self::LAST.0 as f64) * 100.0)
}
Expand Down Expand Up @@ -97,6 +109,10 @@ impl Sat {
Charm::Nineball.set(&mut charms);
}

if self.palindrome() {
Charm::Palindrome.set(&mut charms);
}

if self.coin() {
Charm::Coin.set(&mut charms);
}
Expand Down Expand Up @@ -803,4 +819,18 @@ mod tests {
"failed to parse sat `foo`: invalid percentile",
);
}

#[test]
fn palindrome() {
assert!(Sat(0).palindrome());
assert!(!Sat(10).palindrome());
assert!(Sat(11).palindrome());
}

#[test]
fn palindrome_charm() {
assert!(Charm::Palindrome.is_set(Sat(0).charms()));
assert!(!Charm::Palindrome.is_set(Sat(10).charms()));
assert!(Charm::Palindrome.is_set(Sat(11).charms()));
}
}
1 change: 1 addition & 0 deletions src/templates/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod tests {
<dd>
<span title=coin>🪙</span>
<span title=mythic>🎃</span>
<span title=palindrome>🦋</span>
</dd>
</dl>
.*
Expand Down

0 comments on commit 8ab2aa0

Please sign in to comment.