getWordsFromHash method

Future<List<Word>> getWordsFromHash(
  1. String hash,
  2. bool simulationMode,
  3. List<Word> wordsMap
)

Implementation

Future<List<Word>> getWordsFromHash(
    String hash, bool simulationMode, List<Word> wordsMap) async {
  FlipWordsResponse flipWordsResponse;
  Completer<List<Word>> _completer = new Completer<List<Word>>();

  int nbWords = 0;
  if (simulationMode) {
    try {
      String data = await loadAssets(hash + "_words");
      flipWordsResponse = flipWordsResponseFromJson(data);
    } catch (e) {
      Map<String, dynamic> mapWords = {
        "jsonrpc": "2.0",
        "id": 51,
        "result": {
          "words": [0, 0]
        }
      };
      flipWordsResponse = FlipWordsResponse.fromJson(mapWords);
    }
    nbWords = flipWordsResponse.result.words.length;
  } else {
    try {
      mapParams = {
        'method': FlipWordsRequest.METHOD_NAME,
        'params': [hash],
        'id': 101,
        'key': keyApp
      };

      if (this.nodeType == NORMAL_VPS_NODE) {
        SSHClientStatus sshClientStatus;
        sshClientStatus = await VpsUtil().connectVps(apiUrl, keyApp);
        if (sshClientStatus.sshClientStatus) {
          sshClient = sshClientStatus.sshClient;
        }
        var response = await ssh.HttpClientImpl(
            clientFactory: () =>
                ssh.SSHTunneledBaseClient(sshClientStatus.sshClient)).request(
            apiUrl,
            method: 'POST',
            data: jsonEncode(mapParams),
            headers: requestHeaders);
        if (response.status == 200) {
          flipWordsResponse = flipWordsResponseFromJson(response.text);
          nbWords = flipWordsResponse.result.words.length;
        }
      } else {
        FlipWordsRequest flipWordsRequest =
            FlipWordsRequest.fromJson(mapParams);
        body = json.encode(flipWordsRequest.toJson());
        responseHttp = await http.post(Uri.parse(this.apiUrl),
            body: body, headers: requestHeaders);
        if (responseHttp.statusCode == 200) {
          flipWordsResponse = flipWordsResponseFromJson(responseHttp.body);
          nbWords = flipWordsResponse.result.words.length;
        }
      }
    } catch (e) {}
  }
  List<Word> listWords = new List(nbWords);
  for (int j = 0; j < nbWords; j++) {
    Word word;
    if (flipWordsResponse.result.words[j] == 0) {
      word = new Word(name: "", desc: "");
    } else {
      word = new Word(
          name: wordsMap[flipWordsResponse.result.words[j]].name,
          desc: wordsMap[flipWordsResponse.result.words[j]].desc);
    }
    listWords[j] = word;
  }

  _completer.complete(listWords);
  return _completer.future;
}