tbg/
card.rs

1use crate::attribute::Attribute;
2use crate::misc::{Source, Zone};
3use crate::usize_wrapper::{CardID, PlayerID};
4use std::collections::{BTreeMap, BTreeSet};
5
6#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
7pub struct Card<CardType, AttName>
8where
9    AttName: Ord,
10{
11    id: CardID,
12    owner: Option<PlayerID>,
13    pub src: String,
14    pub zone: Zone,
15    pub card_type: CardType,
16    pub created_by: Source,
17    attributes: BTreeMap<AttName, Attribute>,
18}
19
20impl<CardType, AttName> Card<CardType, AttName>
21where
22    AttName: Default + Ord,
23{
24    pub fn new(
25        id: CardID,
26        card_type: CardType,
27        owner: Option<PlayerID>,
28    ) -> Card<CardType, AttName> {
29        Card {
30            id,
31            owner: owner,
32            src: "Default".to_string(),
33            zone: Zone::Nowhere,
34            card_type,
35            created_by: Source::Game,
36            attributes: Default::default(),
37        }
38    }
39
40    pub fn id(&self) -> CardID {
41        self.id
42    }
43
44    pub fn owner(&self) -> Option<PlayerID> {
45        self.owner
46    }
47
48    pub fn int(&self, att: AttName) -> Option<isize> {
49        self.attributes.get(&att)?.int()
50    }
51
52    pub fn text(&self, att: AttName) -> Option<&str> {
53        self.attributes.get(&att)?.text()
54    }
55
56    pub fn flag(&self, att: AttName) -> Option<bool> {
57        self.attributes.get(&att)?.flag()
58    }
59
60    pub fn set(&self, att: AttName) -> Option<&BTreeSet<Attribute>> {
61        self.attributes.get(&att)?.set()
62    }
63
64    pub fn att(&mut self, name: AttName, att: Attribute) {
65        self.attributes.insert(name, att);
66    }
67}