Skip to content

Commit

Permalink
feat: WIP prediction
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jul 5, 2024
1 parent 380e3fd commit a37cc81
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,40 @@ impl Client {
self.user = None;
self.entered = false;
}

pub fn move_by_delta(
&mut self,
_point: isize,
_delta: isize,
_filename: Option<String>,
) -> Option<String> {
let user = self.user_mut();

// Exit when user hasn't been created.
if user.is_none() {
return None;
}

let user = user.unwrap();
let filename = user.path.clone();

// Exit when current user isn't in any valid file.
if filename.is_none() {
return None;
}

let filename = filename.unwrap();

if !_filename.is_none() {
let _filename = _filename.unwrap();

if filename != _filename {
return None;
}
}

user.move_by_delta(_point, _delta);

Some(filename)
}
}
32 changes: 32 additions & 0 deletions src/handler/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ pub mod update {

const METHOD: &str = "buffer::update";

fn predict_movement(addr: &SocketAddr, room: &mut Room, point: isize, delta: isize) {
let client = room.get_client_mut(addr).unwrap();

let filename = client.move_by_delta(point, delta, None);

let clients = room.get_clients_mut();

for (_addr, _client) in clients.iter_mut() {
if _addr == addr {
continue;
}

_client.move_by_delta(point, delta, filename.clone());
}
}

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
let addr = &channel.get_connection().addr;
let mut room = room.lock().await;
Expand All @@ -32,6 +48,22 @@ pub mod update {
let end = data_isize(json, "end").unwrap();
let contents = data_str(json, "contents").unwrap();

// Predict movement.
{
let delta = if add_or_delete == "delete" {
beg - end
} else {
end - beg
};

// Nothing has changed; return it.
if delta == 0 {
return;
}

predict_movement(&addr, &mut room, beg, delta);
}

// Update the buffer view.
let file = room.get_file_create_mut(&addr, &path, None);
let file = file.unwrap();
Expand Down
30 changes: 30 additions & 0 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,34 @@ impl User {
self.color_cursor = color_cursor;
self.color_region = color_region;
}

/// Move point information by delta.
///
/// # Arguments
///
/// * `_point` - Only effect point after the origin.
/// * `_delta` - Movement delta.
pub fn move_by_delta(&mut self, _point: isize, _delta: isize) {
// Point must exists.
if self.point.is_none() {
return;
}

let point = self.point.unwrap();

// Shift the point.
if _point <= point {
self.point = Some(point + _delta);

// `region_beg`. and `region_end` must exists at the same time.
if !self.region_beg.is_none() {
let region_beg = self.region_beg.unwrap();
let region_end = self.region_end.unwrap();

// Shift the region.
self.region_beg = Some(region_beg + _delta);
self.region_end = Some(region_end + _delta);
}
}
}
}

0 comments on commit a37cc81

Please sign in to comment.