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
//! System for market updates and orchestration

use super::config::MarketConfig;
use super::hook::MarketHook;
use super::service::MarketService;
use super::state::MarketState;
use super::types::{ItemId, MarketEvent, MarketTrend, PriceChange};

/// Market system (orchestrates market updates)
///
/// This system coordinates between service (pure logic), state (mutable data),
/// and hooks (game-specific customization).
pub struct MarketSystem<H: MarketHook> {
    hook: H,
}

impl<H: MarketHook> MarketSystem<H> {
    /// Create a new market system with hook
    pub fn new(hook: H) -> Self {
        Self { hook }
    }

    /// Update all market prices
    ///
    /// This performs a full market update:
    /// 1. Calculate new prices from supply/demand
    /// 2. Apply equilibrium drift
    /// 3. Detect trends
    /// 4. Call hooks
    ///
    /// # Arguments
    ///
    /// * `state` - Mutable market state
    /// * `config` - Market configuration
    ///
    /// # Returns
    ///
    /// Vector of price changes that occurred
    pub async fn update_prices(
        &self,
        state: &mut MarketState,
        config: &MarketConfig,
    ) -> Vec<PriceChange> {
        let mut price_changes = Vec::new();

        for (item_id, data) in state.all_items_mut() {
            let old_price = data.current_price;
            let old_trend = data.trend;

            // Service: Calculate new price from supply/demand
            let new_price = MarketService::calculate_price(data, config);

            // Service: Apply equilibrium drift
            let (new_demand, new_supply) =
                MarketService::calculate_equilibrium_drift(data, config.price_update_rate);
            data.demand = new_demand;
            data.supply = new_supply;

            // Update price in history
            data.update_price(new_price, config.price_history_length);

            // Service: Detect trend
            let new_trend = MarketService::detect_trend(data, config);
            data.trend = new_trend;

            // Hook: Allow custom price adjustment
            let adjusted_price = self
                .hook
                .adjust_price(item_id, new_price, data)
                .await
                .unwrap_or(new_price);

            data.current_price = adjusted_price;

            // Record price change
            if (old_price - adjusted_price).abs() > 0.01 {
                price_changes.push(PriceChange::new(item_id.clone(), old_price, adjusted_price));
            }

            // Note: Trend changes are tracked but not returned
            // Games can listen to events for trend changes
            if old_trend != new_trend {
                // Could emit MarketTrendChangedEvent here
            }
        }

        // Hook: Notify of price updates
        if !price_changes.is_empty() {
            self.hook.on_prices_updated(&price_changes).await;
        }

        price_changes
    }

    /// Apply a market event
    ///
    /// # Arguments
    ///
    /// * `event` - Market event to apply
    /// * `state` - Mutable market state
    /// * `config` - Market configuration
    pub async fn apply_event(
        &self,
        event: MarketEvent,
        state: &mut MarketState,
        config: &MarketConfig,
    ) {
        let affected_items: Vec<ItemId> = if event.affected_items.is_empty() {
            // Apply to all items
            state.all_items().map(|(id, _)| id.clone()).collect()
        } else {
            event.affected_items.clone()
        };

        for item_id in &affected_items {
            if let Some(data) = state.get_item_mut(item_id) {
                // Hook: Check custom event impact multiplier
                let impact_multiplier = self
                    .hook
                    .get_event_impact_multiplier(&event, item_id)
                    .await
                    .unwrap_or(1.0);

                // Apply multiplier to event magnitude
                let mut modified_event = event.clone();
                modified_event.magnitude *= impact_multiplier;

                // Service: Calculate new demand/supply
                let (new_demand, new_supply) =
                    MarketService::apply_event(data, &modified_event, config);

                data.demand = new_demand;
                data.supply = new_supply;
            }
        }

        // Hook: Notify event application
        self.hook.on_event_applied(&event, &affected_items).await;
    }

