issun 0.10.0

A mini game engine for logic-focused games - Build games in ISSUN (一寸) of time
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Orchestration system for perception updates
//!
//! This system coordinates perception updates, confidence decay, and hook calls.

use super::config::PerceptionConfig;
use super::hook::PerceptionHook;
use super::service::PerceptionService;
use super::state::KnowledgeBoardRegistry;
use super::types::{FactionId, GroundTruthFact};
use crate::context::ResourceContext;
use crate::system::System;
use async_trait::async_trait;
use std::any::Any;
use std::sync::Arc;
use std::time::Duration;

/// Perception system (orchestration)
///
/// This system orchestrates:
/// 1. Transforming ground truth into per-faction perceived facts (via hooks)
/// 2. Decaying confidence over time
/// 3. Managing knowledge boards
#[derive(Clone)]
pub struct PerceptionSystem {
    service: PerceptionService,
    hook: Arc<dyn PerceptionHook>,
}

impl PerceptionSystem {
    /// Create a new perception system with a custom hook
    pub fn new(hook: Arc<dyn PerceptionHook>) -> Self {
        Self {
            service: PerceptionService,
            hook,
        }
    }

    /// Update perceptions from ground truths
    ///
    /// This method:
    /// 1. Gets faction accuracies via hook (game-specific logic)
    /// 2. Transforms ground truth into perceived facts using PerceptionService
    /// 3. Updates each faction's knowledge board
    ///
    /// # Arguments
    ///
    /// * `ground_truths` - Array of ground truth facts to perceive
    /// * `resources` - Mutable access to resource context
    ///
    /// # Errors
    ///
    /// Returns error if required resources are not found
    pub async fn update_perceptions(
        &mut self,
        ground_truths: &[GroundTruthFact],
        resources: &mut ResourceContext,
    ) -> Result<(), String> {
        let _config = resources
            .get::<PerceptionConfig>()
            .await
            .ok_or("PerceptionConfig not found")?;

        let mut boards = resources
            .get_mut::<KnowledgeBoardRegistry>()
            .await
            .ok_or("KnowledgeBoardRegistry not found")?;

        for truth in ground_truths {
            // Hook: Get faction-specific accuracies (game-specific logic)
            let faction_accuracies = {
                let boards_ref = &*boards;
                self.hook.get_faction_accuracies(truth, boards_ref).await
            };

            for (faction_id, accuracy) in faction_accuracies {
                let mut rng = rand::thread_rng();

                // Service: Transform ground truth into perceived fact
                let perceived = PerceptionService::perceive_fact(truth, accuracy, &mut rng);

                // Update knowledge board
                if let Some(board) = boards.get_board_mut(&faction_id) {
                    board.update_fact(truth.id.clone(), perceived, accuracy, truth.timestamp);
                }
            }
        }

        Ok(())
    }

