summaryrefslogtreecommitdiff
path: root/src/room/handle.rs
blob: d397c70fc76bc483cf4b106702546325ae229694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::collections::HashSet;

use super::messages::{ClientMessage, ServerMessage};

// send a ServerMessage::Connections to all sockets
pub async fn send_connections(v: &mut super::Clients, added: Option<usize>, removed: Option<usize>, frame: u64) {
	// get the list of connection IDs
	let connections: Vec<usize> = v.iter()
		.enumerate()
		.filter(|(_, n)| n.is_some())
		.map(|(id, _)| id)
		.collect();
	
	super::send(v, |id, _c| {
		Some(ServerMessage::Connections {
			connections: connections.clone(),
			added,
			removed,
			id,
			frame,
		})
	}).await;
}

// handle incoming websocket messages
pub async fn handle(
	v: &mut super::Clients,
	requests: &mut HashSet<(u64, Option<usize>, usize)>, // frame, connection, client id
	pending: &mut Vec<(Option<usize>, Option<usize>)>,
	id: usize,
	msg: ClientMessage,
) {
	match msg {
		// broadcast inputs to every other connection
		ClientMessage::Input { data, frame } => {
			super::broadcast(v, ServerMessage::Input {
				data,
				frame,
				connection: id
			}, Some(id)).await;
		},
		// a client needs the current game state, grab it from another client
		ClientMessage::RequestState { frame, connection } => {
			let count = super::conn_count(v);
			
			if count < 2 { // nobody to request state *from*
				if let Some(Some(client)) = v.get(id) {
					client.send(ServerMessage::State {
						state: serde_json::Value::Null,
						frame: 0,
						connection: None,
					}).await.ok();
				}
				return;
			}
			
			// request state from other clients
			requests.insert((frame, connection, id));
			
			match connection {
				None => {
					super::broadcast(v, ServerMessage::RequestState { frame }, Some(id)).await;
				},
				Some(id) => { // it's to a specific connection
					let Some(Some(client)) = v.get(id) else {
						return;
					};
					client.send(ServerMessage::RequestState { frame }).await.ok();
				},
			}
		},
		// a client responded to a request for game state, tell all the requestees
		ClientMessage::State { state, frame } => {
			let mut new_requests = HashSet::new();
			for (fr, conn, cid) in requests.drain() {
				if
					fr != frame || // this isn't the requested frame
					(conn.is_some() && Some(id) != conn) // this isn't the requested connection
				{
					new_requests.insert((fr, conn, cid));
					continue;
				}
				if let Some(Some(client)) = v.get(cid) {
					client.send(ServerMessage::State {
						state: state.clone(),
						frame,
						connection: Some(id),
					}).await.ok();
				}
			}
			*requests = new_requests;
		},
		// a client said what frame they're on, actually send the connections message
		ClientMessage::Frame { frame } => {
			for (added, removed) in pending.into_iter() {
				send_connections(v, *added, *removed, frame).await;
			}
			*pending = Vec::new();
		},
		ClientMessage::Ping { frame } => {
			if let Some(Some(client)) = v.get(id) {
				client.send(ServerMessage::Pong { frame }).await.ok();
			}
		}
	}
}