VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
DtRoleDefinitionStore Class Reference

DtRoleDefinitionStore — process-wide singleton for role definition files.

At startup, initialize() scans a directory of JSON role definition files and builds an index of available definitions. A specific definition is activated via loadDefinition(); the active definition drives WebSocket entity filtering and command authorization for Player sessions.

No definition is active until loadDefinition() is called. When no definition is active, entityMatchesRoles() returns true (permissive default — all entities are visible).

Role definition JSON format (one file per named definition):

{
"name": "blueVsRed_variant1",
"scenarioName": "blue_vs_red",
"allowSelfAssignment": true,
"roles": [
{
"roleName": "Blue",
"description": "Commands and observes blue-force entities",
"commanded": {
"all": false,
"forces": [1],
"uuids": []
},
"viewed": {
"all": false,
"allCommanded": false,
"contactsFromViewed": false,
"forces": [1],
"uuids": [],
"contactsFromForce": [],
"contactsFromUUID": []
},
"overlays": {
"viewAll": false,
"editAll": false,
"viewOverlays": [],
"editOverlays": [],
"defaultOverlay": "",
"viewNonOverlayByForce": [],
"editNonOverlayByForce": []
}
}
]
}

Thread safety: all public methods are safe to call from concurrent threads.

Public Member Functions

void initialize (const std::string &defsDir, const std::string &defaultDefsDir=std::string(), const std::string &scenarioFilename=std::string())
 
std::vector< DtRoleDefSummary > listDefinitions () const
 
bool loadDefinition (const std::string &name)
 
bool saveDefinition (const std::string &name, const Json::Value &defJson, std::string &outError)
 
std::string activeDefinitionName () const
 
std::string activeDefinitionId () const
 
std::string activeDefinitionDescription () const
 
bool addRole (const DtRoleDefinition &role, std::string &outError)
 
bool removeRole (const std::string &roleName, std::string &outError)
 
bool updateRolePolicy (const std::string &roleName, const Json::Value &partialPolicies, std::string &outError)
 
bool updateRoleDescription (const std::string &roleName, const std::string &description, std::string &outError)
 
void setActiveAllowSelfAssignment (bool allow)
 
void clearActiveConfiguration ()
 
bool loadDefinitionById (const std::string &definitionId)
 
bool saveActiveToSourceDefinition (std::string &outError)
 
std::string saveActiveAsNewDefinition (const std::string &name, const std::string &scenarioName, const std::string &description, std::string &outError)
 
bool setDefinitionScenario (const std::string &definitionId, const std::string &scenarioName, std::string &outError)
 
bool setDefinitionDescription (const std::string &definitionId, const std::string &description, std::string &outError)
 
bool deleteDefinition (const std::string &definitionId, std::string &outError)
 
std::vector< DtRoleDefSummary > listDefinitionsForScenario (const std::string &scenarioFilename) const
 
bool getDefinitionById (const std::string &definitionId, Json::Value &outJson, std::string &outError) const
 
std::vector< std::string > roleNames () const
 
bool getRole (const std::string &roleName, DtRoleDefinition &outRole) const
 
std::vector< DtRoleDefinition > activeRoles () const
 
bool allowSelfAssignment () const
 
uint64_t definitionGeneration () const
 
bool entityMatchesRoles (const std::string &objectName, int forceId, const std::vector< std::string > &roleNames) const
 
bool isEntityCommanded (const std::string &objectName, int forceId, const std::vector< std::string > &roleNames) const
 
bool isTacticalGraphicViewable (const std::string &overlayName, int forceId, const std::vector< std::string > &roleNames) const
 
bool isTacticalGraphicEditable (const std::string &overlayName, int forceId, const std::vector< std::string > &roleNames) const
 
std::string defaultOverlayForRoles (const std::vector< std::string > &roleNames) const
 
void setSubordinateResolver (const DtSubordinateResolver *resolver, tbb::spin_rw_mutex *vrfDataMutex=nullptr)
 

Static Public Member Functions

static DtRoleDefinitionStore & getInstance ()
 
static DtRoleDefinition parseRoleJson (const Json::Value &roleJson)
 

