feat: Random.org dice and daily word matrix, interpreter voices, curated lexicon
Wire cleromancy rolls and the once-per-UTC-day 000-999 word shuffle to RANDOM.ORG JSON-RPC, with a local CSPRNG fallback. Add settings for interpreter personas and a cleaner default oracle prompt. Replace proper names and bureaucratic filler in the 1000-word lexicon with sacred symbols.
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../data/words_data.dart';
|
||||
import '../models/history_entry_model.dart';
|
||||
import '../models/word_model.dart';
|
||||
import 'true_random_service.dart';
|
||||
|
||||
class WordsRepository {
|
||||
static final WordsRepository _instance = WordsRepository._internal();
|
||||
@@ -10,43 +11,112 @@ class WordsRepository {
|
||||
WordsRepository._internal();
|
||||
|
||||
static const String _kHistoryKey = 'oracle_999_sessions_history_v2';
|
||||
static const String _kMappingDateKey = 'oracle_daily_mapping_date_v1';
|
||||
static const String _kMappingOrderKey = 'oracle_daily_mapping_order_v1';
|
||||
static const String _kMappingSourceKey = 'oracle_daily_mapping_source_v1';
|
||||
|
||||
List<WordItem> _sessionWords = [];
|
||||
final List<OracleSession> _sessions = [];
|
||||
bool _useRemoteShuffle = true;
|
||||
String? _dailyMappingDate;
|
||||
RandomSource _dailyMappingSource = RandomSource.local;
|
||||
|
||||
List<WordItem> get allWords => List.unmodifiable(_sessionWords);
|
||||
List<OracleSession> get sessions => List.unmodifiable(_sessions);
|
||||
List<OracleSession> get history => sessions; // Alias for compatibility
|
||||
int get totalWords => _sessionWords.length;
|
||||
String? get dailyMappingDate => _dailyMappingDate;
|
||||
RandomSource get dailyMappingSource => _dailyMappingSource;
|
||||
|
||||
Future<void> init() async {
|
||||
// 1. Initialize random session mapping
|
||||
_initializeSessionMapping();
|
||||
|
||||
// 2. Load stored session history
|
||||
Future<void> init({bool remoteDailyShuffle = true}) async {
|
||||
_useRemoteShuffle = remoteDailyShuffle;
|
||||
await _restoreOrFetchDailyMapping(force: false);
|
||||
await _loadHistoryFromPrefs();
|
||||
}
|
||||
|
||||
void _initializeSessionMapping() {
|
||||
final list = List<WordItem>.from(kRussianTop1000Words);
|
||||
final random = Random();
|
||||
Future<RandomSource> reshuffleSessionNumbers() {
|
||||
return _restoreOrFetchDailyMapping(force: true);
|
||||
}
|
||||
|
||||
// Fisher-Yates shuffle
|
||||
for (int i = list.length - 1; i > 0; i--) {
|
||||
int n = random.nextInt(i + 1);
|
||||
final temp = list[i];
|
||||
list[i] = list[n];
|
||||
list[n] = temp;
|
||||
Future<RandomSource> _restoreOrFetchDailyMapping({required bool force}) async {
|
||||
final today = utcDateStamp();
|
||||
if (!force) {
|
||||
final restored = await _loadPersistedMapping(today);
|
||||
if (restored) return _dailyMappingSource;
|
||||
}
|
||||
|
||||
_sessionWords = [];
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
_sessionWords.add(list[i].copyWith(sessionNumber: i));
|
||||
final lastId = kRussianTop1000Words.length - 1;
|
||||
RangeShuffle shuffle;
|
||||
if (_useRemoteShuffle) {
|
||||
shuffle = await TrueRandomService.instance.permutation(
|
||||
min: 0,
|
||||
max: lastId,
|
||||
);
|
||||
} else {
|
||||
shuffle = RangeShuffle(
|
||||
TrueRandomService.instance.localPermutation(0, lastId),
|
||||
RandomSource.local,
|
||||
);
|
||||
}
|
||||
|
||||
_applyIndexOrder(shuffle.order);
|
||||
_dailyMappingDate = today;
|
||||
_dailyMappingSource = shuffle.source;
|
||||
await _persistMapping(today, shuffle.order, shuffle.source);
|
||||
return shuffle.source;
|
||||
}
|
||||
|
||||
void _applyIndexOrder(List<int> order) {
|
||||
_sessionWords = [
|
||||
for (var i = 0; i < order.length; i++)
|
||||
kRussianTop1000Words[order[i]].copyWith(sessionNumber: i),
|
||||
];
|
||||
}
|
||||
|
||||
Future<bool> _loadPersistedMapping(String today) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final date = prefs.getString(_kMappingDateKey);
|
||||
final stored = prefs.getStringList(_kMappingOrderKey);
|
||||
if (date != today || stored == null) return false;
|
||||
final order = stored.map(int.parse).toList();
|
||||
if (!isPermutation(order, 0, kRussianTop1000Words.length - 1)) {
|
||||
return false;
|
||||
}
|
||||
_applyIndexOrder(order);
|
||||
_dailyMappingDate = date;
|
||||
final sourceName = prefs.getString(_kMappingSourceKey);
|
||||
_dailyMappingSource = sourceName == RandomSource.randomOrg.name
|
||||
? RandomSource.randomOrg
|
||||
: RandomSource.local;
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void reshuffleSessionNumbers() {
|
||||
_initializeSessionMapping();
|
||||
Future<void> _persistMapping(
|
||||
String date,
|
||||
List<int> order,
|
||||
RandomSource source,
|
||||
) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_kMappingDateKey, date);
|
||||
await prefs.setStringList(
|
||||
_kMappingOrderKey,
|
||||
order.map((e) => e.toString()).toList(),
|
||||
);
|
||||
await prefs.setString(_kMappingSourceKey, source.name);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static String utcDateStamp([DateTime? now]) {
|
||||
final d = (now ?? DateTime.now()).toUtc();
|
||||
final y = d.year.toString().padLeft(4, '0');
|
||||
final m = d.month.toString().padLeft(2, '0');
|
||||
final day = d.day.toString().padLeft(2, '0');
|
||||
return '$y-$m-$day';
|
||||
}
|
||||
|
||||
WordItem? getWordBySessionNumber(int number) {
|
||||
@@ -55,7 +125,7 @@ class WordsRepository {
|
||||
}
|
||||
|
||||
WordItem getRandomWord() {
|
||||
final random = Random();
|
||||
final random = Random.secure();
|
||||
final idx = random.nextInt(_sessionWords.length);
|
||||
return _sessionWords[idx];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user