Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing GroupCategory enumeration #112

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `ForcePlayerSlot` API
- `PlayerChangeSlotEvent` emitted when player changes slot
- `StreamUnits` can optionally specify the `category` of the units which may be monitored.

### Fixed
- Corrected `proto` files from camel-casing to snake-casing; not a runtime breaking change but some code generators
Expand All @@ -17,6 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Corrected `proto` files with enumerations to be named correct; compiler-only breaking change, not runtime.
- `coalition.proto` - `AddGroupRequest.Point` - enum `Type` has been renamed to `PointType`
- `coalition.proto` - `AddGroupRequest` - enum members of `Skill` has been prefixed with `SKILL_`
- `StreamUnits` would only monitor the `Plane` groups; now monitors all groups with the default option of `GROUP_CATEGORY_UNSPECIFIED`

### Breaking Changes
- Added `GROUP_CATEGORY_UNSPECIFIED` to `dcs.v0.common.GroupCategory`; breaking change as all indexes have changed.



## [0.3.0] - 2022-01-14
### Added
Expand Down
4 changes: 2 additions & 2 deletions lua/DCS-gRPC/exporters/object.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ GRPC.exporters.unit = function(unit)
numberInGroup = unit:getNumber(),
heading = heading,
speed = speed,
category = unit:getGroup():getCategory(),
category = unit:getGroup():getCategory() + 1, -- Increment for non zero-indexed gRPC enum
}
end

Expand All @@ -55,7 +55,7 @@ GRPC.exporters.group = function(group)
id = tonumber(group:getID()),
name = group:getName(),
coalition = group:getCoalition() + 1, -- Increment for non zero-indexed gRPC enum
category = group:getCategory(),
category = group:getCategory() + 1, -- Increment for non zero-indexed gRPC enum
}
end

Expand Down
13 changes: 11 additions & 2 deletions lua/DCS-gRPC/methods/coalitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ local createGroundGroupTemplate = function(groupTemplate)
end

GRPC.methods.addGroup = function(params)
if params.groupCategory == 0 then
return GRPC.errorInvalidArgument("group category must be specified")
end
if params.country_id == 0 or params.country_id == 15 then
return GRPC.errorInvalidArgument("invalid country code")
end

local template = createGroundGroupTemplate(params.template.groundTemplate)

coalition.addGroup(params.country - 1, params.groupCategory, template) -- Decrement for non zero-indexed gRPC enum
coalition.addGroup(params.country - 1, params.groupCategory - 1, template) -- Decrement for non zero-indexed gRPC enum

return GRPC.success({group = GRPC.exporters.group(Group.getByName(template.name))})
end
Expand All @@ -122,7 +125,13 @@ GRPC.methods.getGroups = function(params)
for _, c in pairs(coalition.side) do
if params.coalition == 0 or params.coalition - 1 == c then -- Decrement for non zero-indexed gRPC enum
-- https://wiki.hoggitworld.com/view/DCS_func_getGroups
local groups = coalition.getGroups(c, params.category)
local getFilteredGroups = function()
if params.category == 0 then
return coalition.getGroups(c)
end
return coalition.getGroups(c, params.category - 1)
end
local groups = getFilteredGroups()

