Skip to content

Commit

Permalink
Support for custom game posting
Browse files Browse the repository at this point in the history
  • Loading branch information
dpleshkov committed Sep 8, 2023
1 parent f21f585 commit 1d9116b
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 153 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ServerList+ Rust API

Rust implementation of the back-end API needed to provide the info for
[ServerList+](https://starblast.dankdmitron.dev/). The back-end was originally written in
Node.js but has been re-written in Rust to lower the memory footprint of the overall
server.



1 change: 1 addition & 0 deletions proxies.txt.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# put proxies in this file
43 changes: 23 additions & 20 deletions src/listener.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::fmt::Formatter;
use std::time::{SystemTime, UNIX_EPOCH};

use futures::{SinkExt, StreamExt};
use futures_enum::{Sink, Stream};
Expand All @@ -14,7 +13,7 @@ use tokio_tungstenite::{client_async_tls, connect_async, MaybeTlsStream, WebSock
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::error::Error as WsError;
use tokio_tungstenite::tungstenite::Message;
use crate::http_utils::translate_color;
use crate::utils::{get_ms_since_epoch, translate_color};

use crate::proxy::{InnerProxy, ProxyStream};

Expand All @@ -28,7 +27,8 @@ enum ListenerResponse {
enum ListenerRequest {
Subscribe,
GetName(u8),
GetState
GetState,
Shutdown
}

enum ListenerError {
Expand Down Expand Up @@ -89,7 +89,7 @@ struct GameDataPlayerCustom {
}

#[derive(Serialize, Deserialize, Clone)]
struct GameDataPlayer {
pub struct GameDataPlayer {
id: u8,
hue: Option<u16>,
friendly: Option<u8>,
Expand All @@ -104,11 +104,11 @@ struct GameDataPlayer {
}

#[derive(Serialize, Deserialize, Clone)]
struct GameDataModeSimplified {
pub struct GameDataModeSimplified {
map_size: u16,
friendly_colors: u8,
unlisted: bool,
id: String,
pub(crate) unlisted: bool,
pub(crate) id: String,
teams: Option<Vec<GameDataTeam>>,
root_mode: Option<String>,
}
Expand All @@ -126,15 +126,15 @@ struct ApiData {
pub struct GameData {
version: u8,
seed: u16,
servertime: u32,
systemid: u16,
pub(crate) servertime: u32,
pub(crate) systemid: u16,
size: u16,
mode: GameDataModeSimplified,
region: String,
obtained: Option<u64>,
players: Option<HashMap<u8, GameDataPlayer>>,
pub(crate) mode: GameDataModeSimplified,
pub(crate) region: String,
pub(crate) obtained: Option<u64>,
pub(crate) players: Option<HashMap<u8, GameDataPlayer>>,
api: Option<ApiData>,
name: String
pub(crate) name: String
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -211,6 +211,10 @@ impl Listener {
None
}

pub async fn stop(&self) {
let _ = self.req(ListenerRequest::Shutdown).await;
}

pub fn is_finished(&self) -> bool {
self.handle.is_finished()
}
Expand Down Expand Up @@ -256,11 +260,6 @@ async fn listener_main(address: String, proxy: Option<String>, game_id: u16, mut

let mut welcome_msg: GameData;

let time_start = SystemTime::now();
let since_the_epoch = time_start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");

match socket_rx.next().await {
None => {
return Ok(());
Expand All @@ -283,7 +282,7 @@ async fn listener_main(address: String, proxy: Option<String>, game_id: u16, mut
}
welcome_msg = serde_json::from_value(msg.data).expect("failed parsing msg");
welcome_msg.players = Some(HashMap::new());
welcome_msg.obtained = Some(since_the_epoch.as_secs() * 1000 + since_the_epoch.subsec_nanos() as u64 / 1_000_000);
welcome_msg.obtained = Some(get_ms_since_epoch());

let mode_id = welcome_msg.mode.id.as_str();
let root_mode = welcome_msg.mode.root_mode.clone();
Expand Down Expand Up @@ -503,6 +502,10 @@ async fn listener_main(address: String, proxy: Option<String>, game_id: u16, mut
let _ = req.1.send(ListenerResponse::None);
}
}
ListenerRequest::Shutdown => {
let _ = req.1.send(ListenerResponse::None);
return Ok(());
}
}
}
None => {
Expand Down
Loading

0 comments on commit 1d9116b

Please sign in to comment.