words method

CaptchaWord words({
  1. List<String>? words,
  2. int captchaLength = 5,
  3. Duration timeOut = const Duration(minutes: 1),
})

Implementation

CaptchaWord words({
  List<String>? words,
  int captchaLength = 5,
  Duration timeOut = const Duration(minutes: 1),
}) {
  words ??= default_words;
  if (captchaLength > words.length) {
    throw Exception("captchaLength melebihi words length");
  }
  String word = randomByDatas(words);
  List<String> results = [word];
  DateTime dateTime = DateTime.now().add(timeOut);

  int total_answer = captchaLength ~/ 4;

  if (total_answer < 5) {
    throw Exception("Captcha word total harus lebih 5");
  }

  while (true) {
    if (dateTime.isExpired()) {
      throw Exception("Captcha word time expired");
    }

    if (results.length == captchaLength) {
      List<int> answers = [];

      while (true) {
        if (dateTime.isExpired()) {
          throw Exception("Captcha word time expired");
        }
        int random_data = Random().nextInt(captchaLength);
        if (answers.length == total_answer) {
          break;
        }
        if (answers.contains(random_data)) {
        } else {
          answers.add(random_data);
        }
      }
      return CaptchaWord.create(
        special_type: "captchaWord",
        answer: answers,
        words: results,
      );
    }
    if (results.contains(word)) {
      word = randomByDatas(words);
    } else {
      results.add(word);
    }
  }
}