cardinal-kernel 0.1.1

Headless, deterministic rules engine for turn-based, TCG-like games.
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::collections::{HashMap, HashSet};
use crate::{
    ids::CardId,
    rules::schema::{CardDef, Ruleset},
    model::command::{Command, StackItem, EffectRef},
};

/// Maps card IDs to their definitions for O(1) lookup during gameplay
pub type CardRegistry = HashMap<u32, CardDef>;

/// Build a card registry from card definitions without validation
pub fn build_registry(cards: &[CardDef]) -> CardRegistry {
    let mut registry = HashMap::new();
    
    for card_def in cards {
        // Parse card ID as u32 if it's numeric, otherwise skip
        if let Ok(card_id) = card_def.id.parse::<u32>() {
            registry.insert(card_id, card_def.clone());
        }
    }
    
    registry
}

/// Build a card registry from card definitions with ruleset validation
/// This validates that cards only reference keywords defined in the ruleset
pub fn build_validated_registry(cards: &[CardDef], ruleset: &Ruleset) -> Result<CardRegistry, String> {
    let mut registry = HashMap::new();
    
    // Build set of valid keyword IDs from ruleset
    let valid_keywords: HashSet<String> = ruleset.keywords.iter()
        .map(|k| k.id.clone())
        .collect();
    
    for card_def in cards {
        // Validate keywords - each keyword must exist in ruleset
        for keyword in &card_def.keywords {
            if !valid_keywords.contains(keyword) {
                let mut sorted_keywords: Vec<_> = valid_keywords.iter().collect();
                sorted_keywords.sort();
                let keyword_list = if sorted_keywords.len() > 10 {
                    format!("{:?} and {} more", &sorted_keywords[..10], sorted_keywords.len() - 10)
                } else {
                    format!("{}", sorted_keywords.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", "))
                };
                
                return Err(format!(
                    "Card '{}' (ID: {}) references undefined keyword '{}'. Valid keywords: {}",
                    card_def.name,
                    card_def.id,
                    keyword,
                    keyword_list
                ));
            }
        }
        
        // Parse card ID as u32 if it's numeric, otherwise skip
        if let Ok(card_id) = card_def.id.parse::<u32>() {
            registry.insert(card_id, card_def.clone());
        }
    }
    
    Ok(registry)
}

/// Get a card definition by ID
pub fn get_card(registry: &CardRegistry, card_id: CardId) -> Option<&CardDef> {
    registry.get(&card_id.0)
}

/// Generate commands from a card's abilities when an event matches a trigger
pub fn generate_ability_commands(
    card_id: CardId,
    event_trigger: &str,
    controller: crate::ids::PlayerId,
    registry: &CardRegistry,
    next_stack_id: &mut u32,
) -> Vec<Command> {
    let mut commands = Vec::new();
    
    if let Some(card_def) = get_card(registry, card_id) {
        for ability in &card_def.abilities {
            // Only fire if the trigger matches
            if ability.trigger == event_trigger {
                // Generate a command for this ability effect
                if let Some(cmd) = effect_to_command(
                    card_id,
                    &ability.effect,
                    &ability.params,
                    controller,
                    next_stack_id,
                ) {
                    commands.push(cmd);
                }
            }
        }
    }
    
    commands
}

