1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::{
    model::{
        card::Card,
        settings::GameSettings,
        stack::{Orientation, Stack, StackDetails, StackSelection},
    },
    utils::vec::SplitOffBounded,
};

use super::{
    Action, Area, AreaId, Held, MoveResult, NotSupported, Result, SelectedArea, SnafuSelectorExt,
    UnselectedArea,
};

#[derive(Copy, Clone, Debug)]
pub struct Selection;

#[derive(Debug)]
pub struct Stock<S> {
    cards: Vec<Card>,
    draw_from_stock_len: usize,
    selection: S,
}

pub type UnselectedStock = Stock<()>;
pub type SelectedStock = Stock<Selection>;

impl<S> Stock<S> {
    fn id(&self) -> AreaId {
        AreaId::Stock
    }

    fn validate_cards(&self, held: &Held) -> Result {
        if held.source == self.id() || held.source == AreaId::Talon {
            // We'll always take back our own cards, and we'll allow cards from the talon to be
            // replaced on us.
            Ok(())
        } else {
            // But no cards from anywhere else.
            NotSupported {
                message: format!("Cannot place cards from area: {:?}", held.source),
            }
            .fail()
        }
    }

    fn give_cards(&mut self, mut held: Held) -> MoveResult<(), Held> {
        match self.validate_cards(&held) {
            Ok(_) => {
                self.cards.append(&mut held.cards);
                MoveResult::Moved(())
            }
            Err(error) => MoveResult::Unmoved(held, error),
        }
    }

    fn take_cards(&mut self, len: usize) -> Held {
        let cards = self.cards.split_off_bounded(len);

        Held {
            source: AreaId::Stock,
            cards,
        }
    }

    fn as_stack(&self, mode: Option<Selection>) -> Stack<'_> {
        Stack {
            cards: &self.cards,
            details: StackDetails {
                orientation: Orientation::Horizontal,
                len: self.cards.len(),
                face_up_len: 0,
                visible_len: 2,
                spread_len: 1,
                selection: mode.map(|_| StackSelection {
                    len: 1,
                    held: false,
                }),
            },
        }
    }

    fn with_selection<T>(self, selection: T) -> Stock<T> {
        Stock {
            cards: self.cards,
            draw_from_stock_len: self.draw_from_stock_len,
            selection,
        }
    }
}

impl UnselectedStock {
    pub fn create(cards: Vec<Card>, settings: &GameSettings) -> Box<dyn UnselectedArea> {
        Box::new(Stock {
            cards,
            draw_from_stock_len: settings.draw_from_stock_len,
            selection: (),
        })
    }
}

impl Area for UnselectedStock {
    fn id(&self) -> AreaId {
        Stock::id(self)
    }

    fn is_selected(&self) -> bool {
        false
    }

    fn is_held(&self) -> bool {
        false
    }

    fn give_cards(&mut self, held: Held) -> MoveResult<(), Held> {
        Stock::give_cards(self, held)
    }

    fn take_cards(&mut self, len: usize) -> Held {
        Stock::take_cards(self, len)
    }

    fn take_all_cards(&mut self) -> Held {
        Stock::take_cards(self, self.cards.len())
    }

    fn peek_top_card(&self) -> Option<&Card> {
        self.cards.last()
    }

    fn as_stack(&self) -> Stack<'_> {
        self.as_stack(None)
    }

    fn as_area(&self) -> &dyn Area {
        self
    }

    fn as_area_mut(&mut self) -> &mut dyn Area {
        self
    }
}

impl Area for SelectedStock {
    fn id(&self) -> AreaId {
        Stock::id(self)
    }

    fn is_selected(&self) -> bool {
        true
    }

    fn is_held(&self) -> bool {
        false
    }

    fn give_cards(&mut self, held: Held) -> MoveResult<(), Held> {
        Stock::give_cards(self, held)
    }

    fn take_cards(&mut self, len: usize) -> Held {
        Stock::take_cards(self, len)
    }

    fn take_all_cards(&mut self) -> Held {
        Stock::take_cards(self, self.cards.len())
    }

    fn peek_top_card(&self) -> Option<&Card> {
        self.cards.last()
    }

    fn as_stack(&self) -> Stack<'_> {
        self.as_stack(Some(self.selection))
    }

    fn as_area(&self) -> &dyn Area {
        self
    }

    fn as_area_mut(&mut self) -> &mut dyn Area {
        self
    }
}

impl UnselectedArea for UnselectedStock {
    fn select(self: Box<Self>) -> MoveResult<Box<dyn SelectedArea>, Box<dyn UnselectedArea>> {
        MoveResult::Moved(Box::new(self.with_selection(Selection)))
    }

    fn select_with_held(
        self: Box<Self>,
        held: Held,
    ) -> MoveResult<Box<dyn SelectedArea>, (Box<dyn UnselectedArea>, Held)> {
        NotSupported {
            message: "Cards in this area cannot be held",
        }
        .fail_move((self, held))
    }
}

impl SelectedArea for SelectedStock {
    fn deselect(self: Box<Self>) -> (Box<dyn UnselectedArea>, Option<Held>) {
        let unselected = Box::new(self.with_selection(()));
        (unselected, None)
    }

    fn activate(&mut self) -> Result<Option<Action>> {
        if self.cards.is_empty() {
            Ok(Some(Action::Restock))
        } else {
            Ok(Some(Action::Draw(self.draw_from_stock_len)))
        }
    }

    fn pick_up(&mut self) -> Result {
        NotSupported {
            message: "Cards in this area cannot be held",
        }
        .fail()
    }
    fn put_down(&mut self) -> Result {
        NotSupported {
            message: "Cards in this area cannot be held",
        }
        .fail()
    }
    fn select_more(&mut self) -> Result {
        NotSupported {
            message: "Cards in this area cannot be held",
        }
        .fail()
    }
    fn select_less(&mut self) -> Result {
        NotSupported {
            message: "Cards in this area cannot be held",
        }
        .fail()
    }

    fn held_from(&self) -> Option<AreaId> {
        None
    }
}