1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
using System;
using System.Linq;
using StardewValley;
namespace StardewModdingAPI.Utilities
{
/// <summary>Represents a Stardew Valley date.</summary>
public class SDate
{
/*********
** Properties
*********/
/// <summary>The internal season names in order.</summary>
private readonly string[] Seasons = { "spring", "summer", "fall", "winter" };
/// <summary>The number of days in a season.</summary>
private readonly int DaysInSeason = 28;
/*********
** Accessors
*********/
/// <summary>The day of month.</summary>
public int Day { get; }
/// <summary>The season name.</summary>
public string Season { get; }
/// <summary>The year.</summary>
public int Year { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="day">The day of month.</param>
/// <param name="season">The season name.</param>
/// <exception cref="ArgumentException">One of the arguments has an invalid value (like day 35).</exception>
public SDate(int day, string season)
: this(day, season, Game1.year) { }
/// <summary>Construct an instance.</summary>
/// <param name="day">The day of month.</param>
/// <param name="season">The season name.</param>
/// <param name="year">The year.</param>
/// <exception cref="ArgumentException">One of the arguments has an invalid value (like day 35).</exception>
public SDate(int day, string season, int year)
{
// validate
if (season == null)
throw new ArgumentNullException(nameof(season));
if (!this.Seasons.Contains(season))
throw new ArgumentException($"Unknown season '{season}', must be one of [{string.Join(", ", this.Seasons)}].");
if (day < 1 || day > this.DaysInSeason)
throw new ArgumentException($"Invalid day '{day}', must be a value from 1 to {this.DaysInSeason}.");
if (year < 1)
throw new ArgumentException($"Invalid year '{year}', must be at least 1.");
// initialise
this.Day = day;
this.Season = season;
this.Year = year;
}
/// <summary>Get a new date with the given number of days added.</summary>
/// <param name="offset">The number of days to add.</param>
/// <returns>Returns the resulting date.</returns>
/// <exception cref="ArithmeticException">The offset would result in an invalid date (like year 0).</exception>
public SDate AddDays(int offset)
{
// simple case
int day = this.Day + offset;
string season = this.Season;
int year = this.Year;
// handle season transition
if (day > this.DaysInSeason || day < 1)
{
// get current season index
int curSeasonIndex = Array.IndexOf(this.Seasons, this.Season);
if (curSeasonIndex == -1)
throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised.");
// get season offset
int seasonOffset = day / this.DaysInSeason;
if (day < 1)
seasonOffset -= 1;
// get new date
day = this.GetWrappedIndex(day, this.DaysInSeason);
season = this.Seasons[this.GetWrappedIndex(curSeasonIndex + seasonOffset, this.Seasons.Length)];
year += seasonOffset / this.Seasons.Length;
}
// validate
if(year < 1)
throw new ArithmeticException($"Adding {offset} days to {this} would result in invalid date {day:00} {season} {year}.");
// return new date
return new SDate(day, season, year);
}
/// <summary>Get a string representation of the date. This is mainly intended for debugging or console messages.</summary>
public override string ToString()
{
return $"{this.Day:00} {this.Season} Y{this.Year}";
}
/// <summary>Get the current in-game date.</summary>
public static SDate Now()
{
return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year);
}
/*********
** Operator methods
*********/
/// <summary>
/// Equality operator. Tests the date being equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>The equality of the dates</returns>
public static bool operator ==(SDate s1, SDate s2)
{
if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season)
return true;
else
return false;
}
/// <summary>
/// Inequality operator. Tests the date being not equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>The inequality of the dates</returns>
public static bool operator !=(SDate s1, SDate s2)
{
if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season)
return false;
else
return true;
}
/// <summary>
/// Less than operator. Tests the date being less than to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are less than</returns>
public static bool operator >(SDate s1, SDate s2)
{
if (s1.Year > s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == "winter" && s2.Season != "winter")
return true;
else if (s1.Season == s2.Season && s1.Day > s2.Day)
return true;
if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
return true;
if (s1.Season == "summer" && s2.Season == "spring")
return true;
}
return false;
}
/// <summary>
/// Less or equal than operator. Tests the date being less than or equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are less than or equal than</returns>
public static bool operator >=(SDate s1, SDate s2)
{
if (s1.Year > s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == "winter" && s2.Season != "winter")
return true;
else if (s1.Season == s2.Season && s1.Day >= s2.Day)
return true;
if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
return true;
if (s1.Season == "summer" && s2.Season == "spring")
return true;
}
return false;
}
/// <summary>
/// Greater or equal than operator. Tests the date being greater than or equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are greater than or equal than</returns>
public static bool operator <=(SDate s1, SDate s2)
{
if (s1.Year < s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == s2.Season && s1.Day <= s2.Day)
return true;
else if (s1.Season == "spring" && s2.Season != "spring")
return true;
if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
return true;
if (s1.Season == "fall" && s2.Season == "winter")
return true;
}
return false;
}
/// <summary>
/// Greater than operator. Tests the date being greater than to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are greater than</returns>
public static bool operator <(SDate s1, SDate s2)
{
if (s1.Year < s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == s2.Season && s1.Day < s2.Day)
return true;
else if (s1.Season == "spring" && s2.Season != "spring")
return true;
if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
return true;
if (s1.Season == "fall" && s2.Season == "winter")
return true;
}
return false;
}
/// <summary>
/// Overrides the equals function.
/// </summary>
/// <param name="obj">Object being compared.</param>
/// <returns>The equalaity of the object.</returns>
public override bool Equals(object obj)
{
return base.Equals(obj);
}
/// <summary>
/// This returns the hashcode of the object
/// </summary>
/// <returns>The hashcode of the object.</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
/*********
** Private methods
*********/
/// <summary>Get the real index in an array which should be treated as a two-way loop.</summary>
/// <param name="index">The index in the looped array.</param>
/// <param name="length">The number of elements in the array.</param>
private int GetWrappedIndex(int index, int length)
{
int wrapped = index % length;
if (wrapped < 0)
wrapped += length;
return wrapped;
}
}
}
|