Holiday System (#3122)

This commit is contained in:
Vera Aguilera Puerto
2021-02-12 10:45:22 +01:00
committed by GitHub
parent 857c65d968
commit 9ee0ec4106
36 changed files with 1086 additions and 24 deletions

View File

@@ -0,0 +1,12 @@
using Content.Server.Holiday.Interfaces;
namespace Content.Server.Holiday.Celebrate
{
public class DefaultHolidayCelebrate : IHolidayCelebrate
{
public void Celebrate(HolidayPrototype holiday)
{
// Nada.
}
}
}

View File

@@ -0,0 +1,22 @@
using Content.Server.Holiday.Interfaces;
using JetBrains.Annotations;
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.Greet
{
[UsedImplicitly]
public class Custom : IHolidayGreet
{
private string _greet;
void IExposeData.ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _greet, "text", string.Empty);
}
public string Greet(HolidayPrototype holiday)
{
return _greet;
}
}
}

View File

@@ -0,0 +1,13 @@
using Content.Server.Holiday.Interfaces;
using Robust.Shared.Localization;
namespace Content.Server.Holiday.Greet
{
public class DefaultHolidayGreet : IHolidayGreet
{
public string Greet(HolidayPrototype holiday)
{
return Loc.GetString("Have a happy {0}!", holiday.Name);
}
}
}

View File

@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using Content.Server.GameTicking;
using Content.Server.Holiday.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameTicking;
using Content.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Holiday
{
// ReSharper disable once ClassNeverInstantiated.Global
public class HolidayManager : IHolidayManager
{
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IGameTicker _gameTicker = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[ViewVariables]
private readonly List<HolidayPrototype> _currentHolidays = new();
[ViewVariables]
private bool _enabled = true;
public void RefreshCurrentHolidays()
{
_currentHolidays.Clear();
if (!_enabled) return;
var now = DateTime.Now;
foreach (var holiday in _prototypeManager.EnumeratePrototypes<HolidayPrototype>())
{
if(holiday.ShouldCelebrate(now))
_currentHolidays.Add(holiday);
}
}
public void DoGreet()
{
foreach (var holiday in _currentHolidays)
{
_chatManager.DispatchServerAnnouncement(holiday.Greet());
}
}
public void DoCelebrate()
{
foreach (var holiday in _currentHolidays)
{
holiday.Celebrate();
}
}
public IEnumerable<HolidayPrototype> GetCurrentHolidays()
{
return _currentHolidays;
}
public bool IsCurrentlyHoliday(string holiday)
{
if (!_prototypeManager.TryIndex(holiday, out HolidayPrototype prototype))
return false;
return _currentHolidays.Contains(prototype);
}
public void Initialize()
{
_configManager.OnValueChanged(CCVars.HolidaysEnabled, OnHolidaysEnableChange, true);
_gameTicker.OnRunLevelChanged += OnRunLevelChanged;
}
private void OnHolidaysEnableChange(bool enabled)
{
_enabled = enabled;
RefreshCurrentHolidays();
}
private void OnRunLevelChanged(GameRunLevelChangedEventArgs eventArgs)
{
if (!_enabled) return;
switch (eventArgs.NewRunLevel)
{
case GameRunLevel.PreRoundLobby:
RefreshCurrentHolidays();
break;
case GameRunLevel.InRound:
DoGreet();
DoCelebrate();
break;
case GameRunLevel.PostRound:
break;
}
}
}
}

View File

@@ -0,0 +1,78 @@
#nullable enable
using System;
using Content.Server.Holiday.Celebrate;
using Content.Server.Holiday.Greet;
using Content.Server.Holiday.Interfaces;
using Content.Server.Holiday.ShouldCelebrate;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using YamlDotNet.RepresentationModel;
namespace Content.Server.Holiday
{
[Prototype("holiday")]
public class HolidayPrototype : IPrototype, IIndexedPrototype, IExposeData
{
[ViewVariables] public string Name { get; private set; } = string.Empty;
[ViewVariables] public string ID { get; private set; } = string.Empty;
[ViewVariables] public byte BeginDay { get; set; } = 1;
[ViewVariables] public Month BeginMonth { get; set; } = Month.Invalid;
/// <summary>
/// Day this holiday will end. Zero means it lasts a single day.
/// </summary>
[ViewVariables] public byte EndDay { get; set; } = 0;
/// <summary>
/// Month this holiday will end in. Invalid means it lasts a single month.
/// </summary>
[ViewVariables] public Month EndMonth { get; set; } = Month.Invalid;
[ViewVariables]
private IHolidayShouldCelebrate _shouldCelebrate = new DefaultHolidayShouldCelebrate();
[ViewVariables]
private IHolidayGreet _greet = new DefaultHolidayGreet();
[ViewVariables]
private IHolidayCelebrate _celebrate = new DefaultHolidayCelebrate();
public void LoadFrom(YamlMappingNode mapping)
{
var serializer = YamlObjectSerializer.NewReader(mapping);
ExposeData(serializer);
}
public void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(this, x => x.ID, "id", string.Empty);
serializer.DataField(this, x => x.Name, "name", string.Empty);
serializer.DataField(this, x => x.BeginDay, "beginDay", (byte)1);
serializer.DataField(this, x => x.BeginMonth, "beginMonth", Month.Invalid);
serializer.DataField(this, x => x.EndDay, "endDay", (byte)0);
serializer.DataField(this, x => x.EndMonth, "endMonth", Month.Invalid);
serializer.DataField(ref _shouldCelebrate, "shouldCelebrate", new DefaultHolidayShouldCelebrate());
serializer.DataField(ref _greet, "greet", new DefaultHolidayGreet());
serializer.DataField(ref _celebrate, "celebrate", new DefaultHolidayCelebrate());
}
public bool ShouldCelebrate(DateTime date)
{
return _shouldCelebrate.ShouldCelebrate(date, this);
}
public string Greet()
{
return _greet.Greet(this);
}
/// <summary>
/// Called before the round starts to set up any festive shenanigans.
/// </summary>
public void Celebrate()
{
_celebrate.Celebrate(this);
}
}
}

