-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.cs
132 lines (109 loc) · 3.35 KB
/
Deck.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public class DeckLoader : DataLoader {
public class CardDef {
[JsonProperty("name")]
public string Name;
[JsonProperty("cost")]
public int Cost;
[JsonProperty("rarity")]
public Card.ERarity Rarity = Card.ERarity.Common;
[JsonProperty("actions")]
public Dictionary<string, object> ActionDefs;
}
public class CardGroup {
[JsonProperty("count")]
public int Count;
[JsonProperty("card")]
public CardDef Card;
}
public static Deck LoadFrom( string absPath )
{
try
{
string encodedDeck = File.ReadAllText(absPath);
var pseudoDeck = JsonConvert.DeserializeObject<List<CardGroup>>(encodedDeck, DataLoader.Settings);
Deck deck = new Deck();
foreach (var group in pseudoDeck)
{
for (int ix = 0; ix < group.Count; ++ix)
{
Card card = new Card(group.Card.Name, group.Card.Cost, group.Card.Rarity);
foreach( var actionName in group.Card.ActionDefs.Keys ) {
var action = ActionFactory.Instance.CreateAction(actionName, group.Card.ActionDefs[actionName]);
card.AddAction(action);
}
deck.AddCard(card);
}
}
return deck;
}
catch( Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
public class Deck {
public LinkedList<Card> Cards {
get;
} = new LinkedList<Card>();
public void AddCard( Card card )
{
Cards.AddLast(card);
}
public void Shuffle()
{
int cardsToShuffle = Cards.Count / 2;
using (var rngSlip = Neo.Utility.DataStructureLibrary<Random>.Instance.CheckOut(DateTime.UtcNow.Millisecond))
{
for (int ix = 0; ix < cardsToShuffle; ++ix)
{
int newPlace = rngSlip.Value.Next(cardsToShuffle - 1, Cards.Count - 1);
var cardNode = Cards.First;
Cards.RemoveFirst();
LinkedListNode<Card> currentNode = Cards.First;
for (int iy = 0; iy < newPlace; ++iy)
{
currentNode = currentNode.Next;
}
Cards.AddAfter(currentNode, cardNode);
}
}
}
public Card DrawTop()
{
if(IsEmpty)
{
return null;
}
Card card = Cards.First.Value;
Cards.RemoveFirst();
return card;
}
public Card DrawRandom()
{
if (IsEmpty)
{
return null;
}
var rngSlip = Neo.Utility.DataStructureLibrary<Random>.Instance.CheckOut(DateTime.UtcNow.Millisecond);
int newIndex = rngSlip.Value.Next(Cards.Count - 1);
rngSlip.Dispose();
LinkedListNode<Card> currentNode = Cards.First;
for (int iy = 0; iy < newIndex; ++iy)
{
currentNode = currentNode.Next;
}
Cards.Remove(currentNode);
return currentNode.Value;
}
public bool IsEmpty {
get {
return Cards.Count <= 0;
}
}
}