    /// Decay confidence for all facts across all factions
    ///
    /// This method:
    /// 1. Decays confidence for each fact using exponential decay
    /// 2. Removes facts below minimum confidence threshold
    ///
    /// Typically called once per game turn.
    ///
    /// # Arguments
    ///
    /// * `resources` - Mutable access to resource context
    /// * `delta_time` - Time elapsed since last decay (in game turns or seconds)
    ///
    /// # Errors
    ///
    /// Returns error if required resources are not found
    pub async fn decay_confidence(
        &self,
        resources: &mut ResourceContext,
        delta_time: Duration,
    ) -> Result<(), String> {
        let config = resources
            .get::<PerceptionConfig>()
            .await
            .ok_or("PerceptionConfig not found")?;

        let mut boards = resources
            .get_mut::<KnowledgeBoardRegistry>()
            .await
            .ok_or("KnowledgeBoardRegistry not found")?;

        // Collect facts to update/remove (we can't mutate while iterating)
        type FactUpdate = (String, super::types::PerceivedFact, f32, u64);
        let mut facts_to_update: Vec<(FactionId, Vec<FactUpdate>)> = Vec::new();
        let mut facts_to_remove: Vec<(FactionId, Vec<String>)> = Vec::new();

        for (faction_id, board) in boards.all_boards_mut() {
            let mut to_update = Vec::new();
            let mut to_remove = Vec::new();

            // Collect fact data first (read-only)
            let fact_data: Vec<_> = board
                .all_facts()
                .map(|(fact_id, perceived)| {
                    let confidence = board.get_confidence(fact_id).unwrap_or(0.0);
                    let timestamp = board.get_last_updated(fact_id).unwrap_or(0);
                    (fact_id.clone(), perceived.clone(), confidence, timestamp)
                })
                .collect();

            // Calculate what to do with each fact
            for (fact_id, perceived, current_confidence, timestamp) in fact_data {
                // Calculate decayed confidence
                let new_confidence = self.service.calculate_confidence_decay(
                    current_confidence,
                    delta_time,
                    config.decay_rate,
                );

                // Check if below threshold
                if new_confidence < config.min_confidence {
                    to_remove.push(fact_id);
                } else {
                    to_update.push((fact_id, perceived, new_confidence, timestamp));
                }
            }

            if !to_update.is_empty() {
                facts_to_update.push((faction_id.clone(), to_update));
            }

            if !to_remove.is_empty() {
                facts_to_remove.push((faction_id.clone(), to_remove));
            }
        }

        // Apply updates
        for (faction_id, updates) in facts_to_update {
            if let Some(board) = boards.get_board_mut(&faction_id) {
                for (fact_id, perceived, new_confidence, timestamp) in updates {
                    board.update_fact(fact_id, perceived, new_confidence, timestamp);
                }
            }
        }

        // Remove low-confidence facts
        for (faction_id, fact_ids) in facts_to_remove {
            if let Some(board) = boards.get_board_mut(&faction_id) {
                for fact_id in fact_ids {
                    board.remove_fact(&fact_id);
                }
            }
        }

        Ok(())
    }

    /// Get a faction's knowledge board (read-only)
    ///
    /// # Arguments
    ///
    /// * `faction_id` - The faction to get the board for
    /// * `resources` - Immutable access to resource context
    ///
    /// # Returns
    ///
    /// Cloned knowledge board, or None if faction not found
    pub async fn get_faction_board(
        &self,
        faction_id: &FactionId,
        resources: &ResourceContext,
    ) -> Option<super::state::KnowledgeBoard> {
        let boards = resources.get::<KnowledgeBoardRegistry>().await?;
        boards.get_board(faction_id).cloned()
    }
}

