1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.action;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.sf.magicproject.action.handler.StandardAction;
26 import net.sf.magicproject.clickable.ability.Ability;
27 import net.sf.magicproject.clickable.targetable.card.CardModelImpl;
28 import net.sf.magicproject.clickable.targetable.card.MCard;
29 import net.sf.magicproject.database.DatabaseCard;
30 import net.sf.magicproject.event.context.ContextEventListener;
31 import net.sf.magicproject.expression.ListExpression;
32 import net.sf.magicproject.test.TestOn;
33
34 /***
35 * Copy a card on the specified card.
36 *
37 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38 * @since 0.94
39 */
40 class CopyCard extends UserAction implements StandardAction {
41
42 /***
43 * Create an instance of CopyCard by reading a file Offset's file must
44 * pointing on the first byte of this action
45 * <ul>
46 * Structure of InputStream : Data[size]
47 * <li>card to copy [TestOn]</li>
48 * <li>card to apply copy [TestOn]</li>
49 * <li>excludes name [boolean]</li>
50 * <li>excludes colors [ListExpression]</li>
51 * <li>excludes idcards [ListExpression]</li>
52 * </ul>
53 *
54 * @param inputFile
55 * file containing this action
56 * @throws IOException
57 * If some other I/O error occurs
58 */
59 CopyCard(InputStream inputFile) throws IOException {
60 super(inputFile);
61 copySource = TestOn.deserialize(inputFile);
62 copyDestination = TestOn.deserialize(inputFile);
63 excludeName = inputFile.read() == 1;
64 excludesColors = new ListExpression(inputFile);
65 excludesIdCards = new ListExpression(inputFile);
66 }
67
68 @Override
69 public final Actiontype getIdAction() {
70 return Actiontype.COPY_CARD;
71 }
72
73 public boolean play(ContextEventListener context, Ability ability) {
74 MCard source = copySource.getCard(ability, context, null);
75 MCard to = copyDestination.getCard(ability, context, null);
76
77 CardModelImpl sourceModel = (CardModelImpl) source.getCardModel();
78 CardModelImpl targetModel = (CardModelImpl) to.getCardModel();
79 CardModelImpl newModel = new CardModelImpl(sourceModel);
80
81
82 newModel.setAbilities(source.cachedAbilities);
83
84
85 int idColor = newModel.getIdColor();
86 for (int color : excludesColors.getList(ability, null, context)) {
87 idColor = (sourceModel.getIdColor() & ~color)
88 | (targetModel.getIdColor() & color);
89 }
90 newModel.setIdColor(idColor);
91
92
93 int idCard = newModel.getIdCard();
94 for (int cardType : excludesIdCards.getList(ability, null, context)) {
95 idCard = (sourceModel.getIdCard() & ~cardType)
96 | (targetModel.getIdCard() & cardType);
97 }
98 newModel.setIdCard(idCard);
99
100
101 final DatabaseCard database;
102 if (excludeName) {
103 newModel.setCardName(targetModel.getCardName());
104 }
105
106 database = new DatabaseCard(newModel, to.getDatabase().getDataProxy(), to
107 .getDatabase().getPictureProxies());
108
109
110 to.setDataBase(database);
111 to.refreshAbilities();
112 return true;
113 }
114
115 @Override
116 public String toString(Ability ability) {
117 return "copy card";
118 }
119
120 /***
121 * The card to copy.
122 */
123 private final TestOn copySource;
124
125 /***
126 * The copy destination.
127 */
128 private final TestOn copyDestination;
129
130 /***
131 * Is the card name is copied.
132 */
133 private final boolean excludeName;
134
135 /***
136 * The colors to exclude.
137 */
138 private final ListExpression excludesColors;
139
140 /***
141 * The idcards to exclude.
142 */
143 private final ListExpression excludesIdCards;
144
145 }