View File

@@ -0,0 +1,15 @@
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.Interfaces
{
public interface IHolidayCelebrate : IExposeData
{
void IExposeData.ExposeData(ObjectSerializer serializer) {}
/// <summary>
/// This method is called before a round starts.
/// Use it to do any fun festive modifications.
/// </summary>
void Celebrate(HolidayPrototype holiday);
}
}

View File

@@ -0,0 +1,10 @@
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.Interfaces
{
public interface IHolidayGreet : IExposeData
{
void IExposeData.ExposeData(ObjectSerializer serializer) { }
string Greet(HolidayPrototype holiday);
}
}

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Content.Server.Holiday.Interfaces
{
public interface IHolidayManager
{
void Initialize();
void RefreshCurrentHolidays();
void DoGreet();
void DoCelebrate();
IEnumerable<HolidayPrototype> GetCurrentHolidays();
bool IsCurrentlyHoliday(string holiday);
}
}

View File

@@ -0,0 +1,11 @@
using System;
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.Interfaces
{
public interface IHolidayShouldCelebrate : IExposeData
{
void IExposeData.ExposeData(ObjectSerializer serializer) {}
bool ShouldCelebrate(DateTime date, HolidayPrototype holiday);
}
}

View File

@@ -0,0 +1,19 @@
namespace Content.Server.Holiday
{
public enum Month : byte
{
Invalid = 0,
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Globalization;
using Content.Server.Holiday.Interfaces;
namespace Content.Server.Holiday.ShouldCelebrate
{
public class ChineseNewYear : IHolidayShouldCelebrate
{
public bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
{
var chinese = new ChineseLunisolarCalendar();
var gregorian = new GregorianCalendar();
var chineseNewYear = chinese.ToDateTime(date.Year, 1, 1, 0, 0, 0, 0);
return date.Day == chineseNewYear.Day && date.Month == chineseNewYear.Month;
}
}
}

View File

@@ -0,0 +1,117 @@
using System;
using System.IO;
using JetBrains.Annotations;
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.ShouldCelebrate
{
/// <summary>
/// Computus for easter calculation.
/// </summary>
[UsedImplicitly]
public class Computus : DefaultHolidayShouldCelebrate, IExposeData
{
private byte _daysEarly = 1;
private byte _daysExtra = 1;
void IExposeData.ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _daysEarly, "daysEarly", (byte) 1);
serializer.DataField(ref _daysExtra, "daysExtra", (byte) 1);
}
public (int day, int month) DoComputus(DateTime date)
{
var currentYear = date.Year;
var m = 0;
var n = 0;
switch (currentYear)
{
case var i when i >= 1900 && i <= 2099:
m = 24;
n = 5;
break;
case var i when i >= 2100 && i <= 2199:
m = 24;
n = 6;
break;
case var i when i >= 2200 && i <= 2299:
m = 25;
n = 0;
break;
// Hello, future person! If you're living in the year >=2300, you might want to fix this method.
// t. earth coder living in 2021
default:
throw new InvalidDataException("Easter machine broke.");
}
var a = currentYear % 19;
var b = currentYear % 4;
var c = currentYear % 7;
var d = (19 * a + m) % 30;
var e = (2 * b + 4 * c + 6 * d + n) % 7;
(int day, int month) easterDate = (0, 0);
if (d + e < 10)
{
easterDate.month = 3;
easterDate.day = (d + e + 22);
} else if (d + e > 9)
{
easterDate.month = 4;
easterDate.day = (d + e - 9);
}
if (easterDate.month == 4 && easterDate.day == 26)
easterDate.day = 19;
if (easterDate.month == 4 && easterDate.day == 25 && d == 28 && e == 6 && a > 10)
easterDate.day = 18;
return easterDate;
}
public override bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
{
if (holiday.BeginMonth == Month.Invalid)
{
var (day, month) = DoComputus(date);
holiday.BeginDay = (byte) day;
holiday.BeginMonth = (Month) month;
holiday.EndDay = (byte) (holiday.BeginDay + _daysExtra);
holiday.EndMonth = holiday.BeginMonth;
// Begins in march, ends in april
if (holiday.EndDay >= 32 && holiday.EndMonth == Month.March)
{
holiday.EndDay -= 31;
holiday.EndMonth++;
}
// Begins in april, ends in june.
if (holiday.EndDay >= 31 && holiday.EndMonth == Month.April)
{
holiday.EndDay -= 30;
holiday.EndMonth++;
}
holiday.BeginDay -= _daysEarly;
// Begins in march, ends in april.
if (holiday.BeginDay <= 0 && holiday.BeginMonth == Month.April)
{
holiday.BeginDay += 31;
holiday.BeginMonth--;
}
}
return base.ShouldCelebrate(date, holiday);
}
}
}

