View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// Copy abilities
82  		newModel.setAbilities(source.cachedAbilities);
83  
84  		// Card colors
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  		// Card types
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 		// Card name
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 		// Update the database
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 }