| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.cb.jset.client.model; |
| 21 |
|
|
| 22 |
|
import org.apache.log4j.Logger; |
| 23 |
|
import org.cb.jset.JSetCard; |
| 24 |
|
import org.cb.jset.CardProperties; |
| 25 |
|
import org.cb.jset.CardSet; |
| 26 |
|
import org.cb.jset.BoardException; |
| 27 |
|
import org.cb.cardboard.AbstractCardBoardModel; |
| 28 |
|
import org.cb.cardboard.Card; |
| 29 |
|
import org.cb.cardboard.CardBoardEvent; |
| 30 |
|
|
| 31 |
|
import java.util.Collections; |
| 32 |
|
import java.util.List; |
| 33 |
|
import java.util.ArrayList; |
| 34 |
|
import java.io.Serializable; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
1 |
public class JSetClientBoardModel extends AbstractCardBoardModel implements SetGameClientBoard, Serializable |
| 46 |
|
{ |
| 47 |
2 |
private transient final Logger _logger = Logger.getLogger(JSetClientBoardModel.class); |
| 48 |
|
|
| 49 |
|
|
| 50 |
1 |
private final List _cards = new ArrayList(); |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
public void clear() |
| 56 |
|
{ |
| 57 |
0 |
int size = _cards.size(); |
| 58 |
0 |
_cards.clear(); |
| 59 |
|
|
| 60 |
0 |
fireIntervalRemoved(0, size - 1); |
| 61 |
0 |
} |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
public int getSize() |
| 68 |
|
{ |
| 69 |
0 |
return _cards.size(); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
public Card getElementAt(final int index) |
| 78 |
|
{ |
| 79 |
0 |
return (Card) _cards.get(index); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
public List getElements() |
| 86 |
|
{ |
| 87 |
70 |
return Collections.unmodifiableList(_cards); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
public int getNbCards() |
| 95 |
|
{ |
| 96 |
35 |
int lNbCards = 0; |
| 97 |
1085 |
for (int i = 0; i < _cards.size(); i++) |
| 98 |
|
{ |
| 99 |
1050 |
final Card card = (Card) _cards.get(i); |
| 100 |
1050 |
if (card != null) |
| 101 |
|
{ |
| 102 |
999 |
lNbCards++; |
| 103 |
|
} |
| 104 |
|
} |
| 105 |
35 |
return lNbCards; |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
public void addCards(final CardProperties[] cards) |
| 114 |
|
{ |
| 115 |
18 |
logCards("before adding: " + CardProperties.toString(cards)); |
| 116 |
|
|
| 117 |
18 |
int indexOfCardsToAdd = 0; |
| 118 |
18 |
int indexInCardsList = 0; |
| 119 |
|
|
| 120 |
|
|
| 121 |
18 |
final List indexesOfCardsChanged = new ArrayList(); |
| 122 |
|
cardsToAdd: |
| 123 |
|
|
| 124 |
558 |
for (; indexInCardsList < _cards.size() && indexOfCardsToAdd < cards.length; indexInCardsList++) |
| 125 |
|
{ |
| 126 |
270 |
final JSetCard oldCard = (JSetCard) _cards.get(indexInCardsList); |
| 127 |
270 |
if (oldCard == null) |
| 128 |
|
{ |
| 129 |
51 |
final CardProperties cardProperties = cards[indexOfCardsToAdd++]; |
| 130 |
51 |
final Card newCard = new org.cb.jset.JSetCard(cardProperties); |
| 131 |
51 |
_cards.set(indexInCardsList, newCard); |
| 132 |
51 |
indexesOfCardsChanged.add(new Integer(indexInCardsList)); |
| 133 |
51 |
continue cardsToAdd; |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
18 |
if (indexesOfCardsChanged.size() > 0) |
| 137 |
|
{ |
| 138 |
17 |
fireContentsChanged(indexesOfCardsChanged); |
| 139 |
|
} |
| 140 |
18 |
if (indexOfCardsToAdd < cards.length) |
| 141 |
|
{ |
| 142 |
1 |
final int firstListIndex = indexInCardsList; |
| 143 |
1 |
int lastListIndex = 0; |
| 144 |
61 |
for (; indexOfCardsToAdd < cards.length; indexOfCardsToAdd++) |
| 145 |
|
{ |
| 146 |
30 |
final CardProperties cardProperties = cards[indexOfCardsToAdd]; |
| 147 |
30 |
final Card card = new org.cb.jset.JSetCard(cardProperties); |
| 148 |
30 |
_cards.add(indexInCardsList++, card); |
| 149 |
30 |
lastListIndex = indexInCardsList; |
| 150 |
|
} |
| 151 |
1 |
fireIntervalAdded(firstListIndex, (lastListIndex - 1)); |
| 152 |
|
} |
| 153 |
18 |
logCards("after adding: " + CardProperties.toString(cards)); |
| 154 |
18 |
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
public void removeSet(final CardSet set) throws BoardException |
| 162 |
|
{ |
| 163 |
17 |
logCards("before matching: " + CardProperties.toString(set.getCards())); |
| 164 |
17 |
final CardProperties[] cards = set.getCards(); |
| 165 |
|
|
| 166 |
17 |
final int[] indexesOnBoard = new class="keyword">int[cards.length]; |
| 167 |
68 |
for (int i = 0; i < indexesOnBoard.length; i++) |
| 168 |
|
{ |
| 169 |
51 |
final int index = _cards.indexOf(new org.cb.jset.JSetCard(CardProperties.findCard(cards[i]))); |
| 170 |
51 |
if (index == -1) |
| 171 |
|
{ |
| 172 |
0 |
_logger.debug("JSetCard " + cards[i] + " not found. Listing contents of board..."); |
| 173 |
0 |
logCards(""); |
| 174 |
0 |
throw new BoardException("JSetCard " + cards[i] + " not found."); |
| 175 |
|
} |
| 176 |
51 |
indexesOnBoard[i] = index; |
| 177 |
|
} |
| 178 |
|
|
| 179 |
68 |
for (int i = 0; i < indexesOnBoard.length; i++) |
| 180 |
|
{ |
| 181 |
51 |
final int lIndex = indexesOnBoard[i]; |
| 182 |
51 |
_cards.set(lIndex, null); |
| 183 |
51 |
fireIntervalRemoved(lIndex, lIndex); |
| 184 |
|
} |
| 185 |
17 |
logCards("after matching: " + CardProperties.toString(set.getCards())); |
| 186 |
17 |
} |
| 187 |
|
|
| 188 |
|
private void logCards(final String message) |
| 189 |
|
{ |
| 190 |
70 |
_logger.debug("Logging cards: " + message); |
| 191 |
2140 |
for (int j = 0; j < _cards.size(); j++) |
| 192 |
|
{ |
| 193 |
2070 |
final org.cb.jset.JSetCard card = (org.cb.jset.JSetCard) _cards.get(j); |
| 194 |
2070 |
_logger.debug("[" + j + "]:" + card); |
| 195 |
|
} |
| 196 |
70 |
} |
| 197 |
|
|
| 198 |
|
private void fireContentsChanged(final List indexes) |
| 199 |
|
{ |
| 200 |
17 |
final CardBoardEvent event = new CardBoardEvent(this, indexes); |
| 201 |
17 |
fireContentsChanged(event); |
| 202 |
17 |
} |
| 203 |
|
|
| 204 |
|
private void fireIntervalAdded(final int firstIndex, class="keyword">final class="keyword">int lastIndex) |
| 205 |
|
{ |
| 206 |
1 |
final CardBoardEvent event = new CardBoardEvent(this, CardBoardEvent.INTERVAL_ADDED, firstIndex, lastIndex); |
| 207 |
1 |
fireIntervalAdded(event); |
| 208 |
1 |
} |
| 209 |
|
|
| 210 |
|
private void fireIntervalRemoved(final int firstIndex, class="keyword">final class="keyword">int lastIndex) |
| 211 |
|
{ |
| 212 |
51 |
final CardBoardEvent event = new CardBoardEvent(this, CardBoardEvent.INTERVAL_REMOVED, firstIndex, lastIndex); |
| 213 |
51 |
fireIntervalRemoved(event); |
| 214 |
51 |
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
} |