Fortune Module Development Guide
Fortune Module Development Guide
Overview
The Fortune module (src/bbs/fortune.rs
) implements the <prefix>FORTUNE
public channel command (default ^FORTUNE
), providing users with random wisdom quotes, programming humor, and inspirational messages.
Architecture
Core Components
- Fortune Database: Static array of user‑provided fortune entries (~400)
- Random Selection: Thread-safe random fortune picker using
rand::thread_rng()
- Helper Functions: Utility functions for testing and diagnostics
Integration Points
- Public Command Parser (
src/bbs/public.rs
): Recognizes<prefix>FORTUNE
commands (default^FORTUNE
) - Rate Limiting: 5-second per-node cooldown via
PublicState::allow_fortune()
- Broadcast System: Messages sent via public channel broadcast only
Fortune Database
Content Categories
- Classic Wisdom (~15 entries): Socrates, Aristotle, Einstein, etc.
- Programming & Tech (~25 entries): Industry wisdom and developer humor
- Literature & Philosophy (~20 entries): Famous quotes and philosophical insights
- Motivational (~30 entries): Inspirational and success-oriented messages
- Clean Humor (~20 entries): Family-friendly jokes and wordplay
- Science & Discovery (~15 entries): Scientific and research-oriented quotes
- Unix/Computing Culture (~15 entries): Technical and Unix-philosophy quotes
Quality Standards
- Maximum 200 characters per entry (mesh network optimized)
- No control characters (except ASCII whitespace)
- Public domain or widely-attributed content only
- Family-friendly and inclusive language
API Reference
Public Functions
/// Returns a random fortune from the database
pub fn get_fortune() -> &'static str
/// Returns the total number of fortunes
pub fn fortune_count() -> usize
/// Returns the length of the longest fortune
pub fn max_fortune_length() -> usize
Usage Example
use meshbbs::bbs::fortune::get_fortune;
let wisdom = get_fortune();
println!("Today's fortune: {}", wisdom);
Testing Strategy
Unit Tests (11 total)
- Database Validation:
fortunes_count_140
: Verifies exact countall_fortunes_under_200_chars
: Length validationall_fortunes_non_empty
: No empty stringsall_fortunes_contain_printable_chars
: Character validation
- Functionality Tests:
fortune_returns_valid_response
: Basic function testfortune_randomness_check
: Distribution verificationfortune_thread_safety_simulation
: Concurrent access test
- Quality Assurance:
fortune_database_quality_checks
: Content diversity verificationfortune_count_matches_array
: Helper function consistencymax_fortune_length_validation
: Length function validationhelper_functions_consistency
: Cross-validation of utilities
Integration Tests
- fortune_behavior.rs: Server integration and rate limiting tests
- public_fortune.rs: Command parsing and basic functionality
Performance Characteristics
- Memory Usage: ~28KB static data (140 × ~200 bytes average)
- CPU Usage: O(1) random selection, minimal overhead
- Thread Safety: Full concurrent access support
- Rate Limiting: 5-second cooldown per node prevents spam
Maintenance
Adding New Fortunes
- Add entries to
FORTUNES
array insrc/bbs/fortune.rs
- Update array size declaration:
const FORTUNES: [&str; NEW_COUNT]
- Update test expectation:
fortunes_count_X()
test function name and assertion - Update documentation: module docs, changelog, user guide
- Run full test suite:
cargo test bbs::fortune
Content Guidelines
- Keep under 200 characters for mesh compatibility
- Attribute quotes when known (
— Author Name
) - Use Unicode em dash (—) for attribution separator
- Avoid controversial, political, or offensive content
- Test for printable characters and proper encoding
- Verify public domain status or fair use
Performance Monitoring
The module includes built-in diagnostics:
use meshbbs::bbs::fortune::{fortune_count, max_fortune_length};
println!("Fortune database: {} entries", fortune_count());
println!("Maximum fortune length: {} chars", max_fortune_length());
Future Enhancements
Potential Features
- Categories: Themed fortune subsets (
<prefix>FORTUNE PROGRAMMING
,<prefix>FORTUNE WISDOM
) - External Database: Load fortunes from external files
- User Favorites: Allow users to save favorite fortunes
- Fortune of the Day: Daily fortune rotation
- Localization: Multi-language fortune support
Implementation Considerations
- Maintain backward compatibility
- Preserve mesh network optimization (200 char limit)
- Consider storage implications for external databases
- Rate limiting adjustments for new features
- Documentation and test coverage for additions
Troubleshooting
Common Issues
- Compilation Errors: Check array size matches content count
- Test Failures: Verify character encoding and content guidelines
- Rate Limiting: Ensure
PublicState
integration is correct - Memory Usage: Monitor static array size for embedded systems
Debug Commands
# Run fortune-specific tests
cargo test bbs::fortune
# Run integration tests
cargo test fortune_behavior
# Check documentation examples
cargo test --doc
# Performance profiling
cargo bench --features fortune
Related Documentation
- Games Documentation - User-facing fortune documentation
- Commands Reference - Command syntax
- Public Commands - Parser and rate limiting
- Server Integration - Broadcast handling