fetchGameUrl function

Future<String> fetchGameUrl(
  1. LaunchConfig? gameLaunchConfig,
  2. Config config
)

Implementation

Future<String> fetchGameUrl(
    LaunchConfig? gameLaunchConfig, Config config) async {
  String? configId = gameLaunchConfig!.configId;
  String? playerId = gameLaunchConfig.playerId;
  String? playerToken = gameLaunchConfig.playerToken;
  String? currency = gameLaunchConfig.currency;
  String DEFAULTINDEXPATH = "dist";
  final response = await http.get(
      Uri.parse(
          '${config.server}/api/launch-config/${config.operatorId}/$configId'),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      });
  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    // {\"configuration\":{\"clientId\":\"horsebet\",\"name\":\"Horse Racing\"},\"gameId\":\"race-server-base\",\"success\":true}
    var rb = response.body;
    var gameLauncherResponse = json.decode(rb) as Map<String, dynamic>;
    String clientId = gameLauncherResponse['configuration']['clientId'];
    String gameId = gameLauncherResponse['gameId'];
    String server = gameLauncherResponse['configuration']['server'];
    String urlData =
        'gameid=$gameId&configid=$configId&server=$server&operatorid=${config.operatorId}&playerid=$playerId&playertoken=$playerToken&currency=$currency';

    if (gameLauncherResponse['configuration']['launch'] != null) {
      Map<String, dynamic> launchParams =
          gameLauncherResponse['configuration']['launch'];
      launchParams.forEach((key, value) {
        urlData += '&$key=$value';
      });
    }

    if (gameLaunchConfig.rawUrl != true) {
      Codec<String, String> stringToBase64 = utf8.fuse(base64);
      String encoded = stringToBase64.encode(urlData);
      urlData = 'token=$encoded';
    }
    String? gameLink;
    if (gameLauncherResponse['configuration']["gameLink"] != null) {
      gameLink = gameLauncherResponse['configuration']["gameLink"];
    } else if (gameLauncherResponse["gameLink"] != null) {
      gameLink = gameLauncherResponse["gameLink"];
    }

    return '$gameLink?$urlData';
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to fetch game launch configuration');
  }
}