| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- [System.Serializable]
- public class DeckData
- {
- public static int m = 256;
- public static int n = 256;
- public static int log_n_m = (int)Mathf.Log(m, n);
- public static int maxCardKind = 37000;
- public static int CardKindCellLength = (int)Math.Ceiling(Mathf.Log(maxCardKind, m));
- #region constructor
- public DeckData(string DeckCode, string ID = "")
- {
- List<int> _DeckCardIDs = new List<int>();
- List<int> _DigitamaDeckCardIDs = new List<int>();
- //separated by commas
- string[] parseByComma = DeckCode.Split(',');
- List<int> DistinctDeckCardIDs = new List<int>();
- List<int> DistinctDeckCardCounts = new List<int>();
- List<int> DistinctDigitamaDeckCardIDs = new List<int>();
- List<int> DistinctDigitamaDeckCardCounts = new List<int>();
- DeckID = ID;
- for (int i = 0; i < parseByComma.Length; i++)
- {
- //deck name
- if (i == 0)
- {
- DeckName = parseByComma[i];
- }
- //Cards in the deck(no duplicates)
- else if (i == 1)
- {
- //Separate every 2 characters
- string[] SplitText = SplitClass.Split(parseByComma[i], CardKindCellLength);
- for (int j = 0; j < SplitText.Length; j++)
- {
- DistinctDeckCardIDs.Add(ConvertBinaryNumber.NStringToInt(SplitText[j], m));
- }
- }
- //Number of each type of card
- else if (i == 2)
- {
- //m-ary string
- string x_m = parseByComma[i];
- if (!string.IsNullOrEmpty(x_m))
- {
- //Convert m-ary number to n-ary number
- string x_n = ConvertBinaryNumber.NKStringToNString(x_m, n, log_n_m);
- //Separate n-ary string by character
- string[] Split_x_n = SplitClass.Split(x_n, 1);
- for (int j = 0; j < Split_x_n.Length; j++)
- {
- //Convert n-ary number to int
- DistinctDeckCardCounts.Add(ConvertBinaryNumber.NStringToInt(Split_x_n[j], n) + 1);
- }
- }
- }
- //Digitama deck cards (no duplicates)
- else if (i == 3)
- {
- //Separate every 2 characters
- string[] SplitText = SplitClass.Split(parseByComma[i], CardKindCellLength);
- for (int j = 0; j < SplitText.Length; j++)
- {
- DistinctDigitamaDeckCardIDs.Add(ConvertBinaryNumber.NStringToInt(SplitText[j], m));
- }
- }
- //Number of each type of card
- else if (i == 4)
- {
- //m-ary string
- string x_m = parseByComma[i];
- if (!string.IsNullOrEmpty(x_m))
- {
- //Convert m-ary number to n-ary number
- string x_n = ConvertBinaryNumber.NKStringToNString(x_m, n, log_n_m);
- //Separate n-ary string by character
- string[] Split_x_n = SplitClass.Split(x_n, 1);
- for (int j = 0; j < Split_x_n.Length; j++)
- {
- //Convert n-ary number to int
- DistinctDigitamaDeckCardCounts.Add(ConvertBinaryNumber.NStringToInt(Split_x_n[j], n) + 1);
- }
- }
- }
- //key card id
- else if (i == 5)
- {
- if (int.TryParse(parseByComma[i], out int value))
- {
- KeyCardId = value;
- }
- }
- }
- for (int i = 0; i < DistinctDeckCardIDs.Count; i++)
- {
- if (i < DistinctDeckCardCounts.Count)
- {
- for (int j = 0; j < DistinctDeckCardCounts[i]; j++)
- {
- _DeckCardIDs.Add(DistinctDeckCardIDs[i]);
- }
- }
- }
- for (int i = 0; i < DistinctDigitamaDeckCardIDs.Count; i++)
- {
- if (i < DistinctDigitamaDeckCardCounts.Count)
- {
- for (int j = 0; j < DistinctDigitamaDeckCardCounts[i]; j++)
- {
- _DigitamaDeckCardIDs.Add(DistinctDigitamaDeckCardIDs[i]);
- }
- }
- }
- DeckCardIDs = _DeckCardIDs;
- DigitamaDeckCardIDs = _DigitamaDeckCardIDs;
- }
- #endregion
- #region sort value
- int _sortValue = 0;
- public int SortValue
- {
- get
- {
- return _sortValue;
- }
- set
- {
- _sortValue = value;
- }
- }
- #endregion
- #region deck id
- string _deckID = "";
- public string DeckID
- {
- get
- {
- if (string.IsNullOrEmpty(_deckID))
- {
- StringBuilder builder = new StringBuilder();
- Enumerable
- .Range(65, 26)
- .Select(e => ((char)e).ToString())
- .Concat(Enumerable.Range(97, 26).Select(e => ((char)e).ToString()))
- .Concat(Enumerable.Range(0, 10).Select(e => e.ToString()))
- .OrderBy(e => Guid.NewGuid())
- .Take(11)
- .ToList().ForEach(e => builder.Append(e));
- _deckID = builder.ToString();
- }
- return _deckID;
- }
- set
- {
- _deckID = value;
- }
- }
- #endregion
- #region deck name
- string _deckName = "";
- public string DeckName
- {
- get
- {
- if (string.IsNullOrEmpty(_deckName))
- {
- return "NewDeck";
- }
- return _deckName;
- }
- set
- {
- _deckName = value;
- }
- }
- #endregion
- #region List of cards included in the deck
- public List<CEntity_Base> DeckCards()
- {
- List<CEntity_Base> deckCards = new List<CEntity_Base>();
- if (DeckCardIDs != null)
- {
- foreach (int DeckCardID in DeckCardIDs)
- {
- CEntity_Base cEntity_Base = ContinuousController.instance.getCardEntityByCardID(DeckCardID);
- if (cEntity_Base != null)
- {
- deckCards.Add(cEntity_Base);
- }
- }
- }
- return deckCards;
- }
- #endregion
- #region List of cards included in the Digitama deck
- public List<CEntity_Base> DigitamaDeckCards()
- {
- List<CEntity_Base> deckCards = new List<CEntity_Base>();
- foreach (int DeckCardID in DigitamaDeckCardIDs)
- {
- CEntity_Base cEntity_Base = ContinuousController.instance.getCardEntityByCardID(DeckCardID);
- if (cEntity_Base != null)
- {
- deckCards.Add(cEntity_Base);
- }
- }
- return deckCards;
- }
- #endregion
- #region all cards in the deck
- public List<CEntity_Base> AllDeckCards()
- {
- List<CEntity_Base> AllDeckCards = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in DigitamaDeckCards())
- {
- AllDeckCards.Add(cEntity_Base);
- }
- foreach (CEntity_Base cEntity_Base in DeckCards())
- {
- AllDeckCards.Add(cEntity_Base);
- }
- return AllDeckCards;
- }
- #endregion
- #region key card
- public int KeyCardId { get; set; } = -1;
- public CEntity_Base KeyCard
- {
- get
- {
- if (KeyCardId >= 0)
- {
- foreach (CEntity_Base Card in ContinuousController.instance.CardList)
- {
- if (Card.CardIndex == KeyCardId)
- {
- return Card;
- }
- }
- }
- if (DeckCards().Count >= 1)
- {
- List<CEntity_Base> deckCards = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in DeckCards())
- {
- deckCards.Add(cEntity_Base);
- }
- deckCards = deckCards
- .OrderBy(value => Array.IndexOf(DataBase.cardKinds, value.cardKind))
- .ThenByDescending(value => value.Level)
- .ThenBy(value => Array.IndexOf(DataBase.cardColors, value.cardColors[0]))
- .ThenByDescending(value => value.PlayCost)
- .ThenByDescending(value => value.DP)
- .ThenBy(value => value.CardIndex)
- .ToList();
- return deckCards[0];
- }
- else if (DigitamaDeckCards().Count >= 1)
- {
- return DigitamaDeckCards()[0];
- }
- return null;
- }
- }
- #endregion
- #region List of card IDs included in the deck
- public List<int> DeckCardIDs { get; set; } = new List<int>();
- public List<int> DigitamaDeckCardIDs { get; set; } = new List<int>();
- #region カードを追加する
- public void AddCard(CEntity_Base cEntity_Base)
- {
- if (cEntity_Base.cardKind == CardKind.DigiEgg)
- {
- AddDigitamaDeckCard(cEntity_Base);
- }
- else
- {
- AddDeckCard(cEntity_Base);
- }
- }
- #endregion
- #region カードを除く
- public void RemoveCard(CEntity_Base cEntity_Base)
- {
- if (cEntity_Base.cardKind == CardKind.DigiEgg)
- {
- RemoveDigitamaDeckCard(cEntity_Base);
- }
- else
- {
- RemoveDeckCard(cEntity_Base);
- }
- }
- #endregion
- #region デッキにカードを追加する
- void AddDeckCard(CEntity_Base cEntity_Base)
- {
- List<CEntity_Base> _DeckCards = DeckCards();
- _DeckCards.Add(cEntity_Base);
- _DeckCards = SortedDeckCardsList(_DeckCards);
- DeckCardIDs = GetDeckCardCodes(_DeckCards);
- }
- #endregion
- #region デッキからカードを抜く
- void RemoveDeckCard(CEntity_Base cEntity_Base)
- {
- List<CEntity_Base> _DeckCards = DeckCards();
- _DeckCards.Remove(cEntity_Base);
- _DeckCards = SortedDeckCardsList(_DeckCards);
- DeckCardIDs = GetDeckCardCodes(_DeckCards);
- }
- #endregion
- #region デジタマデッキにカードを追加する
- void AddDigitamaDeckCard(CEntity_Base cEntity_Base)
- {
- List<CEntity_Base> _DeckCards = DigitamaDeckCards();
- _DeckCards.Add(cEntity_Base);
- _DeckCards = SortedDeckCardsList(_DeckCards);
- DigitamaDeckCardIDs = GetDeckCardCodes(_DeckCards);
- }
- #endregion
- #region デジタマデッキからカードを抜く
- void RemoveDigitamaDeckCard(CEntity_Base cEntity_Base)
- {
- List<CEntity_Base> _DeckCards = DigitamaDeckCards();
- _DeckCards.Remove(cEntity_Base);
- _DeckCards = SortedDeckCardsList(_DeckCards);
- DigitamaDeckCardIDs = GetDeckCardCodes(_DeckCards);
- }
- #endregion
- #region カードリストからカードIDリストを取得
- public static List<int> GetDeckCardCodes(List<CEntity_Base> DeckCards)
- {
- List<int> _DeckCardCodes = new List<int>();
- foreach (CEntity_Base DeckCard in DeckCards)
- {
- _DeckCardCodes.Add(DeckCard.CardIndex);
- }
- return _DeckCardCodes;
- }
- #endregion
- #endregion
- #region Sort card list
- public static List<CEntity_Base> SortedDeckCardsList(List<CEntity_Base> DeckCards)
- {
- List<CEntity_Base> DeckCards1 = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in DeckCards)
- {
- DeckCards1.Add(cEntity_Base);
- }
- DeckCards1 = DeckCards1
- .OrderBy(value => Array.IndexOf(DataBase.cardKinds, value.cardKind))
- .ThenBy(value => value.Level)
- .ThenBy(value => Array.IndexOf(DataBase.cardColors, value.cardColors[0]))
- .ThenByDescending(value => value.PlayCost)
- .ThenByDescending(value => value.DP)
- .ThenBy(value => value.CardIndex)
- .ToList();
- return DeckCards1;
- }
- public static List<CEntity_Base> SortedCardPoolList(List<CEntity_Base> DeckCards)
- {
- List<CEntity_Base> DeckCards1 = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in DeckCards)
- {
- DeckCards1.Add(cEntity_Base);
- }
- DeckCards1 = DeckCards1
- .OrderBy(value => Array.IndexOf(DataBase.SetIDs, value.SetID))
- .ThenBy(value => Array.IndexOf(DataBase.cardColors, value.cardColors[0]))
- .ThenBy(value => value.CardID)
- .ThenBy(value => value.CardIndex)
- .ToList();
- return DeckCards1;
- }
- public static List<CardSource> SortedCardsList(List<CardSource> DeckCards)
- {
- List<CardSource> DeckCards1 = new List<CardSource>();
- foreach (CardSource cardSource in DeckCards)
- {
- DeckCards1.Add(cardSource);
- }
- DeckCards1 = DeckCards1
- .OrderBy(value => value.SetID)
- .ThenBy(value => Array.IndexOf(DataBase.cardKinds, value.CardKind))
- .ThenBy(value => Array.IndexOf(DataBase.cardColors, value.BaseCardColorsFromEntity[0]))
- .ThenByDescending(value => value.BaseEvoCostsFromEntity.Count)
- .ThenByDescending(value => value.CardDP)
- .ThenBy(value => value.CardEntityIndex)
- .ToList();
- return DeckCards1;
- }
- #endregion
- #region Get 256 hexadecimal deck code
- #region Get the 256-decimal deck code from the deck name and deck card.
- public static string GetDeckCode(string _DeckName, List<CEntity_Base> _DeckCards, List<CEntity_Base> _DigitamaDeckCards, CEntity_Base keyCard)
- {
- string _DeckDataString = null;
- //deck name
- _DeckDataString += _DeckName + ",";
- SetDeckCard(_DeckCards);
- SetDeckCard(_DigitamaDeckCards);
- if (keyCard != null)
- {
- _DeckDataString += $"{keyCard.CardIndex},";
- }
- else
- {
- _DeckDataString += $"-1,";
- }
- //Debug.Log($"raw deck cord:{_DeckDataString}");
- void SetDeckCard(List<CEntity_Base> cEntity_Bases)
- {
- //Make the card list a non-duplicate list
- List<CEntity_Base> DistinctDeckCards = cEntity_Bases.Distinct().ToList();
- //Register a card ID list without duplicates
- foreach (CEntity_Base cardData in DistinctDeckCards)
- {
- _DeckDataString += cardData.CardIndex_String;
- }
- _DeckDataString += ",";
- //Save the number of cards of each card type (reduce by 1)
- List<int> _DistinctDeckCardCounts = new List<int>();
- foreach (CEntity_Base cardData in DistinctDeckCards)
- {
- _DistinctDeckCardCounts.Add(cEntity_Bases.Count((_CardData) => _CardData == cardData) - 1);
- }
- string x_n = null;
- //Convert the number of cards in the deck to n-ary number
- for (int i = 0; i < _DistinctDeckCardCounts.Count; i++)
- {
- x_n += ConvertBinaryNumber.IntToNString(_DistinctDeckCardCounts[i], n);
- }
- //Fill with 0s so that the number of digits is log(n)m digits
- if (x_n != null)
- {
- while (x_n.Count() % log_n_m != 0)
- {
- x_n += "0";
- }
- }
- //Convert n-ary number to m-ary number
- if (x_n != null)
- {
- string x_m = ConvertBinaryNumber.NStringToNKString(x_n, n, log_n_m);
- _DeckDataString += x_m;
- }
- _DeckDataString += ",";
- }
- return _DeckDataString;
- }
- #endregion
- #region Get the deck code for this deck
- public string GetThisDeckCode()
- {
- return DeckData.GetDeckCode(DeckName, DeckCards(), DigitamaDeckCards(), KeyCard);
- }
- #endregion
- #endregion
- #region Is the string suitable as a deck code?
- public static bool IsValidDeckCode(string DeckCode)
- {
- if (!DeckCode.Contains(","))
- {
- return false;
- }
- string[] parseByComma = DeckCode.Split(',');
- if (parseByComma.Length != 5 && parseByComma.Length != 6)
- {
- return false;
- }
- if (!string.IsNullOrEmpty(parseByComma[1]))
- {
- string[] SplitText = SplitClass.Split(parseByComma[1], 1);
- for (int j = 0; j < SplitText.Length; j++)
- {
- if (ConvertBinaryNumber.NStringToInt(SplitText[j], m) == 114514)
- {
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- if (!string.IsNullOrEmpty(parseByComma[2]))
- {
- //256進数の文字列
- string x_m = parseByComma[2];
- //256進数をn進数に変換
- string x_n = ConvertBinaryNumber.NKStringToNString(x_m, n, log_n_m);
- if (x_n == "114514")
- {
- Debug.Log("[2]:114514");
- return false;
- }
- }
- else
- {
- return false;
- }
- if (!string.IsNullOrEmpty(parseByComma[3]))
- {
- string[] SplitText = SplitClass.Split(parseByComma[3], 1);
- for (int j = 0; j < SplitText.Length; j++)
- {
- if (ConvertBinaryNumber.NStringToInt(SplitText[j], m) == 114514)
- {
- return false;
- }
- }
- }
- if (!string.IsNullOrEmpty(parseByComma[4]))
- {
- //256進数の文字列
- string x_m = parseByComma[4];
- //256進数をn進数に変換
- string x_n = ConvertBinaryNumber.NKStringToNString(x_m, n, log_n_m);
- if (x_n == "114514")
- {
- Debug.Log("[4]:114514");
- return false;
- }
- }
- if (parseByComma.Length >= 6)
- {
- if (!string.IsNullOrEmpty(parseByComma[5]))
- {
- if (!int.TryParse(parseByComma[5], out int value))
- {
- return false;
- }
- }
- }
- return true;
- }
- #endregion
- #region Can this deck data be used in battle?
- public bool IsValidDeckData()
- {
- //The number of cards in the deck is exactly 50.
- if (DeckCards().Count != 50)
- {
- return false;
- }
- if (DigitamaDeckCards().Count > 5)
- {
- return false;
- }
- foreach (CEntity_Base cEntity_Base in DeckCards())
- {
- if (!cEntity_Base.IsStandardValid)
- {
- return false;
- }
- }
- foreach (CEntity_Base cEntity_Base in DigitamaDeckCards())
- {
- if (!cEntity_Base.IsStandardValid)
- {
- return false;
- }
- }
- return true;
- }
- #endregion
- #region blank deck code
- public static DeckData EmptyDeckData()
- {
- return new DeckData("");
- }
- #endregion
- #region Correct imported deck data
- public DeckData ModifiedDeckData()
- {
- List<CEntity_Base> deckCards = new List<CEntity_Base>();
- List<CEntity_Base> digitamaDeckCards = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in AllDeckCards())
- {
- if (cEntity_Base.cardKind != CardKind.DigiEgg)
- {
- deckCards.Add(cEntity_Base);
- }
- else
- {
- digitamaDeckCards.Add(cEntity_Base);
- }
- }
- List<CEntity_Base> modifiedDeckCards = modifiedList(deckCards);
- List<CEntity_Base> modifiedDigitamaDeckCards = modifiedList(digitamaDeckCards);
- List<CEntity_Base> modifiedList(List<CEntity_Base> cEntity_Bases)
- {
- //Make the card list a non-duplicate list
- List<CEntity_Base> DistinctDeckCards = cEntity_Bases.Distinct().ToList();
- List<CEntity_Base> DistinctDeckCards1 = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in DistinctDeckCards)
- {
- if (DistinctDeckCards1.Count((cEntity_Base1) => cEntity_Base.CardID == cEntity_Base1.CardID) == 0)
- {
- DistinctDeckCards1.Add(cEntity_Base);
- }
- }
- List<CEntity_Base> deckCards = new List<CEntity_Base>();
- foreach (CEntity_Base cEntity_Base in cEntity_Bases)
- {
- deckCards.Add(cEntity_Base);
- }
- //Remove more than the specified number of cards
- foreach (CEntity_Base cEntity_Base in DistinctDeckCards1)
- {
- while (cEntity_Base.SameCardIDCount(deckCards) > cEntity_Base.MaxCountInDeck)
- {
- CEntity_Base removeCard = null;
- foreach (CEntity_Base cEntity_Base1 in deckCards)
- {
- if (cEntity_Base1.CardID == cEntity_Base.CardID)
- {
- removeCard = cEntity_Base1;
- break;
- }
- }
- if (removeCard != null)
- {
- deckCards.Remove(removeCard);
- }
- }
- }
- return deckCards;
- }
- DeckData deckData = new DeckData(GetDeckCode(this._deckName, modifiedDeckCards, modifiedDigitamaDeckCards, KeyCard), DeckID);
- if (!deckData.AllDeckCards().Contains(deckData.KeyCard))
- {
- deckData.KeyCardId = -1;
- }
- DeckData deckData1 = DeckBuildingRule.ModifiedDeckData(deckData);
- return deckData1;
- }
- #endregion
- #region Fixed deck name
- public static string ValidateDeckName(string deckName)
- {
- if (String.IsNullOrEmpty(deckName))
- {
- return deckName;
- }
- StringBuilder sb = new StringBuilder();
- foreach (char c in deckName)
- {
- if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == ' '))
- {
- sb.Append(c);
- }
- }
- deckName = sb.ToString();
- var filter = new ProfanityFilter.ProfanityFilter();
- deckName = filter.CensorString(deckName);
- return deckName;
- }
- #endregion
- }
- #region split string
- public static class SplitClass
- {
- public static string[] Split(this string str, int count)
- {
- var list = new List<string>();
- int length = (int)Math.Ceiling((double)str.Length / count);
- for (int i = 0; i < length; i++)
- {
- int start = count * i;
- if (str.Length <= start)
- {
- break;
- }
- if (str.Length < start + count)
- {
- list.Add(str.Substring(start));
- }
- else
- {
- list.Add(str.Substring(start, count));
- }
- }
- return list.ToArray();
- }
- }
- #endregion
|