-- Advanced  Tools for Mission Editor
local thisModule
local moduleName = "Module5"


local function onCreatePlayer(player)

    local race = ATME.C_Race.getByName("Ma course")
    
    race:addPlayer(player)
end


local function onTimer(events)
    for _id, _ in events:pairs() do
        if events:isCoreEvent(_id) == true then
            thisModule:output(events:getCoreEventType(_id), 1)
            local typeEvent = events:getCoreEventType(_id)
            local datas = events:getCoreEventDatas(_id)
            
            if ATME.isObjectClass(datas.unit, ATME.C_Player) == true then
                if typeEvent == "RACE_START" then
                    datas.unit:display("Départ ...", 5)
                elseif typeEvent == "RACE_TURN_TIME" then
                    local err, h, m, s, d = ATME.convertToHMSd(datas.playTime)
                    local text = string.format("%02d:%02d.%02d", m, s, d)
                    datas.unit:display("Fin du tour N°" .. datas.turnNumber .. 
                                " - Temps : " .. text, 5)
                    
                elseif typeEvent == "RACE_BEST_TURN_TIME" then
                    local err, h, m, s, d = ATME.convertToHMSd(datas.playTime)
                    local text = string.format("%02d:%02d.%02d", m, s, d)
                    datas.race:displayForAllPlayers("Nouveau record du tour pour " ..
                                                    datas.unit:getPseudo() .. " : " .. text,
                                                    5)

                elseif typeEvent == "RACE_PLAYER_BEST_TURN_TIME" then
                    datas.unit:display("Votre meilleur tour", 5)
                    
                elseif typeEvent == "RACE_LAP_TIME" then
                    local err, h, m, s, d = ATME.convertToHMSd(datas.playTime)
                    local text = string.format("%02d:%02d.%02d", m, s, d)
                    datas.unit:display("Porte N°" .. datas.doorNumber .. 
                                       " - Temps : " .. text, 5)
                    
                elseif typeEvent == "RACE_BEST_LAP_TIME" then
                    local err, h, m, s, d = ATME.convertToHMSd(datas.playTime)
                    local text = string.format("%02d:%02d.%02d", m, s, d)
                    datas.race:displayForAllPlayers("Meilleur tps intermédiaire Porte N° " ..
                                                    datas.doorNumber .. " pour " ..
                                                    datas.unit:getPseudo() .. " : " .. text,
                                                    5)
                                
                elseif typeEvent == "RACE_BEST_FINAL_TIME" then
                    datas.race:displayForAllPlayers("Nouveau record pour " ..
                                                    datas.unit:getPseudo(), 5)
                    
                elseif typeEvent == "RACE_PLAYER_BEST_FINAL_TIME" then
                    datas.unit:display("Vous battez votre record de course", 5)
                    
                elseif typeEvent == "RACE_FINAL_TIME" then
                    local err, h, m, s, d = ATME.convertToHMSd(datas.totalPlayTime)
                    local text = "Temps final pour " .. datas.unit:getPseudo() .. " : " ..
                                 string.format("%02d:%02d.%02d", m, s, d) .. "\n"
                    local ranking = datas.race:getRanking()
                    
                    text = text .. "Classement général :\n"
                    for _i, _item in ipairs(ranking) do
                        local err, h, m, s, d = ATME.convertToHMSd(_item.bestTime)
                        local timeText = "--:--:--"
                        if err == false then
                            timeText = string.format("%02d:%02d.%02d", m, s, d)
                        end
                        text = text .. string.format("%2d - ", _i) .. 
                               _item.player:getPseudo() .. " avec " .. timeText .. "\n"
                        
                    end
                    
                    datas.race:displayForAllPlayers(text, 5)
                    
                elseif typeEvent == "RACE_DOOR_MISSED" then
                        datas.unit:display("Porte N°" .. datas.doorNumber .. 
                                           " loupée : pénalité de 10s", 5)
                
                elseif typeEvent == "RACE_PLAYER_TOO_HIGH" then
                    datas.unit:display("Altitude trop élevée : pénalité de 10s", 5)
                end
            end
        end
    end
end

local function startOn(areUnitsAndPlayersInitialized)
    if areUnitsAndPlayersInitialized == false then 
        local myRace = ATME.C_Race("Ma course", "PiquetG", "PiquetD", 2)
        
        myRace:setMaxAltitudeRule(200, 10)
        myRace:setMissedDoorRule(10)
        
        if myRace ~= nil then
            myRace:setLapAtDoor(4)
        end
    end
end


-- MAIN
do
    local newHandlers = {
        onCreatePlayerHandler = onCreatePlayer,
        onDeletePlayerHandler = nil,
        onUpdatePlayerHandler = onUpdatePlayer,
        onTakeoffPlayerHandler = nil,
        onLandingPlayerHandler = nil,
        onStartEnginePlayerHandler = nil,
        onStopEnginePlayerHandler = nil,
        
        onCreateAIUnitHandler = nil,
        onDeleteAIUnitHandler = nil,
        onTakeoffAIUnitHandler = nil,
        onLandingAIUnitHandler = nil,
        onStartEngineAIUnitHandler = nil,
        onStopEngineAIUnitHandler = nil,
        
        onCreateGroupHandler = nil,
        onDeleteGroupHandler = nil,
        onDisableGroupHandler = nil,
        
        onCreateStaticObjectHandler = nil,
        onDeleteStaticObjectHandler = nil,
        
        onTimerHandler = onTimer,
        onModuleStartHandler = startOn,
    }
    
    thisModule = ATME.C_Module(moduleName, newHandlers, true)
end
