Servers instances added

modified:   src/config.rs
	modified:   src/launcher.rs
	modified:   src/main.rs
	modified:   src/minecraft.rs
	modified:   src/www/portable.html
This commit is contained in:
Michael Wain 2025-03-17 15:52:58 +03:00
parent 882a7639a2
commit d64a0e494f
5 changed files with 61 additions and 8 deletions

View File

@ -61,6 +61,10 @@ impl LauncherConfig {
self.user_name = user_name;
}
pub fn servers(&self) -> &Vec<LauncherServer> {
&self.servers
}
pub fn add_server(&mut self, server: LauncherServer) {
self.servers.push(server);
}

View File

@ -107,6 +107,15 @@ impl Launcher {
}
}
pub async fn get_servers_list(&self) -> Vec<(String, String, Option<String>)> {
let mut v = Vec::new();
let servers = self.config.servers();
for server in servers {
v.push((server.domain.clone(), server.credentials.username.clone(), minecraft::server::get_server_icon(&server.domain, server.port).await.unwrap_or(None)));
}
v
}
pub fn get_instances_list(&self) -> Vec<(String, String, String)> {
let mut v = Vec::new();
let mut instances = self.config.launcher_dir();

View File

@ -159,6 +159,18 @@ async fn main() {
responder.respond(Response::new(serde_json::to_vec(&UIMessage { params: v }).unwrap()));
}
"fetch_servers_list" => {
let resp = launcher.get_servers_list().await;
let mut v: Vec<String> = Vec::new();
v.push("fetch_servers_list".to_string());
for (domain, nickname, image) in resp {
v.push(domain);
v.push(nickname);
v.push(image.unwrap_or(String::new()));
}
responder.respond(Response::new(serde_json::to_vec(&UIMessage { params: v }).unwrap()));
}
"check_download_status" => {
if let Ok((percent, text)) = dl_rec.try_recv() {
responder.respond(Response::new(serde_json::to_vec(&UIMessage { params: vec!["update_downloads".to_string(), text, percent.to_string()] }).unwrap()));

View File

@ -195,6 +195,20 @@ pub mod session {
}
}
pub mod server {
use std::error::Error;
use base64::{prelude::BASE64_STANDARD, Engine};
use surf::StatusCode;
pub async fn get_server_icon(server: &str, port: u16) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
let mut r = surf::get(["https://eu.mc-api.net/v3/server/favicon/", server,":", &port.to_string()].concat()).await?;
if r.status() != StatusCode::Ok { return Ok(None); }
let resp = r.body_bytes().await.unwrap();
Ok(Some(["data:image/png;base64,", &BASE64_STANDARD.encode(resp)].concat()))
}
}
pub mod assets {
use std::{collections::HashMap, error::Error, path::PathBuf};
use serde::{Deserialize, Serialize};

View File

@ -60,7 +60,7 @@
<li>
<a
href="#"
onclick="showSection(this, 'servers')"
onclick="showSection(this, 'servers'); showServersSection()"
class="menu-btn group relative flex justify-center rounded-sm px-2 py-1.5 text-gray-500 hover:bg-gray-50 hover:text-gray-700"
>
<i class="fa-solid fa-network-wired"></i>
@ -405,13 +405,6 @@
</div>
<div id="servers-section" class="xsection grid grid-cols-3 gap-4 p-6 w-fill hidden">
<!--<div class="bg-white cursor-pointer hover:bg-green-500 hover:text-white shadow-lg rounded-xl w-48 h-24 flex justify-center items-center">
<img src="https://eu.mc-api.net/v3/server/favicon/mc.hypixel.net" class="w-12 h-12 rounded-full">
<div class="h-fill ms-2">
<h2 class="text-lg font-semibold">Hypixel</h2>
<h2 class="text-sm font-semibold">mc.hypixel.net</h2>
</div>
</div>-->
<div onClick="addServer()" class="bg-white shadow-lg rounded-xl w-48 h-24 flex justify-center items-center text-3xl text-green-500 cursor-pointer hover:bg-green-500 hover:text-white"><i class="fa-solid fa-plus"></i></div>
</div>
</div>
@ -444,6 +437,9 @@
} else if( params[i] == "set_instances_list" ) {
setInstancesList(params.slice(i+1));
break;
} else if( params[i] == "fetch_servers_list" ) {
setServersList(params.slice(i+1));
break;
} else if( params[i] == "locate_java" ) {
setJavaPath(params[i+1]);
break;
@ -534,6 +530,20 @@
}
}
function setServersList(params) {
$(".server-instance").remove();
for( let i = 0; i < params.length; i+=3) {
let instance = `<div class="server-instance bg-white cursor-pointer hover:bg-green-500 hover:text-white shadow-lg rounded-xl w-48 h-24 flex justify-center items-center">
<img src="`+params[i+2]+`" class="w-12 h-12 rounded-full">
<div class="h-fill ms-2">
<h2 class="text-lg font-semibold">`+params[i]+`</h2>
<h2 class="text-sm font-semibold">`+params[i+1]+`</h2>
</div>
</div>`;
$("#servers-section").append(instance);
}
}
function updateDownloads(params) {
let text = params[0];
let percentage = params[1];
@ -547,6 +557,10 @@
}
}
function showServersSection() {
$.post({url: "fetch_servers_list" }, processParams);
}
function showInstancesSection() {
$.post({url: "fetch_instances_list" }, processParams);
}