275 lines
7.9 KiB
Dart
275 lines
7.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:mhunt_launcher/Backend/Structures.dart';
|
|
import 'package:mhunt_launcher/Shared/Helpers.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'DebugHelper.dart';
|
|
|
|
class Backend {
|
|
static const String API_ENDPOINT = Helpers.WEB2_ENDPOINT;
|
|
|
|
static List<GameData> Games = [
|
|
GameData(0, "mhunt", "Metahunt", "High-Stake Battle Royale game with Play to earn abilities", "images/mhunt_thumbnail.png", true, "METAHUNT.exe"),
|
|
GameData(1, "pop3d", "Pop3D", "a game I dont know much about xD", "images/pop3d_thumbnail.png", false, "pop3d.exe"),
|
|
];
|
|
static SharedPreferences? prefs;
|
|
static Directory? docPath;
|
|
|
|
static String Username = "";
|
|
static String displayName = "";
|
|
static String walletAddress = "";
|
|
static String pubKey = "";
|
|
static int SolBalance =0;
|
|
static int TicketsBalance = 0;
|
|
|
|
static Map<String, dynamic> UserJson = {'username': 'test', 'passwd': 'test123'};
|
|
|
|
static Future<bool> Login(String username, String password) async {
|
|
var loginResponse = null;
|
|
init();
|
|
try {
|
|
loginResponse = (await http.post(Uri.parse('${API_ENDPOINT}login.php'), body: <String, String>{"username": username, "password": password}));
|
|
Debug.LogResponse(loginResponse.body.toString(), src: '${API_ENDPOINT}login.php');
|
|
|
|
if(loginResponse.body.toString().contains("no user")){
|
|
return false;
|
|
}
|
|
try {
|
|
Username = username;
|
|
displayName = username;
|
|
|
|
SetUsernamePassword(username, password);
|
|
|
|
return true;
|
|
} catch (e) {}
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
return false;
|
|
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static void init() async {
|
|
prefs = await SharedPreferences.getInstance();
|
|
docPath = await getDownloadsDirectory();
|
|
}
|
|
|
|
static Future<bool> Register(String username, String password, String displayname) async {
|
|
var loginResponse = null;
|
|
init();
|
|
|
|
try {
|
|
loginResponse = (await http.post(Uri.parse('${API_ENDPOINT}register.php'), body: <String, String>{"username": username, "password": password, "display_name": displayname}));
|
|
Debug.LogResponse(loginResponse.body.toString(), src: '${API_ENDPOINT}register.php');
|
|
|
|
try {
|
|
SetUsernamePassword(username, password);
|
|
Username = username;
|
|
Debug.Log("User is " + username);
|
|
displayName = displayname;
|
|
return true;
|
|
} catch (e) {}
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static Future<String> GetPubkey() async{
|
|
String url ='${API_ENDPOINT}get_wallet.php?username=${Username}';
|
|
var response = await http.get(Uri.parse(url));
|
|
Debug.LogResponse(response.body.toString(), src: url);
|
|
pubKey = jsonDecode(response.body.toString())["pub_key"];
|
|
return pubKey;
|
|
}
|
|
|
|
static Future GetWalletBalance() async{
|
|
SolBalance = await GetSolBalance();
|
|
TicketsBalance = await GetTicketsBalance();
|
|
}
|
|
|
|
static Future<int> GetSolBalance() async{
|
|
String url ='${API_ENDPOINT}get_wallet_balance.php?wallet=${pubKey}';
|
|
var response = await http.get(Uri.parse(url));
|
|
Debug.LogResponse(response.body.toString(), src: url);
|
|
return jsonDecode(response.body.toString())["balance"];
|
|
}
|
|
|
|
static Future<int> GetTicketsBalance() async{
|
|
String url ='${API_ENDPOINT}get_tickets_balance.php?wallet=${pubKey}';
|
|
var response = await http.get(Uri.parse(url));
|
|
Debug.LogResponse(response.body.toString(), src: url);
|
|
return jsonDecode(response.body.toString())["balance"];
|
|
}
|
|
|
|
static void SetUsernamePassword(String username, String passwd) {
|
|
UserJson = {'username': username, 'passwd': passwd};
|
|
}
|
|
|
|
static Future<List<LeaderboardEntry>> GetLeaderboard(String gameCode) async {
|
|
String url = '${API_ENDPOINT}/${gameCode}/get_leaderboard.php';
|
|
var leaderboardResponse = await http.get(Uri.parse(url));
|
|
|
|
return LeaderboardEntry.listFromJson(leaderboardResponse.body);
|
|
}
|
|
|
|
static Future<List<Map<String, dynamic>>> GetNewsForGame(int gameId) async {
|
|
String url = '${API_ENDPOINT}${Games[gameId].code}/get_news.php';
|
|
try {
|
|
var res = await http.get(Uri.parse(url));
|
|
List<dynamic> jsonList = jsonDecode(res.body);
|
|
List<Map<String, dynamic>> output = [];
|
|
jsonList.forEach((element) {
|
|
output.add(element as Map<String, dynamic>);
|
|
});
|
|
|
|
return output;
|
|
} catch (e) {
|
|
Debug.LogError(e);
|
|
}
|
|
|
|
return [{}];
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> GetUserStatsForGame(int gameId) async {
|
|
var loginResponse = null;
|
|
init();
|
|
|
|
try {
|
|
String url = '${API_ENDPOINT}${Games[gameId].code}/get_user_stats.php';
|
|
loginResponse = (await http.post(Uri.parse(url), body: <String, String>{"username": Username ?? "test"}));
|
|
Debug.LogResponse(loginResponse.body.toString(), src: url);
|
|
|
|
return jsonDecode(loginResponse.body);
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
static Future<String> CreateRequest() async {
|
|
var loginResponse = null;
|
|
|
|
try {
|
|
String url = '${API_ENDPOINT}create_request.php';
|
|
loginResponse = await http.get(Uri.parse(url));
|
|
Debug.LogResponse(loginResponse.body.toString(), src: url);
|
|
|
|
return loginResponse.body.toString();
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
}
|
|
|
|
return "-1";
|
|
}
|
|
|
|
static Future<String> GetRequestResponse(String id) async {
|
|
var loginResponse = null;
|
|
|
|
try {
|
|
String url = '${API_ENDPOINT}get_request_response.php';
|
|
loginResponse = (await http.post(Uri.parse(url), body: <String, String>{"id": id.toString()}));
|
|
Debug.LogResponse(loginResponse.body.toString(), src: url);
|
|
|
|
return loginResponse.body;
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
}
|
|
|
|
return "-1";
|
|
}
|
|
|
|
static Future<String> GetDisplayName(String id) async {
|
|
var response = null;
|
|
|
|
try {
|
|
String url = '${API_ENDPOINT}get_display_name.php';
|
|
response = (await http.post(Uri.parse(url), body: <String, String>{"id": id.toString()}));
|
|
Debug.LogResponse(response.body.toString(), src: url);
|
|
|
|
return response.body;
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
}
|
|
|
|
return "-1";
|
|
}
|
|
|
|
static Future<bool> GetUsernameValidation(String username) async {
|
|
var loginResponse = null;
|
|
|
|
try {
|
|
String url = '${API_ENDPOINT}validate_username.php';
|
|
loginResponse = (await http.post(Uri.parse(url), body: <String, String>{"username": username}));
|
|
Debug.LogResponse(loginResponse.body.toString(), src: url);
|
|
|
|
if (loginResponse.body == "0") {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
Debug.LogError("Error while login $e");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static Future<String> GetNextGuestUsername() async{
|
|
try{
|
|
String url = '${API_ENDPOINT}get_next_guest_username.php';
|
|
var response = await http.get(Uri.parse(url));
|
|
|
|
return response.body.toString();
|
|
}catch(e){
|
|
|
|
}
|
|
Random random = new Random();
|
|
return 'guest ${random.nextInt(10000000)}';
|
|
}
|
|
|
|
static VaultData vault = VaultData('0x0', 0, 0);
|
|
|
|
static Future<VaultData> RefreshVaultData() async{
|
|
var response = null;
|
|
try{
|
|
String url = '${API_ENDPOINT}get_vault_data.php';
|
|
response = (await http.post(Uri.parse(url), body: <String,String>{"id": Username}));
|
|
Debug.LogResponse(response.body.toString(), src:url);
|
|
|
|
vault = jsonDecode(response.body.toString());
|
|
}catch(e){
|
|
|
|
}
|
|
|
|
return vault;
|
|
}
|
|
}
|
|
|
|
class GameData {
|
|
int id;
|
|
String code;
|
|
String name;
|
|
String description;
|
|
String imagePath;
|
|
bool isAvailable;
|
|
String exeName;
|
|
|
|
GameData(this.id, this.code, this.name, this.description, this.imagePath, this.isAvailable, this.exeName);
|
|
}
|
|
|
|
class VaultData{
|
|
String address;
|
|
int vc;
|
|
int php;
|
|
|
|
VaultData(this.address, this.vc, this.php);
|
|
}
|