Skip to content

Commit

Permalink
Merge branch 'core_dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Ynng committed Jul 17, 2023
2 parents 835a075 + e26b262 commit ba0bbfb
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ target/
# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

## vscode local history
.history/

# jetbrains IDE project files
.idea/

# Test ground should be local to every developer
src/test_ground/

Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ tar = "0.4.38"
tempfile = "3.5.0"
clap = { version = "4.3.0", features = ["derive"] }
once_cell = "1.17.1"
fs3 = "0.5.0"
import_map = "0.15.0"
[dependencies.uuid]
version = "1.1.2"
Expand Down
3 changes: 3 additions & 0 deletions core/bindings/MacroEventKind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type MacroEventKind = "Started" | "MainModuleExecuted" | "Stopped";
4 changes: 4 additions & 0 deletions core/bindings/ProgressionEventID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Snowflake } from "./Snowflake";

export type ProgressionEventID = Snowflake;
2 changes: 1 addition & 1 deletion core/bindings/ProgressionStartValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { InstanceUuid } from "./InstanceUuid";

export type ProgressionStartValue = { type: "InstanceCreation", instance_uuid: InstanceUuid, instance_name: string, port: number, flavour: string, game_type: string, } | { type: "InstanceDelete", instance_uuid: InstanceUuid, };
export type ProgressionStartValue = { type: "InstanceCreation", instance_uuid: InstanceUuid } | { type: "InstanceDelete", instance_uuid: InstanceUuid, };
4 changes: 4 additions & 0 deletions core/deno_bindings/ProgressionEventID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Snowflake } from "./Snowflake.ts";

export type ProgressionEventID = Snowflake;
2 changes: 1 addition & 1 deletion core/deno_bindings/ProgressionStartValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { InstanceUuid } from "./InstanceUuid.ts";

export type ProgressionStartValue = { type: "InstanceCreation", instance_uuid: InstanceUuid, instance_name: string, port: number, flavour: string, game_type: string, } | { type: "InstanceDelete", instance_uuid: InstanceUuid, };
export type ProgressionStartValue = { type: "InstanceCreation", instance_uuid: InstanceUuid } | { type: "InstanceDelete", instance_uuid: InstanceUuid, };
14 changes: 14 additions & 0 deletions core/src/deno_ops/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { TaskPID } from "../../../deno_bindings/TaskPID.ts";
import * as InstanceControl from "../instance_control/instance_control.ts"
import { InstanceEvent } from "../../../deno_bindings/InstanceEvent.ts";
import { InstanceState } from "../../../deno_bindings/InstanceState.ts";
import { ProgressionStartValue } from "../../../deno_bindings/ProgressionStartValue.ts";
import { ProgressionEndValue } from "../../../deno_bindings/ProgressionEndValue.ts";
import { ProgressionEventID } from "../../../deno_bindings/ProgressionEventID.ts";

// re-exports
export type { ClientEvent, TaskPID, InstanceControl, InstanceEvent, InstanceState };
Expand Down Expand Up @@ -63,3 +66,14 @@ export function emitStateChange(state: InstanceState, instanceName: string, inst
ops.emit_state_change(instanceUuid, instanceName, state);
}

export function emitProgressionEventStart(progressionName : string, total : number | null, inner : ProgressionStartValue | null) : ProgressionEventID {
return ops.emit_progression_event_start(progressionName, total, inner);
}

export function emitProgressiontEventUpdate(eventId : ProgressionEventID, progressMessage : string, progress : number) {
ops.emit_progression_event_update(eventId, progressMessage, progress);
}

export function emitProgressionEventEnd(eventId : ProgressionEventID, success : boolean, message : string | null, inner : ProgressionEndValue | null) {
ops.emit_progression_event_end(eventId, success, message, inner);
}
65 changes: 63 additions & 2 deletions core/src/deno_ops/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use deno_core::{
};

use crate::{
event_broadcaster::{EventBroadcaster, PlayerMessage},
events::{Event, InstanceEvent},
event_broadcaster::{EventBroadcaster, PlayerChange, PlayerMessage},
events::{
CausedBy, Event, InstanceEvent, ProgressionEndValue, ProgressionEventID,
ProgressionStartValue,
},
macro_executor::MacroPID,
traits::t_server::State,
types::InstanceUuid,
Expand Down Expand Up @@ -72,6 +75,17 @@ async fn next_instance_system_message(
.await
}

#[op]
async fn next_instance_player_change(
state: Rc<RefCell<OpState>>,
instance_uuid: InstanceUuid,
) -> PlayerChange {
let event_broadcaster = state.borrow().borrow::<EventBroadcaster>().clone();
event_broadcaster
.next_instance_player_change(&instance_uuid)
.await
}

#[op]
fn emit_detach(state: Rc<RefCell<OpState>>, macro_pid: MacroPID) {
let tx = state.borrow().borrow::<EventBroadcaster>().clone();
Expand Down Expand Up @@ -108,6 +122,49 @@ fn emit_state_change(
))
}

#[op]
fn emit_progression_event_start(
state: Rc<RefCell<OpState>>,
progression_name: String,
total: Option<f64>,
inner: Option<ProgressionStartValue>,
) -> ProgressionEventID {
let tx = state.borrow().borrow::<EventBroadcaster>().clone();
let (event, id) =
Event::new_progression_event_start(progression_name, total, inner, CausedBy::System);
tx.send(event);
id
}

#[op]
fn emit_progression_event_update(
state: Rc<RefCell<OpState>>,
event_id: ProgressionEventID,
progress_msg: String,
progress: f64,
) {
let tx = state.borrow().borrow::<EventBroadcaster>().clone();
tx.send(Event::new_progression_event_update(
&event_id,
progress_msg,
progress,
));
}

