From 51749606eca03036beea956a2e23a40599f54315 Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Fri, 9 Sep 2022 04:42:45 -0500 Subject: [PATCH] Day/night cycle for Mafia --- scripts/server/config.js | 14 ++++--- scripts/server/startup.js | 4 ++ scripts/server/timers.js | 81 ++++++++++++++++++++++++++++--------- scripts/server/utilities.js | 15 +++++++ scripts/shared/utilities.js | 26 ++++++++++++ 5 files changed, 116 insertions(+), 24 deletions(-) diff --git a/scripts/server/config.js b/scripts/server/config.js index 1d08152f..d50642f3 100644 --- a/scripts/server/config.js +++ b/scripts/server/config.js @@ -126,8 +126,8 @@ class ServerConfigData { this.discordBotToken = intToBool(dbAssoc["svr_discord_bot_token"]); this.introMusicURL = dbAssoc["svr_intro_music"]; - //this.useRealTime = intToBool(toInteger(dbAssoc["svr_real_time_enabled"])); - //this.realTimeZone = dbAssoc["svr_real_time_timezone"]; + this.useRealTime = intToBool(toInteger(dbAssoc["svr_real_time_enabled"])); + this.realTimeZone = dbAssoc["svr_real_time_timezone"]; this.discord = { sendEvents: intToBool(dbAssoc["svr_discord_send_events"]), @@ -377,10 +377,12 @@ function applyConfigToServer(tempServerConfig) { logToConsole(LOG_INFO, "[VRR.Config]: Applying server config ..."); logToConsole(LOG_DEBUG, "[VRR.Config]: Server config applied successfully!"); - if (isTimeSupported()) { - logToConsole(LOG_DEBUG, `[VRR.Config]: Setting time to to ${tempServerConfig.hour}:${tempServerConfig.minute} with minute duration of ${tempServerConfig.minuteDuration}`); - setGameTime(tempServerConfig.hour, tempServerConfig.minute, tempServerConfig.minuteDuration); - } + updateServerGameTime(); + + //if (isTimeSupported()) { + // logToConsole(LOG_DEBUG, `[VRR.Config]: Setting time to to ${tempServerConfig.hour}:${tempServerConfig.minute} with minute duration of ${tempServerConfig.minuteDuration}`); + // setGameTime(tempServerConfig.hour, tempServerConfig.minute, tempServerConfig.minuteDuration); + //} if (isWeatherSupported()) { logToConsole(LOG_DEBUG, `[VRR.Config]: Setting weather to ${tempServerConfig.weather}`); diff --git a/scripts/server/startup.js b/scripts/server/startup.js index 6b5c86b6..9e6128aa 100644 --- a/scripts/server/startup.js +++ b/scripts/server/startup.js @@ -52,9 +52,13 @@ function initServerScripts() { addAllNetworkEventHandlers(); + checkServerGameTime(); + initAllClients(); initTimers(); + + serverStartTime = getCurrentUnixTimestamp(); } diff --git a/scripts/server/timers.js b/scripts/server/timers.js index 635dd933..e0360e93 100644 --- a/scripts/server/timers.js +++ b/scripts/server/timers.js @@ -125,6 +125,34 @@ function tenMinuteTimerFunction() { //showRandomTipToAllPlayers(); //saveServerDataToDatabase(); //checkInactiveVehicleRespawns(); + + if (getGame() == AGRP_GAME_MAFIA_ONE) { + if (server.mapName == "FREERIDE") { + if (isServerGoingToChangeMapsSoon(getServerConfig().hour, getServerConfig().minute)) { + sendMapChangeWarningToPlayer(null, true); + } + + if (!isNightTime(getServerConfig().hour)) { + getGameConfig().mainWorldScene[getGame()] = "FREERIDENOC"; + removeAllPlayersFromProperties(); + saveServerDataToDatabase(); + game.changeMap(getGameConfig().mainWorldScene[getGame()]); + updateTimeRule(); + } + } else { + if (isServerGoingToChangeMapsSoon()) { + sendMapChangeWarningToPlayer(null, true); + } + + if (!isNightTime(getServerConfig().hour)) { + getGameConfig().mainWorldScene[getGame()] = "FREERIDE"; + removeAllPlayersFromProperties(); + saveServerDataToDatabase(); + game.changeMap(getGameConfig().mainWorldScene[getGame()]); + updateTimeRule(); + } + } + } } // =========================================================================== @@ -202,26 +230,43 @@ function updatePings() { // =========================================================================== function checkServerGameTime() { - if (isGameFeatureSupported("time")) { - return false; + //logToConsole(LOG_DEBUG | LOG_WARN, "[AGRP.Timers] Checking server game time"); + + //if (isGameFeatureSupported("time")) { + // return false; + //} + + if (!getServerConfig().useRealTime) { + if (getServerConfig().minute >= 59) { + getServerConfig().minute = 0; + if (getServerConfig().hour >= 23) { + getServerConfig().hour = 0; + } else { + getServerConfig().hour = getServerConfig().hour + 1; + } + } else { + getServerConfig().minute = getServerConfig().minute + 1; + } + } else { + let dateTime = getCurrentTimeStampWithTimeZone(getServerConfig().realTimeZone); + getServerConfig().hour = dateTime.getHours(); + getServerConfig().minute = dateTime.getMinutes(); } - //if(!getServerConfig().useRealTime) { - //if (getServerConfig().minute >= 59) { - // getServerConfig().minute = 0; - // if (getServerConfig().hour >= 23) { - // getServerConfig().hour = 0; - // } else { - // getServerConfig().hour = getServerConfig().hour + 1; - // } - //} else { - // getServerConfig().minute = getServerConfig().minute + 1; - //} - //} else { - // let dateTime = getCurrentTimeStampWithTimeZone(getServerConfig().realTimeZone); - // getServerConfig().hour = dateTime.getHours(); - // getServerConfig().minute = dateTime.getMinutes(); - //} + if (getGame() == AGRP_GAME_MAFIA_ONE) { + if (isNightTime(getServerConfig().hour) && getGameConfig().mainWorldScene[getGame()] == "FREERIDE") { + getGameConfig().mainWorldScene[getGame()] = "FREERIDENOC"; + game.changeMap(getGameConfig().mainWorldScene[getGame()]); + } else if (!isNightTime(getServerConfig().hour) && getGameConfig().mainWorldScene[getGame()] == "FREERIDENOC") { + getGameConfig().mainWorldScene[getGame()] = "FREERIDE"; + game.changeMap(getGameConfig().mainWorldScene[getGame()]); + } + } + + if (isGameFeatureSupported("time")) { + game.time.hour = getServerConfig().hour; + game.time.minute = getServerConfig().minute; + } updateTimeRule(); } diff --git a/scripts/server/utilities.js b/scripts/server/utilities.js index 5902e9d1..84903af2 100644 --- a/scripts/server/utilities.js +++ b/scripts/server/utilities.js @@ -409,6 +409,14 @@ function updateTimeRule() { if (isTimeSupported()) { server.setRule("Time", makeReadableTime(game.time.hour, game.time.minute)); } + + if (getGame() == AGRP_GAME_MAFIA_ONE) { + if (isNightTime(getServerConfig().hour)) { + server.setRule("Time", "Night"); + } else { + server.setRule("Time", "Day"); + } + } } // =========================================================================== @@ -472,4 +480,11 @@ function updateAllPlayerWeaponDamageStates() { } } +// =========================================================================== + +function removeAllPlayersFromProperties() { + + return false; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/shared/utilities.js b/scripts/shared/utilities.js index dd93fd55..0cf85906 100644 --- a/scripts/shared/utilities.js +++ b/scripts/shared/utilities.js @@ -3188,4 +3188,30 @@ function isMainWorldScene(sceneName) { return (sceneName == "agrp.mainWorldScene"); } +// =========================================================================== + +function isNightTime(hour) { + if (hour >= 7 && hour <= 19) { + return false; + } else { + return true; + } +} + +// =========================================================================== + +function isServerGoingToChangeMapsSoon(hour, minute) { + if (server.mapName == "FREERIDENOC") { + if (hour == 6 && minute >= 30) { + return true + } + } else if (server.mapName == "FREERIDE") { + if (hour == 18 && minute >= 30) { + return true; + } + } + + return false; +} + // =========================================================================== \ No newline at end of file