Skip to content

Commit 08aab34

Browse files
committed
feat(compared_roll): refactoring du roll + ajout compared roll
1 parent 8ba20c6 commit 08aab34

3 files changed

Lines changed: 292 additions & 177 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package fr.jim.entites.dto;
2+
3+
public class ComparedRoll {
4+
5+
private Roll roll;
6+
7+
private String comparator;
8+
9+
private int comparedNumber;
10+
11+
private String[] comparators = {"<", ">"};
12+
13+
public ComparedRoll(String stringRoll) {
14+
this.construireComparedRoll(stringRoll);
15+
16+
}
17+
18+
private void construireComparedRoll(String stringRoll) {
19+
20+
this.roll = new Roll(stringRoll, true);
21+
22+
int comparatorIndex = 0;
23+
String foundComparator = null;
24+
25+
for (String comp : comparators) {
26+
int index = stringRoll.indexOf(comp);
27+
if (index != -1) {
28+
comparatorIndex = index;
29+
foundComparator = comp;
30+
break;
31+
}
32+
}
33+
34+
this.comparator = foundComparator;
35+
this.comparedNumber = Integer
36+
.parseInt(stringRoll.substring(comparatorIndex + foundComparator.length()).trim());
37+
}
38+
39+
public Roll getRoll() {
40+
return roll;
41+
}
42+
43+
public String getComparator() {
44+
return comparator;
45+
}
46+
47+
public int getComparedNumber() {
48+
return comparedNumber;
49+
}
50+
51+
public boolean isSuccess() {
52+
switch (this.comparator) {
53+
case "<":
54+
return this.getRoll().getTotal() < comparedNumber;
55+
case ">":
56+
return this.getRoll().getTotal() > comparedNumber;
57+
}
58+
return false;
59+
}
60+
}

src/main/java/fr/jim/entites/dto/Roll.java

Lines changed: 201 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package fr.jim.entites.dto;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
35
import java.util.ArrayList;
46
import java.util.List;
7+
import java.util.Random;
58

