/*
* This mode uses the TMGL World Cup mode and removes all references to it
*/
#Extends "Modes/TrackMania/Deprecated/TM_Final86TMGL_Online.Script.txt"
// #RequireContext CSmMode

#Include "Libs/Nadeo/Trackmania/Modes/UIModules/TMGLMarkers_Server.Script.txt" as UIModules_TMGLMarkers

#Setting S_PointsLimit 100
#Setting S_RoundsPerMap 3
#Setting S_ForceLapsNb -1
#Setting S_PointsRepartition ""
#Setting S_KOCheckpointNb 0 // Number of checkpoints in lead to validate a K.O.
#Setting S_KOCheckpointTime 1500 // Advance in milliseconds to validate a K.O.
#Setting S_KOValidationDelay 1000 // Delay in milliseconds before validating a K.O.
#Setting S_EnableAmbientSound True //"Enable ambient sound"
#Setting S_WorldRecords "" // Format : {"map uid": {"AcountId": "player account id", "Time": time in milliseconds}, ...}
#Setting S_DelayBetweenRounds 0
#Setting S_ForceRoadSpectatorsNb -1 //< Force the number of spectators displayed on the border of the road
#Setting S_DecoImageUrl_Screen16x9 ""
#Setting S_DecoImageUrl_Screen8x1 ""
#Setting S_DecoImageUrl_Screen16x1 ""
#Setting S_OverridePlayerProfiles ""


#Setting S_DisableSignLiveCam False as "Disable 16x9 live cam for Spectators"
#Setting S_DisplayTeamName True as "Display Marker Name on the Live Ranking Interface"
#Setting S_DisplayTeamColor False  as "Display Team Color on the Live Ranking Interface"
/*
S_OverridePlayerProfiles Json format : (enpty values can be removed)
{
  "56f8dd9f-8581-444e-a2ad-1e8e89eed1a4": {
    "WebServicesUserId": "56f8dd9f-8581-444e-a2ad-1e8e89eed1a4",
    "Nickname": "Beu",
    "Flag": "",
    "TeamColor": "",
    "Cheers": "",
    "MarkerName": "$F0FTeam Beu"
  },
  "iiii-jjjj-kkkk-llll": {
    ...
  },
  ...
}

TeamColor is not used if S_DisplayTeamColor = False
*/

***Match_AfterLoadHud***
***
// Remove TMGL Signs
UIModules::UnloadModules(["UIModule_ChampionTMGL_InfoPanels_2x3",
                        "UIModule_ChampionCup_Sign16x9_1",
                        "UIModule_ChampionCup_Sign16x9_2",
                        "UIModule_ChampionCup_Sign16x9_3",
                        "UIModule_ChampionCup_Sign16x9_4",
                        "UIModule_ChampionCup_Sign16x9_5",
                        "UIModule_ChampionCup_Sign64x10_64x10_Start",
                        "UIModule_ChampionCup_Sign64x10_64x10_Finish",
                        "UIModule_ChampionCup_Sign64x10_64x10_Checkpoint",
                        "FinalTMGL_WinScreen",
                        "UIModule_Champion_Chat",
                        "UIModule_Champion_LapsCounter"]);
***



***Match_InitMap***
***
declare Boolean Last_DisableSignLiveCam;
declare Boolean Last_DisplayTeamName;
declare Boolean Last_DisplayTeamColor;
declare Text Last_OverridePlayerProfiles;


if (Last_DisplayTeamName != S_DisplayTeamName || Last_DisplayTeamColor != S_DisplayTeamColor) {
    Last_DisplayTeamName = S_DisplayTeamName;
    Last_DisplayTeamColor = S_DisplayTeamColor;
    UIModules_LiveRanking::DisplayTeamName(S_DisplayTeamName);
    UIModules_LiveRanking::DisplayTeamColor(S_DisplayTeamColor);
}
if (Last_OverridePlayerProfiles != S_OverridePlayerProfiles) {
    Last_OverridePlayerProfiles = S_OverridePlayerProfiles;
    PlayerProfiles::OverridePlayerProfiles(Teams[0], S_OverridePlayerProfiles);
    ResetMarkersConfig();
}
***

***Match_StartRound***
***
if (S_DisableSignLiveCam) {
    UpdateSignLiveCamera(False);
    Round_UpdateSignLiveCameraTime = Now + 86400000; // re-enable it in 24h
}
Last_DisableSignLiveCam = S_DisableSignLiveCam;
***

***Match_PlayLoop***
***
if (Last_DisableSignLiveCam != S_DisableSignLiveCam) {
    Last_DisableSignLiveCam = S_DisableSignLiveCam;

    if (Last_DisableSignLiveCam) {
        UpdateSignLiveCamera(False);
        Round_UpdateSignLiveCameraTime = Now + 86400000;
    } else {
        UpdateSignLiveCamera(True);
        Round_UpdateSignLiveCameraTime = Now + C_UpdateSignLiveCameraInterval;
    }
}
if (Last_DisplayTeamName != S_DisplayTeamName || Last_DisplayTeamColor != S_DisplayTeamColor) {
    Last_DisplayTeamName = S_DisplayTeamName;
    Last_DisplayTeamColor = S_DisplayTeamColor;
    UIModules_LiveRanking::DisplayTeamName(S_DisplayTeamName);
    UIModules_LiveRanking::DisplayTeamColor(S_DisplayTeamColor);
}
if (Last_OverridePlayerProfiles != S_OverridePlayerProfiles) {
    Last_OverridePlayerProfiles = S_OverridePlayerProfiles;
    PlayerProfiles::OverridePlayerProfiles(Teams[0], S_OverridePlayerProfiles);
    ResetMarkersConfig();
}
***


Void ResetMarkersConfig() {
    // Manialink variables
    declare netwrite Text[Integer] Net_ChampionTMGL_TMGLMarkers_MarkerFrameIds for Teams[0] = [];
    declare netwrite Integer Net_ChampionTMGL_TMGLMarkers_MarkerFrameIdsUpdate for Teams[0] = 0;

    // TMGLMarkers Lib variables
    declare Ident[Ident] LibTMGLMarkers_MarkedPlayerIds for UIManager.UIAll = [];
	declare Integer[Ident] LibTMGLMarkers_MarkerFrameIds for UIManager.UIAll = [];

    declare Text[Integer] MarkerToUpdate;
    foreach (Key => Value in Net_ChampionTMGL_TMGLMarkers_MarkerFrameIds) {
        MarkerToUpdate[Key] = "";
    }

    LibTMGLMarkers_MarkedPlayerIds = [];
    LibTMGLMarkers_MarkerFrameIds = [];
    Net_ChampionTMGL_TMGLMarkers_MarkerFrameIds = MarkerToUpdate;
    Net_ChampionTMGL_TMGLMarkers_MarkerFrameIdsUpdate += 1;


    // Force update at next yield
    foreach (Player in Players) {
        declare Boolean LibTMGLMarkers_NewPlayer for Player = True;
        LibTMGLMarkers_NewPlayer = True;
        break;
    }
}