Skip to content

Commit

Permalink
implement GetUnitPosition method
Browse files Browse the repository at this point in the history
implement GetUnits method

implement GetGroups method
  • Loading branch information
rkusa committed Jul 29, 2021
1 parent 159df8d commit 10d481f
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 24 deletions.
24 changes: 12 additions & 12 deletions lua/exporters/object.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
local GRPC = GRPC
local coord = coord

local function toLatLonPosition(pos)
local lat, lon, alt = coord.LOtoLL(pos)
return {
lat = lat,
lon = lon,
alt = alt,
}
end

GRPC.exporters.unit = function(unit)
return {
id = tonumber(unit:getID()),
name = unit:getName(),
callsign = unit:getCallsign(),
coalition = unit:getCoalition(),
type = unit:getTypeName(),
position = toLatLonPosition(unit:getPoint()),
position = GRPC.toLatLonPosition(unit:getPoint()),
playerName = unit:getPlayerName()
}
end

GRPC.exporters.group = function(group)
return {
id = tonumber(group:getID()),
name = group:getName(),
coalition = group:getCoalition(),
category = group:getCategory(),
}
end

GRPC.exporters.weapon = function(weapon)
return {
id = tonumber(weapon:getName()),
type = weapon:getTypeName(),
position = toLatLonPosition(weapon:getPoint()),
position = GRPC.toLatLonPosition(weapon:getPoint()),
}
end

Expand All @@ -48,7 +48,7 @@ GRPC.exporters.airbase = function(airbase)
coalition = airbase:getCoalition(),
category = airbase:getDesc()['category'],
displayName = airbase:getDesc()['displayName'],
position = toLatLonPosition(airbase:getPoint())
position = GRPC.toLatLonPosition(airbase:getPoint())
}

if airbase:getUnit() then
Expand Down
7 changes: 4 additions & 3 deletions lua/grpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ end
--

GRPC.methods = {}
dofile(GRPC.basePath .. [[methods\coalitions.lua]])
dofile(GRPC.basePath .. [[methods\atmosphere.lua]])
dofile(GRPC.basePath .. [[methods\coalitions.lua]])
dofile(GRPC.basePath .. [[methods\custom.lua]])
dofile(GRPC.basePath .. [[methods\event_stream.lua]])
dofile(GRPC.basePath .. [[methods\group.lua]])
dofile(GRPC.basePath .. [[methods\trigger.lua]])
dofile(GRPC.basePath .. [[methods\unit.lua]])
dofile(GRPC.basePath .. [[methods\world.lua]])
dofile(GRPC.basePath .. [[methods\custom.lua]])
dofile(GRPC.basePath .. [[methods\event_stream.lua]])

--
-- RPC request handler
Expand Down
13 changes: 12 additions & 1 deletion lua/methods/coalitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ GRPC.methods.getPlayers = function(params)
result[i] = GRPC.exporters.unit(unit)
end
return GRPC.success({units = result})
end

end
GRPC.methods.getGroups = function(params)
-- https://wiki.hoggitworld.com/view/DCS_func_getGroups
local groups = coalition.getGroups(params.coalition, params.category)

local result = {}
for i, group in ipairs(groups) do
result[i] = GRPC.exporters.group(group)
end

return GRPC.success({groups = result})
end
24 changes: 24 additions & 0 deletions lua/methods/group.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--
-- RPC unit actions
-- https://wiki.hoggitworld.com/view/DCS_Class_Group
--

local GRPC = GRPC

GRPC.methods.getUnits = function(params)
-- https://wiki.hoggitworld.com/view/DCS_func_getByName
local group = Group.getByName(params.groupName)
if group == nil then
return GRPC.errorNotFound("group does not exist")
end

-- https://wiki.hoggitworld.com/view/DCS_func_getUnits
local units = group:getUnits()

local result = {}
for i, unit in ipairs(units) do
result[i] = GRPC.exporters.unit(unit)
end

return GRPC.success({units = result})
end
15 changes: 14 additions & 1 deletion lua/methods/unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ GRPC.methods.getRadar = function(params)
active = active,
target = grpcTable
})
end
end

GRPC.methods.getUnitPosition = function(params)
-- https://wiki.hoggitworld.com/view/DCS_func_getByName
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit does not exist")
end

return GRPC.success({
-- https://wiki.hoggitworld.com/view/DCS_func_getPoint
position = GRPC.toLatLonPosition(unit:getPoint()),
})
end
11 changes: 10 additions & 1 deletion protos/coalitions.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ message GetPlayersRequest {

message GetPlayersResponse {
repeated Unit units = 1;
}
}

message GetGroupsRequest {
Coalition coalition = 1;
optional Group.GroupCategory category = 2;
}