View File

@@ -0,0 +1,26 @@
using System;
using Content.Server.Holiday.Interfaces;
using JetBrains.Annotations;
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.ShouldCelebrate
{
/// <summary>
/// For a holiday that occurs on a certain day of the year.
/// </summary>
[UsedImplicitly]
public class DayOfYear : IHolidayShouldCelebrate
{
private uint _dayOfYear;
void IExposeData.ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _dayOfYear, "dayOfYear", 1u);
}
public bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
{
return date.DayOfYear == _dayOfYear;
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Globalization;
using Content.Server.Holiday.Interfaces;
namespace Content.Server.Holiday.ShouldCelebrate
{
public class DefaultHolidayShouldCelebrate : IHolidayShouldCelebrate
{
public virtual bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
{
if (holiday.EndDay == 0)
holiday.EndDay = holiday.BeginDay;
if (holiday.EndMonth == Month.Invalid)
holiday.EndMonth = holiday.BeginMonth;
// Holiday spans multiple months in one year.
if(holiday.EndMonth > holiday.BeginMonth)
{
// In final month.
if (date.Month == (int) holiday.EndMonth && date.Day <= holiday.EndDay)
return true;
// In first month.
if (date.Month == (int) holiday.BeginMonth && date.Day >= holiday.BeginDay)
return true;
// Holiday spans more than 2 months, and we're in the middle.
if (date.Month > (int) holiday.BeginMonth && date.Month < (int) holiday.EndMonth)
return true;
}
// Holiday starts and stops in the same month.
else if (holiday.EndMonth == holiday.BeginMonth)
{
if (date.Month == (int) holiday.BeginMonth && date.Day >= holiday.BeginDay && date.Day <= holiday.EndDay)
return true;
}
// Holiday starts in one year and ends in the next.
else
{
// Holiday ends next year.
if (date.Month >= (int) holiday.BeginMonth && date.Day >= holiday.BeginDay)
return true;
// Holiday started last year.
if (date.Month <= (int) holiday.EndMonth && date.Day <= holiday.EndDay)
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using Content.Server.Holiday.Interfaces;
using JetBrains.Annotations;
namespace Content.Server.Holiday.ShouldCelebrate
{
/// <summary>
/// For Friday the 13th. Spooky!
/// </summary>
[UsedImplicitly]
public class FridayThirteenth : IHolidayShouldCelebrate
{
public bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
{
return date.Day == 13 && date.DayOfWeek == DayOfWeek.Friday;
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Globalization;
using JetBrains.Annotations;
using Robust.Shared.Serialization;
namespace Content.Server.Holiday.ShouldCelebrate
{
/// <summary>
/// For a holiday that happens the first instance of a weekday on a month.
/// </summary>
[UsedImplicitly]
public class WeekdayInMonth : DefaultHolidayShouldCelebrate, IExposeData
{
private DayOfWeek _weekday;
private uint _occurrence;
void IExposeData.ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _weekday, "weekday", DayOfWeek.Monday);
serializer.DataField(ref _occurrence, "occurrence", 1u);
}
public override bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
{
// Occurrence NEEDS to be between 1 and 4.
_occurrence = Math.Max(1, Math.Min(_occurrence, 4));
var calendar = new GregorianCalendar();
var d = new DateTime(date.Year, date.Month, 1, calendar);
for (var i = 1; i <= 7; i++)
{
if (d.DayOfWeek != _weekday)
{
d = d.AddDays(1);
continue;
}
d = d.AddDays(7 * (_occurrence-1));
return date.Day == d.Day;
}
return false;
}
}
}