/// Convert a card ability effect into an engine Command
fn effect_to_command(
    source: CardId,
    effect_kind: &str,
    params: &std::collections::HashMap<String, String>,
    controller: crate::ids::PlayerId,
    stack_id: &mut u32,
) -> Option<Command> {
    let id = *stack_id;
    *stack_id += 1;

    // Check if this is a scripted effect (indicated by "script:" prefix)
    if effect_kind.starts_with("script:") {
        let script_name = effect_kind.strip_prefix("script:").unwrap_or(effect_kind);
        
        // Validate script name is not empty
        if script_name.is_empty() {
            return None;
        }
        
        return Some(Command::PushStack {
            item: StackItem {
                id,
                source: Some(source),
                controller,
                effect: EffectRef::Scripted(script_name.to_string()),
            },
        });
    }

    match effect_kind {
        "damage" => {
            let amount = params.get("amount")
                .and_then(|s| s.parse::<i32>().ok())
                .unwrap_or(1);
            
            let effect_str = Box::leak(format!("damage_{}", amount).into_boxed_str());
            
            Some(Command::PushStack {
                item: StackItem {
                    id,
                    source: Some(source),
                    controller,
                    effect: EffectRef::Builtin(effect_str),
                },
            })
        }
        "draw" => {
            let amount = params.get("amount")
                .and_then(|s| s.parse::<u32>().ok())
                .unwrap_or(1);
            
            let effect_str = Box::leak(format!("draw_{}", amount).into_boxed_str());
            
            Some(Command::PushStack {
                item: StackItem {
                    id,
                    source: Some(source),
                    controller,
                    effect: EffectRef::Builtin(effect_str),
                },
            })
        }
        "gain_life" => {
            let amount = params.get("amount")
                .and_then(|s| s.parse::<i32>().ok())
                .unwrap_or(1);
            
            let effect_str = Box::leak(format!("gain_life_{}", amount).into_boxed_str());
            
            Some(Command::PushStack {
                item: StackItem {
                    id,
                    source: Some(source),
                    controller,
                    effect: EffectRef::Builtin(effect_str),
                },
            })
        }
        "pump" => {
            let power = params.get("power")
                .and_then(|s| s.parse::<i32>().ok())
                .unwrap_or(1);
            let toughness = params.get("toughness")
                .and_then(|s| s.parse::<i32>().ok())
                .unwrap_or(1);
            
            let effect_str = Box::leak(format!("pump_{}_{}", power, toughness).into_boxed_str());
            
            Some(Command::PushStack {
                item: StackItem {
                    id,
                    source: Some(source),
                    controller,
                    effect: EffectRef::Builtin(effect_str),
                },
            })
        }
        _ => {
            // Unknown effect type - skip
            None
        }
    }
}

/// Check if a card has a specific keyword
pub fn card_has_keyword(card_def: &CardDef, keyword_id: &str) -> bool {
    card_def.keywords.iter().any(|k| k == keyword_id)
}

/// Get a card's stat value by key
pub fn get_card_stat<'a>(card_def: &'a CardDef, stat_key: &str) -> Option<&'a String> {
    card_def.stats.get(stat_key)
}

/// Get a card's stat as an integer
pub fn get_card_stat_i32(card_def: &CardDef, stat_key: &str) -> Option<i32> {
    card_def.stats.get(stat_key)
        .and_then(|s| s.parse::<i32>().ok())
}