message GetGroupsResponse {
repeated Group groups = 1;
}
15 changes: 14 additions & 1 deletion protos/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ message Unit {
optional string player_name = 7;
}

message Group {
enum GroupCategory {
AIRPLANE = 0;
HELICOPTER = 1;
GROUND = 2;
SHIP = 3;
TRAIN = 4;
}

uint32 id = 1;
string name = 2;
}

message Weapon {
uint32 id = 1;
string type = 2;
Expand Down Expand Up @@ -82,4 +95,4 @@ message Target {
}
}

// End of Object subtypes
// End of Object subtypes
20 changes: 16 additions & 4 deletions protos/dcs.proto
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
syntax = "proto3";

import "atmosphere.proto";
import "coalitions.proto";
import "common.proto";
import "custom.proto";
import "event_stream.proto";
import "group.proto";
import "trigger.proto";
import "unit.proto";
import "world.proto";
import "custom.proto";
import "common.proto";
import "coalitions.proto";

package dcs;

Expand All @@ -25,6 +26,9 @@ service Atmosphere {
service Coalitions {
// https://wiki.hoggitworld.com/view/DCS_func_getPlayers
rpc GetPlayers(GetPlayersRequest) returns (GetPlayersResponse) {}

// https://wiki.hoggitworld.com/view/DCS_func_getGroups
rpc GetGroups(GetGroupsRequest) returns (GetGroupsResponse) {}
}

service Custom {
Expand All @@ -42,7 +46,7 @@ service Mission {

// Streams unit updates
// TODO https://github.com/DCS-gRPC/rust-server/issues/8
// Envisioned RPC Signature:
// Envisioned RPC Signature:
// rpc StreamUnits(StreamUnitsRequest) returns (stream Unit) {}
}

Expand Down Expand Up @@ -87,9 +91,17 @@ service Triggers {
rpc SignalFlare(SignalFlareRequest) returns (EmptyResponse) {}
}

service Groups {
// https://wiki.hoggitworld.com/view/DCS_func_getUnits
rpc GetUnits(GetUnitsRequest) returns (GetUnitsResponse) {}
}

service Units {
// https://wiki.hoggitworld.com/view/DCS_func_getRadar
rpc GetRadar(GetRadarRequest) returns (GetRadarResponse) {}

// https://wiki.hoggitworld.com/view/DCS_func_getPoint
rpc GetPosition(GetUnitPositionRequest) returns (GetUnitPositionResponse) {}
}

service World {
Expand Down
13 changes: 13 additions & 0 deletions protos/group.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

import "common.proto";

package dcs;

message GetUnitsRequest {
string group_name = 2;
}

message GetUnitsResponse {
repeated Unit units = 1;
}
10 changes: 9 additions & 1 deletion protos/unit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ message GetRadarResponse {
Airbase airbase = 7;
Cargo cargo = 8;
}
}
}

message GetUnitPositionRequest {
string name = 1;
}

message GetUnitPositionResponse {
Position position = 1;
}
28 changes: 28 additions & 0 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::shutdown::{AbortableStream, ShutdownHandle};
use dcs::atmosphere_server::Atmosphere;
use dcs::coalitions_server::Coalitions;
use dcs::custom_server::Custom;
use dcs::groups_server::Groups;
use dcs::mission_server::Mission;
use dcs::triggers_server::Triggers;
use dcs::units_server::Units;
Expand Down Expand Up @@ -223,6 +224,25 @@ impl Coalitions for RPC {
let res: GetPlayersResponse = self.request("getPlayers", request).await?;
Ok(Response::new(res))
}

async fn get_groups(
&self,
request: Request<GetGroupsRequest>,
) -> Result<Response<GetGroupsResponse>, Status> {
let res: GetGroupsResponse = self.request("getGroups", request).await?;
Ok(Response::new(res))
}
}

#[tonic::async_trait]
impl Groups for RPC {
async fn get_units(
&self,
request: Request<GetUnitsRequest>,
) -> Result<Response<GetUnitsResponse>, Status> {
let res: GetUnitsResponse = self.request("getUnits", request).await?;
Ok(Response::new(res))
}
}

#[tonic::async_trait]
Expand All @@ -234,6 +254,14 @@ impl Units for RPC {
let res: GetRadarResponse = self.request("getRadar", request).await?;
Ok(Response::new(res))
}

async fn get_position(
&self,
request: Request<GetUnitPositionRequest>,
) -> Result<Response<GetUnitPositionResponse>, Status> {
let res: GetUnitPositionResponse = self.request("getUnitPosition", request).await?;
Ok(Response::new(res))
}
}

#[tonic::async_trait]
Expand Down

0 comments on commit 10d481f

Please sign in to comment.