use serde::{Deserialize, Serialize};
use crate::card::{Color, Suit};
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
struct IgnoredSchema;
impl Serialize for IgnoredSchema {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_none()
}
}
impl<'de> Deserialize<'de> for IgnoredSchema {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let _: Option<String> = Option::deserialize(d)?;
Ok(IgnoredSchema)
}
}
#[cfg(feature = "schema")]
impl schemars::JsonSchema for IgnoredSchema {
fn schema_name() -> String {
"SchemaUri".to_string()
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<Option<String>>::json_schema(gen)
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Rules {
#[serde(rename = "$schema", default, skip_serializing)]
_schema: IgnoredSchema,
pub name: String,
#[serde(default, skip_serializing_if = "is_default_pack")]
pub pack: Pack,
pub groups: Vec<GroupRules>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stock: Option<StockRules>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub redeal: Option<RedealRules>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub deal: Vec<DealStep>,
#[serde(
default,
deserialize_with = "one_or_many",
skip_serializing_if = "Vec::is_empty"
)]
pub removal: Vec<RemovalRules>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub score: Option<ScoreRules>,
#[serde(default, skip_serializing_if = "is_default_win")]
pub win: WinRule,
#[serde(default = "yes", skip_serializing_if = "is_true")]
pub auto_flip: bool,
#[serde(default = "yes", skip_serializing_if = "is_true")]
pub safe_autoplay: bool,
#[serde(default, skip_serializing_if = "SearchHints::is_default")]
pub search: SearchHints,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub choose_base: Option<ChooseBase>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub latch_group: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub deck_first: Vec<CardRef>,
#[serde(default, skip_serializing_if = "is_false")]
pub separate_decks: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub deck_last: Vec<CardRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub deck_last_rank: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub aux: Option<AuxRules>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extract_second: Option<ExtractSecond>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rounds: Option<RoundRules>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct CardRef {
pub suit: Suit,
pub rank: u8,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ChooseBase {
pub from: String,
#[serde(default)]
pub refill_from_stock: bool,
#[serde(default)]
pub deal_waste: bool,
#[serde(default)]
pub free_moves: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub also_from: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub lock_until_base: Vec<String>,
}
impl ChooseBase {
pub fn from(group: &str) -> ChooseBase {
ChooseBase {
from: group.to_string(),
refill_from_stock: false,
deal_waste: false,
free_moves: false,
also_from: None,
lock_until_base: Vec::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields, default)]
#[non_exhaustive]
pub struct SearchHints {
pub progress_weight: u32,
pub down_weight: u32,
pub disorder_weight: u32,
pub blocker_weight: u32,
pub cell_weight: u32,
pub stock_weight: u32,
pub fast_share: u32,
}
impl Default for SearchHints {
fn default() -> Self {
SearchHints {
progress_weight: 4,
down_weight: 2,
disorder_weight: 1,
blocker_weight: 1,
cell_weight: 1,
stock_weight: 1,
fast_share: 50,
}
}
}
impl Rules {
pub fn new(name: &str, pack: Pack, groups: Vec<GroupRules>, deal: Vec<DealStep>) -> Rules {
Rules {
_schema: IgnoredSchema,
name: name.to_string(),
pack,
groups,
stock: None,
redeal: None,
deal,
removal: Vec::new(),
score: None,
win: WinRule::AllOnFoundations,
auto_flip: true,
safe_autoplay: true,
search: SearchHints::default(),
choose_base: None,
latch_group: None,
deck_first: Vec::new(),
separate_decks: false,
deck_last: Vec::new(),
deck_last_rank: None,
aux: None,
extract_second: None,
rounds: None,
}
}
}
impl SearchHints {
pub fn is_default(&self) -> bool {
*self == SearchHints::default()
}
}
fn yes() -> bool {
true
}
fn one() -> u16 {
1
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Pack {
#[serde(default = "one_u8", skip_serializing_if = "is_one_u8")]
pub copies: u8,
#[serde(default = "all_suits", skip_serializing_if = "is_all_suits")]
pub suits: Vec<Suit>,
#[serde(default = "all_ranks", skip_serializing_if = "is_all_ranks")]
pub ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "is_zero_u8")]
pub jokers: u8,
}
fn is_false(b: &bool) -> bool {
!*b
}
fn is_true(b: &bool) -> bool {
*b
}
fn is_zero_u8(n: &u8) -> bool {
*n == 0
}
fn is_zero_u16(n: &u16) -> bool {
*n == 0
}
fn is_zero_u32(n: &u32) -> bool {
*n == 0
}
fn is_one_u8(n: &u8) -> bool {
*n == 1
}
fn is_one_u16(n: &u16) -> bool {
*n == 1
}
fn is_default_pack(p: &Pack) -> bool {
*p == Pack::default()
}
fn is_default_win(w: &WinRule) -> bool {
*w == WinRule::AllOnFoundations
}
fn is_all_suits(s: &[Suit]) -> bool {
s == Suit::ALL
}
fn is_all_ranks(r: &[u8]) -> bool {
r.len() == 13 && r.iter().enumerate().all(|(i, &x)| x == (i + 1) as u8)
}
fn one_u8() -> u8 {
1
}
fn all_suits() -> Vec<Suit> {
Suit::ALL.to_vec()
}
fn all_ranks() -> Vec<u8> {
(1..=13).collect()
}
impl Default for Pack {
fn default() -> Self {
Pack {
copies: 1,
suits: all_suits(),
ranks: all_ranks(),
jokers: 0,
}
}
}
impl Pack {
pub fn new(suits: &[Suit], ranks: &[u8]) -> Pack {
Pack {
copies: 1,
suits: suits.to_vec(),
ranks: ranks.to_vec(),
jokers: 0,
}
}
pub fn with_copies(mut self, n: u8) -> Pack {
self.copies = n;
self
}
pub fn decks(n: u8) -> Pack {
Pack {
copies: n,
..Pack::default()
}
}
pub fn len(&self) -> usize {
self.copies as usize * self.suits.len() * self.ranks.len() + self.jokers as usize
}
pub fn with_jokers(mut self, n: u8) -> Pack {
self.jokers = n;
self
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PileKind {
Tableau,
Foundation,
Cell,
Reserve,
Stock,
Waste,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct GroupRules {
pub name: String,
pub kind: PileKind,
#[serde(default = "one", skip_serializing_if = "is_one_u16")]
pub count: u16,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub build: Option<Build>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty: Option<EmptyRule>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moves: Option<MoveUnit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_cards: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_cards: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_accept: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub accepts_from: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_accepts_from: Option<Vec<String>>,
#[serde(
default,
deserialize_with = "one_or_many",
skip_serializing_if = "Vec::is_empty"
)]
pub refill_from: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refill_count: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub layout: Option<Layout>,
#[serde(default, skip_serializing_if = "is_false")]
pub complete_runs: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reach: Option<Vec<u16>>,
#[serde(default, skip_serializing_if = "is_false")]
pub reach_both_ways: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub reach_grid: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_needs_group_empty: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_whole_from_group: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub run_must_fit_build: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub move_runs_by: Vec<Build>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub repeat_rank: Option<u16>,
#[serde(default, skip_serializing_if = "is_false")]
pub dropdown: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from_pile_map: Option<FromPileMap>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub clear_tops: Option<ClearTops>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub first_round_bans: Vec<FirstRoundBan>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub row_start: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub replace_on_drop: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub compact: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub suit: Option<Suit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exclude_suit: Option<Suit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_suit: Option<Suit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub color: Option<Color>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub piles: Vec<PileOverride>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wild: Option<bool>,
#[serde(default, skip_serializing_if = "is_false")]
pub requires_rank_on_prev: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub match_longest_rank: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_max_accept: Option<u16>,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_only_when_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_when_stock_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_when_waste_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub compact_face_up: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub refill_largest: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub refill_on_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub requires_prev_non_empty: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_same_rank: Option<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub moves_when_stock_empty: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moves_when_any_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moves_when_same_index_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moves_when_slice_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub requires_same_index_non_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub accept_when_same_index_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub move_from_frontier: Option<Frontier>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_open_after_bases: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_open_when_full: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub empty_needs_first_full: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub flip_only_last: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_from_same_index: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub lock_build_dir: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub no_flip: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub refill_same_index: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub refill_nearest: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub require_valid_base: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub swap_bases: Option<SwapBases>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub swap_on_waste_match: Option<Adjacency>,
#[serde(default, skip_serializing_if = "is_false")]
pub supermove_complete_run: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unrestricted_from: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "is_false")]
pub lock_pile_dir: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub freeze_when_built: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_on_face_down: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub one_way_split: Option<u16>,
#[serde(default, skip_serializing_if = "is_false")]
pub move_face_down_top: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub rank_sequence: Vec<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub non_empty_max_accept: Option<u16>,
#[serde(default, skip_serializing_if = "is_false")]
pub unique_suits: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub unique_ranks: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub cyclic_ranks: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub same_suit_group: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub same_rank_group: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub clear_when_full: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub clear_same_rank: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub auto_foundation_rank: Option<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub flip_when_dealt_out: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub spill_when_hollow: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub compact_when_stock_empty: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub run_len: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refill_when_empty: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub no_build_single_while_stock: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub refill_first_face_down: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flip_when_same_index_full: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flip_when_same_index_non_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub face_down_while_same_index_non_empty: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub flip_after_prev_empty: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flip_when_all_at_most: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flip_when_any_empty: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_any_on_single: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_requires_same_index_non_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_match_suit_of: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub from_requires_left_empty: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub push_off_to: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub push_off_bypasses_accept: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub fission_shift: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub wild_permissive: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub joker_anywhere: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub joker_only: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub no_jokers: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub accept_when_piles_full: Option<PilesFull>,
#[serde(default, skip_serializing_if = "is_false")]
pub joker_bomb: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sum_limit: Option<SumLimit>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub refill_divert_ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_accept: Option<u16>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub requires_same_index_groups_non_empty: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from_pile_sets: Option<FromPileSets>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flush_at_len: Option<FlushAtLen>,
#[serde(default, skip_serializing_if = "is_false")]
pub match_depth_suit: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub secondary_stock: Option<SecondaryStock>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moves_when_group_cards_at_least: Option<(String, u16)>,
#[serde(default, skip_serializing_if = "is_false")]
pub accept_first_round_only: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_accept_len: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refill_group_on_leave: Option<LeaveRefill>,
#[serde(default, skip_serializing_if = "is_false")]
pub refill_self_on_leave: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub staging: Option<Staging>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub pair_flush: Vec<PairFlush>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tiered_set: Option<TieredSet>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub staged_empty: Option<StagedEmpty>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub even_segments: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refill_when_stock_at_most: Option<u16>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct StagedEmpty {
pub stride: u16,
pub source: String,
}
impl StagedEmpty {
pub fn new(stride: u16, source: &str) -> StagedEmpty {
StagedEmpty {
stride,
source: source.to_string(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct TieredSet {
pub to: String,
pub bonus: u16,
pub joker_value: u16,
}
impl TieredSet {
pub fn new(to: &str, bonus: u16, joker_value: u16) -> TieredSet {
TieredSet {
to: to.to_string(),
bonus,
joker_value,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct SecondaryStock {
pub to: String,
#[serde(default, skip_serializing_if = "is_false")]
pub unlock_when_to_empty: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stay_unlocked_below: Option<u16>,
}
impl SecondaryStock {
pub fn to(group: &str) -> SecondaryStock {
SecondaryStock {
to: group.to_string(),
unlock_when_to_empty: false,
stay_unlocked_below: None,
}
}
pub fn unlock_when_to_empty(mut self) -> Self {
self.unlock_when_to_empty = true;
self
}
pub fn stay_unlocked_below(mut self, n: u16) -> Self {
self.stay_unlocked_below = Some(n);
self
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SwapBases {
BothSingle,
EitherPile,
Crossing,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Frontier {
Last,
First,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ClearTops {
pub to: String,
pub sum: u16,
}
impl ClearTops {
pub fn new(to: &str, sum: u16) -> ClearTops {
ClearTops {
to: to.to_string(),
sum,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct FromPileMap {
pub group: String,
pub to: Vec<i16>,
#[serde(default, skip_serializing_if = "is_false")]
pub only_when_stock_non_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub only_non_empty_dest: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub except_ranks: Vec<u8>,
}
impl FromPileMap {
pub fn new(group: &str, to: &[i16]) -> FromPileMap {
FromPileMap {
group: group.to_string(),
to: to.to_vec(),
only_when_stock_non_empty: false,
only_non_empty_dest: false,
except_ranks: Vec::new(),
}
}
pub fn only_when_stock_non_empty(mut self) -> Self {
self.only_when_stock_non_empty = true;
self
}
pub fn only_non_empty_dest(mut self) -> Self {
self.only_non_empty_dest = true;
self
}
pub fn except_ranks(mut self, ranks: &[u8]) -> Self {
self.except_ranks = ranks.to_vec();
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct SumLimit {
pub target: u16,
pub values: Vec<u16>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub lift_ranks: Vec<u8>,
}
impl SumLimit {
pub fn new(target: u16, values: &[u16]) -> SumLimit {
SumLimit {
target,
values: values.to_vec(),
lift_ranks: Vec::new(),
}
}
pub fn lift_ranks(mut self, ranks: &[u8]) -> Self {
self.lift_ranks = ranks.to_vec();
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct PilesFull {
pub group: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub piles: Vec<u16>,
}
impl PilesFull {
pub fn new(group: &str, piles: &[u16]) -> PilesFull {
PilesFull {
group: group.to_string(),
piles: piles.to_vec(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct FromPileSets {
pub group: String,
pub to: Vec<Vec<u16>>,
}
impl FromPileSets {
pub fn new(group: &str, to: Vec<Vec<u16>>) -> FromPileSets {
FromPileSets {
group: group.to_string(),
to,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct FlushAtLen {
pub len: u16,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pull_from: Option<String>,
pub to: String,
}
impl FlushAtLen {
pub fn new(len: u16, to: &str) -> FlushAtLen {
FlushAtLen {
len,
pull_from: None,
to: to.to_string(),
}
}
pub fn pull_from(mut self, group: &str) -> Self {
self.pull_from = Some(group.to_string());
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct LeaveRefill {
pub group: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reshuffle_from: Option<String>,
}
impl LeaveRefill {
pub fn new(group: &str) -> LeaveRefill {
LeaveRefill {
group: group.to_string(),
reshuffle_from: None,
}
}
pub fn reshuffle_from(mut self, group: &str) -> Self {
self.reshuffle_from = Some(group.to_string());
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Staging {
pub to: String,
pub court_from: u8,
pub pip_sum: u16,
#[serde(default, skip_serializing_if = "is_zero_u8")]
pub pip_courts: u8,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub court_set: Vec<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub distinct_ranks: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_courts: Option<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub cap_pip_sum: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exact_at_pips: Option<u8>,
}
impl Staging {
pub fn new(to: &str, court_from: u8, pip_sum: u16) -> Staging {
Staging {
to: to.to_string(),
court_from,
pip_sum,
pip_courts: 0,
court_set: Vec::new(),
distinct_ranks: false,
max_courts: None,
cap_pip_sum: false,
exact_at_pips: None,
}
}
pub fn pip_courts(mut self, n: u8) -> Self {
self.pip_courts = n;
self
}
pub fn court_set(mut self, ranks: &[u8]) -> Self {
self.court_set = ranks.to_vec();
self
}
pub fn distinct_ranks(mut self) -> Self {
self.distinct_ranks = true;
self
}
pub fn max_courts(mut self, n: u8) -> Self {
self.max_courts = Some(n);
self
}
pub fn cap_pip_sum(mut self) -> Self {
self.cap_pip_sum = true;
self
}
pub fn exact_at_pips(mut self, n: u8) -> Self {
self.exact_at_pips = Some(n);
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct PairFlush {
pub bottom_ranks: Vec<u8>,
pub to: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub first: Option<u8>,
}
impl PairFlush {
pub fn new(bottom_ranks: &[u8], to: &str) -> PairFlush {
PairFlush {
bottom_ranks: bottom_ranks.to_vec(),
to: to.to_string(),
first: None,
}
}
pub fn first(mut self, rank: u8) -> Self {
self.first = Some(rank);
self
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct FirstRoundBan {
pub to: Vec<u16>,
pub from_group: String,
pub from: Vec<u16>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct PileOverride {
pub piles: Vec<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub build: Option<Build>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty: Option<EmptyRule>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moves: Option<MoveUnit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_cards: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_cards: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_accept: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_max_accept: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub non_empty_max_accept: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub suit: Option<Suit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub empty_suit: Option<Suit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub color: Option<Color>,
}
impl PileOverride {
pub fn on(piles: &[u16]) -> PileOverride {
PileOverride {
piles: piles.to_vec(),
..PileOverride::default()
}
}
pub fn build(mut self, b: Build) -> Self {
self.build = Some(b);
self
}
pub fn empty(mut self, e: EmptyRule) -> Self {
self.empty = Some(e);
self
}
pub fn suit(mut self, s: Suit) -> Self {
self.suit = Some(s);
self
}
pub fn empty_suit(mut self, s: Suit) -> Self {
self.empty_suit = Some(s);
self
}
}
fn one_or_many<'de, D, T>(d: D) -> Result<Vec<T>, D::Error>
where
D: serde::Deserializer<'de>,
T: Deserialize<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum OneOrMany<T> {
One(T),
Many(Vec<T>),
}
Ok(match OneOrMany::<T>::deserialize(d)? {
OneOrMany::One(t) => vec![t],
OneOrMany::Many(v) => v,
})
}
impl GroupRules {
pub fn new(name: &str, kind: PileKind, count: u16) -> GroupRules {
GroupRules {
name: name.to_string(),
kind,
count,
build: None,
empty: None,
moves: None,
max_cards: None,
min_cards: None,
max_accept: None,
accepts_from: None,
empty_accepts_from: None,
refill_from: Vec::new(),
refill_count: None,
reach: None,
reach_both_ways: false,
reach_grid: false,
empty_needs_group_empty: None,
accept_whole_from_group: false,
run_must_fit_build: false,
move_runs_by: Vec::new(),
repeat_rank: None,
dropdown: false,
from_pile_map: None,
clear_tops: None,
first_round_bans: Vec::new(),
row_start: None,
replace_on_drop: None,
compact: false,
suit: None,
empty_suit: None,
exclude_suit: None,
color: None,
piles: Vec::new(),
layout: None,
complete_runs: false,
wild: None,
requires_rank_on_prev: false,
match_longest_rank: false,
empty_max_accept: None,
accept_only_when_empty: false,
accept_when_stock_empty: false,
accept_when_waste_empty: false,
compact_face_up: false,
refill_largest: false,
refill_on_empty: false,
requires_prev_non_empty: false,
max_same_rank: None,
moves_when_stock_empty: false,
moves_when_any_empty: None,
moves_when_same_index_empty: None,
moves_when_slice_empty: None,
requires_same_index_non_empty: None,
accept_when_same_index_empty: None,
move_from_frontier: None,
empty_open_after_bases: None,
empty_open_when_full: None,
empty_needs_first_full: false,
flip_only_last: false,
final_from_same_index: None,
lock_build_dir: false,
no_flip: false,
refill_same_index: false,
refill_nearest: false,
require_valid_base: false,
swap_bases: None,
swap_on_waste_match: None,
supermove_complete_run: false,
unrestricted_from: None,
lock_pile_dir: false,
freeze_when_built: false,
accept_on_face_down: false,
one_way_split: None,
move_face_down_top: false,
rank_sequence: Vec::new(),
non_empty_max_accept: None,
unique_suits: false,
unique_ranks: false,
cyclic_ranks: false,
same_suit_group: false,
same_rank_group: false,
clear_when_full: None,
clear_same_rank: false,
run_len: None,
compact_when_stock_empty: false,
flip_when_dealt_out: false,
spill_when_hollow: false,
auto_foundation_rank: None,
refill_when_empty: None,
no_build_single_while_stock: false,
refill_first_face_down: false,
flip_when_same_index_full: None,
flip_when_same_index_non_empty: None,
face_down_while_same_index_non_empty: None,
flip_after_prev_empty: false,
flip_when_all_at_most: None,
flip_when_any_empty: None,
accept_any_on_single: false,
empty_requires_same_index_non_empty: None,
empty_match_suit_of: None,
from_requires_left_empty: false,
push_off_to: Vec::new(),
push_off_bypasses_accept: false,
fission_shift: false,
wild_permissive: false,
joker_anywhere: false,
joker_only: false,
no_jokers: false,
accept_when_piles_full: None,
joker_bomb: false,
sum_limit: None,
refill_divert_ranks: Vec::new(),
min_accept: None,
requires_same_index_groups_non_empty: Vec::new(),
from_pile_sets: None,
flush_at_len: None,
match_depth_suit: false,
secondary_stock: None,
moves_when_group_cards_at_least: None,
accept_first_round_only: false,
min_accept_len: None,
refill_group_on_leave: None,
refill_self_on_leave: false,
staging: None,
pair_flush: Vec::new(),
tiered_set: None,
staged_empty: None,
even_segments: None,
refill_when_stock_at_most: None,
}
}
pub fn build(mut self, by: BuildBy, dir: Dir) -> Self {
self.build = Some(Build {
by,
dir,
..Build::default()
});
self
}
pub fn build_rule(mut self, b: Build) -> Self {
self.build = Some(b);
self
}
pub fn empty(mut self, e: EmptyRule) -> Self {
self.empty = Some(e);
self
}
pub fn moves(mut self, m: MoveUnit) -> Self {
self.moves = Some(m);
self
}
pub fn max_cards(mut self, n: u16) -> Self {
self.max_cards = Some(n);
self
}
pub fn min_cards(mut self, n: u16) -> Self {
self.min_cards = Some(n);
self
}
pub fn max_accept(mut self, n: u16) -> Self {
self.max_accept = Some(n);
self
}
pub fn accepts_from(mut self, groups: &[&str]) -> Self {
self.accepts_from = Some(groups.iter().map(|s| s.to_string()).collect());
self
}
pub fn empty_accepts_from(mut self, groups: &[&str]) -> Self {
self.empty_accepts_from = Some(groups.iter().map(|s| s.to_string()).collect());
self
}
pub fn refill_from(mut self, groups: &[&str]) -> Self {
self.refill_from = groups.iter().map(|s| s.to_string()).collect();
self
}
pub fn refill_count(mut self, n: u16) -> Self {
self.refill_count = Some(n);
self
}
pub fn suit(mut self, s: Suit) -> Self {
self.suit = Some(s);
self
}
pub fn empty_suit(mut self, s: Suit) -> Self {
self.empty_suit = Some(s);
self
}
pub fn pile(mut self, o: PileOverride) -> Self {
self.piles.push(o);
self
}
pub fn layout(mut self, l: Layout) -> Self {
self.layout = Some(l);
self
}
pub fn complete_runs(mut self) -> Self {
self.complete_runs = true;
self
}
pub fn wild(mut self, wild: bool) -> Self {
self.wild = Some(wild);
self
}
pub fn requires_rank_on_prev(mut self) -> Self {
self.requires_rank_on_prev = true;
self
}
pub fn match_longest_rank(mut self) -> Self {
self.match_longest_rank = true;
self
}
pub fn empty_max_accept(mut self, n: u16) -> Self {
self.empty_max_accept = Some(n);
self
}
pub fn accept_only_when_empty(mut self) -> Self {
self.accept_only_when_empty = true;
self
}
pub fn accept_when_stock_empty(mut self) -> Self {
self.accept_when_stock_empty = true;
self
}
pub fn compact_face_up(mut self) -> Self {
self.compact_face_up = true;
self
}
pub fn refill_largest(mut self) -> Self {
self.refill_largest = true;
self
}
pub fn refill_on_empty(mut self) -> Self {
self.refill_on_empty = true;
self
}
pub fn requires_prev_non_empty(mut self) -> Self {
self.requires_prev_non_empty = true;
self
}
pub fn max_same_rank(mut self, n: u8) -> Self {
self.max_same_rank = Some(n);
self
}
pub fn moves_when_stock_empty(mut self) -> Self {
self.moves_when_stock_empty = true;
self
}
pub fn moves_when_any_empty(mut self, group: &str) -> Self {
self.moves_when_any_empty = Some(group.to_string());
self
}
pub fn accept_when_same_index_empty(mut self, group: &str) -> Self {
self.accept_when_same_index_empty = Some(group.to_string());
self
}
pub fn requires_same_index_non_empty(mut self, group: &str) -> Self {
self.requires_same_index_non_empty = Some(group.to_string());
self
}
pub fn moves_when_same_index_empty(mut self, group: &str) -> Self {
self.moves_when_same_index_empty = Some(group.to_string());
self
}
pub fn moves_when_slice_empty(mut self, group: &str) -> Self {
self.moves_when_slice_empty = Some(group.to_string());
self
}
pub fn flip_only_last(mut self) -> Self {
self.flip_only_last = true;
self
}
pub fn final_from_same_index(mut self, group: &str) -> Self {
self.final_from_same_index = Some(group.to_string());
self
}
pub fn lock_build_dir(mut self) -> Self {
self.lock_build_dir = true;
self
}
pub fn no_flip(mut self) -> Self {
self.no_flip = true;
self
}
pub fn refill_same_index(mut self) -> Self {
self.refill_same_index = true;
self
}
pub fn refill_nearest(mut self) -> Self {
self.refill_nearest = true;
self
}
pub fn require_valid_base(mut self) -> Self {
self.require_valid_base = true;
self
}
pub fn swap_bases(mut self, mode: SwapBases) -> Self {
self.swap_bases = Some(mode);
self
}
pub fn swap_on_waste_match(mut self, adjacency: Adjacency) -> Self {
self.swap_on_waste_match = Some(adjacency);
self
}
pub fn supermove_complete_run(mut self) -> Self {
self.supermove_complete_run = true;
self
}
pub fn unrestricted_from(mut self, groups: &[&str]) -> Self {
self.unrestricted_from = Some(groups.iter().map(|s| s.to_string()).collect());
self
}
pub fn lock_pile_dir(mut self) -> Self {
self.lock_pile_dir = true;
self
}
pub fn freeze_when_built(mut self) -> Self {
self.freeze_when_built = true;
self
}
pub fn rank_sequence(mut self, ranks: &[u8]) -> Self {
self.rank_sequence = ranks.to_vec();
self
}
pub fn accept_any_on_single(mut self) -> Self {
self.accept_any_on_single = true;
self
}
pub fn empty_requires_same_index_non_empty(mut self, group: &str) -> Self {
self.empty_requires_same_index_non_empty = Some(group.to_string());
self
}
pub fn empty_match_suit_of(mut self, group: &str) -> Self {
self.empty_match_suit_of = Some(group.to_string());
self
}
pub fn from_requires_left_empty(mut self) -> Self {
self.from_requires_left_empty = true;
self
}
pub fn push_off_to(mut self, groups: &[&str]) -> Self {
self.push_off_to = groups.iter().map(|s| s.to_string()).collect();
self
}
pub fn push_off_bypasses_accept(mut self) -> Self {
self.push_off_bypasses_accept = true;
self
}
pub fn fission_shift(mut self) -> Self {
self.fission_shift = true;
self
}
pub fn wild_permissive(mut self) -> Self {
self.wild_permissive = true;
self
}
pub fn joker_anywhere(mut self) -> Self {
self.joker_anywhere = true;
self
}
pub fn joker_only(mut self) -> Self {
self.joker_only = true;
self
}
pub fn no_jokers(mut self) -> Self {
self.no_jokers = true;
self
}
pub fn accept_when_piles_full(mut self, group: &str, piles: &[u16]) -> Self {
self.accept_when_piles_full = Some(PilesFull::new(group, piles));
self
}
pub fn refill_divert_ranks(mut self, ranks: &[u8]) -> Self {
self.refill_divert_ranks = ranks.to_vec();
self
}
pub fn sum_limit(mut self, s: SumLimit) -> Self {
self.sum_limit = Some(s);
self
}
pub fn joker_bomb(mut self) -> Self {
self.joker_bomb = true;
self
}
pub fn min_accept(mut self, n: u16) -> Self {
self.min_accept = Some(n);
self
}
pub fn requires_same_index_groups_non_empty(mut self, groups: &[&str]) -> Self {
self.requires_same_index_groups_non_empty = groups.iter().map(|s| s.to_string()).collect();
self
}
pub fn from_pile_sets(mut self, sets: FromPileSets) -> Self {
self.from_pile_sets = Some(sets);
self
}
pub fn flush_at_len(mut self, flush: FlushAtLen) -> Self {
self.flush_at_len = Some(flush);
self
}
pub fn match_depth_suit(mut self) -> Self {
self.match_depth_suit = true;
self
}
pub fn secondary_stock(mut self, s: SecondaryStock) -> Self {
self.secondary_stock = Some(s);
self
}
pub fn moves_when_group_cards_at_least(mut self, group: &str, min_cards: u16) -> Self {
self.moves_when_group_cards_at_least = Some((group.to_string(), min_cards));
self
}
pub fn accept_first_round_only(mut self) -> Self {
self.accept_first_round_only = true;
self
}
pub fn pair_flush(mut self, flush: PairFlush) -> Self {
self.pair_flush.push(flush);
self
}
pub fn tiered_set(mut self, t: TieredSet) -> Self {
self.tiered_set = Some(t);
self
}
pub fn staged_empty(mut self, s: StagedEmpty) -> Self {
self.staged_empty = Some(s);
self
}
pub fn even_segments(mut self, separator_from: u8) -> Self {
self.even_segments = Some(separator_from);
self
}
pub fn refill_when_stock_at_most(mut self, n: u16) -> Self {
self.refill_when_stock_at_most = Some(n);
self
}
pub fn staging(mut self, staging: Staging) -> Self {
self.staging = Some(staging);
self
}
pub fn refill_group_on_leave(mut self, refill: LeaveRefill) -> Self {
self.refill_group_on_leave = Some(refill);
self
}
pub fn min_accept_len(mut self, n: u16) -> Self {
self.min_accept_len = Some(n);
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Build {
pub by: BuildBy,
#[serde(default)]
pub dir: Dir,
#[serde(default, skip_serializing_if = "is_false")]
pub wrap: bool,
#[serde(default = "one_u8", skip_serializing_if = "is_one_u8")]
pub step: u8,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub suit_steps: Vec<(Suit, u8)>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub blocked_ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub gap_ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub extra_pairs: Vec<(u8, u8)>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub extra_over: Vec<(u8, u8)>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub not_over: Vec<(u8, u8)>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub suit_cycle: Vec<Suit>,
}
impl Build {
pub fn new(by: BuildBy, dir: Dir) -> Build {
Build {
by,
dir,
..Build::default()
}
}
pub fn wrap(mut self) -> Self {
self.wrap = true;
self
}
pub fn step(mut self, step: u8) -> Self {
self.step = step;
self
}
pub fn suit_steps(mut self, steps: &[(Suit, u8)]) -> Self {
self.suit_steps = steps.to_vec();
self
}
pub fn blocked_ranks(mut self, ranks: &[u8]) -> Self {
self.blocked_ranks = ranks.to_vec();
self
}
pub fn suit_cycle(mut self, suits: &[Suit]) -> Self {
self.suit_cycle = suits.to_vec();
self
}
pub fn gap_ranks(mut self, ranks: &[u8]) -> Self {
self.gap_ranks = ranks.to_vec();
self
}
}
impl Default for Build {
fn default() -> Self {
Build {
by: BuildBy::None,
dir: Dir::Down,
wrap: false,
step: 1,
suit_steps: Vec::new(),
blocked_ranks: Vec::new(),
gap_ranks: Vec::new(),
extra_pairs: Vec::new(),
extra_over: Vec::new(),
not_over: Vec::new(),
suit_cycle: Vec::new(),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BuildBy {
None,
Any,
Suit,
Alternate,
SameColor,
OtherSuit,
SuitOrRank,
SuitOrSameRank,
SuitCycle,
RankSuitColor,
SuitChain,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Dir {
#[default]
Down,
Up,
Either,
Same,
Any,
UpAny,
DownAny,
DownOrSame,
DoubleUp,
Mirror,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum EmptyRule {
None,
Any,
Rank(u8),
Ranks(Vec<u8>),
DealtBase,
DealtBaseOffset(i8),
AfterLeft { first: u8 },
AfterNeighbor { first: u8 },
AfterPile { group: String, offset: i8 },
SameBase,
AfterRing {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
left_extra: Vec<(u8, u8)>,
},
AfterPileBottom {
group: String,
pile: u16,
offset: i8,
},
ExceptRanks(Vec<u8>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum MoveUnit {
None,
Top,
Run,
SuitRun,
AnyFaceUp,
Supermove,
Whole,
TopOrWhole,
AltColorRun,
SameColorRun,
SuitRunOrSameRank,
SuitSequence,
AnySingle,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Layout {
Pyramid { rows: u16 },
TriPeaks,
Covers { covered_by: Vec<Vec<u16>> },
Grid { columns: u16 },
GridVertical { columns: u16 },
GridBottomOpen { columns: u16 },
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct StockRules {
pub to: String,
#[serde(default = "one", skip_serializing_if = "is_one_u16")]
pub count: u16,
#[serde(default, skip_serializing_if = "is_zero_u32")]
pub redeals: u32,
#[serde(default, skip_serializing_if = "is_false")]
pub unlimited_redeals: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub require_no_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub auto: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub first_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub only_when_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub skip_empty: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub face_up: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub count_per_round: Vec<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_to: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub piles: Option<Vec<u16>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_piles: Option<Vec<u16>>,
#[serde(default, skip_serializing_if = "is_false")]
pub deal_per_pile: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub drop_to_before_deal: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub deal_down_then_up: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_reverse: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_flip_stock: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_gather_only: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_shuffle: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_deal_all_alternating: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub deal_empty_first: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub divert_ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub piles_per_round: Vec<Vec<u16>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_split_to: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_also_to: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub require_no_empty_group: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub require_group_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub auto_for_empty: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub skip_built_from: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_deals: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub skip_run_with_rank: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub open_next_on_rank: Option<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub fill_grid_holes_then_row: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub fill_grid_holes_repeat: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_redistribute_reverse: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub skip_when_same_index_full: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub redeal_from_groups: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub redeal_waste_onto_group: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bookend: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub only_when_empty_first_round: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub top_up_group_to: Option<(String, u16)>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub redeal_switch_to_auto_refill: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sweep_to_discard_round1: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sweep_group_on_waste: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub deal_after_play_to: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub redeal_bottom_count_from: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub fill_empty_or_reshuffle: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reshuffle_at_counter: Option<i16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fan_rows: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub wrap_mid_deal: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bonus_redeals: Option<BonusRedeals>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct BonusRedeals {
pub groups: Vec<String>,
pub threshold: u16,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_redeals: Option<u32>,
}
impl BonusRedeals {
pub fn new(groups: &[&str], threshold: u16) -> BonusRedeals {
BonusRedeals {
groups: groups.iter().map(|s| (*s).to_string()).collect(),
threshold,
max_redeals: None,
}
}
pub fn max_redeals(mut self, cap: u32) -> Self {
self.max_redeals = Some(cap);
self
}
}
impl StockRules {
pub fn to(group: &str) -> StockRules {
StockRules {
to: group.to_string(),
count: 1,
redeals: 0,
unlimited_redeals: false,
require_no_empty: false,
auto: false,
first_empty: false,
only_when_empty: false,
skip_empty: false,
face_up: false,
count_per_round: Vec::new(),
final_to: None,
piles: None,
final_piles: None,
deal_per_pile: false,
drop_to_before_deal: None,
deal_down_then_up: false,
redeal_reverse: false,
redeal_flip_stock: false,
redeal_gather_only: false,
redeal_shuffle: false,
redeal_deal_all_alternating: false,
deal_empty_first: false,
divert_ranks: Vec::new(),
piles_per_round: Vec::new(),
final_split_to: None,
final_also_to: None,
require_no_empty_group: None,
require_group_empty: None,
auto_for_empty: None,
skip_built_from: None,
max_deals: None,
skip_run_with_rank: None,
open_next_on_rank: None,
fill_grid_holes_then_row: false,
fill_grid_holes_repeat: false,
redeal_redistribute_reverse: false,
skip_when_same_index_full: None,
redeal_from_groups: Vec::new(),
redeal_waste_onto_group: false,
bookend: None,
only_when_empty_first_round: false,
top_up_group_to: None,
redeal_switch_to_auto_refill: None,
sweep_to_discard_round1: None,
sweep_group_on_waste: None,
deal_after_play_to: None,
redeal_bottom_count_from: None,
fill_empty_or_reshuffle: false,
reshuffle_at_counter: None,
fan_rows: None,
wrap_mid_deal: false,
bonus_redeals: None,
}
}
pub fn wrap_mid_deal(mut self) -> Self {
self.wrap_mid_deal = true;
self
}
pub fn bonus_redeals(mut self, bonus: BonusRedeals) -> Self {
self.bonus_redeals = Some(bonus);
self
}
pub fn reshuffle_at_counter(mut self, value: i16) -> Self {
self.reshuffle_at_counter = Some(value);
self
}
pub fn fill_empty_or_reshuffle(mut self) -> Self {
self.fill_empty_or_reshuffle = true;
self
}
pub fn fan_rows(mut self, group: &str) -> Self {
self.fan_rows = Some(group.to_string());
self
}
pub fn redeal_bottom_count_from(mut self, group: &str) -> Self {
self.redeal_bottom_count_from = Some(group.to_string());
self
}
pub fn deal_after_play_to(mut self, group: &str) -> Self {
self.deal_after_play_to = Some(group.to_string());
self
}
pub fn open_next_on_rank(mut self, rank: u8) -> Self {
self.open_next_on_rank = Some(rank);
self
}
pub fn redeal_redistribute_reverse(mut self) -> Self {
self.redeal_redistribute_reverse = true;
self
}
pub fn skip_when_same_index_full(mut self, group: &str) -> Self {
self.skip_when_same_index_full = Some(group.to_string());
self
}
pub fn redeal_from_groups(mut self, groups: &[&str]) -> Self {
self.redeal_from_groups = groups.iter().map(|s| s.to_string()).collect();
self
}
pub fn bookend(mut self, group: &str) -> Self {
self.bookend = Some(group.to_string());
self
}
pub fn redeal_waste_onto_group(mut self) -> Self {
self.redeal_waste_onto_group = true;
self
}
pub fn only_when_empty_first_round(mut self) -> Self {
self.only_when_empty_first_round = true;
self
}
pub fn top_up_group_to(mut self, group: &str, len: u16) -> Self {
self.top_up_group_to = Some((group.to_string(), len));
self
}
pub fn redeal_switch_to_auto_refill(mut self, group: &str) -> Self {
self.redeal_switch_to_auto_refill = Some(group.to_string());
self
}
pub fn sweep_to_discard_round1(mut self, discard: &str) -> Self {
self.sweep_to_discard_round1 = Some(discard.to_string());
self
}
pub fn sweep_group_on_waste(mut self, group: &str) -> Self {
self.sweep_group_on_waste = Some(group.to_string());
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct RedealRules {
pub group: String,
#[serde(default, skip_serializing_if = "is_zero_u32")]
pub times: u32,
#[serde(default, skip_serializing_if = "is_false")]
pub unlimited: bool,
pub how: RedealHow,
}
impl RedealRules {
pub fn new(group: &str, how: RedealHow) -> RedealRules {
RedealRules {
group: group.to_string(),
times: 1,
unlimited: false,
how,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RedealHow {
Regroup { per_pile: u16 },
Shuffle { per_pile: u16 },
GridShuffle,
Compact {
#[serde(default, skip_serializing_if = "Option::is_none")]
refill_from: Option<String>,
},
GridShuffleRandomGaps,
RotateBottom,
DealRows,
RedistributePile,
ShuffleRedeal,
ShuffleTopUp { per_pile: u16 },
GridShuffleEndGaps,
DealStaircase,
CompactColumns {
#[serde(default, skip_serializing_if = "Option::is_none")]
refill_from: Option<String>,
},
FillEmpty { refill_from: String },
ShuffleFill {
per_pile: u16,
reserve: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
divert_ranks: Vec<u8>,
},
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct DealStep {
pub to: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub piles: Option<Vec<u16>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub count: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub counts: Option<Vec<u16>>,
#[serde(default, skip_serializing_if = "is_false")]
pub rest: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ranks: Option<Vec<u8>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u16>,
#[serde(default = "yes", skip_serializing_if = "is_true")]
pub face_up: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub skip_ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub kings_to_bottom: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub alternate_colors: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub retry_after_divert: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub one_each: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub collect: Option<Vec<u8>>,
#[serde(default, skip_serializing_if = "is_false")]
pub set_base: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub of_base: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub one_per_suit: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub split_on_rank: Option<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub split_after: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub auto_foundation: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub divert_ranks: Option<Vec<u8>>,
#[serde(default, skip_serializing_if = "is_false")]
pub drain_foundations: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub stop_when_out: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub route: Option<DealRoute>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub set_aside: Option<SetAside>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct SetAside {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub rules: Vec<AsideRule>,
#[serde(default, skip_serializing_if = "is_zero_u16")]
pub per_round: u16,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct AsideRule {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub rank_matches_pile: bool,
pub count: u16,
}
impl SetAside {
pub fn new(per_round: u16) -> SetAside {
SetAside {
rules: Vec::new(),
per_round,
}
}
pub fn rule(mut self, ranks: &[u8], rank_matches_pile: bool, count: u16) -> Self {
self.rules.push(AsideRule {
ranks: ranks.to_vec(),
rank_matches_pile,
count,
});
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum DealRoute {
Rank { first_rank: u8 },
Suit,
Chain { rank: u8, to: String },
Split { rank: u8, to: String },
}
impl DealStep {
pub fn to(group: &str) -> DealStep {
DealStep {
to: group.to_string(),
face_up: true,
..DealStep::default()
}
}
pub fn each(mut self, n: u16) -> Self {
self.count = Some(n);
self
}
pub fn counts(mut self, c: &[u16]) -> Self {
self.counts = Some(c.to_vec());
self
}
pub fn rest(mut self) -> Self {
self.rest = true;
self
}
pub fn set_aside(mut self, sa: SetAside) -> Self {
self.set_aside = Some(sa);
self
}
pub fn piles(mut self, p: &[u16]) -> Self {
self.piles = Some(p.to_vec());
self
}
pub fn ranks(mut self, r: &[u8]) -> Self {
self.ranks = Some(r.to_vec());
self
}
pub fn down(mut self) -> Self {
self.face_up = false;
self
}
pub fn skip_ranks(mut self, ranks: &[u8]) -> Self {
self.skip_ranks = ranks.to_vec();
self
}
pub fn kings_to_bottom(mut self) -> Self {
self.kings_to_bottom = true;
self
}
pub fn collect(mut self, ranks: &[u8]) -> Self {
self.collect = Some(ranks.to_vec());
self
}
pub fn one_each(mut self) -> Self {
self.one_each = true;
self
}
pub fn set_base(mut self) -> Self {
self.set_base = true;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct RemovalRules {
pub kind: RemovalKind,
pub from: Vec<String>,
pub to: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub adjacent: Option<Adjacency>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub exclude_ranks: Vec<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub needs_latch: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_len: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_sum: Option<u8>,
#[serde(default, skip_serializing_if = "is_false")]
pub face_ten: bool,
}
impl RemovalRules {
pub fn new(kind: RemovalKind, from: &[&str], to: &str) -> RemovalRules {
RemovalRules {
kind,
from: from.iter().map(|s| s.to_string()).collect(),
to: to.to_string(),
adjacent: None,
exclude_ranks: Vec::new(),
needs_latch: false,
min_len: None,
max_sum: None,
face_ten: false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Adjacency {
Eight,
Four,
SameLine,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RemovalKind {
PairsSum {
total: u8,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
alone: Vec<u8>,
},
LowerOfSuit,
SameRank,
RankSet { ranks: Vec<u8> },
ReachSuitOrRank { distances: Vec<u16> },
BetweenMatch { max_len: u8 },
ContiguousSumMultiple { divisor: u8, max_len: u8 },
SameRankIn { ranks: Vec<u8> },
RankDistance { distances: Vec<u8> },
PairsSumOtherSuit { total: u8 },
SameSuit,
PairsSumSameSuit { total: u8 },
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum WinRule {
#[default]
AllOnFoundations,
Empty(Vec<String>),
PilesEmpty {
group: String,
piles: Vec<u16>,
empty: Vec<String>,
},
Count { group: String, cards: u16 },
CountAndEmpty {
group: String,
cards: u16,
empty: Vec<String>,
},
CountAndSpread {
group: String,
cards: u16,
spread: String,
},
AtMostPiles { group: String, piles: u16 },
Rows {
group: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
length: Option<u16>,
},
Score { at_least: u32 },
RoundScore { at_least: u32 },
AllFull { group: String },
EachRowScore { at_least: u32 },
ColorSplit { group: String, red: u16 },
RowsBuilt {
group: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
length: Option<u16>,
build: Build,
},
PackOrder { group: String },
PilesBuilt {
group: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
length: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
build: Option<Build>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
empty: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
non_empty: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pile: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
bottom: Option<u8>,
},
RingRuns {
group: String,
runs: u16,
length: u16,
},
AllIsolated {
group: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
empty: Vec<String>,
},
TargetCard {
group: String,
discard: String,
cards: u16,
#[serde(default, skip_serializing_if = "is_zero_u16")]
exclude_piles: u16,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct RoundRules {
pub crib: String,
pub hands: Vec<String>,
pub starter: String,
pub discard: String,
#[serde(default, skip_serializing_if = "is_zero_u16")]
pub crib_down: u16,
#[serde(default = "six", skip_serializing_if = "is_six")]
pub hand_size: u16,
#[serde(default = "four", skip_serializing_if = "is_four")]
pub keep: u16,
#[serde(default, skip_serializing_if = "is_zero_u16")]
pub next_hand_after: u16,
#[serde(default, skip_serializing_if = "CribbagePoints::is_standard")]
pub points: CribbagePoints,
}
fn six() -> u16 {
6
}
fn is_six(n: &u16) -> bool {
*n == 6
}
fn four() -> u16 {
4
}
fn is_four(n: &u16) -> bool {
*n == 4
}
impl RoundRules {
pub fn new(crib: &str, hand: &str, starter: &str, discard: &str) -> RoundRules {
RoundRules {
crib: crib.to_string(),
hands: vec![hand.to_string()],
starter: starter.to_string(),
discard: discard.to_string(),
crib_down: 0,
hand_size: 6,
keep: 4,
next_hand_after: 0,
points: CribbagePoints::default(),
}
}
pub fn second_hand(mut self, hand: &str, after: u16) -> Self {
self.hands.push(hand.to_string());
self.next_hand_after = after;
self
}
pub fn crib_down(mut self, n: u16) -> Self {
self.crib_down = n;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScoreRules {
pub group: String,
pub hands: Hands,
}
impl ScoreRules {
pub fn new(group: &str, hands: Hands) -> ScoreRules {
ScoreRules {
group: group.to_string(),
hands,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Hands {
Poker(PokerTable),
Cribbage(CribbageRules),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct CribbageRules {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub starter: Option<String>,
#[serde(default, skip_serializing_if = "CribbagePoints::is_standard")]
pub points: CribbagePoints,
}
impl CribbageRules {
pub fn with_starter(group: &str) -> CribbageRules {
CribbageRules {
starter: Some(group.to_string()),
points: CribbagePoints::default(),
}
}
}
impl Default for CribbageRules {
fn default() -> Self {
CribbageRules {
starter: None,
points: CribbagePoints::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields, default)]
#[non_exhaustive]
pub struct CribbagePoints {
pub fifteen: u32,
pub pair: u32,
pub run_card: u32,
pub flush: u32,
pub flush_starter: u32,
pub nobs: u32,
pub heels: u32,
}
impl Default for CribbagePoints {
fn default() -> Self {
CribbagePoints {
fifteen: 2,
pair: 2,
run_card: 1,
flush: 4,
flush_starter: 1,
nobs: 1,
heels: 2,
}
}
}
impl CribbagePoints {
pub fn is_standard(&self) -> bool {
*self == CribbagePoints::default()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[non_exhaustive]
pub enum PokerTable {
Named(PokerScoring),
Points(PokerPoints),
}
impl PokerTable {
pub fn points(&self) -> PokerPoints {
match self {
PokerTable::Named(PokerScoring::American) => PokerPoints {
pair: 2,
two_pair: 5,
three: 10,
straight: 15,
flush: 20,
full_house: 25,
four: 50,
straight_flush: 75,
royal_flush: 100,
},
PokerTable::Named(PokerScoring::English) => PokerPoints {
pair: 1,
two_pair: 3,
three: 6,
straight: 12,
flush: 5,
full_house: 10,
four: 16,
straight_flush: 30,
royal_flush: 30,
},
PokerTable::Points(p) => p.clone(),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PokerScoring {
American,
English,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields, default)]
#[non_exhaustive]
pub struct PokerPoints {
pub pair: u32,
pub two_pair: u32,
pub three: u32,
pub straight: u32,
pub flush: u32,
pub full_house: u32,
pub four: u32,
pub straight_flush: u32,
pub royal_flush: u32,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ExtractSecond {
pub from: String,
pub to: Vec<String>,
}
impl ExtractSecond {
pub fn new(from: &str, to: &[&str]) -> ExtractSecond {
ExtractSecond {
from: from.to_string(),
to: to.iter().map(|s| s.to_string()).collect(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct AuxRules {
#[serde(default, skip_serializing_if = "is_zero_pair")]
pub initial: [i16; 2],
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub updates: Vec<AuxUpdate>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub gates: Vec<AuxGate>,
}
fn is_zero_pair(p: &[i16; 2]) -> bool {
*p == [0, 0]
}
impl AuxRules {
pub fn new(initial: [i16; 2]) -> AuxRules {
AuxRules {
initial,
updates: Vec::new(),
gates: Vec::new(),
}
}
pub fn update(mut self, on: AuxEvent, when: Option<AuxCond>, slot: u8, op: AuxOp) -> Self {
self.updates.push(AuxUpdate { on, when, slot, op });
self
}
pub fn gate(mut self, on: AuxEvent, require: AuxCond) -> Self {
self.gates.push(AuxGate { on, require });
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct AuxUpdate {
pub on: AuxEvent,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub when: Option<AuxCond>,
pub slot: u8,
pub op: AuxOp,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct AuxGate {
pub on: AuxEvent,
pub require: AuxCond,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AuxEvent {
Deal,
Transfer {
#[serde(default, skip_serializing_if = "Option::is_none")]
from: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
to: Option<String>,
},
Refill {
#[serde(default, skip_serializing_if = "Option::is_none")]
group: Option<String>,
},
Extract,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AuxOp {
Set(i16),
Add {
by: i16,
#[serde(default, skip_serializing_if = "Option::is_none")]
modulo: Option<i16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
floor: Option<i16>,
},
SourcePile,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AuxCond {
Counter {
slot: u8,
cmp: AuxCmp,
value: i16,
},
AnyEmpty {
group: String,
},
AnyNonEmpty {
group: String,
},
RankIsCounter {
slot: u8,
offset: i16,
},
SharesSetWithSource {
slot: u8,
sets: Vec<Vec<u16>>,
},
Not(Box<AuxCond>),
All(Vec<AuxCond>),
Any(Vec<AuxCond>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AuxCmp {
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
}
impl Rules {
pub fn from_json(s: &str) -> Result<Rules, serde_json::Error> {
serde_json::from_str(s)
}
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(self).expect("rules serialize")
}
pub fn patch(&self, patch: &serde_json::Value) -> Result<Rules, serde_json::Error> {
let mut base = serde_json::to_value(self)?;
merge_json_value(&mut base, patch);
serde_json::from_value(base)
}
pub fn patch_json(&self, patch_json: &str) -> Result<Rules, serde_json::Error> {
let patch: serde_json::Value = serde_json::from_str(patch_json)?;
self.patch(&patch)
}
}
fn merge_json_value(base: &mut serde_json::Value, patch: &serde_json::Value) {
match (base, patch) {
(serde_json::Value::Object(base_map), serde_json::Value::Object(patch_map)) => {
for (k, v) in patch_map {
if v.is_null() {
base_map.remove(k);
} else if let Some(existing) = base_map.get_mut(k) {
merge_json_value(existing, v);
} else {
base_map.insert(k.clone(), v.clone());
}
}
}
(base_slot, patch_val) => {
*base_slot = patch_val.clone();
}
}
}
#[cfg(test)]
mod patch_tests {
#[test]
fn patch_json_modifies_preset_fields() {
let kl1 = crate::presets::klondike1();
let patched = kl1
.patch_json(r#"{"stock":{"count":3,"unlimited_redeals":false,"redeals":2}}"#)
.expect("patch should succeed");
let st = patched.stock.as_ref().unwrap();
assert_eq!(st.count, 3);
assert!(!st.unlimited_redeals);
assert_eq!(st.redeals, 2);
assert_eq!(patched.name, kl1.name);
assert_eq!(patched.groups.len(), kl1.groups.len());
assert!(crate::Game::new(patched).is_ok());
}
}