[c5a3d1]: Classes / Field / Deck.cs  Maximize  Restore  History

Download this file

139 lines (114 with data), 4.9 kB

  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
using System;
using System.Collections;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
namespace YuGiOh_Ultra {
public sealed class Deck : IEnumerable<ICard> {
public readonly uint Id;
public string Name { get; private set; }
//the real deck structure on database
private readonly Dictionary<ICard, uint> _realcardsstructure = new Dictionary<ICard, uint>();
public CardStack Cards; //deck in game
public uint Count { get { return Cards.Count; } }
/// <summary>
/// Load Deck whit id = deckid
/// </summary>
/// <param name="deckid">the id of the deck</param>
public Deck( uint deckid ) { //load realcards structure!
this.Id = deckid;
var con = new MySqlConnection( Com.connStr );
con.Open();
var cmd = new MySqlCommand( "SELECT `name` FROM `deck` WHERE `id` =" + Id + " LIMIT 1;", con );
var reader = cmd.ExecuteReader();
if( !reader.Read() ) throw new Exception("Can't load Deck!");
Name = reader.GetString( "name" );
{
var cc = new MySqlConnection( Com.connStr );
cc.Open();
var deckcards =
new MySqlCommand(
"SELECT cards.*, `quantity` FROM `deck_cards` JOIN cards ON ( card = code ) WHERE `deckid` =" +
Id + " AND `location` = 'd'", cc );
var readcards = deckcards.ExecuteReader();
while ( readcards.Read() ) {
var c = ACard.Load( readcards );
if ( c != null ) _realcardsstructure.Add( c, readcards.GetUInt16( "quantity" ) );
else throw new InvalidCardException();
}
deckcards.Dispose();
readcards.Close();
cc.Close();
}
Reset();
reader.Close();
cmd.Dispose();
con.Close();
}
public void AddToStructure( ICard c ) {
if( c == null ) throw new InvalidCardException();
if ( _realcardsstructure.ContainsKey( c ) ) _realcardsstructure[c]++;
else _realcardsstructure.Add( c, 1 );
}
/// <summary>
/// Remove 1 occurence of the card
/// </summary>
/// <param name="c">The card</param>
public void RemoveFromStructure( ICard c ) {
if ( !_realcardsstructure.ContainsKey( c ) ) return;
_realcardsstructure[c]--;
if ( _realcardsstructure[c] == 0 ) _realcardsstructure.Remove( c );
}
/// <summary>
/// draws 1 card
/// </summary>
/// <returns>returns the drew card, if deck is empity returns null</returns>
public ICard Draw() {
if ( Cards.IsEmpity() ) throw new EmptyStackException();
return Cards.Remove();
}
/// <summary>
/// draws num cards
/// </summary>
/// <param name="num">number of cards</param>
/// <returns>returns a list of drew cards</returns>
public IList<ICard> Draw( uint num ) {
var r = new List<ICard>( (int)num );
for( int i = 0; i < num; i++ ) r.Add( Draw() );
return r;
}
public void Shuffle() { Cards.Shuffle(); }
/// <summary>
/// Reloads cards after battle, also Shuffles
/// </summary>
public void Reset() {
Cards = new CardStack();
foreach( var c in GetStructure() ) Cards.AddTop(c);
Shuffle();
}
public void Save() {
//string s = "";
//foreach (var c in _realcardsstructure) { s += c.Key.Code + "|"; }
//s = s.Remove( s.Length - 1, 1 );
//var con = new MySqlConnection( Com.connStr );
//con.Open();
//new MySqlCommand( "UPDATE `deck` SET `cards` = '" + s + "' WHERE `id` =" + _id, con).ExecuteNonQuery();
//con.Close();
var con = new MySqlConnection( Com.connStr );
con.Open();
foreach ( var c in _realcardsstructure ) {
new MySqlCommand( "REPLACE INTO `deck_cards` (`deckid`, `location`, `card`, `quantity`) VALUES ("+
Id +", 'd', '"+ c.Key.Code +"', "+ c.Value +");", con ).ExecuteNonQuery();
}
con.Close();
}
public IEnumerator<ICard> GetEnumerator() { return Cards.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return Cards.GetEnumerator(); }
public IEnumerable<ICard> GetStructure() {
var l = new Queue<ICard>();
foreach ( var c in _realcardsstructure ) {
for( int i=0; i < c.Value; i++ ) l.Enqueue(c.Key);
}
return l;
}
}
}