summaryrefslogtreecommitdiff
path: root/src/main/java/controlleur/Instanciation.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/controlleur/Instanciation.java')
-rw-r--r--src/main/java/controlleur/Instanciation.java304
1 files changed, 304 insertions, 0 deletions
diff --git a/src/main/java/controlleur/Instanciation.java b/src/main/java/controlleur/Instanciation.java
new file mode 100644
index 0000000..3ee39d0
--- /dev/null
+++ b/src/main/java/controlleur/Instanciation.java
@@ -0,0 +1,304 @@
1package controlleur;
2
3import java.sql.PreparedStatement;
4import java.sql.ResultSet;
5import java.sql.SQLException;
6import java.util.ArrayList;
7
8
9import application.Battle;
10import modele.Aptitude;
11import modele.AptitudeArme;
12import modele.Arme;
13import modele.ArmeDist;
14import modele.ArmeMelee;
15import modele.Armee;
16import modele.ArmeeListe;
17import modele.Faction;
18import modele.Figurine;
19import modele.Unit;
20import modele.User;
21
22public class Instanciation {
23
24
25 //classe qui fait des appel à la BDD pour instancier les classes modele
26
27 private static BDD conec;
28 private static Battle battle_data = new Battle();
29
30 public static Battle getBattleData() {
31 return battle_data;
32 }
33 public static void resetBattle() {
34 battle_data = null;
35 battle_data = new Battle();
36 }
37
38 //recupere la liste des faction de la BDD
39 public static ArrayList<Faction> getFaction() {
40
41 ArrayList<Faction> rendu = new ArrayList<Faction>();
42 ArrayList<String> nom = new ArrayList<String>();
43 try {
44 conec = new BDD();
45 nom = conec.select("SELECT nom_faction FROM faction;" );
46
47 for (String string : nom) {
48 rendu.add(new Faction(string));
49 }
50
51 }catch(SQLException e) {
52
53 }
54 conec.close();
55 return rendu;
56 }
57
58 //recupere la liste des armée d'une Faction précise
59 public static ArrayList<Armee> getArmee(Faction faction){
60 ArrayList<Armee> rendu = new ArrayList<Armee>();
61 ArrayList<String> nom = new ArrayList<String>();
62 try {
63 conec = new BDD();
64
65 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() );
66
67
68 for (int i = 0;i<nom.size();i = i+2) {
69
70 rendu.add(new Armee(nom.get(i),nom.get(i+1),faction));
71 }
72
73 }catch(SQLException e) {
74
75 }
76 conec.close();
77 return rendu;
78 }
79
80 //Récupere la liste des unités d'une armée précise
81 public static ArrayList<Unit> getUniteOfArmy(Armee armee){
82 ArrayList<Unit> rendu = new ArrayList<Unit>();
83 ArrayList<String> nom = new ArrayList<String>();
84 try {
85 conec = new BDD();
86 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() );
87
88
89 for (int i = 0;i<nom.size();i = i+4) {
90 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));
91 }
92
93
94
95
96 }catch(SQLException e) {
97
98 }
99 conec.close();
100 return rendu;
101 }
102
103 public static ArrayList<Unit> getUniteOfArmyLimited(Armee armee){
104 ArrayList<Unit> rendu = new ArrayList<Unit>();
105 ArrayList<String> nom = new ArrayList<String>();
106 try {
107 conec = new BDD();
108 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() );
109
110
111 for (int i = 0;i<nom.size();i = i+4) {
112 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));
113 }
114
115
116
117
118 }catch(SQLException e) {
119
120 }
121 conec.close();
122 return rendu;
123 }
124
125
126 public static void getArmyLists(User session)
127 {
128 conec = new BDD();
129 String sql = "SELECT nom_liste, description_liste, data_liste FROM liste WHERE id_utilisateur = ?;";
130 try {
131 PreparedStatement stat = conec.getPreparedStatement(sql, session.getId());
132 ResultSet rs = stat.executeQuery();
133 session.getListes().clear();
134 while(rs.next()){
135 ArmeeListe one_list = new ArmeeListe(rs.getString("nom_liste"), rs.getString("description_liste"), rs.getString("data_liste"));
136 session.addArmee(one_list);
137 }
138 } catch (SQLException e) {
139 e.printStackTrace();
140 }
141 conec.close();
142 }
143
144 public static void getUnitsOfAList(ArmeeListe list) {
145 conec = new BDD();
146 String sql = "SELECT nom_unite, points_unite, logo_unite, nom_armee, logo_armee"
147 + " FROM unite JOIN contenir USING (id_unite) JOIN liste USING (id_liste) JOIN armee USING (id_armee)"
148 + " WHERE liste.nom_liste = ? AND unite.id_unite = contenir.id_unite AND contenir.id_liste = liste.id_liste;";
149
150 try {
151 PreparedStatement stat = conec.getPreparedStatement(sql, list.getName());
152 ResultSet rs = stat.executeQuery();
153
154 while(rs.next()) {
155 list.addUnit(new Unit(Instanciation.getFigurine(rs.getString(1)),
156 rs.getString(1), rs.getInt(2), rs.getString(3),
157 new Armee(rs.getString(4), rs.getString(5))));
158 }
159 }
160 catch (SQLException e) {
161 // TODO Auto-generated catch block
162 e.printStackTrace();
163 }
164 conec.close();
165 }
166
167 //Récupere les Figurine présente dans une unité précise
168 public static ArrayList<Figurine> getFigurine(String unitName){
169 ArrayList<Figurine> rendu = new ArrayList<Figurine>();
170 ArrayList<String> temp = new ArrayList<String>();
171 try {
172
173 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 "
174 + "JOIN remplir r USING (id_figurine) JOIN unite u USING(id_unite) WHERE u.nom_unite = ?;",unitName );
175
176
177 for (int i = 0;i<temp.size();i = i+8) {
178 for (int j =0;j< Integer.parseInt(temp.get(7));j++) {
179 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))));
180
181 }
182 }
183
184
185
186
187 }catch(SQLException e) {
188
189 }
190
191 return rendu;
192 }
193
194 public static ArrayList<Figurine> getFigurine2(String unitName){
195 ArrayList<Figurine> rendu = new ArrayList<Figurine>();
196 ArrayList<String> temp = new ArrayList<String>();
197 try {
198 conec = new BDD();
199 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 "
200 + "JOIN remplir r USING (id_figurine) JOIN unite u USING(id_unite) WHERE u.nom_unite = ?;",unitName );
201
202
203 for (int i = 0;i<temp.size();i = i+8) {
204 for (int j =0;j< Integer.parseInt(temp.get(7));j++) {
205 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))));
206
207 }
208 }
209
210
211
212
213 }catch(SQLException e) {
214
215 }
216 conec.close();
217 return rendu;
218 }
219
220 //Récupère les armes d'une figurine donnée
221 public static ArrayList<Arme> getArme(String figNom){
222 ArrayList<Arme> rendu = new ArrayList<Arme>();
223 ArrayList<String> nom = new ArrayList<String>();
224 try {
225
226 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 );
227
228
229 for (int i = 0;i<nom.size();i = i+8) {
230 if (nom.get(i+7) == null) {
231 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))));
232 }else {
233 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))));
234 }
235 }
236
237
238
239
240
241 }catch(SQLException e) {
242
243 }
244
245 return rendu;
246 }
247
248 //Récupère les Aptitude d'une figurine donnée
249 public static ArrayList<Aptitude> getAptitude(String figNom){
250 ArrayList<Aptitude> rendu = new ArrayList<Aptitude>();
251 ArrayList<String> nom = new ArrayList<String>();
252 try {
253
254 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 );
255
256
257 for (int i = 0;i<nom.size(); i++) {
258 rendu.add(new Aptitude(nom.get(i)));
259 }
260
261
262
263
264
265
266 }catch(SQLException e) {
267
268 }
269
270 return rendu;
271 }
272
273 //Récupère les Aptitude d'une arme donnée
274 public static ArrayList<AptitudeArme> getAptitudeArme(String armeNom){
275 ArrayList<AptitudeArme> rendu = new ArrayList<AptitudeArme>();
276 ArrayList<String> nom = new ArrayList<String>();
277 try {
278
279 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 );
280
281
282 for (int i = 0;i<nom.size();i = i++) {
283 rendu.add(new AptitudeArme(nom.get(i)));
284 }
285
286
287
288
289
290 }catch(SQLException e) {
291
292 }
293
294 return rendu;
295 }
296
297 //Insert une Armée crée dans l'application dans la base de donée en la liant à un utilisateur
298 public static void insertListe(ArmeeListe armee,User session) {
299 conec = new BDD();
300 conec.insertListe(armee, session);
301 conec.close();
302 }
303
304}