Adds saving throws to monster card.

This commit is contained in:
2020-09-01 21:59:16 -07:00
parent c461ca2188
commit 257ecb7758
5 changed files with 227 additions and 0 deletions

View File

@@ -3,11 +3,18 @@ package com.majinnaibu.monstercards.models;
import com.majinnaibu.monstercards.helpers.StringHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
public class Monster {
public Monster() {
mSavingThrows = new HashSet<>();
}
private String mName;
public String getName() {
return mName;
@@ -456,4 +463,127 @@ public class Monster {
return String.format(Locale.US, "%d (%+d)", getCharismaScore(), getCharismaModifier());
}
private HashSet<SavingThrow> mSavingThrows;
public Set<SavingThrow> getSavingThrows() {
return mSavingThrows;
}
public void addSavingThrow(SavingThrow savingThrow) {
mSavingThrows.add(savingThrow);
}
public void removeSavingThrow(SavingThrow savingThrow) {
mSavingThrows.remove(savingThrow);
}
public void clearSavingThrows() {
mSavingThrows.clear();
}
public String getSavingThrowsDescription() {
SavingThrow[] elements = new SavingThrow[mSavingThrows.size()];
elements = mSavingThrows.toArray(elements);
Arrays.sort(elements);
StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (SavingThrow st : elements) {
if (!isFirst) {
sb.append(", ");
}
String name = st.getName();
sb.append(String.format(Locale.US, "%s%s %+d", name.substring(0,1).toUpperCase(Locale.US), name.substring(1), getAbilityModifier(name) + getProficiencyBonus()));
isFirst = false;
}
return sb.toString();
}
public int getProficiencyBonus() {
String challengeRating = getChallengeRating();
if ("*".equals(challengeRating)) {
return getCustomProficiencyBonus();
} else if (
"0".equals(challengeRating) ||
"1/8".equals(challengeRating) ||
"1/4".equals(challengeRating) ||
"1/2".equals(challengeRating) ||
"1".equals(challengeRating) ||
"2".equals(challengeRating) ||
"3".equals(challengeRating) ||
"4".equals(challengeRating)
) {
return 2;
} else if (
"5".equals(challengeRating) ||
"6".equals(challengeRating) ||
"7".equals(challengeRating) ||
"8".equals(challengeRating)
) {
return 3;
} else if (
"9".equals(challengeRating) ||
"10".equals(challengeRating) ||
"11".equals(challengeRating) ||
"12".equals(challengeRating)
) {
return 4;
} else if (
"13".equals(challengeRating) ||
"14".equals(challengeRating) ||
"15".equals(challengeRating) ||
"16".equals(challengeRating)
) {
return 5;
} else if (
"17".equals(challengeRating) ||
"18".equals(challengeRating) ||
"19".equals(challengeRating) ||
"20".equals(challengeRating)
) {
return 6;
} else if (
"21".equals(challengeRating) ||
"22".equals(challengeRating) ||
"23".equals(challengeRating) ||
"24".equals(challengeRating)
) {
return 7;
} else if (
"25".equals(challengeRating) ||
"26".equals(challengeRating) ||
"27".equals(challengeRating) ||
"28".equals(challengeRating)
) {
return 8;
} else if (
"29".equals(challengeRating) ||
"30".equals(challengeRating)
) {
return 9;
} else {
return 0;
}
}
private String mChallengeRating;
public String getChallengeRating() {
return mChallengeRating;
}
public void setChallengeRating(String challengeRating) {
mChallengeRating = challengeRating;
// TODO: update proficiency bonus based on CR
}
private String mCustomChallengeRating;
public String getCustomChallengeRating() {
return mCustomChallengeRating;
}
public void setCustomChallengeRating(String challengeRating) {
mCustomChallengeRating = challengeRating;
}
private int mCustomProficiencyBonus;
public int getCustomProficiencyBonus() {
return mCustomProficiencyBonus;
}
public void setCustomProficiencyBonus(int proficiencyBonus) {
mCustomProficiencyBonus = proficiencyBonus;
}
}

View File

@@ -0,0 +1,37 @@
package com.majinnaibu.monstercards.models;
import java.util.Comparator;
public class SavingThrow implements Comparator<SavingThrow>, Comparable<SavingThrow> {
public SavingThrow(String name, int order) {
mName = name;
mOrder = order;
}
private String mName;
public String getName() {
return mName;
}
public void setName(String value) {
mName = value;
}
private int mOrder;
public int getOrder() {
return mOrder;
}
public void setOrder(int value) {
mOrder = value;
}
@Override
public int compareTo(SavingThrow o) {
return Integer.compare(this.getOrder(), o.getOrder());
}
@Override
public int compare(SavingThrow o1, SavingThrow o2) {
return o1.getOrder() - o2.getOrder();
}
}

View File

@@ -14,7 +14,9 @@ import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.majinnaibu.monstercards.R;
import com.majinnaibu.monstercards.helpers.StringHelper;
import com.majinnaibu.monstercards.models.Monster;
import com.majinnaibu.monstercards.models.SavingThrow;
@SuppressWarnings("FieldCanBeLocal")
public class MonsterFragment extends Fragment {
@@ -57,7 +59,18 @@ public class MonsterFragment extends Fragment {
monster.setIntelligenceScore(Integer.parseInt("10"));
monster.setWisdomScore(Integer.parseInt("14"));
monster.setCharismaScore(Integer.parseInt("15"));
// Saving Throws
monster.addSavingThrow(new SavingThrow("str", 0));
monster.addSavingThrow(new SavingThrow("dex", 1));
monster.addSavingThrow(new SavingThrow("con", 2));
monster.addSavingThrow(new SavingThrow("int", 3));
monster.addSavingThrow(new SavingThrow("wis", 4));
monster.addSavingThrow(new SavingThrow("cha", 5));
// Challenge Rating
monster.setChallengeRating("*");
monster.setCustomChallengeRating("Infinite (0XP)");
monster.setCustomProficiencyBonus(4);
// END remove block
monsterViewModel = new ViewModelProvider(this).get(MonsterViewModel.class);
View root = inflater.inflate(R.layout.fragment_monster, container, false);
@@ -151,6 +164,19 @@ public class MonsterFragment extends Fragment {
}
});
final TextView monsterSavingThrows = root.findViewById(R.id.saving_throws);
monsterViewModel.getSavingThrows().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(String savingThrows) {
if (StringHelper.isNullOrEmpty(savingThrows)) {
monsterSavingThrows.setVisibility(View.GONE);
} else {
monsterSavingThrows.setVisibility(View.VISIBLE);
}
monsterSavingThrows.setText(Html.fromHtml("<b>Saving Throws</b> " + savingThrows));
}
});
return root;
}
}