#[async_trait]
impl System for PerceptionSystem {
    fn name(&self) -> &'static str {
        "perception_system"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl Default for PerceptionSystem {
    fn default() -> Self {
        Self::new(Arc::new(super::hook::DefaultPerceptionHook))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plugin::subjective_reality::types::*;
    use std::collections::HashMap;

    // Mock hook for testing
    #[derive(Clone)]
    struct MockPerceptionHook {
        accuracies: HashMap<FactionId, f32>,
    }

    #[async_trait::async_trait]
    impl PerceptionHook for MockPerceptionHook {
        async fn get_faction_accuracies(
            &self,
            _truth: &GroundTruthFact,
            _boards: &KnowledgeBoardRegistry,
        ) -> HashMap<FactionId, f32> {
            self.accuracies.clone()
        }
    }

    #[tokio::test]
    async fn test_update_perceptions() {
        let mut resources = ResourceContext::new();

        // Setup
        let config = PerceptionConfig::default();
        resources.insert(config);

        let mut registry = KnowledgeBoardRegistry::new();
        registry.register_faction("faction_a".into());
        registry.register_faction("faction_b".into());
        resources.insert(registry);

        // Create system with mock hook
        let mut hook_accuracies = HashMap::new();
        hook_accuracies.insert("faction_a".into(), 0.9);
        hook_accuracies.insert("faction_b".into(), 0.6);

        let hook = Arc::new(MockPerceptionHook {
            accuracies: hook_accuracies,
        });

        let mut system = PerceptionSystem::new(hook);

        // Create ground truth
        let truth = GroundTruthFact::new(
            "fact_001",
            FactType::MilitaryStrength {
                faction: "enemy".into(),
                strength: 1000,
            },
            100,
        );

        // Update perceptions
        let result = system.update_perceptions(&[truth], &mut resources).await;
        assert!(result.is_ok());

        // Verify both factions have the fact
        let boards = resources.get::<KnowledgeBoardRegistry>().await.unwrap();

        let board_a = boards.get_board(&"faction_a".into()).unwrap();
        assert!(board_a.has_fact(&"fact_001".into()));
        assert_eq!(board_a.get_confidence(&"fact_001".into()), Some(0.9));

        let board_b = boards.get_board(&"faction_b".into()).unwrap();
        assert!(board_b.has_fact(&"fact_001".into()));
        assert_eq!(board_b.get_confidence(&"fact_001".into()), Some(0.6));
    }

    #[tokio::test]
    async fn test_decay_confidence() {
        let mut resources = ResourceContext::new();

        // Setup with 50% decay rate for fast testing
        let config = PerceptionConfig::default().with_decay_rate(0.5);
        resources.insert(config);

        let mut registry = KnowledgeBoardRegistry::new();
        registry.register_faction("faction_a".into());

        // Add a fact with initial confidence 1.0
        {
            let board = registry.get_board_mut(&"faction_a".into()).unwrap();
            board.update_fact(
                "fact_001".into(),
                PerceivedFact::new(
                    FactType::MilitaryStrength {
                        faction: "enemy".into(),
                        strength: 1000,
                    },
                    1.0,
                    Duration::from_secs(0),
                    None,
                ),
                1.0,
                100,
            );
        }

        resources.insert(registry);

        let system = PerceptionSystem::default();

        // Decay for 1 turn (50% decay → confidence should be 0.5)
        let result = system
            .decay_confidence(&mut resources, Duration::from_secs(1))
            .await;
        assert!(result.is_ok());

        // Verify confidence decayed
        let boards = resources.get::<KnowledgeBoardRegistry>().await.unwrap();
        let board = boards.get_board(&"faction_a".into()).unwrap();
        let confidence = board.get_confidence(&"fact_001".into()).unwrap();

        assert!(
            (confidence - 0.5).abs() < 0.01,
            "confidence: {}",
            confidence
        );
    }

    #[tokio::test]
    async fn test_decay_removes_low_confidence_facts() {
        let mut resources = ResourceContext::new();

        // Setup with high decay rate and high min_confidence threshold
        let config = PerceptionConfig::default()
            .with_decay_rate(0.9) // 90% decay
            .with_min_confidence(0.5); // Remove if below 50%
        resources.insert(config);

        let mut registry = KnowledgeBoardRegistry::new();
        registry.register_faction("faction_a".into());

        // Add a fact
        {
            let board = registry.get_board_mut(&"faction_a".into()).unwrap();
            board.update_fact(
                "fact_001".into(),
                PerceivedFact::new(
                    FactType::MilitaryStrength {
                        faction: "enemy".into(),
                        strength: 1000,
                    },
                    1.0,
                    Duration::from_secs(0),
                    None,
                ),
                1.0,
                100,
            );
        }

        resources.insert(registry);

        let system = PerceptionSystem::default();

        // Decay for 1 turn (90% decay → confidence = 0.1, below 0.5 threshold)
        let result = system
            .decay_confidence(&mut resources, Duration::from_secs(1))
            .await;
        assert!(result.is_ok());

        // Verify fact was removed
        let boards = resources.get::<KnowledgeBoardRegistry>().await.unwrap();
        let board = boards.get_board(&"faction_a".into()).unwrap();
        assert!(!board.has_fact(&"fact_001".into()));
    }

    #[tokio::test]
    async fn test_get_faction_board() {
        let mut resources = ResourceContext::new();

        let mut registry = KnowledgeBoardRegistry::new();
        registry.register_faction("faction_a".into());

        // Add a fact
        {
            let board = registry.get_board_mut(&"faction_a".into()).unwrap();
            board.update_fact(
                "fact_001".into(),
                PerceivedFact::new(
                    FactType::MarketPrice {
                        item: "wheat".into(),
                        price: 15.5,
                    },
                    0.8,
                    Duration::from_secs(0),
                    None,
                ),
                0.8,
                100,
            );
        }

        resources.insert(registry);

        let system = PerceptionSystem::default();

        // Get board
        let board = system
            .get_faction_board(&"faction_a".into(), &resources)
            .await;

        assert!(board.is_some());
        let board = board.unwrap();
        assert_eq!(board.fact_count(), 1);
        assert!(board.has_fact(&"fact_001".into()));
    }

    #[tokio::test]
    async fn test_get_faction_board_not_found() {
        let resources = ResourceContext::new();
        let system = PerceptionSystem::default();

        let board = system
            .get_faction_board(&"nonexistent".into(), &resources)
            .await;

        assert!(board.is_none());
    }
}