Private Member Functions

 DtRoleDefinitionStore ()=default
 
void createDefaultDefinition ()
 
Json::Value serializeActiveDefinition () const
 

Static Private Member Functions

static std::vector< std::string > scanDirectory (const std::string &dir)
 
static std::string generateUuid ()
 

Private Attributes

tbb::spin_rw_mutex myMutex
 
std::string myDefsDir
 
std::map< std::string,
std::string > 
myNameToFilePath
 
std::map< std::string,
DtRoleDefSummary > 
myIndex
 
std::map< std::string,
std::string > 
myIdToName
 
std::string myActiveDefinitionName
 
std::string myActiveDefinitionId
 
std::string myActiveScenarioName
 
std::string myActiveDefinitionDescription
 
std::map< std::string,
DtRoleDefinition > 
myActiveRoles
 
bool myAllowSelfAssignment = true
 
std::atomic< uint64_t > myDefinitionGeneration {0}
 
const DtSubordinateResolver * mySubordinateResolver = nullptr
 
tbb::spin_rw_mutex * myVrfDataMutex = nullptr
 

Constructor & Destructor Documentation

DtRoleDefinitionStore::DtRoleDefinitionStore ( )
privatedefault

Member Function Documentation

static DtRoleDefinitionStore& DtRoleDefinitionStore::getInstance ( )
static

Returns the process-wide singleton instance.

void DtRoleDefinitionStore::initialize ( const std::string &  defsDir,
const std::string &  defaultDefsDir = std::string(),
const std::string &  scenarioFilename = std::string() 
)

Scans defsDir for *.json role definition files and builds the index. Must be called from main() before any background threads start. The scenario directory contributes all definitions. The default directory contributes only the shared "Default" definition. If neither directory contributes any files, the built-in Blue/Red definition is activated.

Active-definition selection order after indexing: 1) A definition whose scenarioName exactly matches scenarioFilename 2) A definition named "Default" (case-insensitive) 3) The first indexed definition (stable fallback)

std::vector<DtRoleDefSummary> DtRoleDefinitionStore::listDefinitions ( ) const

Returns summary information for all available definition files.

bool DtRoleDefinitionStore::loadDefinition ( const std::string &  name)

Loads and activates the named definition. Replaces any previously active definition.

Returns
false if no definition with that name was found in the index.
bool DtRoleDefinitionStore::saveDefinition ( const std::string &  name,
const Json::Value &  defJson,
std::string &  outError 
)

Saves a new role definition to disk and registers it in the index.

Parameters
nameThe unique name for this definition (used as file base name).
defJsonThe complete definition JSON (must contain "name", "roles" fields).
outErrorPopulated with a human-readable error string on failure. Policy: only "Default" may have empty scenarioName. All non-default definitions must include scenarioName.
Returns
false if policy fails, directory is not writable, or name is already taken.
std::string DtRoleDefinitionStore::activeDefinitionName ( ) const

Returns the name of the currently active definition, or an empty string if none.

std::string DtRoleDefinitionStore::activeDefinitionId ( ) const

Returns the definitionId of the currently active definition, or empty string if unsaved.

std::string DtRoleDefinitionStore::activeDefinitionDescription ( ) const

Returns the description of the currently active definition, or empty string if none.

bool DtRoleDefinitionStore::addRole ( const DtRoleDefinition &  role,
std::string &  outError 
)

Adds a role to the active runtime configuration.

Returns
false and populates outError if roleName already exists.
bool DtRoleDefinitionStore::removeRole ( const std::string &  roleName,
std::string &  outError 
)

Removes a role from the active runtime configuration.

Returns
false and populates outError if roleName does not exist.
bool DtRoleDefinitionStore::updateRolePolicy ( const std::string &  roleName,
const Json::Value &  partialPolicies,
std::string &  outError 
)

Deep-merges partial policy fields into an existing role.

Returns
false and populates outError if roleName does not exist or JSON is invalid.
bool DtRoleDefinitionStore::updateRoleDescription ( const std::string &  roleName,
const std::string &  description,
std::string &  outError 
)

Updates the optional user-facing description for an existing role.

