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
//! Core types for economy plugin

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};

/// Unique identifier for a currency
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CurrencyId(pub String);

impl CurrencyId {
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }
}

impl fmt::Display for CurrencyId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for CurrencyId {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

/// Metadata definition for a currency
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrencyDefinition {
    pub id: CurrencyId,
    pub name: String,
    pub symbol: String,
    pub decimals: u8,
}

/// Currency value with safe arithmetic operations
///
/// This type wraps an `i64` and provides saturating arithmetic to prevent overflow/underflow.
/// Negative values represent debt.
///
/// # Example
///
/// ```ignore
/// use issun::plugin::economy::Currency;
///
/// let balance = Currency::new(1000);
/// let cost = Currency::new(300);
/// let remaining = balance - cost;
/// assert_eq!(remaining.amount(), 700);
/// ```
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Currency(i64);

impl Currency {
    /// Zero currency constant
    pub const ZERO: Currency = Currency(0);

    /// Create a new currency value
    pub fn new(amount: i64) -> Self {
        Self(amount)
    }

    /// Get the raw amount
    pub fn amount(&self) -> i64 {
        self.0
    }

    /// Add with saturation (won't overflow)
    pub fn saturating_add(self, other: Currency) -> Currency {
        Currency(self.0.saturating_add(other.0))
    }

    /// Subtract with saturation (won't underflow)
    pub fn saturating_sub(self, other: Currency) -> Currency {
        Currency(self.0.saturating_sub(other.0))
    }

    /// Check if currency is positive
    pub fn is_positive(&self) -> bool {
        self.0 > 0
    }

    /// Check if currency is zero
    pub fn is_zero(&self) -> bool {
        self.0 == 0
    }

    /// Check if currency is negative (debt)
    pub fn is_negative(&self) -> bool {
        self.0 < 0
    }

    /// Get absolute value
    pub fn abs(&self) -> Currency {
        Currency(self.0.abs())
    }
}

impl fmt::Display for Currency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Add for Currency {
    type Output = Currency;

    fn add(self, other: Currency) -> Currency {
        Currency(self.0 + other.0)
    }
}

impl AddAssign for Currency {
    fn add_assign(&mut self, other: Currency) {
        self.0 += other.0;
    }
}

impl Sub for Currency {
    type Output = Currency;

    fn sub(self, other: Currency) -> Currency {
        Currency(self.0 - other.0)
    }
}

impl SubAssign for Currency {
    fn sub_assign(&mut self, other: Currency) {
        self.0 -= other.0;
    }
}

impl From<i64> for Currency {
    fn from(amount: i64) -> Self {
        Currency::new(amount)
    }
}

impl From<Currency> for i64 {
    fn from(currency: Currency) -> i64 {
        currency.0
    }
}

// ============================================================================
// Root Resource System Types
// ============================================================================

/// Unique identifier for a resource
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ResourceId(pub String);

impl ResourceId {
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }
}

impl fmt::Display for ResourceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for ResourceId {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

/// Type of resource behavior
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResourceType {
    /// Stock resource: accumulated and stored (Gold, Food, Iron)
    Stock,
    /// Flow resource: represents per-turn generation capacity (GDP, Production)
    Flow { per_turn: bool },
    /// Abstract resource: represents abstract values (National Power, Influence)
    Abstract,
    /// Hybrid: combines multiple aspects
    Hybrid,
}

/// Metadata definition for a resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceDefinition {
    pub id: ResourceId,
    pub name: String,
    pub description: String,
    pub resource_type: ResourceType,
    pub is_infinite: bool,
    pub metadata: HashMap<String, String>,
}

impl ResourceDefinition {
    pub fn new(
        id: impl Into<String>,
        name: impl Into<String>,
        description: impl Into<String>,
        resource_type: ResourceType,
    ) -> Self {
        Self {
            id: ResourceId::new(id),
            name: name.into(),
            description: description.into(),
            resource_type,
            is_infinite: true,
            metadata: HashMap::new(),
        }
    }

    pub fn with_finite(mut self) -> Self {
        self.is_infinite = false;
        self
    }

    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

// ============================================================================
// Currency Exchange System Types
// ============================================================================

/// Type of exchange rate
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RateType {
    /// Static rate that doesn't change
    Static,
    /// Dynamic rate that can be updated
    Dynamic,
}

/// Exchange rate between two currencies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExchangeRate {
    pub from: CurrencyId,
    pub to: CurrencyId,
    /// How many units of 'to' currency you get for 1 unit of 'from' currency
    pub rate: f64,
    pub rate_type: RateType,
}

