rename TrueTime to ProgramTime and don't share it

This commit is contained in:
Isse 2023-10-01 17:05:14 +02:00
parent 7d37646dac
commit d638215b88
10 changed files with 47 additions and 57 deletions

View File

@ -376,7 +376,6 @@ impl Client {
let ServerInit::GameSync {
entity_package,
time_of_day,
true_time,
max_group_size,
client_timeout,
world_map,
@ -415,7 +414,6 @@ impl Client {
state.ecs_mut().register::<comp::Last<CharacterState>>();
let entity = state.ecs_mut().apply_entity_package(entity_package);
*state.ecs_mut().write_resource() = time_of_day;
*state.ecs_mut().write_resource() = true_time;
*state.ecs_mut().write_resource() = PlayerEntity(Some(entity));
state.ecs_mut().insert(material_stats);
state.ecs_mut().insert(ability_map);
@ -1931,9 +1929,9 @@ impl Client {
self.tick_terrain()?;
// Send a ping to the server once every second
if self.state.get_true_time() - self.last_server_ping > 1. {
if self.state.get_program_time() - self.last_server_ping > 1. {
self.send_msg_err(PingMsg::Ping)?;
self.last_server_ping = self.state.get_true_time();
self.last_server_ping = self.state.get_program_time();
}
// 6) Update the server about the player's physics attributes.
@ -2268,16 +2266,9 @@ impl Client {
return Err(Error::Other("Failed to find entity from uid.".into()));
}
},
ServerGeneral::TimeOfDay(
time_of_day,
calendar,
new_time,
new_true_time,
time_scale,
) => {
ServerGeneral::TimeOfDay(time_of_day, calendar, new_time, time_scale) => {
self.target_time_of_day = Some(time_of_day);
*self.state.ecs_mut().write_resource() = calendar;
*self.state.ecs_mut().write_resource() = new_true_time;
*self.state.ecs_mut().write_resource() = time_scale;
let mut time = self.state.ecs_mut().write_resource::<Time>();
// Avoid side-eye from Einstein
@ -2601,8 +2592,8 @@ impl Client {
self.send_msg_err(PingMsg::Pong)?;
},
PingMsg::Pong => {
self.last_server_pong = self.state.get_true_time();
self.last_ping_delta = self.state.get_true_time() - self.last_server_ping;
self.last_server_pong = self.state.get_program_time();
self.last_ping_delta = self.state.get_program_time() - self.last_server_ping;
// Maintain the correct number of deltas for calculating the rolling average
// ping. The client sends a ping to the server every second so we should be
@ -2673,18 +2664,18 @@ impl Client {
// Check that we have an valid connection.
// Use the last ping time as a 1s rate limiter, we only notify the user once per
// second
if self.state.get_true_time() - self.last_server_ping > 1. {
let duration_since_last_pong = self.state.get_true_time() - self.last_server_pong;
if self.state.get_program_time() - self.last_server_ping > 1. {
let duration_since_last_pong = self.state.get_program_time() - self.last_server_pong;
// Dispatch a notification to the HUD warning they will be kicked in {n} seconds
const KICK_WARNING_AFTER_REL_TO_TIMEOUT_FRACTION: f64 = 0.75;
if duration_since_last_pong
>= (self.client_timeout.as_secs() as f64
* KICK_WARNING_AFTER_REL_TO_TIMEOUT_FRACTION)
&& self.state.get_true_time() - duration_since_last_pong > 0.
&& self.state.get_program_time() - duration_since_last_pong > 0.
{
frontend_events.push(Event::DisconnectionNotification(
(self.state.get_true_time() - duration_since_last_pong).round() as u64,
(self.state.get_program_time() - duration_since_last_pong).round() as u64,
));
}
}
@ -2692,10 +2683,10 @@ impl Client {
let msg_count = self.handle_messages(&mut frontend_events)?;
if msg_count == 0
&& self.state.get_true_time() - self.last_server_pong
&& self.state.get_program_time() - self.last_server_pong
> self.client_timeout.as_secs() as f64
{
dbg!(self.state.get_true_time());
dbg!(self.state.get_program_time());
dbg!(self.last_server_pong);
return Err(Error::ServerTimeout);
}
@ -2896,9 +2887,18 @@ impl Client {
// Advance state time manually since we aren't calling `State::tick`
self.state
.ecs()
.write_resource::<common::resources::TrueTime>()
.write_resource::<common::resources::ProgramTime>()
.0 += dt.as_secs_f64();
let time_scale = *self
.state
.ecs()
.read_resource::<common::resources::TimeScale>();
self.state
.ecs()
.write_resource::<common::resources::Time>()
.0 += dt.as_secs_f64() * time_scale.0;
// Handle new messages from the server.
self.handle_new_messages()?;
@ -2922,9 +2922,9 @@ impl Client {
drop(terrain);
// Send a ping to the server once every second
if self.state.get_time() - self.last_server_ping > 1. {
if self.state.get_program_time() - self.last_server_ping > 1. {
self.send_msg_err(PingMsg::Ping)?;
self.last_server_ping = self.state.get_time();
self.last_server_ping = self.state.get_program_time();
}
// 6) Update the server about the player's physics attributes.

View File

@ -11,7 +11,7 @@ use common::{
lod,
outcome::Outcome,
recipe::{ComponentRecipeBook, RecipeBook, RepairRecipeBook},
resources::{Time, TimeOfDay, TimeScale, TrueTime},
resources::{Time, TimeOfDay, TimeScale},
shared_server_config::ServerConstants,
terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize},
trade::{PendingTrade, SitePrices, TradeId, TradeResult},
@ -60,7 +60,6 @@ pub enum ServerInit {
GameSync {
entity_package: sync::EntityPackage<EcsCompPacket>,
time_of_day: TimeOfDay,
true_time: TrueTime,
max_group_size: u32,
client_timeout: Duration,
world_map: crate::msg::world_msg::WorldMapMsg,
@ -196,7 +195,7 @@ pub enum ServerGeneral {
ChatMsg(comp::ChatMsg),
ChatMode(comp::ChatMode),
SetPlayerEntity(Uid),
TimeOfDay(TimeOfDay, Calendar, Time, TrueTime, TimeScale),
TimeOfDay(TimeOfDay, Calendar, Time, TimeScale),
EntitySync(sync::EntitySyncPackage),
CompSync(sync::CompSyncPackage<EcsCompPacket>, u64),
CreateEntity(sync::EntityPackage<EcsCompPacket>),
@ -343,7 +342,7 @@ impl ServerMsg {
| ServerGeneral::ChatMsg(_)
| ServerGeneral::ChatMode(_)
| ServerGeneral::SetPlayerEntity(_)
| ServerGeneral::TimeOfDay(_, _, _, _, _)
| ServerGeneral::TimeOfDay(_, _, _, _)
| ServerGeneral::EntitySync(_)
| ServerGeneral::CompSync(_, _)
| ServerGeneral::CreateEntity(_)

View File

@ -11,9 +11,9 @@ pub struct TimeOfDay(pub f64);
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Time(pub f64);
/// A resource that stores the real tick, unaffected by `TimeScale`.
/// A resource that stores the real tick, local to the server/client.
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct TrueTime(pub f64);
pub struct ProgramTime(pub f64);
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct TimeScale(pub f64);

View File

@ -13,8 +13,8 @@ use common::{
mounting::{Mount, Rider, VolumeRider, VolumeRiders},
outcome::Outcome,
resources::{
DeltaTime, EntitiesDiedLastTick, GameMode, PlayerEntity, PlayerPhysicsSettings, Time,
TimeOfDay, TimeScale, TrueTime,
DeltaTime, EntitiesDiedLastTick, GameMode, PlayerEntity, PlayerPhysicsSettings,
ProgramTime, Time, TimeOfDay, TimeScale,
},
shared_server_config::ServerConstants,
slowjob::SlowJobPool,
@ -269,7 +269,7 @@ impl State {
ecs.insert(Calendar::default());
ecs.insert(WeatherGrid::new(Vec2::zero()));
ecs.insert(Time(0.0));
ecs.insert(TrueTime(0.0));
ecs.insert(ProgramTime(0.0));
ecs.insert(TimeScale(1.0));
// Register unsynced resources used by the ECS.
@ -441,7 +441,7 @@ impl State {
/// Get the current true in-game time, unaffected by time_scale.
///
/// Note that this does not correspond to the time of day.
pub fn get_true_time(&self) -> f64 { self.ecs.read_resource::<TrueTime>().0 }
pub fn get_program_time(&self) -> f64 { self.ecs.read_resource::<ProgramTime>().0 }
/// Get the current delta time.
pub fn get_delta_time(&self) -> f32 { self.ecs.read_resource::<DeltaTime>().0 }
@ -643,7 +643,7 @@ impl State {
self.ecs.write_resource::<TimeOfDay>().0 +=
dt.as_secs_f64() * server_constants.day_cycle_coefficient * time_scale;
self.ecs.write_resource::<Time>().0 += dt.as_secs_f64() * time_scale;
self.ecs.write_resource::<TrueTime>().0 += dt.as_secs_f64();
self.ecs.write_resource::<ProgramTime>().0 += dt.as_secs_f64();
// Update delta time.
// Beyond a delta time of MAX_DELTA_TIME, start lagging to avoid skipping

View File

@ -203,7 +203,7 @@ impl Client {
| ServerGeneral::ChatMsg(_)
| ServerGeneral::ChatMode(_)
| ServerGeneral::SetPlayerEntity(_)
| ServerGeneral::TimeOfDay(_, _, _, _, _)
| ServerGeneral::TimeOfDay(_, _, _, _)
| ServerGeneral::EntitySync(_)
| ServerGeneral::CompSync(_, _)
| ServerGeneral::CreateEntity(_)

View File

@ -43,7 +43,7 @@ use common::{
npc::{self, get_npc_name},
outcome::Outcome,
parse_cmd_args,
resources::{BattleMode, PlayerPhysicsSettings, Secs, Time, TimeOfDay, TimeScale, TrueTime},
resources::{BattleMode, PlayerPhysicsSettings, Secs, Time, TimeOfDay, TimeScale},
rtsim::{Actor, Role},
terrain::{Block, BlockKind, CoordinateConversions, SpriteKind, TerrainChunkSize},
uid::Uid,
@ -1092,7 +1092,6 @@ fn handle_time(
server.state.mut_resource::<TimeOfDay>().0 = new_time;
let time = server.state.ecs().read_resource::<Time>();
let true_time = server.state.ecs().read_resource::<TrueTime>();
// Update all clients with the new TimeOfDay (without this they would have to
// wait for the next 100th tick to receive the update).
@ -1106,7 +1105,6 @@ fn handle_time(
TimeOfDay(new_time),
(*calendar).clone(),
*time,
*true_time,
*time_scale,
))
});
@ -1160,7 +1158,6 @@ fn handle_time_scale(
let mut tod_lazymsg = None;
let clients = server.state.ecs().read_storage::<Client>();
let time = server.state.ecs().read_resource::<Time>();
let true_time = server.state.ecs().read_resource::<TrueTime>();
let time_of_day = server.state.ecs().read_resource::<TimeOfDay>();
let calendar = server.state.ecs().read_resource::<Calendar>();
for client in (&clients).join() {
@ -1169,7 +1166,6 @@ fn handle_time_scale(
*time_of_day,
(*calendar).clone(),
*time,
*true_time,
TimeScale(scale),
))
});

View File

@ -8,7 +8,7 @@ use common::{
mounting::Rider,
outcome::Outcome,
region::{Event as RegionEvent, RegionMap},
resources::{PlayerPhysicsSettings, Time, TimeOfDay, TimeScale, TrueTime},
resources::{PlayerPhysicsSettings, Time, TimeOfDay, TimeScale},
terrain::TerrainChunkSize,
uid::Uid,
vol::RectVolSize,
@ -32,7 +32,6 @@ impl<'a> System<'a> for Sys {
TrackedStorages<'a>,
ReadExpect<'a, TimeOfDay>,
ReadExpect<'a, Time>,
ReadExpect<'a, TrueTime>,
ReadExpect<'a, Calendar>,
ReadExpect<'a, TimeScale>,
ReadExpect<'a, RegionMap>,
@ -66,7 +65,6 @@ impl<'a> System<'a> for Sys {
tracked_storages,
time_of_day,
time,
true_time,
calendar,
time_scale,
region_map,
@ -436,7 +434,6 @@ impl<'a> System<'a> for Sys {
*time_of_day,
(*calendar).clone(),
*time,
*true_time,
*time_scale,
))
});

View File

@ -2,7 +2,7 @@ use crate::client::Client;
use common::{
comp::{ChatMode, ChatType, Content, Group, Player},
event::{EventBus, ServerEvent},
resources::TrueTime,
resources::ProgramTime,
uid::Uid,
};
use common_ecs::{Job, Origin, Phase, System};
@ -81,7 +81,7 @@ impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
Read<'a, EventBus<ServerEvent>>,
Read<'a, TrueTime>,
Read<'a, ProgramTime>,
ReadStorage<'a, Uid>,
ReadStorage<'a, ChatMode>,
ReadStorage<'a, Player>,
@ -95,7 +95,7 @@ impl<'a> System<'a> for Sys {
fn run(
_job: &mut Job<Self>,
(entities, server_event_bus, time, uids, chat_modes, players, groups, mut clients): Self::SystemData,
(entities, server_event_bus, program_time, uids, chat_modes, players, groups, mut clients): Self::SystemData,
) {
(&entities, &mut clients, players.maybe())
.par_join()
@ -117,7 +117,7 @@ impl<'a> System<'a> for Sys {
if let Ok(1_u64..=u64::MAX) = res {
// Update client ping.
client.last_ping = time.0
client.last_ping = program_time.0
}
},
);

View File

@ -1,7 +1,7 @@
use crate::{client::Client, Settings};
use common::{
event::{EventBus, ServerEvent},
resources::TrueTime,
resources::ProgramTime,
};
use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::PingMsg;
@ -26,7 +26,7 @@ impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
Read<'a, EventBus<ServerEvent>>,
Read<'a, TrueTime>,
Read<'a, ProgramTime>,
WriteStorage<'a, Client>,
Read<'a, Settings>,
);
@ -37,7 +37,7 @@ impl<'a> System<'a> for Sys {
fn run(
_job: &mut Job<Self>,
(entities, server_event_bus, time, mut clients, settings): Self::SystemData,
(entities, server_event_bus, program_time, mut clients, settings): Self::SystemData,
) {
(&entities, &mut clients).par_join().for_each_init(
|| server_event_bus.emitter(),
@ -59,11 +59,11 @@ impl<'a> System<'a> for Sys {
},
Ok(1_u64..=u64::MAX) => {
// Update client ping.
client.last_ping = time.0
client.last_ping = program_time.0
},
Ok(0) => {
let last_ping: f64 = client.last_ping;
if time.0 - last_ping > settings.client_timeout.as_secs() as f64
if program_time.0 - last_ping > settings.client_timeout.as_secs() as f64
// Timeout
{
info!(?entity, "timeout error with client, disconnecting");
@ -71,7 +71,7 @@ impl<'a> System<'a> for Sys {
entity,
common::comp::DisconnectReason::Timeout,
));
} else if time.0 - last_ping
} else if program_time.0 - last_ping
> settings.client_timeout.as_secs() as f64 * 0.5
{
// Try pinging the client if the timeout is nearing.

View File

@ -9,7 +9,7 @@ use common::{
comp::{self, Admin, Player, Stats},
event::{EventBus, ServerEvent},
recipe::{default_component_recipe_book, default_recipe_book, default_repair_recipe_book},
resources::{TimeOfDay, TrueTime},
resources::TimeOfDay,
shared_server_config::ServerConstants,
uid::{IdMaps, Uid},
};
@ -48,7 +48,6 @@ pub struct ReadData<'a> {
settings: ReadExpect<'a, Settings>,
editable_settings: ReadExpect<'a, EditableSettings>,
time_of_day: Read<'a, TimeOfDay>,
true_time: Read<'a, TrueTime>,
material_stats: ReadExpect<'a, comp::item::MaterialStatManifest>,
ability_map: ReadExpect<'a, comp::item::tool::AbilityMap>,
map: ReadExpect<'a, WorldMapMsg>,
@ -330,7 +329,6 @@ impl<'a> System<'a> for Sys {
.trackers
.create_entity_package_with_uid(entity, *uid, None, None, None),
time_of_day: *read_data.time_of_day,
true_time: *read_data.true_time,
max_group_size: read_data.settings.max_player_group_size,
client_timeout: read_data.settings.client_timeout,
world_map: (*read_data.map).clone(),