View File

@@ -32,6 +32,8 @@ public class MonsterViewModel extends ViewModel {
mWisdom.setValue("");
mCharisma = new MutableLiveData<>();
mCharisma.setValue("");
mSavingThrows = new MutableLiveData<>();
mSavingThrows.setValue("");
}
private MutableLiveData<String> mName;
@@ -78,6 +80,10 @@ public class MonsterViewModel extends ViewModel {
public LiveData<String> getCharisma() {
return mCharisma;
}
private MutableLiveData<String> mSavingThrows;
public LiveData<String> getSavingThrows() {
return mSavingThrows;
}
private Monster mMonster;
public void setMonster(Monster monster) {
@@ -93,5 +99,6 @@ public class MonsterViewModel extends ViewModel {
mIntelligence.setValue(monster.getIntelligenceDescription());
mWisdom.setValue(monster.getWisdomDescription());
mCharisma.setValue(monster.getCharismaDescription());
mSavingThrows.setValue(monster.getSavingThrowsDescription());
}
}

View File

@@ -290,6 +290,33 @@
android:textAlignment="center"
tools:text="15 (+2)" />
<ImageView
android:id="@+id/divider3"
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/section_divider"
android:scaleType="fitXY"
android:src="@drawable/ic_section_divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/strength" />
<TextView
android:id="@+id/saving_throws"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider3"
tools:text="Saving Throws" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>