diff --git a/ReallifeGamemode.Server/Commands/AdminCommands.cs b/ReallifeGamemode.Server/Commands/AdminCommands.cs
index 01f7d261..97c0d552 100644
--- a/ReallifeGamemode.Server/Commands/AdminCommands.cs
+++ b/ReallifeGamemode.Server/Commands/AdminCommands.cs
@@ -2169,18 +2169,15 @@ namespace ReallifeGamemode.Server.Commands
return;
}
- Weather weatherBefore = NAPI.World.GetWeather();
- NAPI.World.SetWeather(weather);
- Weather weatherAfter = NAPI.World.GetWeather();
+ Weather weatherBefore = World.WeatherSync.Weather;
+ World.WeatherSync.SetWeather(weather);
+ Weather weatherAfter = World.WeatherSync.Weather;
+
+ ChatService.SendMessage(player, "~w~Wetter geändert: " + weatherAfter);
if (!weatherBefore.Equals(weatherAfter))
{
- ChatService.SendMessage(player, "~w~Wetter geändert: " + NAPI.World.GetWeather());
- NAPI.Notification.SendNotificationToAll("Das Wetter wurde von ~g~" + player.Name + " ~s~auf ~g~" + NAPI.World.GetWeather() + "~s~ geändert.", true);
- }
- else
- {
- ChatService.SendMessage(player, "~w~Das Wetter konnte nicht geändert werden");
+ NAPI.Notification.SendNotificationToAll("Das Wetter wurde von ~g~" + player.Name + " ~s~auf ~g~" + weatherAfter + "~s~ geändert.", true);
}
}
diff --git a/ReallifeGamemode.Server/World/WeatherSync.cs b/ReallifeGamemode.Server/World/WeatherSync.cs
new file mode 100644
index 00000000..04c8519c
--- /dev/null
+++ b/ReallifeGamemode.Server/World/WeatherSync.cs
@@ -0,0 +1,104 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Timers;
+using GTANetworkAPI;
+
+/**
+* @overview Life of German Reallife - WeatherSync
+* @author Alex_qp
+* @copyright (c) 2008 - 2021 Life of German
+*/
+
+namespace ReallifeGamemode.Server.World
+{
+
+ ///
+ /// Serverwide WeatherSystem (+ sync)
+ ///
+ static class WeatherSync
+ {
+ // SETTINGS
+ private const uint _syncInterval = 5 * 60 * 1000; // 5 min
+ private static readonly int[] _countRange = new int[] { 3, 5 }; // min, max both inclusive. How often should weather be synced before actual changing?
+
+ // INTERNAL
+ private static int _count = 0;
+ private static int _targetCount = GetNewTargetCount();
+
+ ///
+ /// Get/Set the current weather.
+ /// Only use in main thread!
+ ///
+ public static Weather Weather
+ {
+ get { return NAPI.World.GetWeather(); }
+ set { NAPI.World.SetWeather(value); }
+ }
+
+ ///
+ /// Set the current weather by string.
+ ///
+ /// Only use in main thread!
+ ///
+ ///
+ public static void SetWeather(string weather)
+ {
+ NAPI.World.SetWeather(weather);
+ }
+
+ private static Timer weatherTimer;
+
+ ///
+ /// Loads/Starts the WeatherSync.
+ ///
+ public static void Load()
+ {
+ weatherTimer = new Timer(_syncInterval);
+ weatherTimer.Start();
+ weatherTimer.Elapsed += OnWeatherTimer;
+ }
+
+ private static int GetNewTargetCount()
+ {
+ return new Random().Next(_countRange[0], _countRange[1] + 1);
+ }
+
+ private static Weather GetRandomWeather(Weather[] weathers)
+ {
+ return weathers[new Random().Next(weathers.Length)];
+ }
+
+ private static void OnWeatherTimer(object sender, ElapsedEventArgs e)
+ {
+ NAPI.Task.Run(() =>
+ {
+ if (_count < _targetCount)
+ {
+ // sync weather for all players
+ _count++;
+ Weather = Weather;
+ }
+ else
+ {
+ // change weather and reset counters
+ _count = 0;
+ _targetCount = GetNewTargetCount();
+ Weather = Weather switch
+ {
+ Weather.EXTRASUNNY => GetRandomWeather(new Weather[] { Weather.EXTRASUNNY, Weather.CLEAR }),
+ Weather.CLEAR => GetRandomWeather(new Weather[] { Weather.EXTRASUNNY, Weather.CLEAR, Weather.CLOUDS, Weather.SMOG }),
+ Weather.CLOUDS => GetRandomWeather(new Weather[] { Weather.CLEAR, Weather.CLOUDS, Weather.SMOG, Weather.FOGGY, Weather.OVERCAST }),
+ Weather.SMOG => GetRandomWeather(new Weather[] { Weather.CLOUDS, Weather.SMOG, Weather.FOGGY, Weather.OVERCAST }),
+ Weather.FOGGY => GetRandomWeather(new Weather[] { Weather.CLOUDS, Weather.SMOG, Weather.FOGGY, Weather.OVERCAST, Weather.RAIN }),
+ Weather.OVERCAST => GetRandomWeather(new Weather[] { Weather.CLOUDS, Weather.SMOG, Weather.RAIN, Weather.CLEARING }), // may add overcast
+ Weather.RAIN => GetRandomWeather(new Weather[] { Weather.OVERCAST, Weather.RAIN, Weather.CLEARING, Weather.THUNDER }),
+ Weather.THUNDER => GetRandomWeather(new Weather[] { Weather.RAIN, Weather.THUNDER, Weather.CLEARING }),
+ Weather.CLEARING => GetRandomWeather(new Weather[] { Weather.CLEAR, Weather.CLOUDS, Weather.CLEARING }),
+ _ => GetRandomWeather(new Weather[] { Weather.EXTRASUNNY, Weather.CLEAR }),
+ };
+ }
+ });
+ }
+ }
+}