#[op]
fn emit_progression_event_end(
state: Rc<RefCell<OpState>>,
event_id: ProgressionEventID,
success: bool,
message: Option<String>,
inner: Option<ProgressionEndValue>,
) {
let tx = state.borrow().borrow::<EventBroadcaster>().clone();
tx.send(Event::new_progression_event_end(
event_id, success, message, inner,
));
}

pub fn register_all_event_ops(
worker_options: &mut deno_runtime::worker::WorkerOptions,
event_broadcaster: EventBroadcaster,
Expand All @@ -124,6 +181,10 @@ pub fn register_all_event_ops(
next_instance_output::decl(),
next_instance_player_message::decl(),
next_instance_system_message::decl(),
next_instance_player_change::decl(),
emit_progression_event_start::decl(),
emit_progression_event_update::decl(),
emit_progression_event_end::decl(),
])
.state(|state| {
state.put(event_broadcaster);
Expand Down
28 changes: 27 additions & 1 deletion core/src/event_broadcaster.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::HashSet;

use tokio::sync::broadcast::{Receiver, Sender};
use tracing::error;

use crate::{
events::{Event, EventInner, InstanceEvent, InstanceEventInner},
traits::t_server::State,
traits::{t_player::Player, t_server::State},
types::InstanceUuid,
};

Expand All @@ -18,6 +20,13 @@ pub struct PlayerMessage {
pub message: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PlayerChange {
player_list: HashSet<Player>,
players_joined: HashSet<Player>,
players_left: HashSet<Player>,
}

impl EventBroadcaster {
pub fn new(capacity: usize) -> (Self, Receiver<Event>) {
let (event_tx, rx) = tokio::sync::broadcast::channel(capacity);
Expand Down Expand Up @@ -105,6 +114,23 @@ impl EventBroadcaster {
}
}
}
pub async fn next_instance_player_change(&self, instance_uuid: &InstanceUuid) -> PlayerChange {
loop {
let event = self.next_instance_event(instance_uuid).await;
if let InstanceEventInner::PlayerChange {
player_list,
players_joined,
players_left,
} = event.instance_event_inner
{
return PlayerChange {
player_list,
players_joined,
players_left,
};
}
}
}
}

impl From<EventBroadcaster> for Sender<Event> {
Expand Down
7 changes: 3 additions & 4 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ pub enum ProgressionEndValue {
pub enum ProgressionStartValue {
InstanceCreation {
instance_uuid: InstanceUuid,
instance_name: String,
port: u32,
flavour: String,
game_type: String,
},
InstanceDelete {
instance_uuid: InstanceUuid,
Expand Down Expand Up @@ -284,6 +280,9 @@ pub fn new_fs_event(operation: FSOperation, target: FSTarget, caused_by: CausedB
}
}

#[derive(Serialize, Deserialize, TS)]
#[serde(transparent)]
#[ts(export)]
pub struct ProgressionEventID(Snowflake);

#[derive(Serialize, Deserialize, Clone, Debug, TS, PartialEq)]
Expand Down
6 changes: 0 additions & 6 deletions core/src/handlers/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ pub async fn create_minecraft_instance(
let uuid = instance_uuid.clone();
let instance_name = setup_config.name.clone();
let event_broadcaster = state.event_broadcaster.clone();
let port = setup_config.port;
let flavour = setup_config.flavour.clone();
let caused_by = CausedBy::User {
user_id: requester.uid.clone(),
user_name: requester.username.clone(),
Expand All @@ -121,10 +119,6 @@ pub async fn create_minecraft_instance(
Some(10.0),
Some(ProgressionStartValue::InstanceCreation {
instance_uuid: uuid.clone(),
instance_name: instance_name.clone(),
port,
flavour: flavour.to_string(),
game_type: "minecraft".to_string(),
}),
caused_by,
);
Expand Down
2 changes: 1 addition & 1 deletion core/src/implementations/minecraft/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl TServer for MinecraftInstance {
} else if let Some(PlayerMessage { player, message }) =
parse_player_msg(&line)
{
let _ = event_broadcaster.send(Event {
event_broadcaster.send(Event {
event_inner: EventInner::InstanceEvent(InstanceEvent {
instance_uuid: uuid.clone(),
instance_event_inner:
Expand Down
16 changes: 15 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, EnvFilter}
use traits::{t_configurable::TConfigurable, t_server::MonitorReport, t_server::TServer};
use types::{DotLodestoneConfig, InstanceUuid};
use uuid::Uuid;
use fs3::FileExt;

pub mod auth;
pub mod db;
mod deno_ops;
Expand Down Expand Up @@ -409,6 +411,16 @@ pub async fn run(
check_for_core_update().await;
output_sys_info();

let lockfile_path = lodestone_path.join("lodestone.lock");
let file = if lockfile_path.as_path().exists() {
std::fs::File::open(lockfile_path.as_path()).expect("failed to open lockfile")
} else {
std::fs::File::create(lockfile_path.as_path()).expect("failed to create lockfile")
};
if file.try_lock_exclusive().is_err() {
panic!("Another instance of lodestone might be running");
}

let _ = migrate(&lodestone_path).map_err(|e| {
error!("Error while migrating lodestone: {}. Lodestone will still start, but one or more instance may be in an erroneous state", e);
});
Expand Down Expand Up @@ -644,6 +656,8 @@ pub async fn run(
.unwrap();
}
});
// capture file into the move block
let _file = file;
select! {
_ = write_to_db_task => info!("Write to db task exited"),
_ = event_buffer_task => info!("Event buffer task exited"),
Expand Down Expand Up @@ -710,4 +724,4 @@ pub async fn run(
guard,
shutdown_tx,
)
}
}

0 comments on commit ba0bbfb

Please sign in to comment.