Skip to content

Commit

Permalink
optimized structure and add terminal manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ezTxmMC committed Jan 22, 2025
1 parent 13055d3 commit 4fbdf01
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target/
_test/
.idea/
*.lock
data/
1 change: 0 additions & 1 deletion data/launch.json

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn downloadBungeecord() {
fn download_bungeecord() {
let name = "bungeecord.jar";
let url = "https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar";
let _ = downloader::download(name, url).await;
}

fn downloadVelocity() {
fn download_velocity() {
let name = "velocity.jar";
let url = "https://api.papermc.io/v2/projects/velocity/versions/3.3.0-SNAPSHOT/builds/390/downloads/velocity-3.3.0-SNAPSHOT-390.jar";
let _ = downloader::download(name, url).await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
let _ = downloader::download(name, url).await;
}*/

fn downloadPaper(supportedVersions: SupportedVersions) {
fn download_paper(supported_versions: SupportedVersions) {
let name = "paper.jar";
let url = "https://api.papermc.io/v2/projects/paper/versions/1.20.6/builds/78/downloads/paper-1.20.6-78.jar";
let _ = downloader::download(name, url).await;
}

fn downloadPurpur(supportedVersions: SupportedVersions) {
fn download_purpur(supported_versions: SupportedVersions) {
let name = "purpur.jar";
let url = "https://api.purpurmc.org/v2/purpur/1.20.6/2207/download";
let _ = downloader::download(name, url).await;
}

fn downloadFolia(supportedVersions: SupportedVersions) {
fn download_folia(supported_versions: SupportedVersions) {
let name = "folia.jar";
let url = "https://api.papermc.io/v2/projects/folia/versions/1.20.6/builds/5/downloads/folia-1.20.6-5.jar";
let _ = downloader::download(name, url).await;
Expand Down
14 changes: 0 additions & 14 deletions src/group.rs

This file was deleted.

19 changes: 19 additions & 0 deletions src/groups/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::grouptypes::ProxySoftwareType;
use super::grouptypes::ServerSoftwareType;
use super::grouptypes::SupportedVersions;
use super::grouptypes::StoreType;

fn create_proxy_group(name: &str, proxy_software_type: ProxySoftwareType,
min_memory: i64, max_memory: i64, max_players: i32, store_type: StoreType) {

}

fn create_lobby_group(name: &str, server_software_type: ServerSoftwareType, supported_versions: SupportedVersions,
min_memory: i64, max_memory: i64, max_players: i32, store_type: StoreType) {

}

fn create_server_group(name: &str, server_software_type: ServerSoftwareType, supported_versions: SupportedVersions,
min_memory: i64, max_memory: i64, max_players: i32, store_type: StoreType) {

}
10 changes: 5 additions & 5 deletions src/grouptypes.rs → src/groups/grouptypes.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
enum ServiceType {
pub(crate) enum ServiceType {
PROXY,
LOBBY,
SERVER
}

enum ProxySoftwareType {
pub(crate) enum ProxySoftwareType {
BUNGEECORD,
VELOCITY
}

enum ServerSoftwareType {
pub(crate) enum ServerSoftwareType {
SPIGOT,
PAPER,
PURPUR,
FOLIA,
MINESTOM
}

enum SupportedVersions {
pub(crate) enum SupportedVersions {
V1_19_4,
V1_20_1,
V1_20_4,
V1_20_6,
V1_21_1
}

enum StoreType {
pub(crate) enum StoreType {
PERMANENTLY,
TEMPORARY
}
97 changes: 18 additions & 79 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,23 @@
mod cloud_terminal;
mod terminal {
pub(crate) mod cloud_terminal;
pub(crate) mod terminal_manager;
}
mod database;
mod json_config;

use cloud_terminal::CloudTerminal;
use json_config::JsonConfig;

fn main() {
let mut config = JsonConfig::new("data", "launch");

let main_terminal = CloudTerminal::new("main");
let setup_terminal = CloudTerminal::new("setup");
let group_setup_terminal = CloudTerminal::new("group-setup");

let mut current_terminal = main_terminal.get_current_terminal();

let need_setup = !config.get("Host").is_some();
let mut setup_step = 0;

if need_setup {
current_terminal = setup_terminal.get_current_terminal();
current_terminal.writeline("On which ip addresse should run the cloud?");
}
mod config {
pub(crate) mod json_config;
}
mod service;

current_terminal.clear();
mod groups {
mod group;
mod grouptypes;
}

loop {
let input = current_terminal.readline();
let current_terminal_name = current_terminal.name.as_str();
use terminal::terminal_manager::TerminalManager;
use config::json_config::JsonConfig;

match current_terminal_name {
"main" => {
if input == "clear" || input == "cls" {
current_terminal.clear();
continue;
}
if input == "shutdown" {
break;
}
}
"setup" => {
match setup_step {
0 => {
config.set("Host", input.clone());
current_terminal.writeline("On which port should run the cloud?");
setup_step += 1;
continue;
}
1 => {
config.set("Port", input.clone());
current_terminal.writeline("How many memory should use the cloud? (in GB)");
setup_step += 1;
continue;
}
2 => {
config.set("Memory", input.clone());
current_terminal.clear();
current_terminal = main_terminal.get_current_terminal();
setup_step += 1;
continue;
}
_ => {
current_terminal
.writeline("Setup has been cancelled caused an unknown error.");
break;
}
}
}
"group-setup" => {}
_ => {
if current_terminal_name.starts_with("service-") {
if input == "leave" {
current_terminal = main_terminal.get_current_terminal();
continue;
}
// TODO: Send Command into java application in terminal
continue;
}
current_terminal.writeline("Unknown terminal.");
}
}
current_terminal.writeline(&format!("Unknown command: {}", input));
}
fn main() {
let config: JsonConfig = JsonConfig::new("data", "launch");
let mut terminal_manager: TerminalManager = TerminalManager::new(config);
terminal_manager.start_terminal();
}
4 changes: 2 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn createPermanentlyService() {
fn create_permanently_service() {

}

fn createTemporaryService() {
fn create_temporary_service() {

}
2 changes: 1 addition & 1 deletion src/cloud_terminal.rs → src/terminal/cloud_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl CloudTerminal {
input.trim().to_string()
}

pub fn writeline(&self, message: &str) {
pub fn write_line(&self, message: &str) {
println!("{}", message);
}

Expand Down
93 changes: 93 additions & 0 deletions src/terminal/terminal_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use super::cloud_terminal::CloudTerminal;
use crate::config::json_config::JsonConfig;

pub struct TerminalManager {
pub(crate) launch_config: JsonConfig,
pub(crate) main_terminal: CloudTerminal,
pub(crate) setup_terminal: CloudTerminal,
pub(crate) group_setup_terminal: CloudTerminal,
}

impl TerminalManager {

pub fn new(launch_config: JsonConfig) -> Self {
let launch_config = launch_config;
Self {
launch_config,
main_terminal: CloudTerminal::new("main"),
setup_terminal: CloudTerminal::new("setup"),
group_setup_terminal: CloudTerminal::new("group-setup")
}
}

pub fn start_terminal(&mut self) {
let mut current_terminal = self.main_terminal.get_current_terminal();

let need_setup = !self.launch_config.get("Host").is_some();
let mut setup_step = 0;

current_terminal.clear();

if need_setup {
current_terminal = self.setup_terminal.get_current_terminal();
current_terminal.write_line("On which ip address should run the cloud?");
}

loop {
let input = current_terminal.readline();
let current_terminal_name = current_terminal.name.as_str();

match current_terminal_name {
"main" => {
if input == "clear" || input == "cls" {
current_terminal.clear();
continue;
}
if input == "shutdown" || input == "exit" || input == "quit" || input == "stop" {
break;
}
}
"setup" => match setup_step {
0 => {
self.launch_config.set("Host", input.clone());
current_terminal.write_line("Which port should the cloud run on?");
setup_step += 1;
continue;
}
1 => {
self.launch_config.set("Port", input.clone());
current_terminal
.write_line("How many memory should use the cloud? (in GB)");
setup_step += 1;
continue;
}
2 => {
self.launch_config.set("Memory", input.clone());
current_terminal.clear();
current_terminal = self.main_terminal.get_current_terminal();
setup_step += 1;
continue;
}
_ => {
current_terminal
.write_line("Setup has been cancelled caused an unknown error.");
break;
}
},
"group-setup" => {}
_ => {
if current_terminal_name.starts_with("service-") {
if input == "leave" {
current_terminal = self.main_terminal.get_current_terminal();
continue;
}
// TODO: Send Command into java application in terminal
continue;
}
current_terminal.write_line("Unknown terminal.");
}
}
current_terminal.write_line(&format!("Unknown command: {}", input));
}
}
}
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn main() {
let working_dir_clone = working_dir.clone();
let keep_running_clone = Arc::clone(&keep_running);
let handle = thread::spawn(move || {
start(&jar_file_name_clone, &working_dir_clone, keep_running_clone);
start_terminal(&jar_file_name_clone, &working_dir_clone, keep_running_clone);
});
// Warte auf Benutzereingabe für den Stop-Befehl
Expand All @@ -93,7 +93,7 @@ async fn main() {
});
}
fn start(jar_file_name: &str, working_dir: &str, keep_running: Arc<AtomicBool>) {
fn start_terminal(jar_file_name: &str, working_dir: &str, keep_running: Arc<AtomicBool>) {
let mut cmd = Command::new("java");
cmd.arg("-jar")
.arg(jar_file_name)
Expand Down

0 comments on commit 4fbdf01

Please sign in to comment.