/// Get a card's stat as an integer, returning Result for better error handling
pub fn parse_card_stat_i32(card_def: &CardDef, stat_key: &str) -> Result<i32, String> {
    match card_def.stats.get(stat_key) {
        None => Err(format!("Stat '{}' not found on card '{}'", stat_key, card_def.name)),
        Some(value) => value.parse::<i32>()
            .map_err(|_| format!("Stat '{}' on card '{}' has invalid integer value: '{}'", stat_key, card_def.name, value))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rules::schema::{Keyword, Ruleset, GameInfo, PlayerRules, TurnStructure};
    
    fn minimal_ruleset() -> Ruleset {
        Ruleset {
            game: GameInfo {
                id: "test".to_string(),
                name: "Test Game".to_string(),
                version: "1.0".to_string(),
                description: "Test".to_string(),
            },
            players: PlayerRules {
                min_players: 2,
                max_players: 2,
                starting_life: 20,
                max_life: 100,
                starting_hand_size: 5,
                max_hand_size: 10,
                min_deck_size: 40,
                max_deck_size: 60,
                mulligan_rule: "none".to_string(),
                first_player_rule: "random".to_string(),
            },
            zones: vec![],
            resources: vec![],
            turn: TurnStructure {
                priority_system: true,
                skip_first_turn_draw_for_first_player: false,
                phases: vec![],
            },
            actions: vec![],
            stack: crate::rules::schema::StackRules {
                enabled: true,
                resolve_order: "lifo".to_string(),
                auto_resolve_on_pass: true,
            },
            trigger_kinds: vec![],
            keywords: vec![
                Keyword {
                    id: "flying".to_string(),
                    name: "Flying".to_string(),
                    description: "Can only be blocked by flying creatures".to_string(),
                },
                Keyword {
                    id: "quick".to_string(),
                    name: "Quick".to_string(),
                    description: "Can be played at instant speed".to_string(),
                },
            ],
            win_conditions: vec![],
            loss_conditions: vec![],
            cards: vec![],
        }
    }
    
    #[test]
    fn test_validate_valid_keywords() {
        let ruleset = minimal_ruleset();
        let mut card = CardDef {
            id: "1".to_string(),
            name: "Test Card".to_string(),
            card_type: "creature".to_string(),
            cost: None,
            description: None,
            abilities: vec![],
            script_path: None,
            keywords: vec!["flying".to_string()],
            stats: std::collections::HashMap::new(),
        };
        
        let result = build_validated_registry(&[card.clone()], &ruleset);
        assert!(result.is_ok());
        
        // Test with multiple valid keywords
        card.keywords = vec!["flying".to_string(), "quick".to_string()];
        let result = build_validated_registry(&[card], &ruleset);
        assert!(result.is_ok());
    }
    
    #[test]
    fn test_validate_invalid_keyword() {
        let ruleset = minimal_ruleset();
        let card = CardDef {
            id: "1".to_string(),
            name: "Test Card".to_string(),
            card_type: "creature".to_string(),
            cost: None,
            description: None,
            abilities: vec![],
            script_path: None,
            keywords: vec!["invalid_keyword".to_string()],
            stats: std::collections::HashMap::new(),
        };
        
        let result = build_validated_registry(&[card], &ruleset);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("undefined keyword"));
    }
    
    #[test]
    fn test_card_has_keyword() {
        let card = CardDef {
            id: "1".to_string(),
            name: "Test Card".to_string(),
            card_type: "creature".to_string(),
            cost: None,
            description: None,
            abilities: vec![],
            script_path: None,
            keywords: vec!["flying".to_string(), "quick".to_string()],
            stats: std::collections::HashMap::new(),
        };
        
        assert!(card_has_keyword(&card, "flying"));
        assert!(card_has_keyword(&card, "quick"));
        assert!(!card_has_keyword(&card, "haste"));
    }
    
    #[test]
    fn test_get_card_stats() {
        let mut stats = std::collections::HashMap::new();
        stats.insert("power".to_string(), "3".to_string());
        stats.insert("toughness".to_string(), "4".to_string());
        
        let card = CardDef {
            id: "1".to_string(),
            name: "Test Creature".to_string(),
            card_type: "creature".to_string(),
            cost: None,
            description: None,
            abilities: vec![],
            script_path: None,
            keywords: vec![],
            stats,
        };
        
        assert_eq!(get_card_stat(&card, "power"), Some(&"3".to_string()));
        assert_eq!(get_card_stat_i32(&card, "power"), Some(3));
        assert_eq!(get_card_stat_i32(&card, "toughness"), Some(4));
        assert_eq!(get_card_stat(&card, "missing"), None);
    }
    
    #[test]
    fn test_parse_card_stat_i32() {
        let mut stats = std::collections::HashMap::new();
        stats.insert("power".to_string(), "3".to_string());
        stats.insert("invalid".to_string(), "not_a_number".to_string());
        
        let card = CardDef {
            id: "1".to_string(),
            name: "Test Creature".to_string(),
            card_type: "creature".to_string(),
            cost: None,
            description: None,
            abilities: vec![],
            script_path: None,
            keywords: vec![],
            stats,
        };
        
        // Valid stat
        assert_eq!(parse_card_stat_i32(&card, "power"), Ok(3));
        
        // Missing stat
        assert!(parse_card_stat_i32(&card, "missing").is_err());
        assert!(parse_card_stat_i32(&card, "missing").unwrap_err().contains("not found"));
        
        // Invalid integer
        assert!(parse_card_stat_i32(&card, "invalid").is_err());
        assert!(parse_card_stat_i32(&card, "invalid").unwrap_err().contains("invalid integer value"));
    }
}