impl ExchangeRate {
    pub fn new(from: CurrencyId, to: CurrencyId, rate: f64) -> Self {
        Self {
            from,
            to,
            rate,
            rate_type: RateType::Static,
        }
    }

    pub fn dynamic(from: CurrencyId, to: CurrencyId, rate: f64) -> Self {
        Self {
            from,
            to,
            rate,
            rate_type: RateType::Dynamic,
        }
    }

    /// Calculate how much 'to' currency you get from 'from_amount'
    pub fn convert(&self, from_amount: Currency) -> Currency {
        let result = (from_amount.amount() as f64 * self.rate).round() as i64;
        Currency::new(result)
    }
}

// ============================================================================
// Resource to Currency Conversion Types
// ============================================================================

/// Conversion rule from resource to currency
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversionRule {
    pub resource: ResourceId,
    pub currency: CurrencyId,
    /// How many units of currency you get for 1 unit of resource
    pub conversion_rate: f64,
}

impl ConversionRule {
    pub fn new(resource: ResourceId, currency: CurrencyId, conversion_rate: f64) -> Self {
        Self {
            resource,
            currency,
            conversion_rate,
        }
    }

    /// Calculate how much currency you get from resource_amount
    pub fn convert(&self, resource_amount: i64) -> Currency {
        let result = (resource_amount as f64 * self.conversion_rate).round() as i64;
        Currency::new(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_currency_creation() {
        let c = Currency::new(100);
        assert_eq!(c.amount(), 100);
    }

    #[test]
    fn test_currency_add() {
        let a = Currency::new(100);
        let b = Currency::new(50);
        assert_eq!((a + b).amount(), 150);
    }

    #[test]
    fn test_currency_sub() {
        let a = Currency::new(100);
        let b = Currency::new(30);
        assert_eq!((a - b).amount(), 70);
    }

    #[test]
    fn test_currency_saturating_add() {
        let a = Currency::new(i64::MAX - 10);
        let b = Currency::new(100);
        assert_eq!(a.saturating_add(b), Currency::new(i64::MAX));
    }

    #[test]
    fn test_currency_saturating_sub() {
        let a = Currency::new(i64::MIN + 10);
        let b = Currency::new(100);
        assert_eq!(a.saturating_sub(b), Currency::new(i64::MIN));
    }

    #[test]
    fn test_currency_predicates() {
        assert!(Currency::new(100).is_positive());
        assert!(!Currency::new(0).is_positive());
        assert!(!Currency::new(-100).is_positive());

        assert!(Currency::new(0).is_zero());
        assert!(!Currency::new(100).is_zero());

        assert!(Currency::new(-100).is_negative());
        assert!(!Currency::new(0).is_negative());
    }

    #[test]
    fn test_currency_abs() {
        assert_eq!(Currency::new(-100).abs(), Currency::new(100));
        assert_eq!(Currency::new(100).abs(), Currency::new(100));
    }

    #[test]
    fn test_currency_display() {
        let c = Currency::new(12345);
        assert_eq!(format!("{}", c), "12345");
    }

    #[test]
    fn test_currency_from_i64() {
        let c: Currency = 500.into();
        assert_eq!(c.amount(), 500);
    }

    #[test]
    fn test_resource_definition_builder() {
        let resource =
            ResourceDefinition::new("gold", "Gold", "Precious metal", ResourceType::Stock)
                .with_finite()
                .with_metadata("rarity", "legendary");

        assert_eq!(resource.id.0, "gold");
        assert_eq!(resource.name, "Gold");
        assert!(!resource.is_infinite);
        assert_eq!(resource.metadata.get("rarity").unwrap(), "legendary");
    }

    #[test]
    fn test_exchange_rate_convert() {
        let rate = ExchangeRate::new(CurrencyId::new("usd"), CurrencyId::new("jpy"), 150.0);

        let usd = Currency::new(100);
        let jpy = rate.convert(usd);
        assert_eq!(jpy.amount(), 15000);
    }

    #[test]
    fn test_conversion_rule_convert() {
        let rule = ConversionRule::new(
            ResourceId::new("gold_ore"),
            CurrencyId::new("gold_coin"),
            10.0,
        );

        let currency = rule.convert(5);
        assert_eq!(currency.amount(), 50);
    }

    #[test]
    fn test_exchange_rate_fractional() {
        let rate = ExchangeRate::new(CurrencyId::new("eur"), CurrencyId::new("usd"), 1.1);

        let eur = Currency::new(100);
        let usd = rate.convert(eur);
        assert_eq!(usd.amount(), 110);
    }
}