WheelSectorAngles.fromConfig constructor

WheelSectorAngles.fromConfig(
  1. WheelConfig config
)

创建角度表;相同权重使用等分公式,非有限或非正权重抛 ArgumentError。

Implementation

factory WheelSectorAngles.fromConfig(WheelConfig config) {
  final weights = config.prizes.map((p) => p.sectorWeight).toList();
  if (weights.any((w) => !w.isFinite || w <= 0)) {
    throw ArgumentError.value(weights, _sectorWeightField, '必须为有限正数');
  }
  final count = weights.length;
  final uniform = count == 0 || weights.every((w) => w == weights.first);
  final prizeDeg = count == 0 ? 0.0 : kLuckyFullTurnDeg / count;
  final starts = <double>[];
  final sweeps = <double>[];
  if (!uniform) {
    final sum = weights.fold(0.0, (a, b) => a + b);
    if (!sum.isFinite) {
      throw ArgumentError.value(sum, _sectorWeightField, '总和必须有限');
    }
    var start = 0.0;
    for (final weight in weights) {
      final sweep = (weight / sum) * kLuckyFullTurnDeg;
      starts.add(start);
      sweeps.add(sweep);
      start += sweep;
    }
  }
  return WheelSectorAngles._(count, uniform, prizeDeg,
      List.unmodifiable(starts), List.unmodifiable(sweeps));
}