Skip to content

Commit

Permalink
feat(grpc-api): Расширенная информация о пользователях в комнаде.
Browse files Browse the repository at this point in the history
  • Loading branch information
avkviring committed Apr 24, 2024
1 parent 11b715f commit 84fac9d
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 262 deletions.
26 changes: 20 additions & 6 deletions proto/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ service RealtimeServerManagementService {
*/
rpc DeleteRoom(DeleteRoomRequest) returns (DeleteRoomResponse);

rpc GetRoomsMembersCount(EmptyRequest) returns(GetRoomsMembersCountResponse);
rpc GetRoomsMembers(EmptyRequest) returns(GetRoomsMembersResponse);
}


Expand Down Expand Up @@ -135,15 +135,29 @@ message GetRoomsResponse {
repeated uint64 rooms = 1;
}

message GetRoomsMembersCountResponse {
repeated RoomMembersCountResponse rooms = 1;
message GetRoomsMembersResponse {
repeated RoomMembersResponse rooms = 1;
}

message RoomMembersCountResponse {
message RoomMembersResponse {
uint64 room = 1;
uint32 members = 2;
uint32 connected_members = 3;
repeated Member members = 2;
}

message Member {
uint64 id = 1;
MemberStatus status = 2;
}

enum MemberStatus {
Created = 0;
CreatedNotConnectedAndDeleted = 1;
Connected = 2;
Attached = 3;
Disconnected = 4;
Detached = 5;
}

enum FieldType {
Long = 0;
Double = 1;
Expand Down
25 changes: 24 additions & 1 deletion rust/Server/src/server/manager/grpc/from.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::server::manager::grpc;
use crate::server::manager::grpc::proto;
use crate::server::manager::grpc::proto::field_value::Variant;
use crate::server::manager::grpc::proto::{GameObjectConfig, GameObjectTemplate, ItemConfig};
use crate::server::manager::grpc::proto::{GameObjectConfig, GameObjectTemplate, ItemConfig, Member, MemberStatus};
use crate::server::room::config::{member, object, room};
use crate::server::room::member::{RoomMember, RoomMemberStatus};
use cheetah_common::room::access::AccessGroups;
use cheetah_common::room::buffer::Buffer;
use cheetah_common::room::field::FieldId;
Expand Down Expand Up @@ -73,3 +74,25 @@ impl From<GameObjectTemplate> for object::GameObjectCreateParams {
}
}
}

impl From<RoomMember> for Member {
fn from(value: RoomMember) -> Self {
Member {
id: value.id,
status: MemberStatus::from(value.status).into(),
}
}
}

impl From<RoomMemberStatus> for MemberStatus {
fn from(value: RoomMemberStatus) -> Self {
match value {
RoomMemberStatus::Created => MemberStatus::Created,
RoomMemberStatus::CreatedNotConnectedAndDeleted => MemberStatus::CreatedNotConnectedAndDeleted,
RoomMemberStatus::Connected => MemberStatus::Connected,
RoomMemberStatus::Attached => MemberStatus::Attached,
RoomMemberStatus::Detached => MemberStatus::Detached,
RoomMemberStatus::Disconnected => MemberStatus::Disconnected,
}
}
}
47 changes: 29 additions & 18 deletions rust/Server/src/server/manager/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use crate::server::manager::grpc::proto::realtime_server_management_service_server::RealtimeServerManagementService;
use crate::server::manager::grpc::proto::{
CreateMemberRequest, CreateMemberResponse, CreateSuperMemberRequest, DeleteMemberRequest, DeleteMemberResponse, DeleteRoomRequest, DeleteRoomResponse, EmptyRequest, GetRoomsMembersCountResponse,
GetRoomsResponse, ProbeRequest, ProbeResponse, RoomIdResponse, RoomMembersCountResponse, RoomTemplate,
};
use crate::server::manager::grpc::proto::CreateMemberRequest;
use crate::server::manager::grpc::proto::CreateMemberResponse;
use crate::server::manager::grpc::proto::CreateSuperMemberRequest;
use crate::server::manager::grpc::proto::DeleteMemberRequest;
use crate::server::manager::grpc::proto::DeleteMemberResponse;
use crate::server::manager::grpc::proto::DeleteRoomRequest;
use crate::server::manager::grpc::proto::DeleteRoomResponse;
use crate::server::manager::grpc::proto::EmptyRequest;
use crate::server::manager::grpc::proto::GetRoomsMembersResponse;
use crate::server::manager::grpc::proto::GetRoomsResponse;
use crate::server::manager::grpc::proto::ProbeRequest;
use crate::server::manager::grpc::proto::ProbeResponse;
use crate::server::manager::grpc::proto::RoomIdResponse;
use crate::server::manager::grpc::proto::RoomMembersResponse;
use crate::server::manager::grpc::proto::RoomTemplate;
use crate::server::manager::{ManagementTaskError, ManagementTaskExecutionError};
use crate::server::room::command::ServerCommandError;
use crate::server::room::config::member::MemberCreateParams;
Expand Down Expand Up @@ -109,19 +120,18 @@ impl RealtimeServerManagementService for RealtimeServerManagementServiceImpl {
server.delete_room(room_id).map(|_| Response::new(DeleteRoomResponse {})).map_err(Status::from)
}

async fn get_rooms_members_count(&self, _request: Request<EmptyRequest>) -> Result<Response<GetRoomsMembersCountResponse>, Status> {
async fn get_rooms_members(&self, _request: Request<EmptyRequest>) -> Result<Response<GetRoomsMembersResponse>, Status> {
self.server_manager
.lock()
.await
.get_rooms_member_count()
.map(|rooms| {
Response::new(GetRoomsMembersCountResponse {
Response::new(GetRoomsMembersResponse {
rooms: rooms
.into_iter()
.map(|r| RoomMembersCountResponse {
.map(|r| RoomMembersResponse {
room: r.room_id,
members: r.members as u32,
connected_members: r.connected_members as u32,
members: r.members.into_iter().map(From::from).collect(),
})
.collect(),
})
Expand Down Expand Up @@ -150,7 +160,7 @@ impl From<ManagementTaskError> for Status {
#[cfg(test)]
mod test {
use crate::server::manager::grpc::proto::realtime_server_management_service_server::RealtimeServerManagementService;
use crate::server::manager::grpc::proto::{DeleteMemberRequest, DeleteRoomRequest, EmptyRequest, RoomMembersCountResponse};
use crate::server::manager::grpc::proto::{DeleteMemberRequest, DeleteRoomRequest, EmptyRequest, Member, MemberStatus, RoomMembersResponse};
use crate::server::manager::grpc::{RealtimeServerManagementServiceImpl, SUPER_MEMBER_KEY_ENV};
use crate::server::manager::ServerManager;
use crate::server::room::config::member::MemberCreateParams;
Expand Down Expand Up @@ -181,7 +191,7 @@ mod test {
let service = RealtimeServerManagementServiceImpl::new(Arc::clone(&server_manager));
let room_1 = server_manager.lock().await.create_room(Default::default()).unwrap();
let room_2 = server_manager.lock().await.create_room(Default::default()).unwrap();
server_manager
let member_id = server_manager
.lock()
.await
.create_member(
Expand All @@ -195,17 +205,18 @@ mod test {
)
.unwrap();

let rooms_response = service.get_rooms_members_count(Request::new(EmptyRequest::default())).await.unwrap();
let rooms_response = service.get_rooms_members(Request::new(EmptyRequest::default())).await.unwrap();
let rooms = rooms_response.get_ref();
assert!(rooms.rooms.contains(&RoomMembersCountResponse {
assert!(rooms.rooms.contains(&RoomMembersResponse {
room: room_1,
members: 1,
connected_members: 0
members: vec![Member {
id: member_id,
status: MemberStatus::Created.into()
}],
}));
assert!(rooms.rooms.contains(&RoomMembersCountResponse {
assert!(rooms.rooms.contains(&RoomMembersResponse {
room: room_2,
members: 0,
connected_members: 0
members: Default::default(),
}));
assert_eq!(rooms.rooms.len(), 2);
}
Expand Down
46 changes: 26 additions & 20 deletions rust/Server/src/server/manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
pub mod grpc;
use crate::server::room::command::ServerCommandError;
use crate::server::room::config::member::MemberCreateParams;
use crate::server::room::config::room::RoomCreateParams;
use crate::server::room::Room;
use crate::server::room_registry::RoomNotFoundError;
use crate::server::Server;
use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration;
use cheetah_game_realtime_protocol::others::member_id::MemberAndRoomId;
use cheetah_game_realtime_protocol::{RoomId, RoomMemberId};
use std::net::UdpSocket;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{RecvTimeoutError, SendError, Sender};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration;
use cheetah_game_realtime_protocol::others::member_id::MemberAndRoomId;
use cheetah_game_realtime_protocol::{RoomId, RoomMemberId};
use thiserror::Error;

use crate::server::room::command::ServerCommandError;
use crate::server::room::config::member::MemberCreateParams;
use crate::server::room::config::room::RoomCreateParams;
use crate::server::room::member::RoomMember;
use crate::server::room::Room;
use crate::server::room_registry::RoomNotFoundError;
use crate::server::Server;

pub mod grpc;

///
/// Управление сервером
/// - запуск сервера в отдельном потоке
Expand All @@ -34,7 +38,7 @@ pub enum ManagementTask {
Dump(RoomId),
GetRooms,
GetCreatedRoomsCount,
GetRoomsMemberCount,
GetRoomsMembers,
DeleteRoom(RoomId),
}

Expand All @@ -45,16 +49,15 @@ pub enum ManagementTaskResult {
DeleteMember,
Dump(Option<Room>),
GetRooms(Vec<RoomId>),
GetRoomsMemberCount(Vec<RoomMembersCount>),
GetRoomsMemberCount(Vec<RoomMembers>),
GetCreatedRoomsCount(usize),
DeleteRoom,
}

#[derive(Debug)]
pub struct RoomMembersCount {
pub struct RoomMembers {
pub room_id: RoomId,
pub members: usize,
pub connected_members: usize,
pub members: Vec<RoomMember>,
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -139,8 +142,8 @@ impl ServerManager {
})?
}

pub(crate) fn get_rooms_member_count(&self) -> Result<Vec<RoomMembersCount>, ManagementTaskError> {
self.execute_task(ManagementTask::GetRoomsMemberCount).map(|res| {
pub(crate) fn get_rooms_member_count(&self) -> Result<Vec<RoomMembers>, ManagementTaskError> {
self.execute_task(ManagementTask::GetRoomsMembers).map(|res| {
if let ManagementTaskResult::GetRoomsMemberCount(rooms) = res {
Ok(rooms)
} else {
Expand Down Expand Up @@ -210,12 +213,15 @@ impl ServerManager {

#[cfg(test)]
mod test {
use std::time::Duration;

use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration;

use cheetah_common::network::bind_to_free_socket;

use crate::server::manager::ServerManager;
use crate::server::room::config::member::MemberCreateParams;
use crate::server::room::config::room::RoomCreateParams;
use cheetah_common::network::bind_to_free_socket;
use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration;
use std::time::Duration;

#[test]
fn should_get_rooms() {
Expand Down
9 changes: 4 additions & 5 deletions rust/Server/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::server::manager::{ManagementTask, ManagementTaskChannel, ManagementTaskExecutionError, ManagementTaskResult, RoomMembersCount};
use crate::server::manager::{ManagementTask, ManagementTaskChannel, ManagementTaskExecutionError, ManagementTaskResult, RoomMembers};
use crate::server::measurer::Measurer;
use crate::server::network::Network;
use crate::server::room::command::ServerCommandError;
Expand Down Expand Up @@ -91,13 +91,12 @@ impl Server {
ManagementTask::DeleteMember(id) => self.delete_member(id).map(|_| ManagementTaskResult::DeleteMember)?,
ManagementTask::Dump(room_id) => ManagementTaskResult::Dump(self.rooms.get(&room_id).cloned()),
ManagementTask::GetRooms => ManagementTaskResult::GetRooms(self.rooms.rooms().map(|r| r.0).copied().collect()),
ManagementTask::GetRoomsMemberCount => ManagementTaskResult::GetRoomsMemberCount(
ManagementTask::GetRoomsMembers => ManagementTaskResult::GetRoomsMemberCount(
self.rooms
.rooms()
.map(|(room_id, room)| RoomMembersCount {
.map(|(room_id, room)| RoomMembers {
room_id: *room_id,
members: room.members.len(),
connected_members: room.members.iter().filter(|p| p.1.connected).count(),
members: room.members.iter().map(|i| i.1.clone()).collect(),
})
.collect(),
),
Expand Down
5 changes: 2 additions & 3 deletions rust/Server/src/server/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ mod tests {

use crate::server::network::Network;
use crate::server::room::config::member::MemberCreateParams;
use crate::server::room::member::RoomMember;
use crate::server::room::member::{RoomMember, RoomMemberStatus};
use crate::server::room_registry::Rooms;
use cheetah_common::network::bind_to_free_socket;
use cheetah_game_realtime_protocol::codec::cipher::Cipher;
Expand Down Expand Up @@ -296,8 +296,7 @@ mod tests {
let member_template = MemberCreateParams::new_member(Default::default(), Default::default());
let member = RoomMember {
id: 100,
connected: false,
attached: false,
status: RoomMemberStatus::Created,
template: member_template.clone(),
out_commands: Default::default(),
};
Expand Down
2 changes: 1 addition & 1 deletion rust/Server/src/server/room/command/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod tests {
#[test]
fn should_create() {
let (mut room, member_id) = setup(AccessGroups(0b11));
room.mark_as_connected_in_test(member_id).unwrap();
room.mark_as_attached_in_test(member_id).unwrap();

let object_id = GameObjectId::new(1, GameObjectOwner::Member(member_id));
let command = CreateGameObject {
Expand Down
4 changes: 2 additions & 2 deletions rust/Server/src/server/room/command/created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ mod tests {
#[test]
pub(crate) fn should_send_commands() {
let (mut room, object_id, member1, member2) = setup_two_players();
room.mark_as_connected_in_test(member1).unwrap();
room.mark_as_connected_in_test(member2).unwrap();
room.mark_as_attached_in_test(member1).unwrap();
room.mark_as_attached_in_test(member2).unwrap();
let command = C2SCreatedGameObject::new(object_id, false, None);
created_object(&command, &mut room, member1).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions rust/Server/src/server/room/command/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ mod tests {
let mut room = Room::new(0, template);
let member_a_id = room.register_member(MemberCreateParams::stub(access_groups));
let member_b_id = room.register_member(MemberCreateParams::stub(access_groups));
room.mark_as_connected_in_test(member_a_id).unwrap();
room.mark_as_connected_in_test(member_b_id).unwrap();
room.mark_as_attached_in_test(member_a_id).unwrap();
room.mark_as_attached_in_test(member_b_id).unwrap();

let object_id = room.test_create_object_with_created_state(GameObjectOwner::Member(member_a_id), access_groups, Default::default()).id;
room.test_out_commands.clear();
Expand Down
6 changes: 3 additions & 3 deletions rust/Server/src/server/room/command/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ mod tests {
let member2 = room.register_member(MemberCreateParams::stub(access_groups));
let member3 = room.register_member(MemberCreateParams::stub(access_groups));

room.mark_as_connected_in_test(member1).unwrap();
room.mark_as_connected_in_test(member2).unwrap();
room.mark_as_connected_in_test(member3).unwrap();
room.mark_as_attached_in_test(member1).unwrap();
room.mark_as_attached_in_test(member2).unwrap();
room.mark_as_attached_in_test(member3).unwrap();

let object = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member1), access_groups, Default::default());
object.created = true;
Expand Down
11 changes: 6 additions & 5 deletions rust/Server/src/server/room/command/room.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::server::room::command::ServerCommandError;
use crate::server::room::member::RoomMemberStatus;
use crate::server::room::object::S2CCommandsCollector;
use crate::server::room::Room;
use cheetah_common::room::object::GameObjectTemplateId;
use cheetah_game_realtime_protocol::RoomMemberId;

pub fn attach_to_room(room: &mut Room, member_id: RoomMemberId) -> Result<(), ServerCommandError> {
let member = room.get_member_mut(&member_id)?;
member.attached = true;
member.status = RoomMemberStatus::Attached;
let access_group = member.template.groups;
let mut command_collector = Vec::<(GameObjectTemplateId, S2CCommandsCollector)>::new();
room.objects
Expand All @@ -27,7 +28,7 @@ pub fn attach_to_room(room: &mut Room, member_id: RoomMemberId) -> Result<(), Se

pub fn detach_from_room(room: &mut Room, member_id: RoomMemberId) -> Result<(), ServerCommandError> {
let member = room.get_member_mut(&member_id)?;
member.attached = false;
member.status = RoomMemberStatus::Detached;
Ok(())
}

Expand All @@ -50,8 +51,8 @@ mod tests {
let groups_b = AccessGroups(0b10);
let member_b = room.register_member(MemberCreateParams::stub(groups_b));

room.mark_as_connected_in_test(member_a).unwrap();
room.mark_as_connected_in_test(member_b).unwrap();
room.mark_as_attached_in_test(member_a).unwrap();
room.mark_as_attached_in_test(member_b).unwrap();

let object_a_1 = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member_b), groups_a, Default::default());
object_a_1.created = true;
Expand All @@ -78,7 +79,7 @@ mod tests {
let mut room = Room::new(0, template);
let groups = AccessGroups(0b100);
let member = room.register_member(MemberCreateParams::stub(groups));
room.mark_as_connected_in_test(member).unwrap();
room.mark_as_attached_in_test(member).unwrap();

let object = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member), groups, Default::default());
object.created = true;
Expand Down
Loading

0 comments on commit 84fac9d

Please sign in to comment.