Returns
false and populates outError if roleName does not exist.
void DtRoleDefinitionStore::setActiveAllowSelfAssignment ( bool  allow)

Changes the allowSelfAssignment setting in the active configuration.

void DtRoleDefinitionStore::clearActiveConfiguration ( )

Resets the active configuration to empty (no roles, no source definition).

bool DtRoleDefinitionStore::loadDefinitionById ( const std::string &  definitionId)

Loads a definition by its definitionId (UUID) instead of by name.

Returns
false if no definition with that ID is indexed.
bool DtRoleDefinitionStore::saveActiveToSourceDefinition ( std::string &  outError)

Saves the active runtime configuration back to its source definition file.

Returns
false if there is no source definition (activeDefinitionId is empty), or on I/O error.
std::string DtRoleDefinitionStore::saveActiveAsNewDefinition ( const std::string &  name,
const std::string &  scenarioName,
const std::string &  description,
std::string &  outError 
)

Saves the active runtime configuration as a new definition with the given name. Generates a new definitionId. Updates myActiveDefinitionId to the new ID. Policy: scenarioName must be non-empty (scenario must be loaded).

Returns
The new definitionId, or empty string on failure (outError populated).
bool DtRoleDefinitionStore::setDefinitionScenario ( const std::string &  definitionId,
const std::string &  scenarioName,
std::string &  outError 
)

Updates the scenarioName field for an existing saved definition by definitionId. Policy: "Default" may only be unassigned (empty scenarioName). Non-default definitions must keep a non-empty scenarioName.

Returns
false if the ID is not found or the file cannot be updated.
bool DtRoleDefinitionStore::setDefinitionDescription ( const std::string &  definitionId,
const std::string &  description,
std::string &  outError 
)

Updates the description field for an existing saved definition by definitionId.

Returns
false if the ID is not found or the file cannot be updated.
bool DtRoleDefinitionStore::deleteDefinition ( const std::string &  definitionId,
std::string &  outError 
)

Deletes a saved definition file by its definitionId.

Returns
false if the ID is not found or is the active definition's source.
std::vector<DtRoleDefSummary> DtRoleDefinitionStore::listDefinitionsForScenario ( const std::string &  scenarioFilename) const

Lists definitions filtered by scenarioName matching the given filename.

bool DtRoleDefinitionStore::getDefinitionById ( const std::string &  definitionId,
Json::Value &  outJson,
std::string &  outError 
) const

Reads and returns the full JSON content of a saved definition by its definitionId.

Returns
false if not found or on read error.
std::vector<std::string> DtRoleDefinitionStore::roleNames ( ) const

Returns the names of all roles in the active definition. Returns an empty vector if no definition is active.

bool DtRoleDefinitionStore::getRole ( const std::string &  roleName,
DtRoleDefinition &  outRole 
) const

Populates outRole with the definition for the named role in the active definition.

Returns
false if the role name is not in the active definition.
std::vector<DtRoleDefinition> DtRoleDefinitionStore::activeRoles ( ) const

Returns a snapshot of all role definitions in the active definition set.

bool DtRoleDefinitionStore::allowSelfAssignment ( ) const

Returns whether player self-assignment is allowed by the active definition. Returns true when no definition is active.

uint64_t DtRoleDefinitionStore::definitionGeneration ( ) const

Monotonically increasing counter incremented each time a new definition is activated. Listeners compare against a cached value to detect changes without polling.

bool DtRoleDefinitionStore::entityMatchesRoles ( const std::string &  objectName,
int  forceId,
const std::vector< std::string > &  roleNames 
) const

Returns true if the entity passes the visibility filter for at least one of the given roles. Returns true when roleNames is empty or no definition is active (permissive default).

Filter union logic: an entity is visible to a role when any of the following hold:

  • viewed.all is true, OR
  • forceId is in viewed.forces, OR
  • objectName is in viewed.uuids, OR
  • viewed.allCommanded is true AND the entity is commanded by this role.
bool DtRoleDefinitionStore::isEntityCommanded ( const std::string &  objectName,
int  forceId,
const std::vector< std::string > &  roleNames 
) const

