Skip to content
Snippets Groups Projects
Select Git revision
  • b19094a09c7d9543eca27f0af45e162adb021c6c
  • master default protected
  • 12-improve-ai
  • 14-improve-app-metadata
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.2_24 protected
  • Release_0.7.1_23 protected
  • Release_0.7.0_22 protected
  • Release_0.6.0_21 protected
  • Release_0.5.0_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.2_18 protected
  • Release_0.3.1_17 protected
  • Release_0.3.0_16 protected
  • Release_0.2.1_15 protected
  • Release_0.2.0_14 protected
  • Release_0.1.1_13 protected
  • Release_0.1.0_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
  • Release_0.0.8_8 protected
  • Release_0.0.7_7 protected
24 results

robot_player.dart

Blame
  • random_pick.dart 3.23 KiB
    import 'dart:async';
    import 'package:html/parser.dart';
    import 'package:html/dom.dart';
    import 'package:http/http.dart' as http;
    import 'dart:convert';
    import 'package:flutter/services.dart';
    import 'dart:math' show Random;
    import 'package:diacritic/diacritic.dart';
    
    import '../words/list_french_words.dart';
    
    class RandomPick {
      final String level;
      RandomPick(this.level);
    
      String _url = '';
      String _word = '';
      String _clue = '';
      final random = Random();
    
      init() async {
        switch (level) {
          case 'Thèmes':
            await wordFromLocal();
            break;
          case 'Avancé':
            _url =
                'https://www.palabrasaleatorias.com/mots-aleatoires.php?fs=1&fs2=0&Submit=Nouveau+mot';
            await wordFromWeb(_url);
            break;
          case 'Junior':
            _url =
                'https://www.palabrasaleatorias.com/mots-aleatoires.php?fs=1&fs2=1&Submit=Nouveau+mot';
            await wordFromWeb(_url);
            break;
          case 'Expert':
            await wordFromPackage();
            break;
          default:
            await wordFromLocal();
        }
      }
    
      Future<void> wordFromWeb(String url) async {
        try {
          var response = await http.Client().get(Uri.parse(url));
          if (response.statusCode == 200) {
            var html = parse(utf8.decode(response.bodyBytes));
            Element element = html.getElementsByTagName('div').last;
            String word = _validate(element.text);
            if (word == '') {
              throw Exception();
            }
            _word = word;
          } else {
            throw Exception();
          }
        } catch (e) {
          _word = '';
        }
      }
    
      String _validate(String word) {
        String wordToValidate = word.trim();
        if (wordToValidate.contains(' ')) {
          return '';
        }
        wordToValidate = removeDiacritics(wordToValidate);
        wordToValidate = wordToValidate.toUpperCase();
        wordToValidate = wordToValidate.replaceAll(RegExp('[0-9]'), '');
        wordToValidate = wordToValidate.replaceAll(RegExp('[^A-Z]'), '');
        if (wordToValidate.length < 3 || wordToValidate.length > 12) {
          return '';
        }
        return wordToValidate;
      }
    
      Future _waitList() => Future(() {
            final completer = Completer();
            int indexRandom = random.nextInt(listFrenchWords.length);
            completer.complete(listFrenchWords.sublist(indexRandom, indexRandom + 1).join('\n'));
            return completer.future;
          });
    
      wordFromPackage() async {
        var wordList = await _waitList();
        var word = _validate(wordList);
        _word = word;
      }
    
      Future<void> wordFromLocal() async {
        String jsonString;
        try {
          jsonString = await rootBundle.loadString('assets/files/word-list-fr.json');
          final jsonResponse = await json.decode(jsonString);
          var wordList = jsonResponse[jsonResponse.keys.toList().join()];
          int randomCategoryIndex = random.nextInt(wordList.length);
          _clue = wordList[randomCategoryIndex]['clue'];
          List<String> words = [];
          for (var word in wordList[randomCategoryIndex]['words']) {
            words.add(word);
          }
          String word = words[random.nextInt(words.length)];
          if (word != '') {
            _word = word;
          } else {
            _word = 'UNEXPECTED ERROR';
          }
        } catch (e) {
          _word = 'UNEXPECTED ERROR';
        }
      }
    
      String get word => _word;
    
      String get clue => _clue;
    }