forked from lavina/lavina
make irc update handler a separate function
This commit is contained in:
parent
1e17e017cf
commit
30db029390
|
@ -221,58 +221,15 @@ async fn handle_registered_socket<'a>(
|
|||
} else {
|
||||
len
|
||||
};
|
||||
handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, &player_id, writer).await?;
|
||||
handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, writer).await?;
|
||||
buffer.clear();
|
||||
},
|
||||
update = connection.receiver.recv() => {
|
||||
match update.unwrap() {
|
||||
Updates::RoomJoined { new_member_id, room_id } => {
|
||||
if player_id == new_member_id {
|
||||
if let Some(room) = rooms.get_room(&room_id) {
|
||||
let room_info = room.get_room_info().await;
|
||||
let chan = Chan::Global(room_id.as_bytes().clone());
|
||||
produce_on_join_cmd_messages(&config, &user, &chan, &room_info, writer).await?;
|
||||
writer.flush().await?;
|
||||
} else {
|
||||
log::warn!("Received join to a non-existant room");
|
||||
}
|
||||
} else {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(new_member_id.as_bytes().clone()),
|
||||
body: ServerMessageBody::Join(Chan::Global(room_id.as_bytes().clone())),
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
}
|
||||
},
|
||||
Updates::RoomLeft { room_id, former_member_id } => {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(former_member_id.as_bytes().clone()),
|
||||
body: ServerMessageBody::Part(Chan::Global(room_id.as_bytes().clone())),
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
},
|
||||
Updates::NewMessage { author_id, room_id, body } => {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(author_id.as_bytes().clone()),
|
||||
body: ServerMessageBody::PrivateMessage { target: Recipient::Chan(Chan::Global(room_id.as_bytes().clone())), body: body.as_bytes().to_vec() }
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
},
|
||||
Updates::RoomTopicChanged { room_id, new_topic } => {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.as_bytes().to_vec()),
|
||||
body: ServerMessageBody::N332Topic {
|
||||
client: user.nickname.clone(),
|
||||
chat: Chan::Global(room_id.as_bytes().clone()),
|
||||
topic: new_topic,
|
||||
},
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
},
|
||||
if let Some(update) = update {
|
||||
handle_update(&config, &user, &player_id, writer, &rooms, update).await?;
|
||||
} else {
|
||||
log::warn!("Player is terminated, must terminate the connection");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -281,12 +238,71 @@ async fn handle_registered_socket<'a>(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_update(
|
||||
config: &ServerConfig,
|
||||
user: &RegisteredUser,
|
||||
player_id: &PlayerId,
|
||||
writer: &mut (impl AsyncWrite + Unpin),
|
||||
rooms: &RoomRegistry,
|
||||
update: Updates
|
||||
) -> Result<()> {
|
||||
match update {
|
||||
Updates::RoomJoined { new_member_id, room_id } => {
|
||||
if player_id == &new_member_id {
|
||||
if let Some(room) = rooms.get_room(&room_id) {
|
||||
let room_info = room.get_room_info().await;
|
||||
let chan = Chan::Global(room_id.as_bytes().clone());
|
||||
produce_on_join_cmd_messages(&config, &user, &chan, &room_info, writer).await?;
|
||||
writer.flush().await?;
|
||||
} else {
|
||||
log::warn!("Received join to a non-existant room");
|
||||
}
|
||||
} else {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(new_member_id.as_bytes().clone()),
|
||||
body: ServerMessageBody::Join(Chan::Global(room_id.as_bytes().clone())),
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
}
|
||||
},
|
||||
Updates::RoomLeft { room_id, former_member_id } => {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(former_member_id.as_bytes().clone()),
|
||||
body: ServerMessageBody::Part(Chan::Global(room_id.as_bytes().clone())),
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
},
|
||||
Updates::NewMessage { author_id, room_id, body } => {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(author_id.as_bytes().clone()),
|
||||
body: ServerMessageBody::PrivateMessage { target: Recipient::Chan(Chan::Global(room_id.as_bytes().clone())), body: body.as_bytes().to_vec() }
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
},
|
||||
Updates::RoomTopicChanged { room_id, new_topic } => {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.as_bytes().to_vec()),
|
||||
body: ServerMessageBody::N332Topic {
|
||||
client: user.nickname.clone(),
|
||||
chat: Chan::Global(room_id.as_bytes().clone()),
|
||||
topic: new_topic,
|
||||
},
|
||||
}.write_async(writer).await?;
|
||||
writer.flush().await?
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_incoming_message(
|
||||
buffer: &[u8],
|
||||
config: &ServerConfig,
|
||||
user: &RegisteredUser,
|
||||
user_handle: &mut PlayerConnection,
|
||||
player_id: &PlayerId,
|
||||
writer: &mut (impl AsyncWrite + Unpin),
|
||||
) -> Result<()> {
|
||||
let parsed = client_message(buffer);
|
||||
|
|
Loading…
Reference in New Issue