From 0cf1f6b09019b829736d05bbdd2af3a8707f6e6c Mon Sep 17 00:00:00 2001 From: zontreck Date: Sat, 4 Nov 2023 15:15:33 -0700 Subject: [PATCH] Adds an autorestart page --- lib/autorestart.dart | 164 +++++++++++++++++++++++++++++++++++++++++ lib/game.dart | 28 ++++++- lib/main.dart | 2 + lib/settingsEntry.dart | 6 +- 4 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 lib/autorestart.dart diff --git a/lib/autorestart.dart b/lib/autorestart.dart new file mode 100644 index 0000000..8de4487 --- /dev/null +++ b/lib/autorestart.dart @@ -0,0 +1,164 @@ +import 'dart:math'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:hive/hive.dart'; +import 'package:servermanager/settings.dart'; + +part 'autorestart.g.dart'; + +@HiveType(typeId: 3) +class AutomaticRestartInfo { + @HiveField(0, defaultValue: 0) + final int hours; + + @HiveField(1, defaultValue: 0) + final int minutes; + + @HiveField(2, defaultValue: 0) + final int seconds; + + @HiveField(3, defaultValue: false) + final bool enabled; + + const AutomaticRestartInfo( + {this.hours = 0, + this.minutes = 0, + this.seconds = 0, + this.enabled = false}); +} + +class AutoRestartPage extends StatefulWidget { + AutoRestartPage({super.key}); + + @override + AutoRestartState createState() => AutoRestartState(); +} + +class AutoRestartState extends State { + Settings settings = Settings(); + bool firstDisplay = true; + + bool enabled = false; + int seconds = 0; + int minutes = 0; + int hours = 0; + + @override + Widget build(BuildContext context) { + if (firstDisplay) { + var args = + ModalRoute.of(context)!.settings.arguments as AutomaticRestartInfo; + enabled = args.enabled; + seconds = args.seconds; + minutes = args.minutes; + hours = args.hours; + + firstDisplay = false; + } + return Scaffold( + appBar: AppBar( + title: Text("Automatic Restart"), + backgroundColor: Color.fromARGB(255, 100, 0, 0), + ), + body: WillPopScope( + onWillPop: () async { + Navigator.pop( + context, + AutomaticRestartInfo( + enabled: enabled, + hours: hours, + minutes: minutes, + seconds: seconds)); + + return true; + }, + child: SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column(children: [ + SwitchListTile( + value: enabled, + onChanged: (val) { + setState(() { + enabled = !enabled; + }); + }, + title: Text("Enabled"), + ), + Row( + children: [ + SizedBox( + width: 256, + child: ListTile( + title: Text("Hours"), + subtitle: Text("${hours}"), + ), + ), + Expanded( + child: Slider( + max: 24, + min: 0, + value: hours.toDouble(), + onChanged: (value) { + setState(() { + hours = value.toInt(); + }); + }, + ), + ) + ], + ), + Row( + children: [ + SizedBox( + width: 256, + child: ListTile( + title: Text("Minutes"), + subtitle: Text("${minutes}"), + ), + ), + Expanded( + child: Slider( + max: 60, + min: 0, + value: minutes.toDouble(), + onChanged: (value) { + setState(() { + minutes = value.toInt(); + }); + }, + ), + ) + ], + ), + Row( + children: [ + SizedBox( + width: 256, + child: ListTile( + title: Text("Seconds"), + subtitle: Text("${seconds}"), + ), + ), + Expanded( + child: Slider( + max: 60, + min: 0, + value: seconds.toDouble(), + onChanged: (value) { + setState(() { + seconds = value.toInt(); + }); + }, + ), + ) + ], + ) + ]), + )), + ), + ); + } +} diff --git a/lib/game.dart b/lib/game.dart index c4a7b26..06e156b 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:crypto/crypto.dart'; import 'package:flutter/material.dart'; +import 'package:servermanager/autorestart.dart'; import 'package:servermanager/mod.dart'; import 'package:servermanager/pathtools.dart'; import 'package:servermanager/settings.dart'; @@ -178,7 +179,13 @@ class GameServerPageState extends State { print("Hash: ${hash}"); // Update the mod instance - settings.inst!.mods[index] = M; + setState(() { + settings.inst!.mods[index] = Mod( + mod_hash: hash, + mod_id: M.mod_id, + mod_pak: name, + mod_name: M.mod_name); + }); } } } @@ -187,8 +194,21 @@ class GameServerPageState extends State { settings.Write(); }); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text("Mods have been scanned"))); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text("Mods have been scanned and updated."))); + }, + ), + ListTile( + title: Text("Configure AutoRestart"), + leading: Icon(Icons.timer), + onTap: () async { + var reply = await Navigator.pushNamed( + context, "/server/autorestart", + arguments: settings.inst!.timer); + + setState(() { + settings.inst!.timer = reply as AutomaticRestartInfo; + }); }, ), ListTile( @@ -262,6 +282,8 @@ class ModManagerState extends State { arguments: Mod( mod_id: mod.mod_id, mod_name: mod.mod_name, + mod_pak: mod.mod_pak, + mod_hash: mod.mod_hash, newMod: false)); if (reply != null) { diff --git a/lib/main.dart b/lib/main.dart index 68cf2c7..1d44175 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:hive_flutter/adapters.dart'; +import 'package:servermanager/autorestart.dart'; import 'package:servermanager/credentials.dart'; import 'package:servermanager/game.dart'; import 'package:servermanager/home.dart'; @@ -36,6 +37,7 @@ class MyApp extends StatelessWidget { settings: appSettings, ), "/server": (context) => GameServerPage(settings: appSettings), + "/server/autorestart": (context) => AutoRestartPage(), "/server/mods": (context) => ModManager(settings: appSettings), "/server/mods/edit": (context) => ModPage(), "/steamcmd/creds": (context) => CredentialsPrompt() diff --git a/lib/settingsEntry.dart b/lib/settingsEntry.dart index 26780e1..c4b7515 100644 --- a/lib/settingsEntry.dart +++ b/lib/settingsEntry.dart @@ -1,4 +1,5 @@ import 'package:hive/hive.dart'; +import 'package:servermanager/autorestart.dart'; import 'package:servermanager/credentials.dart'; import 'package:servermanager/mod.dart'; @@ -6,9 +7,12 @@ part 'settingsEntry.g.dart'; @HiveType(typeId: 0) class SettingsEntry { - @HiveField(0) + @HiveField(0, defaultValue: []) List mods = []; @HiveField(3) Credentials? steam_creds; + + @HiveField(4, defaultValue: AutomaticRestartInfo()) + AutomaticRestartInfo timer = AutomaticRestartInfo(); }