for _, group in ipairs(groups) do
table.insert(result, GRPC.exporters.group(group))
Expand Down
2 changes: 1 addition & 1 deletion protos/dcs/coalition/v0/coalition.proto
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ message AddGroupResponse {

message GetGroupsRequest {
dcs.common.v0.Coalition coalition = 1;
optional dcs.common.v0.GroupCategory category = 2;
dcs.common.v0.GroupCategory category = 2;
}

message GetGroupsResponse {
Expand Down
11 changes: 6 additions & 5 deletions protos/dcs/common/v0/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ message Group {
* Group category enumerator.
*/
enum GroupCategory {
GROUP_CATEGORY_AIRPLANE = 0;
GROUP_CATEGORY_HELICOPTER = 1;
GROUP_CATEGORY_GROUND = 2;
GROUP_CATEGORY_SHIP = 3;
GROUP_CATEGORY_TRAIN = 4;
GROUP_CATEGORY_UNSPECIFIED = 0;
GROUP_CATEGORY_AIRPLANE = 1;
GROUP_CATEGORY_HELICOPTER = 2;
GROUP_CATEGORY_GROUND = 3;
GROUP_CATEGORY_SHIP = 4;
GROUP_CATEGORY_TRAIN = 5;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions protos/dcs/mission/v0/mission.proto
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ message StreamUnitsRequest {
// poll rate for stationary units. Set it to the same value as `poll_rate` to
// disable the backoff. Default: 30
optional uint32 max_backoff = 2;

// The type of the unit to stream movements. Different categories of units
// would move at different speeds, which allows the stream to be configured
// with the appropriate polling rates. `GROUP_CATEGORY_UNSPECIFIED` would
// return all the units.
dcs.common.v0.GroupCategory category = 3;
}

message StreamUnitsResponse {
Expand Down
23 changes: 17 additions & 6 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures_util::TryFutureExt;
use stubs::coalition::v0::coalition_service_server::CoalitionService;
use stubs::coalition::v0::GetGroupsRequest;
use stubs::common;
use stubs::common::v0::{Coalition, Position, Unit};
use stubs::common::v0::{Coalition, GroupCategory, Position, Unit};
use stubs::group::v0::group_service_server::GroupService;
use stubs::group::v0::GetUnitsRequest;
use stubs::mission::v0::stream_events_response::Event;
Expand All @@ -30,6 +30,7 @@ pub async fn stream_units(
let poll_rate = opts.poll_rate.unwrap_or(5);
let max_backoff = Duration::from_secs(opts.max_backoff.unwrap_or(30).max(poll_rate) as u64);
let poll_rate = Duration::from_secs(poll_rate as u64);
let category = GroupCategory::from_i32(opts.category).unwrap_or(GroupCategory::Unspecified);
let mut state = State {
units: HashMap::new(),
ctx: Context {
Expand All @@ -48,7 +49,7 @@ pub async fn stream_units(
.rpc
.get_groups(Request::new(GetGroupsRequest {
coalition: coalition.into(),
category: None,
category: opts.category,
}))
.map_ok(|res| res.into_inner().groups)
}),
Expand Down Expand Up @@ -98,7 +99,7 @@ pub async fn stream_units(
tokio::select! {
// listen to events that update the current state
Some(stubs::mission::v0::StreamEventsResponse { event: Some(event), .. }) = events.next() => {
handle_event(&mut state, event).await?;
handle_event(&mut state, event, category).await?;
}

// poll units for updates
Expand All @@ -125,7 +126,11 @@ struct Context {
}

/// Update the given [State] based on the given [Event].
async fn handle_event(state: &mut State, event: Event) -> Result<(), Error> {
async fn handle_event(
state: &mut State,
event: Event,
category: GroupCategory,
) -> Result<(), Error> {
match event {
Event::Birth(BirthEvent {
initiator:
Expand All @@ -134,8 +139,14 @@ async fn handle_event(state: &mut State, event: Event) -> Result<(), Error> {
}),
..
}) => {
state.ctx.tx.send(Ok(Update::Unit(unit.clone()))).await?;
state.units.insert(unit.name.clone(), UnitState::new(unit));
// if we are monitoring all the categories, let's watch it.
// otherwise, we need to be selective on the units we are monitoring
let unit_category =
GroupCategory::from_i32(unit.category).unwrap_or(GroupCategory::Unspecified);
if category == unit_category || category == GroupCategory::Unspecified {
state.ctx.tx.send(Ok(Update::Unit(unit.clone()))).await?;
state.units.insert(unit.name.clone(), UnitState::new(unit));
}
}

// The dead event is known to not fire reliably in certain cases. This is fine here, because
Expand Down