Returns true if the entity is commanded by at least one of the given roles. A role commands an entity when: commanded.all is true, OR forceId is in commanded.forces, OR objectName is in commanded.uuids.

bool DtRoleDefinitionStore::isTacticalGraphicViewable ( const std::string &  overlayName,
int  forceId,
const std::vector< std::string > &  roleNames 
) const

Returns true if the tactical graphic is viewable by at least one of the given roles. Returns true when roleNames is empty or no definition is active (permissive default).

  • Non-empty overlayName: visible when overlays.viewAll OR overlayName is in overlays.viewOverlays.
  • Empty overlayName (no overlay): visible when overlays.viewAll OR forceId is in overlays.viewNonOverlayByForce.
bool DtRoleDefinitionStore::isTacticalGraphicEditable ( const std::string &  overlayName,
int  forceId,
const std::vector< std::string > &  roleNames 
) const

Returns true if the tactical graphic is editable by at least one of the given roles. Returns true when roleNames is empty (permissive default).

  • Non-empty overlayName: editable when overlays.editAll OR overlayName is in overlays.editOverlays.
  • Empty overlayName (no overlay): editable when overlays.editAll OR forceId is in overlays.editNonOverlayByForce.
std::string DtRoleDefinitionStore::defaultOverlayForRoles ( const std::vector< std::string > &  roleNames) const

Returns the default overlay name for the first assigned role that specifies one, or an empty string if none of the roles sets a default.

static DtRoleDefinition DtRoleDefinitionStore::parseRoleJson ( const Json::Value &  roleJson)
static

Parses a role definition from a JSON object using the standard role definition file format. Public to allow controllers to reuse the parsing logic without duplicating it.

void DtRoleDefinitionStore::setSubordinateResolver ( const DtSubordinateResolver *  resolver,
tbb::spin_rw_mutex *  vrfDataMutex = nullptr 
)

Sets the subordinate resolver used to expand includeSubordinates entries.

static std::vector<std::string> DtRoleDefinitionStore::scanDirectory ( const std::string &  dir)
staticprivate

Returns full file paths for every *.json file found directly in dir.

void DtRoleDefinitionStore::createDefaultDefinition ( )
private

Activates a built-in default definition containing "Blue" (friendly force) and "Red" (opposing force) roles. Called when no definition files are found in the definitions directory.

static std::string DtRoleDefinitionStore::generateUuid ( )
staticprivate

Generates a random UUID in 8-4-4-4-12 lowercase hex format.

Json::Value DtRoleDefinitionStore::serializeActiveDefinition ( ) const
private

Serializes the active configuration to a complete JSON definition object. Caller must hold at least a read lock on myMutex.

Member Data Documentation

tbb::spin_rw_mutex DtRoleDefinitionStore::myMutex
mutableprivate
std::string DtRoleDefinitionStore::myDefsDir
private
std::map<std::string, std::string> DtRoleDefinitionStore::myNameToFilePath
private

name -> absolute file path

std::map<std::string, DtRoleDefSummary> DtRoleDefinitionStore::myIndex
private

name -> summary

std::map<std::string, std::string> DtRoleDefinitionStore::myIdToName
private

definitionId -> name

std::string DtRoleDefinitionStore::myActiveDefinitionName
private
std::string DtRoleDefinitionStore::myActiveDefinitionId
private

UUID of the currently loaded definition, or empty.

std::string DtRoleDefinitionStore::myActiveScenarioName
private

scenarioName from the loaded file, or empty.

std::string DtRoleDefinitionStore::myActiveDefinitionDescription
private

Description from the loaded file, or empty.

std::map<std::string, DtRoleDefinition> DtRoleDefinitionStore::myActiveRoles
private

roleName -> DtRoleDefinition

bool DtRoleDefinitionStore::myAllowSelfAssignment = true
private
std::atomic<uint64_t> DtRoleDefinitionStore::myDefinitionGeneration {0}
private
const DtSubordinateResolver* DtRoleDefinitionStore::mySubordinateResolver = nullptr
private
tbb::spin_rw_mutex* DtRoleDefinitionStore::myVrfDataMutex = nullptr
private

The documentation for this class was generated from the following file:

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)