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
//! Configuration for holacracy plugin
//!
//! Defines tunable parameters for task assignment, bidding, and self-organization.

use crate::resources::Resource;
use serde::{Deserialize, Serialize};

/// Configuration for holacracy plugin
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HolacracyConfig {
    /// Task assignment mode
    pub assignment_mode: TaskAssignmentMode,

    /// Bidding configuration
    pub bidding: BiddingConfig,

    /// Maximum number of tasks a member can have assigned
    pub max_tasks_per_member: usize,

    /// Maximum number of roles a member can fill
    pub max_roles_per_member: usize,

    /// Task priority boost for critical tasks (multiplier)
    pub critical_priority_boost: f32,

    /// Skill match weight in bid scoring (0.0-1.0)
    pub skill_match_weight: f32,

    /// Workload weight in bid scoring (0.0-1.0)
    pub workload_weight: f32,

    /// Interest weight in bid scoring (0.0-1.0)
    pub interest_weight: f32,

    /// Enable dynamic role switching
    pub enable_role_switching: bool,

    /// Role switching cooldown (in turns)
    pub role_switch_cooldown: u64,

    /// Minimum skill level to bid on operational roles (0.0-1.0)
    pub min_skill_level_for_bid: f32,

    /// Maximum circle nesting depth
    pub max_circle_depth: usize,
}

impl HolacracyConfig {
    /// Create a new config with validation
    pub fn new() -> Result<Self, String> {
        let config = Self::default();
        config.validate()?;
        Ok(config)
    }

    /// Validate configuration parameters
    pub fn validate(&self) -> Result<(), String> {
        // Validate weights
        let total_weight = self.skill_match_weight + self.workload_weight + self.interest_weight;
        if (total_weight - 1.0).abs() > 0.01 {
            return Err(format!(
                "Bid scoring weights must sum to 1.0, got {}",
                total_weight
            ));
        }

        if self.skill_match_weight < 0.0
            || self.skill_match_weight > 1.0
            || self.workload_weight < 0.0
            || self.workload_weight > 1.0
            || self.interest_weight < 0.0
            || self.interest_weight > 1.0
        {
            return Err("All bid weights must be in range 0.0-1.0".to_string());
        }

        // Validate min skill level
        if self.min_skill_level_for_bid < 0.0 || self.min_skill_level_for_bid > 1.0 {
            return Err("min_skill_level_for_bid must be in range 0.0-1.0".to_string());
        }

        // Validate bidding config
        self.bidding.validate()?;

        // Validate limits
        if self.max_tasks_per_member == 0 {
            return Err("max_tasks_per_member must be at least 1".to_string());
        }

        if self.max_roles_per_member == 0 {
            return Err("max_roles_per_member must be at least 1".to_string());
        }

        if self.max_circle_depth == 0 {
            return Err("max_circle_depth must be at least 1".to_string());
        }

        Ok(())
    }

    /// Builder: Set assignment mode
    pub fn with_assignment_mode(mut self, mode: TaskAssignmentMode) -> Self {
        self.assignment_mode = mode;
        self
    }

    /// Builder: Set bidding config
    pub fn with_bidding_config(mut self, config: BiddingConfig) -> Self {
        self.bidding = config;
        self
    }

    /// Builder: Set max tasks per member
    pub fn with_max_tasks(mut self, max: usize) -> Self {
        self.max_tasks_per_member = max;
        self
    }

    /// Builder: Set max roles per member
    pub fn with_max_roles(mut self, max: usize) -> Self {
        self.max_roles_per_member = max;
        self
    }

    /// Builder: Set skill weights
    pub fn with_skill_weights(mut self, skill: f32, workload: f32, interest: f32) -> Self {
        self.skill_match_weight = skill;
        self.workload_weight = workload;
        self.interest_weight = interest;
        self
    }

    /// Builder: Enable/disable role switching
    pub fn with_role_switching(mut self, enabled: bool) -> Self {
        self.enable_role_switching = enabled;
        self
    }

    /// Builder: Set role switching cooldown
    pub fn with_role_switch_cooldown(mut self, cooldown: u64) -> Self {
        self.role_switch_cooldown = cooldown;
        self
    }

    /// Builder: Set minimum skill level for bidding
    pub fn with_min_skill_level(mut self, level: f32) -> Self {
        self.min_skill_level_for_bid = level;
        self
    }

    /// Builder: Set maximum circle depth
    pub fn with_max_circle_depth(mut self, depth: usize) -> Self {
        self.max_circle_depth = depth;
        self
    }
}

impl Default for HolacracyConfig {
    fn default() -> Self {
        Self {
            assignment_mode: TaskAssignmentMode::SemiAutonomous,
            bidding: BiddingConfig::default(),
            max_tasks_per_member: 5,
            max_roles_per_member: 3,
            critical_priority_boost: 2.0,
            skill_match_weight: 0.5,
            workload_weight: 0.3,
            interest_weight: 0.2,
            enable_role_switching: true,
            role_switch_cooldown: 5,
            min_skill_level_for_bid: 0.3,
            max_circle_depth: 5,
        }
    }
}

/// Mode for task assignment
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum TaskAssignmentMode {
    /// Fully autonomous bidding - tasks auto-assigned to best bid
    FullyAutonomous,
    /// Semi-autonomous - best bids presented, requires approval
    #[default]
    SemiAutonomous,
    /// Manual - all assignments require explicit approval
    Manual,
}

