Skip to content

Commit

Permalink
File watcher and save submit
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelovila committed Sep 29, 2023
1 parent 81a840e commit a8a33fd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
37 changes: 28 additions & 9 deletions client/lib/logic/game_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:path/path.dart' as p;
import 'package:http/http.dart' as http;
import 'package:watcher/watcher.dart';
import 'package:process_run/shell.dart';
import 'package:http/http.dart' as http;
import 'package:client/logic/requester.dart';

typedef TournamentStatusCallback = void Function(String tournamentStatus);

var shell;

Future gameHandler(
String playerId,
TournamentStatusCallback updateGameStatus,
) async {
Timer.periodic(
Expand All @@ -21,7 +25,10 @@ Future gameHandler(
srlResponse['tournamentStarted'] == true) {
timer.cancel();
updateGameStatus("c");
await startGame();
if (shell != null) {
shell.kill();
}
await startGame(playerId);
} else {
updateGameStatus("t");
}
Expand All @@ -30,7 +37,7 @@ Future gameHandler(
);
}

Future startGame() async {
Future startGame(String playerId) async {
// FOLDER STRUCTURE
//
// [ROOTDIR]
Expand All @@ -49,6 +56,13 @@ Future startGame() async {
final String userDir =
(await Directory(p.join(rootDir, "game_dir/user_dir")).create()).path;
final String saveFilePath = p.join(userDir, "profile1/world1.stsg");

final http.Response response = await getSaveFile(playerId);
if (response.statusCode == 200) {
await File(saveFilePath).create(recursive: true);
File(saveFilePath).writeAsBytesSync(response.bodyBytes);
}

String binName = "";
if (Platform.isLinux) {
binName = "supertux.AppImage";
Expand All @@ -60,13 +74,18 @@ Future startGame() async {
final String binPath = p.join(rootDir, "game_dir/supertux/bin/$binName");

var env = ShellEnvironment()..vars["SUPERTUX2_USER_DIR"] = userDir;
var shell = Shell(environment: env);
shell.run(binPath);
var cshell = Shell(environment: env);
cshell.run(binPath);

Timer.periodic(
const Duration(milliseconds: 500),
(timer) async {},
FileWatcher fileWatcher = FileWatcher(
saveFilePath,
pollingDelay: const Duration(milliseconds: 500),
);
fileWatcher.events.listen((event) {
if (event.type == ChangeType.MODIFY) {
sendSaveFile(playerId, File(saveFilePath).readAsStringSync());
}
});
}

Future startTraining() async {
Expand All @@ -92,6 +111,6 @@ Future startTraining() async {
final String binPath = p.join(rootDir, "game_dir/supertux/bin/$binName");

var env = ShellEnvironment()..vars["SUPERTUX2_USER_DIR"] = userDir;
var shell = Shell(environment: env);
shell = Shell(environment: env);
shell.run(binPath);
}
21 changes: 21 additions & 0 deletions client/lib/logic/requester.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'package:http/http.dart' as http;

const String baseUrl = "http://localhost:3000";
Expand Down Expand Up @@ -25,3 +26,23 @@ Future<http.Response> getSaveFile(String id) async {
return http.Response('{"error": "Could not connect to server"}', 500);
}
}

Future<http.Response> sendSaveFile(String id, String saveFile) async {
try {
var uri = Uri.parse('$baseUrl/player/submit/$id');
var request = http.MultipartRequest('POST', uri);
var fileBytes = utf8.encode(saveFile);
var stream = http.ByteStream.fromBytes(fileBytes);
var multipartFile = http.MultipartFile('file', stream, fileBytes.length,
filename: 'filename.txt');
request.files.add(multipartFile);
var response = await request.send();
if (response.statusCode == 200) {
return http.Response('{"success": "Sent"}', 200);
} else {
return http.Response('{"error": "Could not connect to server"}', 500);
}
} catch (e) {
return http.Response('{"error": "Could not connect to server"}', 500);
}
}
8 changes: 4 additions & 4 deletions client/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
useMaterial3: true,
),
home: const PageHolder(title: 'Torneio de Super Tux - GLUA'),
home: const PageHolder(title: "Torneio de Super Tux - GLUA"),
);
}
}
Expand Down Expand Up @@ -53,10 +53,10 @@ class _PageHolderState extends State<PageHolder> {
}

// t - trainning; c - competition; f - finnished
String tournamentSatus = "t";
String tournamentStatus = "t";
void updateTournamentStatus(String newStatus) {
setState(() {
tournamentSatus = newStatus;
tournamentStatus = newStatus;
});
}

Expand Down Expand Up @@ -140,7 +140,7 @@ class _PageHolderState extends State<PageHolder> {
context,
),
onGamePage(
tournamentSatus,
tournamentStatus,
controller,
context,
),
Expand Down
1 change: 1 addition & 0 deletions client/lib/widgets/nameverification_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Widget nameVerificationPage(
icon: const Icon(Icons.check_outlined),
onPressed: () => {
gameHandler(
playerId,
updateTournamentStatus,
),
controller.nextPage(
Expand Down
8 changes: 8 additions & 0 deletions client/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
watcher:
dependency: "direct main"
description:
name: watcher
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
flutter_pin_code_fields: ^2.2.0
process_run: ^0.13.1
path: ^1.8.0
watcher: ^1.1.0
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit a8a33fd

Please sign in to comment.