summaryrefslogtreecommitdiff
path: root/src/main/java/controlleur/Instanciation.java
blob: 3ee39d0f4d77b216b447840973cbd63e150171e9 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package controlleur;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import application.Battle;
import modele.Aptitude;
import modele.AptitudeArme;
import modele.Arme;
import modele.ArmeDist;
import modele.ArmeMelee;
import modele.Armee;
import modele.ArmeeListe;
import modele.Faction;
import modele.Figurine;
import modele.Unit;
import modele.User;

public class Instanciation {

	
	//classe qui fait des appel à la BDD pour instancier les classes modele
	
	private static BDD conec;
	private static Battle battle_data = new Battle();
	
	public static Battle getBattleData() {
		return battle_data;
	}
	public static void resetBattle() {
		battle_data = null;
		battle_data = new Battle();
	}
	
	//recupere la liste des faction de la BDD
	public static ArrayList<Faction> getFaction() {
		
		ArrayList<Faction> rendu = new ArrayList<Faction>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			conec = new BDD();
			nom = conec.select("SELECT nom_faction FROM faction;" );
			
			for (String string : nom) {
				rendu.add(new Faction(string));
			}
			
		}catch(SQLException e) {
			
		}
		conec.close();
		return rendu;
	}
	
	//recupere la liste des armée d'une Faction précise
	public static ArrayList<Armee> getArmee(Faction faction){
		ArrayList<Armee> rendu = new ArrayList<Armee>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			conec = new BDD();

			nom = conec.select("SELECT a.nom_armee,a.logo_armee FROM armee a JOIN faction f USING (id_faction) WHERE f.nom_faction = ?;",faction.getNom() );
			
			
			for (int i = 0;i<nom.size();i = i+2) {
				
				rendu.add(new Armee(nom.get(i),nom.get(i+1),faction));
			}
			
		}catch(SQLException e) {
			
		}
		conec.close();
		return rendu;
	}
	
	//Récupere la liste des unités d'une armée précise
	public static ArrayList<Unit> getUniteOfArmy(Armee armee){
		ArrayList<Unit> rendu = new ArrayList<Unit>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			conec = new BDD();
			nom = conec.select("SELECT u.id_unite, u.nom_unite,  u.points_unite,u.logo_unite FROM unite u JOIN armee a USING (id_armee) WHERE a.nom_armee = ? LIMIT 20;",armee.getName() );
			
			
			for (int i = 0;i<nom.size();i = i+4) {
				rendu.add(new Unit(Instanciation.getFigurine(nom.get(i+1)),Integer.parseInt(nom.get(i)),nom.get(i+1), Integer.parseInt(nom.get(i+2)),nom.get(i+3),armee));
			}
			
			
			
			
		}catch(SQLException e) {
			
		}
		conec.close();
		return rendu;
	}
	
	public static ArrayList<Unit> getUniteOfArmyLimited(Armee armee){
		ArrayList<Unit> rendu = new ArrayList<Unit>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			conec = new BDD();
			nom = conec.select("SELECT u.id_unite, u.nom_unite,  u.points_unite,u.logo_unite FROM unite u JOIN armee a USING (id_armee) WHERE a.nom_armee = ? LIMIT 20;",armee.getName() );
			
			
			for (int i = 0;i<nom.size();i = i+4) {
				rendu.add(new Unit(null,Integer.parseInt(nom.get(i)),nom.get(i+1), Integer.parseInt(nom.get(i+2)),nom.get(i+3),armee));
			}
			
			
			
			
		}catch(SQLException e) {
			
		}
		conec.close();
		return rendu;
	}
	
	
	public static void getArmyLists(User session)
	{
		conec = new BDD();
		String sql = "SELECT nom_liste, description_liste, data_liste FROM liste WHERE id_utilisateur = ?;";
		try {
			PreparedStatement stat = conec.getPreparedStatement(sql, session.getId());
			ResultSet rs = stat.executeQuery();
			session.getListes().clear();
			while(rs.next()){
				ArmeeListe one_list = new ArmeeListe(rs.getString("nom_liste"), rs.getString("description_liste"), rs.getString("data_liste"));
				session.addArmee(one_list);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		conec.close();
	}
	
	public static void getUnitsOfAList(ArmeeListe list) {
		conec = new BDD();
		String sql = "SELECT nom_unite, points_unite, logo_unite, nom_armee, logo_armee"
			+ " FROM unite JOIN contenir USING (id_unite) JOIN liste USING (id_liste) JOIN armee USING (id_armee)"
			+ " WHERE liste.nom_liste = ? AND unite.id_unite = contenir.id_unite AND contenir.id_liste = liste.id_liste;";
		
		try {
			PreparedStatement stat = conec.getPreparedStatement(sql, list.getName());
			ResultSet rs = stat.executeQuery();
			
			while(rs.next()) {
				list.addUnit(new Unit(Instanciation.getFigurine(rs.getString(1)),
						rs.getString(1), rs.getInt(2), rs.getString(3),
						new Armee(rs.getString(4), rs.getString(5))));
			}
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		conec.close();
	}
	
	//Récupere les Figurine présente dans une unité précise
	public static ArrayList<Figurine> getFigurine(String unitName){
		ArrayList<Figurine> rendu = new ArrayList<Figurine>();
		ArrayList<String> temp = new ArrayList<String>();
		try {
			
			temp = conec.select("SELECT f.nom_figurine,f.M,f.E,f.SV,f.PV,f.CD,f.CO,r.nb_figurine FROM figurine f "
					+ "JOIN remplir r USING (id_figurine) JOIN unite u USING(id_unite) WHERE u.nom_unite = ?;",unitName );
			
			
			for (int i = 0;i<temp.size();i = i+8) {
				for (int j =0;j< Integer.parseInt(temp.get(7));j++) {
					rendu.add(new Figurine(Instanciation.getArme(temp.get(i)),Instanciation.getAptitude(temp.get(i)),temp.get(i),"",temp.get(i+1),Integer.parseInt(temp.get(i+2)),Integer.parseInt(temp.get(i+3)),Integer.parseInt(temp.get(i+4)),Integer.parseInt(temp.get(i+5)),Integer.parseInt(temp.get(i+6))));
			
				}
			}
			
			
			
			
		}catch(SQLException e) {
			
		}
		
		return rendu;
	}
	
	public static ArrayList<Figurine> getFigurine2(String unitName){
		ArrayList<Figurine> rendu = new ArrayList<Figurine>();
		ArrayList<String> temp = new ArrayList<String>();
		try {
			conec = new BDD();
			temp = conec.select("SELECT f.nom_figurine,f.M,f.E,f.SV,f.PV,f.CD,f.CO,r.nb_figurine FROM figurine f "
					+ "JOIN remplir r USING (id_figurine) JOIN unite u USING(id_unite) WHERE u.nom_unite = ?;",unitName );
			
			
			for (int i = 0;i<temp.size();i = i+8) {
				for (int j =0;j< Integer.parseInt(temp.get(7));j++) {
					rendu.add(new Figurine(Instanciation.getArme(temp.get(i)),Instanciation.getAptitude(temp.get(i)),temp.get(i),"",temp.get(i+1),Integer.parseInt(temp.get(i+2)),Integer.parseInt(temp.get(i+3)),Integer.parseInt(temp.get(i+4)),Integer.parseInt(temp.get(i+5)),Integer.parseInt(temp.get(i+6))));
			
				}
			}
			
			
			
			
		}catch(SQLException e) {
			
		}
		conec.close();
		return rendu;
	}
	
	//Récupère les armes d'une figurine donnée
	public static ArrayList<Arme> getArme(String figNom){
		ArrayList<Arme> rendu = new ArrayList<Arme>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			
			nom = conec.select("SELECT a.nom_arme,a.PORTEE,a.A,a.F,a.PA,a.D,t.CT,m.CC FROM figurine f JOIN equiper e USING(id_figurine) JOIN arme a USING (id_arme) LEFT JOIN tir t USING (id_tir) LEFT JOIN melee m USING(id_melee) WHERE f.nom_figurine = ?;",figNom );
			
			
			for (int i = 0;i<nom.size();i = i+8) {
				if (nom.get(i+7) == null) {
					rendu.add(new ArmeDist(nom.get(i),Instanciation.getAptitudeArme(nom.get(i)),nom.get(i+1),nom.get(i+2),Integer.parseInt(nom.get(i+3)),Integer.parseInt(nom.get(i+4)),nom.get(i+5),Integer.parseInt(nom.get(i+6))));
				}else {
					rendu.add(new ArmeMelee(nom.get(i),Instanciation.getAptitudeArme(nom.get(i)),nom.get(i+1),nom.get(i+2),Integer.parseInt(nom.get(i+3)),Integer.parseInt(nom.get(i+4)),nom.get(i+5),Integer.parseInt(nom.get(i+7))));
				}
			}
			
			
			
			
			
		}catch(SQLException e) {
			
		}
		
		return rendu;
	}
	
	//Récupère les Aptitude d'une figurine donnée
	public static ArrayList<Aptitude> getAptitude(String figNom){
		ArrayList<Aptitude> rendu = new ArrayList<Aptitude>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			
			nom = conec.select("SELECT a.nom_aptitude FROM aptitude a JOIN permettre USING (id_aptitude) JOIN figurine f USING(id_figurine) WHERE f.nom_figurine = ?;",figNom );
			
			
			for (int i = 0;i<nom.size(); i++) {
				rendu.add(new Aptitude(nom.get(i)));
			}
			
			
			
			
			
			
		}catch(SQLException e) {
			
		}
		
		return rendu;
	}
	
	//Récupère les Aptitude d'une arme donnée
	public static ArrayList<AptitudeArme> getAptitudeArme(String armeNom){
		ArrayList<AptitudeArme> rendu = new ArrayList<AptitudeArme>();
		ArrayList<String> nom = new ArrayList<String>();
		try {
			
			nom = conec.select("SELECT a.nom_aptitude_arme FROM aptitude_arme a JOIN posseder USING (id_aptitude_arme) JOIN arme ar USING(id_arme) WHERE ar.nom_arme = ?;",armeNom );
			
			
			for (int i = 0;i<nom.size();i = i++) {
				rendu.add(new AptitudeArme(nom.get(i)));
			}
			
			
			
			
			
		}catch(SQLException e) {
			
		}
		
		return rendu;
	}
	
	//Insert une Armée crée dans l'application dans la base de donée en la liant à un utilisateur
	public static void insertListe(ArmeeListe armee,User session) {
		conec = new BDD();
		conec.insertListe(armee, session);
		conec.close();
	}
	
}