/// Configuration for bidding system
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BiddingConfig {
    /// Duration of bidding period (in turns)
    pub bidding_duration: u64,

    /// Minimum number of bids required before auto-assignment
    pub min_bids_required: usize,

    /// Enable bid retractions (members can withdraw bids)
    pub allow_bid_retraction: bool,

    /// Penalty for bid retraction (reduces future bid scores)
    pub retraction_penalty: f32,

    /// Enable overbidding (members can bid on more than max_tasks)
    pub allow_overbidding: bool,

    /// Overbid penalty multiplier (reduces bid score if overbidding)
    pub overbid_penalty_multiplier: f32,
}

impl BiddingConfig {
    pub fn validate(&self) -> Result<(), String> {
        if self.bidding_duration == 0 {
            return Err("bidding_duration must be at least 1".to_string());
        }

        if self.retraction_penalty < 0.0 || self.retraction_penalty > 1.0 {
            return Err("retraction_penalty must be in range 0.0-1.0".to_string());
        }

        if self.overbid_penalty_multiplier < 0.0 {
            return Err("overbid_penalty_multiplier must be non-negative".to_string());
        }

        Ok(())
    }

    /// Builder: Set bidding duration
    pub fn with_duration(mut self, duration: u64) -> Self {
        self.bidding_duration = duration;
        self
    }

    /// Builder: Set minimum bids required
    pub fn with_min_bids(mut self, min: usize) -> Self {
        self.min_bids_required = min;
        self
    }

    /// Builder: Enable/disable bid retraction
    pub fn with_retraction(mut self, allowed: bool, penalty: f32) -> Self {
        self.allow_bid_retraction = allowed;
        self.retraction_penalty = penalty;
        self
    }

    /// Builder: Enable/disable overbidding
    pub fn with_overbidding(mut self, allowed: bool, penalty: f32) -> Self {
        self.allow_overbidding = allowed;
        self.overbid_penalty_multiplier = penalty;
        self
    }
}

impl Default for BiddingConfig {
    fn default() -> Self {
        Self {
            bidding_duration: 3,
            min_bids_required: 1,
            allow_bid_retraction: true,
            retraction_penalty: 0.1,
            allow_overbidding: false,
            overbid_penalty_multiplier: 0.5,
        }
    }
}

// Implement Resource trait for HolacracyConfig
impl Resource for HolacracyConfig {}

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

    #[test]
    fn test_config_default() {
        let config = HolacracyConfig::default();
        assert_eq!(config.assignment_mode, TaskAssignmentMode::SemiAutonomous);
        assert_eq!(config.max_tasks_per_member, 5);
        assert_eq!(config.max_roles_per_member, 3);
    }

    #[test]
    fn test_config_validation_success() {
        let config = HolacracyConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validation_weights_sum() {
        let config = HolacracyConfig {
            skill_match_weight: 0.5,
            workload_weight: 0.3,
            interest_weight: 0.1, // Sum = 0.9, should fail
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_weight_range() {
        let config = HolacracyConfig {
            skill_match_weight: 1.5, // Out of range
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_min_skill_level() {
        let config = HolacracyConfig {
            min_skill_level_for_bid: 1.5, // Out of range
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_builder() {
        let config = HolacracyConfig::default()
            .with_assignment_mode(TaskAssignmentMode::FullyAutonomous)
            .with_max_tasks(10)
            .with_max_roles(5)
            .with_role_switching(false);

        assert_eq!(config.assignment_mode, TaskAssignmentMode::FullyAutonomous);
        assert_eq!(config.max_tasks_per_member, 10);
        assert_eq!(config.max_roles_per_member, 5);
        assert!(!config.enable_role_switching);
    }

    #[test]
    fn test_config_builder_with_skill_weights() {
        let config = HolacracyConfig::default().with_skill_weights(0.6, 0.3, 0.1);

        assert_eq!(config.skill_match_weight, 0.6);
        assert_eq!(config.workload_weight, 0.3);
        assert_eq!(config.interest_weight, 0.1);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_bidding_config_default() {
        let config = BiddingConfig::default();
        assert_eq!(config.bidding_duration, 3);
        assert_eq!(config.min_bids_required, 1);
        assert!(config.allow_bid_retraction);
    }

    #[test]
    fn test_bidding_config_validation_success() {
        let config = BiddingConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_bidding_config_validation_duration() {
        let config = BiddingConfig {
            bidding_duration: 0,
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_bidding_config_validation_penalty_range() {
        let config = BiddingConfig {
            retraction_penalty: 1.5,
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_bidding_config_builder() {
        let config = BiddingConfig::default()
            .with_duration(5)
            .with_min_bids(2)
            .with_retraction(false, 0.0)
            .with_overbidding(true, 0.3);

        assert_eq!(config.bidding_duration, 5);
        assert_eq!(config.min_bids_required, 2);
        assert!(!config.allow_bid_retraction);
        assert!(config.allow_overbidding);
        assert_eq!(config.overbid_penalty_multiplier, 0.3);
    }

    #[test]
    fn test_assignment_mode_default() {
        assert_eq!(
            TaskAssignmentMode::default(),
            TaskAssignmentMode::SemiAutonomous
        );
    }

    #[test]
    fn test_config_new() {
        let result = HolacracyConfig::new();
        assert!(result.is_ok());
    }

    #[test]
    fn test_config_validation_max_tasks() {
        let config = HolacracyConfig {
            max_tasks_per_member: 0,
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_max_roles() {
        let config = HolacracyConfig {
            max_roles_per_member: 0,
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_max_circle_depth() {
        let config = HolacracyConfig {
            max_circle_depth: 0,
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }
}