diff --git a/Moose Development/Moose/Core/Database.lua b/Moose Development/Moose/Core/Database.lua index 1e8bd40d4..75f7be002 100644 --- a/Moose Development/Moose/Core/Database.lua +++ b/Moose Development/Moose/Core/Database.lua @@ -126,6 +126,8 @@ function DATABASE:New() self:SetEventPriority( 1 ) self:HandleEvent( EVENTS.Birth, self._EventOnBirth ) + -- DCS 2.9 fixed CA event for players -- TODO: reset unit when leaving + self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit ) self:HandleEvent( EVENTS.Dead, self._EventOnDeadOrCrash ) self:HandleEvent( EVENTS.Crash, self._EventOnDeadOrCrash ) self:HandleEvent( EVENTS.RemoveUnit, self._EventOnDeadOrCrash ) @@ -810,6 +812,7 @@ function DATABASE:AddPlayer( UnitName, PlayerName ) self.PLAYERUNITS[PlayerName] = self:FindUnit( UnitName ) self.PLAYERSJOINED[PlayerName] = PlayerName end + end --- Deletes a player from the DATABASE based on the Player Name. @@ -1470,39 +1473,43 @@ function DATABASE:_EventOnDeadOrCrash( Event ) end ---- Handles the OnPlayerEnterUnit event to fill the active players table (with the unit filter applied). +--- Handles the OnPlayerEnterUnit event to fill the active players table for CA units (with the unit filter applied). -- @param #DATABASE self -- @param Core.Event#EVENTDATA Event function DATABASE:_EventOnPlayerEnterUnit( Event ) self:F2( { Event } ) if Event.IniDCSUnit then - if Event.IniObjectCategory == 1 then + -- Player entering a CA slot + if Event.IniObjectCategory == 1 and Event.IniGroup and Event.IniGroup:IsGround() then + + local IsPlayer = Event.IniDCSUnit:getPlayerName() + if IsPlayer then - -- Add unit. - self:AddUnit( Event.IniDCSUnitName ) + -- Debug info. + self:I(string.format("Player '%s' joined GROUND unit '%s' of group '%s'", tostring(Event.IniPlayerName), tostring(Event.IniDCSUnitName), tostring(Event.IniDCSGroupName))) + + local client= self.CLIENTS[Event.IniDCSUnitName] --Wrapper.Client#CLIENT + + -- Add client in case it does not exist already. + if not client then + client=self:AddClient(Event.IniDCSUnitName) + end + + -- Add player. + client:AddPlayer(Event.IniPlayerName) - -- Ini unit. - Event.IniUnit = self:FindUnit( Event.IniDCSUnitName ) - - -- Add group. - self:AddGroup( Event.IniDCSGroupName ) - - -- Get player unit. - local PlayerName = Event.IniDCSUnit:getPlayerName() - - if PlayerName then - - if not self.PLAYERS[PlayerName] then - self:AddPlayer( Event.IniDCSUnitName, PlayerName ) + -- Add player. + if not self.PLAYERS[Event.IniPlayerName] then + self:AddPlayer( Event.IniUnitName, Event.IniPlayerName ) end - local Settings = SETTINGS:Set( PlayerName ) - Settings:SetPlayerMenu( Event.IniUnit ) + -- Player settings. + local Settings = SETTINGS:Set( Event.IniPlayerName ) + Settings:SetPlayerMenu(Event.IniUnit) - else - self:E("ERROR: getPlayerName() returned nil for event PlayerEnterUnit") end + end end end @@ -1513,15 +1520,26 @@ end -- @param Core.Event#EVENTDATA Event function DATABASE:_EventOnPlayerLeaveUnit( Event ) self:F2( { Event } ) - + + local function FindPlayerName(UnitName) + local playername = nil + for _name,_unitname in pairs(self.PLAYERS) do + if _unitname == UnitName then + playername = _name + break + end + end + return playername + end + if Event.IniUnit then if Event.IniObjectCategory == 1 then -- Try to get the player name. This can be buggy for multicrew aircraft! - local PlayerName = Event.IniUnit:GetPlayerName() - - if PlayerName then --and self.PLAYERS[PlayerName] then + local PlayerName = Event.IniUnit:GetPlayerName() or FindPlayerName(Event.IniUnitName) + + if PlayerName then -- Debug info. self:I(string.format("Player '%s' left unit %s", tostring(PlayerName), tostring(Event.IniUnitName)))