addAlgorithm method

Future<int> addAlgorithm(
  1. {required Algorithm a}
)

Implementation

Future<int> addAlgorithm({
  required Algorithm a,
}) async {
  if (a.studentID == -1 && a.sessionID == -1) {
    return -1;
  }

  final Map<String, dynamic> collected = <String, dynamic>{};
  for (final String i in a.collector.data.keys) {
    collected[i.toLowerCase()] = a.collector.data[i]!.isNotEmpty;
  }
  final List<String> commands = List<String>.from(
    CatInterpreter()
        .allCommandsBuffer
        .map((SimpleContainer e) => e.toString()),
  );
  // commands.removeAt(0);
  collected["commands"] = commands.joinToString();
  collected["schema"] = SchemasReader().currentIndex;
  collected["description"] = "";
  collected["algorithmDimension"] =
      CatInterpreter().getResults.partialCatScore;

  final Either<String, Map<String, dynamic>> res = await mappingPostRequest(
    "/algorithms",
    collected,
  ).run();

  final int algorithmID =
      res.getOrElse((String l) => <String, dynamic>{})["algorithm"];

  final bool visible = a.context.read<VisibilityNotifier>().finalState;

  final int state = a.context.read<TypeUpdateNotifier>().lowestState;

  final Either<String, Map<String, dynamic>> res2 = await mappingPostRequest(
    "/results",
    <String, dynamic>{
      "studentID": a.studentID,
      "schemaID": SchemasReader().currentIndex,
      "algorithmID": algorithmID,
      "domain": "virtual",
      "voice": false,
      "schema": false,
      "visualFeedback": visible,
      "gesture": state == 0 && a.complete,
      "blocks": state == 1 && a.complete,
      "text": state == 2 && a.complete,
      "artefactDimension": a.complete ? state + 1 : 0,
      "time": a.context.read<TimeKeeper>().rawTime,
      "timeStamp": DateTime.now().toIso8601String(),
      "complete": a.complete,
      "coloredCorrectly": computeColoredCorretly(),
      "colored": computeColored(),
    },
  ).run();

  final int resID = res2.getOrElse((String l) => <String, dynamic>{})["id"];

  return CatLogger().commitLogs(resID);
}