Repository URL to install this package:
Version:
2024.6.10 ▾
|
/*
* Copyright 2016 Game Server Services, Inc. or its affiliates. All Rights
* Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Gs2.Core.Model;
using Gs2.Util.LitJson;
#if UNITY_2017_1_OR_NEWER
using UnityEngine.Scripting;
#endif
namespace Gs2.Gs2Mission.Model
{
#if UNITY_2017_1_OR_NEWER
[Preserve]
#endif
public class CounterScopeModel : IComparable
{
public string ResetType { set; get; } = null!;
public int? ResetDayOfMonth { set; get; } = null!;
public string ResetDayOfWeek { set; get; } = null!;
public int? ResetHour { set; get; } = null!;
public CounterScopeModel WithResetType(string resetType) {
this.ResetType = resetType;
return this;
}
public CounterScopeModel WithResetDayOfMonth(int? resetDayOfMonth) {
this.ResetDayOfMonth = resetDayOfMonth;
return this;
}
public CounterScopeModel WithResetDayOfWeek(string resetDayOfWeek) {
this.ResetDayOfWeek = resetDayOfWeek;
return this;
}
public CounterScopeModel WithResetHour(int? resetHour) {
this.ResetHour = resetHour;
return this;
}
#if UNITY_2017_1_OR_NEWER
[Preserve]
#endif
public static CounterScopeModel FromJson(JsonData data)
{
if (data == null) {
return null;
}
return new CounterScopeModel()
.WithResetType(!data.Keys.Contains("resetType") || data["resetType"] == null ? null : data["resetType"].ToString())
.WithResetDayOfMonth(!data.Keys.Contains("resetDayOfMonth") || data["resetDayOfMonth"] == null ? null : (int?)(data["resetDayOfMonth"].ToString().Contains(".") ? (int)double.Parse(data["resetDayOfMonth"].ToString()) : int.Parse(data["resetDayOfMonth"].ToString())))
.WithResetDayOfWeek(!data.Keys.Contains("resetDayOfWeek") || data["resetDayOfWeek"] == null ? null : data["resetDayOfWeek"].ToString())
.WithResetHour(!data.Keys.Contains("resetHour") || data["resetHour"] == null ? null : (int?)(data["resetHour"].ToString().Contains(".") ? (int)double.Parse(data["resetHour"].ToString()) : int.Parse(data["resetHour"].ToString())));
}
public JsonData ToJson()
{
return new JsonData {
["resetType"] = ResetType,
["resetDayOfMonth"] = ResetDayOfMonth,
["resetDayOfWeek"] = ResetDayOfWeek,
["resetHour"] = ResetHour,
};
}
public void WriteJson(JsonWriter writer)
{
writer.WriteObjectStart();
if (ResetType != null) {
writer.WritePropertyName("resetType");
writer.Write(ResetType.ToString());
}
if (ResetDayOfMonth != null) {
writer.WritePropertyName("resetDayOfMonth");
writer.Write((ResetDayOfMonth.ToString().Contains(".") ? (int)double.Parse(ResetDayOfMonth.ToString()) : int.Parse(ResetDayOfMonth.ToString())));
}
if (ResetDayOfWeek != null) {
writer.WritePropertyName("resetDayOfWeek");
writer.Write(ResetDayOfWeek.ToString());
}
if (ResetHour != null) {
writer.WritePropertyName("resetHour");
writer.Write((ResetHour.ToString().Contains(".") ? (int)double.Parse(ResetHour.ToString()) : int.Parse(ResetHour.ToString())));
}
writer.WriteObjectEnd();
}
public int CompareTo(object obj)
{
var other = obj as CounterScopeModel;
var diff = 0;
if (ResetType == null && ResetType == other.ResetType)
{
// null and null
}
else
{
diff += ResetType.CompareTo(other.ResetType);
}
if (ResetDayOfMonth == null && ResetDayOfMonth == other.ResetDayOfMonth)
{
// null and null
}
else
{
diff += (int)(ResetDayOfMonth - other.ResetDayOfMonth);
}
if (ResetDayOfWeek == null && ResetDayOfWeek == other.ResetDayOfWeek)
{
// null and null
}
else
{
diff += ResetDayOfWeek.CompareTo(other.ResetDayOfWeek);
}
if (ResetHour == null && ResetHour == other.ResetHour)
{
// null and null
}
else
{
diff += (int)(ResetHour - other.ResetHour);
}
return diff;
}
public void Validate() {
{
switch (ResetType) {
case "notReset":
case "daily":
case "weekly":
case "monthly":
break;
default:
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("counterScopeModel", "mission.counterScopeModel.resetType.error.invalid"),
});
}
}
if (ResetType == "monthly") {
if (ResetDayOfMonth < 1) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("counterScopeModel", "mission.counterScopeModel.resetDayOfMonth.error.invalid"),
});
}
if (ResetDayOfMonth > 31) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("counterScopeModel", "mission.counterScopeModel.resetDayOfMonth.error.invalid"),
});
}
}
if (ResetType == "weekly") {
switch (ResetDayOfWeek) {
case "sunday":
case "monday":
case "tuesday":
case "wednesday":
case "thursday":
case "friday":
case "saturday":
break;
default:
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("counterScopeModel", "mission.counterScopeModel.resetDayOfWeek.error.invalid"),
});
}
}
if ((ResetType =="monthly" || ResetType == "weekly" || ResetType == "daily")) {
if (ResetHour < 0) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("counterScopeModel", "mission.counterScopeModel.resetHour.error.invalid"),
});
}
if (ResetHour > 23) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("counterScopeModel", "mission.counterScopeModel.resetHour.error.invalid"),
});
}
}
}
public object Clone() {
return new CounterScopeModel {
ResetType = ResetType,
ResetDayOfMonth = ResetDayOfMonth,
ResetDayOfWeek = ResetDayOfWeek,
ResetHour = ResetHour,
};
}
}
}