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.Gs2Lottery.Model
{
#if UNITY_2017_1_OR_NEWER
[Preserve]
#endif
public class BoxItems : IComparable
{
public string BoxId { set; get; } = null!;
public string PrizeTableName { set; get; } = null!;
public string UserId { set; get; } = null!;
public Gs2.Gs2Lottery.Model.BoxItem[] Items { set; get; } = null!;
public BoxItems WithBoxId(string boxId) {
this.BoxId = boxId;
return this;
}
public BoxItems WithPrizeTableName(string prizeTableName) {
this.PrizeTableName = prizeTableName;
return this;
}
public BoxItems WithUserId(string userId) {
this.UserId = userId;
return this;
}
public BoxItems WithItems(Gs2.Gs2Lottery.Model.BoxItem[] items) {
this.Items = items;
return this;
}
private static System.Text.RegularExpressions.Regex _regionRegex = new System.Text.RegularExpressions.Regex(
@"grn:gs2:(?<region>.+):(?<ownerId>.+):lottery:(?<namespaceName>.+):user:(?<userId>.+):box:items:(?<prizeTableName>.+)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
public static string GetRegionFromGrn(
string grn
)
{
var match = _regionRegex.Match(grn);
if (!match.Success || !match.Groups["region"].Success)
{
return null;
}
return match.Groups["region"].Value;
}
private static System.Text.RegularExpressions.Regex _ownerIdRegex = new System.Text.RegularExpressions.Regex(
@"grn:gs2:(?<region>.+):(?<ownerId>.+):lottery:(?<namespaceName>.+):user:(?<userId>.+):box:items:(?<prizeTableName>.+)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
public static string GetOwnerIdFromGrn(
string grn
)
{
var match = _ownerIdRegex.Match(grn);
if (!match.Success || !match.Groups["ownerId"].Success)
{
return null;
}
return match.Groups["ownerId"].Value;
}
private static System.Text.RegularExpressions.Regex _namespaceNameRegex = new System.Text.RegularExpressions.Regex(
@"grn:gs2:(?<region>.+):(?<ownerId>.+):lottery:(?<namespaceName>.+):user:(?<userId>.+):box:items:(?<prizeTableName>.+)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
public static string GetNamespaceNameFromGrn(
string grn
)
{
var match = _namespaceNameRegex.Match(grn);
if (!match.Success || !match.Groups["namespaceName"].Success)
{
return null;
}
return match.Groups["namespaceName"].Value;
}
private static System.Text.RegularExpressions.Regex _userIdRegex = new System.Text.RegularExpressions.Regex(
@"grn:gs2:(?<region>.+):(?<ownerId>.+):lottery:(?<namespaceName>.+):user:(?<userId>.+):box:items:(?<prizeTableName>.+)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
public static string GetUserIdFromGrn(
string grn
)
{
var match = _userIdRegex.Match(grn);
if (!match.Success || !match.Groups["userId"].Success)
{
return null;
}
return match.Groups["userId"].Value;
}
private static System.Text.RegularExpressions.Regex _prizeTableNameRegex = new System.Text.RegularExpressions.Regex(
@"grn:gs2:(?<region>.+):(?<ownerId>.+):lottery:(?<namespaceName>.+):user:(?<userId>.+):box:items:(?<prizeTableName>.+)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
public static string GetPrizeTableNameFromGrn(
string grn
)
{
var match = _prizeTableNameRegex.Match(grn);
if (!match.Success || !match.Groups["prizeTableName"].Success)
{
return null;
}
return match.Groups["prizeTableName"].Value;
}
#if UNITY_2017_1_OR_NEWER
[Preserve]
#endif
public static BoxItems FromJson(JsonData data)
{
if (data == null) {
return null;
}
return new BoxItems()
.WithBoxId(!data.Keys.Contains("boxId") || data["boxId"] == null ? null : data["boxId"].ToString())
.WithPrizeTableName(!data.Keys.Contains("prizeTableName") || data["prizeTableName"] == null ? null : data["prizeTableName"].ToString())
.WithUserId(!data.Keys.Contains("userId") || data["userId"] == null ? null : data["userId"].ToString())
.WithItems(!data.Keys.Contains("items") || data["items"] == null || !data["items"].IsArray ? new Gs2.Gs2Lottery.Model.BoxItem[]{} : data["items"].Cast<JsonData>().Select(v => {
return Gs2.Gs2Lottery.Model.BoxItem.FromJson(v);
}).ToArray());
}
public JsonData ToJson()
{
JsonData itemsJsonData = null;
if (Items != null && Items.Length > 0)
{
itemsJsonData = new JsonData();
foreach (var item in Items)
{
itemsJsonData.Add(item.ToJson());
}
}
return new JsonData {
["boxId"] = BoxId,
["prizeTableName"] = PrizeTableName,
["userId"] = UserId,
["items"] = itemsJsonData,
};
}
public void WriteJson(JsonWriter writer)
{
writer.WriteObjectStart();
if (BoxId != null) {
writer.WritePropertyName("boxId");
writer.Write(BoxId.ToString());
}
if (PrizeTableName != null) {
writer.WritePropertyName("prizeTableName");
writer.Write(PrizeTableName.ToString());
}
if (UserId != null) {
writer.WritePropertyName("userId");
writer.Write(UserId.ToString());
}
if (Items != null) {
writer.WritePropertyName("items");
writer.WriteArrayStart();
foreach (var item in Items)
{
if (item != null) {
item.WriteJson(writer);
}
}
writer.WriteArrayEnd();
}
writer.WriteObjectEnd();
}
public int CompareTo(object obj)
{
var other = obj as BoxItems;
var diff = 0;
if (BoxId == null && BoxId == other.BoxId)
{
// null and null
}
else
{
diff += BoxId.CompareTo(other.BoxId);
}
if (PrizeTableName == null && PrizeTableName == other.PrizeTableName)
{
// null and null
}
else
{
diff += PrizeTableName.CompareTo(other.PrizeTableName);
}
if (UserId == null && UserId == other.UserId)
{
// null and null
}
else
{
diff += UserId.CompareTo(other.UserId);
}
if (Items == null && Items == other.Items)
{
// null and null
}
else
{
diff += Items.Length - other.Items.Length;
for (var i = 0; i < Items.Length; i++)
{
diff += Items[i].CompareTo(other.Items[i]);
}
}
return diff;
}
public void Validate() {
{
if (BoxId.Length > 1024) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("boxItems", "lottery.boxItems.boxId.error.tooLong"),
});
}
}
{
if (PrizeTableName.Length > 128) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("boxItems", "lottery.boxItems.prizeTableName.error.tooLong"),
});
}
}
{
if (UserId.Length > 128) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("boxItems", "lottery.boxItems.userId.error.tooLong"),
});
}
}
{
if (Items.Length > 1000) {
throw new Gs2.Core.Exception.BadRequestException(new [] {
new RequestError("boxItems", "lottery.boxItems.items.error.tooMany"),
});
}
}
}
public object Clone() {
return new BoxItems {
BoxId = BoxId,
PrizeTableName = PrizeTableName,
UserId = UserId,
Items = Items.Clone() as Gs2.Gs2Lottery.Model.BoxItem[],
};
}
}
}