69
public class Roll {
710

811
private List<String> resultats = new ArrayList<>();
9-
private List<String> resultatsRolls = new ArrayList<>();
12+
private List<String> intitules = new ArrayList<>();
13+
14+
private StringBuilder totalString = new StringBuilder("**Total = ");
1015

11-
private StringBuilder total = new StringBuilder("**Total = ");
16+
private int total;
1217

1318
private int nbLancers;
1419

@@ -18,31 +23,203 @@ public class Roll {
1823

1924
private String symbole = "";
2025

21-
public Roll() {
26+
private boolean isComparedRoll = false;
27+
28+
public Roll(String stringRoll) {
29+
this.construireRoll(stringRoll);
30+
this.alimenterIntitules(stringRoll);
31+
this.alimenterResultatsEtTotal(stringRoll);
2232
}
2333

24-
public List<String> getResultats() {
25-
return resultats;
34+
public Roll(String stringRoll, boolean isComparedRoll) {
35+
this.isComparedRoll = isComparedRoll;
36+
this.construireRoll(stringRoll);
37+
this.alimenterIntitules(stringRoll);
38+
this.alimenterResultatsEtTotal(stringRoll);
39+
}
40+
41+
private void construireRoll(String stringRoll) {
42+
43+
if (this.isComparedRoll) {
44+
stringRoll = stringRoll.split("[<>]")[0].trim();
45+
}
46+
47+
this.setSymbole("");
48+
49+
// Déterminer tous les attributs du stringRoll
50+
if (stringRoll.contains("d") || stringRoll.contains("D")) {
51+
52+
// 1d6-1 1d12+2
53+
if (stringRoll.contains("-") || stringRoll.contains("+")) {
54+
55+
String[] valeurs = stringRoll.split("[dD]");
56+
this.setNbLancers(Integer.parseInt(valeurs[0]));
57+
this.setNbFaces(Integer.parseInt(valeurs[1].split("[\\+\\-]")[0]));
58+
this.setModificateur(Integer.parseInt(valeurs[1].split("[\\+\\-]")[1]));
59+
this.setSymbole(valeurs[1].contains("+") ? "+" : "-");
60+
61+
62+
} else {
63+
// 1d6 2d12
64+
String[] valeurs = stringRoll.split("[dD]");
65+
this.setNbLancers(Integer.parseInt(valeurs[0]));
66+
this.setNbFaces(Integer.parseInt(valeurs[1]));
67+
}
68+
69+
} else {
70+
// 6 12
71+
this.setNbLancers(Integer.parseInt("1"));
72+
this.setNbFaces(Integer.parseInt(stringRoll.split("[\\+\\-]")[0]));
73+
74+
// 6-1 12+2
75+
if (stringRoll.contains("-") || stringRoll.contains("+")) {
76+
77+
this.setModificateur(Integer.parseInt(stringRoll.split("[\\+\\-]")[1]));
78+
this.setSymbole(stringRoll.contains("+") ? "+" : "-");
79+
80+
}
81+
82+
}
83+
84+
}
85+
86+
private void alimenterIntitules(String stringRoll) {
87+
// Alimenter les intitulés des rolls.
88+
for (int z = 0; z < this.getNbLancers(); z++) {
89+
90+
if (!stringRoll.contains("d") && !stringRoll.contains("D")) {
91+
92+
if (this.getModificateur() != 0) {
93+
94+
this.getIntitules().add("1d" + stringRoll.split("[\\+\\-]")[0]);
95+
96+
} else {
97+
98+
this.getIntitules().add("1d" + stringRoll);
99+
100+
}
101+
102+
103+
} else {
104+
105+
if (this.getModificateur() != 0) {
106+
107+
this.getIntitules().add("1d" + stringRoll.split("[dD]")[1].split("[\\+\\-]")[0]);
108+
109+
} else {
110+
111+
this.getIntitules().add("1d" + stringRoll.split("[dD]")[1]);
112+
113+
}
114+
115+
116+
}
117+
}
26118
}
27119

28-
public void setResultats(List<String> resultats) {
29-
this.resultats = resultats;
120+
private void alimenterResultatsEtTotal(String stringRoll) {
121+
122+
int res;
123+
int total = 0;
124+
Random random = new Random();
125+
126+
// Alimenter les résultats des rolls
127+
for (int i = 0; i < this.getNbLancers(); i++) {
128+
129+
res = random.nextInt(this.getNbFaces()) + 1;
130+
131+
if (StringUtils.isEmpty(this.getSymbole())) {
132+
133+
total += (res);
134+
this.getResultats().add(Integer.toString(res));
135+
136+
} else {
137+
138+
if ("+".equals(this.getSymbole())) {
139+
140+
total += res + this.getModificateur();
141+
this.getResultats().add((Integer.toString(res)));
142+
143+
} else {
144+
145+
total += res - this.getModificateur();
146+
this.getResultats().add((Integer.toString(res)));
147+
148+
149+
}
150+
}
151+
152+
153+
}
154+
155+
// Alimenter le total du Roll
156+
if (this.getNbLancers() > 1) {
157+
158+
if ("+".equals(this.getSymbole())) {
159+
160+
total -= this.getModificateur() * (this.getNbLancers() - 1);
161+
162+
this.getTotalString()
163+
.append((total - this.getModificateur()) + " " + this.getSymbole() + " " +
164+
this.getModificateur() + " = " + total + "**");
165+
this.total = total;
166+
167+
} else if ("-".equals(this.getSymbole())) {
168+
169+
total += this.getModificateur() * (this.getNbLancers() - 1);
170+
171+
this.getTotalString()
172+
.append((total + this.getModificateur()) + " " + this.getSymbole() + " " +
173+
this.getModificateur() + " = " + total + "**");
174+
175+
this.total = total;
176+
177+
} else {
178+
179+
this.getTotalString().append(total + "**");
180+
181+
this.total = total;
182+
}
183+
} else {
184+
185+
if ("+".equals(this.getSymbole())) {
186+
this.getTotalString()
187+
.append((total - this.getModificateur()) + " " + this.getSymbole() + " " +
188+
this.getModificateur() + " = " + total + "**");
189+
this.total = total;
190+
191+
} else if ("-".equals(this.getSymbole())) {
192+
193+
total += this.getModificateur() * (this.getNbLancers() - 1);
194+
195+
this.getTotalString()
196+
.append((total + this.getModificateur()) + " " + this.getSymbole() + " " +
197+
this.getModificateur() + " = " + total + "**");
198+
this.total = total;
199+
200+
} else {
201+
202+
this.total = total;
203+
}
204+
205+
}
30206
}
31207

32-
public List<String> getResultatsRolls() {
33-
return resultatsRolls;
208+
209+
public List<String> getResultats() {
210+
return resultats;
34211
}
35212

36-
public void setResultatsRolls(List<String> resultatsRolls) {
37-
this.resultatsRolls = resultatsRolls;
213+
public List<String> getIntitules() {
214+
return intitules;
38215
}
39216

40-
public StringBuilder getTotal() {
41-
return total;
217+
public StringBuilder getTotalString() {
218+
return totalString;
42219
}
43220

44-
public void setTotal(StringBuilder total) {
45-
this.total = total;
221+
public int getTotal() {
222+
return total;
46223
}
47224

48225
public int getNbLancers() {
@@ -79,38 +256,38 @@ public void setSymbole(String symbole) {
79256

80257
@Override public String toString() {
81258

82-
if (this.getResultatsRolls().size() == 1 && this.getModificateur() == 0) {
259+
if (this.getIntitules().size() == 1 && this.getModificateur() == 0) {
83260
if (this.nbFaces == 20 && "20".equals(this.getResultats().get(0))) {
84-
return ":crossed_swords: " + this.getResultatsRolls().get(0) + " = " + this.getResultats().get(0) +
261+
return ":crossed_swords: " + this.getIntitules().get(0) + " = " + this.getResultats().get(0) +
85262
" :crossed_swords:\n-------------------\n";
86263
} else if (this.nbFaces == 20 && "1".equals(this.getResultats().get(0))) {
87264

88-
return ":csob: " + this.getResultatsRolls().get(0) + " = " + this.getResultats().get(0) +
265+
return ":csob: " + this.getIntitules().get(0) + " = " + this.getResultats().get(0) +
89266
" :sob:\n-------------------\n";
90267
} else {
91268

92-
return this.getResultatsRolls().get(0) + " = " + this.getResultats().get(0) +
269+
return this.getIntitules().get(0) + " = " + this.getResultats().get(0) +
93270
"\n-------------------\n";
94271
}
95272
} else {
96273

97274
StringBuilder sb = new StringBuilder();
98-
for (int i = 0; i < this.getResultatsRolls().size(); i++) {
275+
for (int i = 0; i < this.getIntitules().size(); i++) {
99276

100277
if (this.nbFaces == 20 && "20".equals(this.getResultats().get(i))) {
101278

102-
sb.append(":crossed_swords: " + this.getResultatsRolls().get(i) + " = " + this.getResultats().get(i)
279+
sb.append(":crossed_swords: " + this.getIntitules().get(i) + " = " + this.getResultats().get(i)
103280
+ " :crossed_swords:\n");
104281
} else if (this.nbFaces == 20 && "1".equals(this.getResultats().get(i))) {
105282

106-
sb.append(":sob: " + this.getResultatsRolls().get(i) + " = " + this.getResultats().get(i)
283+
sb.append(":sob: " + this.getIntitules().get(i) + " = " + this.getResultats().get(i)
107284
+ " :sob:\n");
108285
} else {
109-
sb.append(this.getResultatsRolls().get(i) + " = " + this.getResultats().get(i) + "\n");
286+
sb.append(this.getIntitules().get(i) + " = " + this.getResultats().get(i) + "\n");
110287
}
111288
}
112289

113-
sb.append(this.getTotal() + "\n-------------------\n");
290+
sb.append(this.getTotalString() + "\n-------------------\n");
114291

115292
return sb.toString();
116293

0 commit comments

Comments
 (0)