    /// Get items with specific trend
    ///
    /// # Arguments
    ///
    /// * `state` - Market state
    /// * `trend` - Trend to filter by
    ///
    /// # Returns
    ///
    /// Vector of item IDs matching the trend
    pub fn get_items_by_trend(state: &MarketState, trend: MarketTrend) -> Vec<ItemId> {
        state
            .all_items()
            .filter(|(_, data)| data.trend == trend)
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Manually set demand/supply for an item
    ///
    /// # Arguments
    ///
    /// * `state` - Mutable market state
    /// * `item_id` - Item to update
    /// * `demand` - New demand value (None = unchanged)
    /// * `supply` - New supply value (None = unchanged)
    pub fn set_demand_supply(
        state: &mut MarketState,
        item_id: &ItemId,
        demand: Option<f32>,
        supply: Option<f32>,
    ) {
        if let Some(data) = state.get_item_mut(item_id) {
            if let Some(d) = demand {
                data.demand = d.clamp(0.0, 1.0);
            }
            if let Some(s) = supply {
                data.supply = s.clamp(0.0, 1.0);
            }
        }
    }

    /// Get market summary statistics
    ///
    /// # Arguments
    ///
    /// * `state` - Market state
    ///
    /// # Returns
    ///
    /// Tuple of (average_price, total_items, volatile_items_count)
    pub fn get_market_summary(state: &MarketState) -> (f32, usize, usize) {
        let total_items = state.item_count();

        let total_price: f32 = state.all_items().map(|(_, data)| data.current_price).sum();

        let average_price = if total_items > 0 {
            total_price / total_items as f32
        } else {
            0.0
        };

        let volatile_count = Self::get_items_by_trend(state, MarketTrend::Volatile).len();

        (average_price, total_items, volatile_count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plugin::market::{DefaultMarketHook, MarketData};

    fn create_test_state() -> MarketState {
        let mut state = MarketState::new();
        state.register_item("water", 10.0);
        state.register_item("ammo", 50.0);
        state.register_item("medicine", 100.0);
        state
    }

    fn create_config() -> MarketConfig {
        MarketConfig::default()
    }

    #[tokio::test]
    async fn test_update_prices() {
        let system = MarketSystem::new(DefaultMarketHook);
        let mut state = create_test_state();
        let config = create_config();

        // Set imbalanced supply/demand
        MarketSystem::<DefaultMarketHook>::set_demand_supply(
            &mut state,
            &"water".to_string(),
            Some(0.9),
            Some(0.3),
        );

        let changes = system.update_prices(&mut state, &config).await;

        // Price should change due to supply/demand imbalance
        assert!(!changes.is_empty());
    }

    #[tokio::test]
    async fn test_update_prices_no_change() {
        let system = MarketSystem::new(DefaultMarketHook);
        let mut state = create_test_state();
        let config = create_config();

        // Equilibrium: no change expected
        let _changes = system.update_prices(&mut state, &config).await;

        // Might have small changes due to drift, or none
        // We just verify it doesn't panic
    }

    #[tokio::test]
    async fn test_apply_event_demand_shock() {
        let system = MarketSystem::new(DefaultMarketHook);
        let mut state = create_test_state();
        let config = create_config();

        let event = MarketEvent::demand_shock(vec!["water".to_string()], 0.5);

        system.apply_event(event, &mut state, &config).await;

        let data = state.get_item(&"water".to_string()).unwrap();
        assert!(data.demand > 0.5); // Demand should increase
    }

    #[tokio::test]
    async fn test_apply_event_all_items() {
        let system = MarketSystem::new(DefaultMarketHook);
        let mut state = create_test_state();
        let config = create_config();

        // Event with empty affected_items = apply to all
        let event = MarketEvent::demand_shock(vec![], 0.3);

        system.apply_event(event, &mut state, &config).await;

        // All items should have increased demand
        for (_, data) in state.all_items() {
            assert!(data.demand > 0.5);
        }
    }

    #[tokio::test]
    async fn test_get_items_by_trend() {
        let mut state = create_test_state();

        // Manually set trends
        state.get_item_mut(&"water".to_string()).unwrap().trend = MarketTrend::Rising;
        state.get_item_mut(&"ammo".to_string()).unwrap().trend = MarketTrend::Rising;
        state.get_item_mut(&"medicine".to_string()).unwrap().trend = MarketTrend::Stable;

        let rising_items =
            MarketSystem::<DefaultMarketHook>::get_items_by_trend(&state, MarketTrend::Rising);

        assert_eq!(rising_items.len(), 2);
    }

    #[tokio::test]
    async fn test_set_demand_supply() {
        let mut state = create_test_state();

        MarketSystem::<DefaultMarketHook>::set_demand_supply(
            &mut state,
            &"water".to_string(),
            Some(0.8),
            Some(0.3),
        );

        let data = state.get_item(&"water".to_string()).unwrap();
        assert_eq!(data.demand, 0.8);
        assert_eq!(data.supply, 0.3);
    }

    #[tokio::test]
    async fn test_set_demand_supply_clamping() {
        let mut state = create_test_state();

        MarketSystem::<DefaultMarketHook>::set_demand_supply(
            &mut state,
            &"water".to_string(),
            Some(1.5),  // Should clamp to 1.0
            Some(-0.5), // Should clamp to 0.0
        );

        let data = state.get_item(&"water".to_string()).unwrap();
        assert_eq!(data.demand, 1.0);
        assert_eq!(data.supply, 0.0);
    }

    #[tokio::test]
    async fn test_set_demand_supply_partial() {
        let mut state = create_test_state();

        // Set only demand
        MarketSystem::<DefaultMarketHook>::set_demand_supply(
            &mut state,
            &"water".to_string(),
            Some(0.7),
            None,
        );

        let data = state.get_item(&"water".to_string()).unwrap();
        assert_eq!(data.demand, 0.7);
        assert_eq!(data.supply, 0.5); // Unchanged
    }

    #[tokio::test]
    async fn test_get_market_summary() {
        let state = create_test_state();

        let (avg_price, total, volatile) =
            MarketSystem::<DefaultMarketHook>::get_market_summary(&state);

        assert_eq!(total, 3);
        // Average of 10, 50, 100 = 53.33...
        assert!((avg_price - 53.33).abs() < 0.1);
        assert_eq!(volatile, 0); // No volatile items initially
    }

    #[tokio::test]
    async fn test_custom_hook() {
        #[derive(Clone, Copy)]
        struct CustomHook;

        #[async_trait::async_trait]
        impl MarketHook for CustomHook {
            async fn adjust_price(
                &self,
                item_id: &ItemId,
                calculated_price: f32,
                _data: &MarketData,
            ) -> Option<f32> {
                // Price floor on water
                if item_id == "water" {
                    Some(calculated_price.max(5.0))
                } else {
                    Some(calculated_price)
                }
            }

            async fn get_event_impact_multiplier(
                &self,
                _event: &MarketEvent,
                _item_id: &ItemId,
            ) -> Option<f32> {
                // Dampen all events by 50%
                Some(0.5)
            }
        }

        let system = MarketSystem::new(CustomHook);
        let mut state = create_test_state();
        let config = create_config();

        // Test price floor
        MarketSystem::<CustomHook>::set_demand_supply(
            &mut state,
            &"water".to_string(),
            Some(0.1),
            Some(0.9),
        );

        let _changes = system.update_prices(&mut state, &config).await;
        let water_price = state.get_price(&"water".to_string()).unwrap();

        assert!(water_price >= 5.0); // Price floor enforced

        // Test dampened event
        let event = MarketEvent::demand_shock(vec!["ammo".to_string()], 0.8);
        system.apply_event(event, &mut state, &config).await;

        // Event impact should be dampened
        // Original: 0.8 * 0.3 (config) = 0.24
        // Dampened: 0.24 * 0.5 = 0.12
        // Expected demand: 0.5 + 0.12 = 0.62 (approximately)
        let ammo_demand = state.get_item(&"ammo".to_string()).unwrap().demand;
        assert!((ammo_demand - 0.62).abs() < 0.05);
    }
}