Ability score stuff

This commit is contained in:
Sijmen 2021-11-13 20:28:15 +01:00
parent 89578e13b7
commit 418a2a7a3e
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
2 changed files with 343 additions and 83 deletions

View File

@ -13,21 +13,22 @@ body {
font-family: "Ubuntu", sans-serif; font-family: "Ubuntu", sans-serif;
} }
label {
display: block;
font-weight: bold;
}
.field { .field {
margin: 0 8px; margin: 0 8px;
} }
.field label { .field label {
display: block;
font-weight: bold;
font-size: 0.8em; font-size: 0.8em;
font-variant: small-caps; font-variant: small-caps;
margin-bottom: 8px; margin-bottom: 8px;
} }
.field label.label-inline {
display: inline;
}
.ability-scores .field input { .ability-scores .field input {
width: 65px; width: 65px;
} }

View File

@ -2,12 +2,21 @@ use yew::services::console::ConsoleService;
#[derive(Debug)] #[derive(Debug)]
pub enum Message { pub enum Message {
Strength(yew::ChangeData), StrengthScore(yew::ChangeData),
Dexterity(yew::ChangeData), DexterityScore(yew::ChangeData),
Constitution(yew::ChangeData), ConstitutionScore(yew::ChangeData),
Intelligence(yew::ChangeData), IntelligenceScore(yew::ChangeData),
Wisdom(yew::ChangeData), WisdomScore(yew::ChangeData),
Charisma(yew::ChangeData), CharismaScore(yew::ChangeData),
ProficiencyBonus(yew::ChangeData),
ProficiencyStrengthSaving(yew::ChangeData),
ProficiencyDexteritySaving(yew::ChangeData),
ProficiencyConstitutionSaving(yew::ChangeData),
ProficiencyIntelligenceSaving(yew::ChangeData),
ProficiencyWisdomSaving(yew::ChangeData),
ProficiencyCharismaSaving(yew::ChangeData),
} }
pub struct AbilityScores { pub struct AbilityScores {
@ -19,6 +28,99 @@ pub struct AbilityScores {
intelligence: i32, intelligence: i32,
wisdom: i32, wisdom: i32,
charisma: i32, charisma: i32,
proficiency_bonus: i32,
proficiency_strength_saving: bool,
proficiency_dexterity_saving: bool,
proficiency_constitution_saving: bool,
proficiency_intelligence_saving: bool,
proficiency_wisdom_saving: bool,
proficiency_charisma_saving: bool,
}
impl AbilityScores {
fn score_to_modifier(score: i32) -> i32 {
(score - 10) / 2
}
pub fn strength_modifier(&self) -> i32 {
Self::score_to_modifier(self.strength)
}
pub fn dexterity_modifier(&self) -> i32 {
Self::score_to_modifier(self.dexterity)
}
pub fn constitution_modifier(&self) -> i32 {
Self::score_to_modifier(self.constitution)
}
pub fn intelligence_modifier(&self) -> i32 {
Self::score_to_modifier(self.intelligence)
}
pub fn wisdom_modifier(&self) -> i32 {
Self::score_to_modifier(self.wisdom)
}
pub fn charisma_modifier(&self) -> i32 {
Self::score_to_modifier(self.charisma)
}
pub fn strength_saving(&self) -> i32 {
Self::score_to_modifier(self.strength)
+ if self.proficiency_strength_saving {
self.proficiency_bonus
} else {
0
}
}
pub fn dexterity_saving(&self) -> i32 {
Self::score_to_modifier(self.dexterity)
+ if self.proficiency_dexterity_saving {
self.proficiency_bonus
} else {
0
}
}
pub fn constitution_saving(&self) -> i32 {
Self::score_to_modifier(self.constitution)
+ if self.proficiency_constitution_saving {
self.proficiency_bonus
} else {
0
}
}
pub fn intelligence_saving(&self) -> i32 {
Self::score_to_modifier(self.intelligence)
+ if self.proficiency_intelligence_saving {
self.proficiency_bonus
} else {
0
}
}
pub fn wisdom_saving(&self) -> i32 {
Self::score_to_modifier(self.wisdom)
+ if self.proficiency_wisdom_saving {
self.proficiency_bonus
} else {
0
}
}
pub fn charisma_saving(&self) -> i32 {
Self::score_to_modifier(self.charisma)
+ if self.proficiency_charisma_saving {
self.proficiency_bonus
} else {
0
}
}
} }
impl yew::Component for AbilityScores { impl yew::Component for AbilityScores {
@ -28,37 +130,68 @@ impl yew::Component for AbilityScores {
fn create(_props: Self::Properties, link: yew::ComponentLink<Self>) -> Self { fn create(_props: Self::Properties, link: yew::ComponentLink<Self>) -> Self {
Self { Self {
link, link,
strength: 10, strength: 10,
dexterity: 10, dexterity: 10,
constitution: 10, constitution: 10,
intelligence: 10, intelligence: 10,
wisdom: 10, wisdom: 10,
charisma: 10, charisma: 10,
proficiency_bonus: 2,
proficiency_strength_saving: false,
proficiency_dexterity_saving: false,
proficiency_constitution_saving: false,
proficiency_intelligence_saving: false,
proficiency_wisdom_saving: false,
proficiency_charisma_saving: false,
} }
} }
fn update(&mut self, message: Self::Message) -> yew::ShouldRender { fn update(&mut self, message: Self::Message) -> yew::ShouldRender {
match message { match message {
Message::Strength(yew::ChangeData::Value(strength)) => { Message::StrengthScore(yew::ChangeData::Value(value)) => {
self.strength = strength.parse().unwrap_or(10); self.strength = value.parse().unwrap_or(10);
} }
Message::Dexterity(yew::ChangeData::Value(dexterity)) => { Message::DexterityScore(yew::ChangeData::Value(value)) => {
self.dexterity = dexterity.parse().unwrap_or(10); self.dexterity = value.parse().unwrap_or(10);
} }
Message::Constitution(yew::ChangeData::Value(constitution)) => { Message::ConstitutionScore(yew::ChangeData::Value(value)) => {
self.constitution = constitution.parse().unwrap_or(10); self.constitution = value.parse().unwrap_or(10);
} }
Message::Intelligence(yew::ChangeData::Value(intelligence)) => { Message::IntelligenceScore(yew::ChangeData::Value(value)) => {
self.intelligence = intelligence.parse().unwrap_or(10); self.intelligence = value.parse().unwrap_or(10);
} }
Message::Wisdom(yew::ChangeData::Value(wisdom)) => { Message::WisdomScore(yew::ChangeData::Value(value)) => {
self.wisdom = wisdom.parse().unwrap_or(10); self.wisdom = value.parse().unwrap_or(10);
} }
Message::Charisma(yew::ChangeData::Value(charisma)) => { Message::CharismaScore(yew::ChangeData::Value(value)) => {
self.charisma = charisma.parse().unwrap_or(10); self.charisma = value.parse().unwrap_or(10);
}
Message::ProficiencyBonus(yew::ChangeData::Value(value)) => {
self.proficiency_bonus = value.parse().unwrap_or(2);
}
Message::ProficiencyStrengthSaving(_) => {
self.proficiency_strength_saving = !self.proficiency_strength_saving;
}
Message::ProficiencyDexteritySaving(_) => {
self.proficiency_dexterity_saving = !self.proficiency_dexterity_saving;
}
Message::ProficiencyConstitutionSaving(_) => {
self.proficiency_constitution_saving = !self.proficiency_constitution_saving;
}
Message::ProficiencyIntelligenceSaving(_) => {
self.proficiency_intelligence_saving = !self.proficiency_intelligence_saving;
}
Message::ProficiencyWisdomSaving(_) => {
self.proficiency_wisdom_saving = !self.proficiency_wisdom_saving;
}
Message::ProficiencyCharismaSaving(_) => {
self.proficiency_charisma_saving = !self.proficiency_charisma_saving;
} }
_ => { _ => {
ConsoleService::log(&format!("{:?}", message)); ConsoleService::error(&format!("{:?}", message));
return false; return false;
} }
} }
@ -72,15 +205,16 @@ impl yew::Component for AbilityScores {
fn view(&self) -> yew::Html { fn view(&self) -> yew::Html {
yew::html! { yew::html! {
<div class="columns">
<div class="ability-scores"> <div class="ability-scores">
<div class="field"> <div class="field">
<input <input
type="number" type="number"
id="ability-strength" id="ability-strength"
value={self.strength.to_string()} value={self.strength.to_string()}
onchange=self.link.callback(|e| Message::Strength(e)) onchange=self.link.callback(Message::StrengthScore)
/> />
{(self.strength - 10) / 2} {self.strength_modifier()}
<label for="ability-strength">{"Strength"}</label> <label for="ability-strength">{"Strength"}</label>
</div> </div>
<div class="field"> <div class="field">
@ -88,9 +222,9 @@ impl yew::Component for AbilityScores {
type="number" type="number"
id="ability-dexterity" id="ability-dexterity"
value={self.dexterity.to_string()} value={self.dexterity.to_string()}
onchange=self.link.callback(|e| Message::Dexterity(e)) onchange=self.link.callback(Message::DexterityScore)
/> />
{(self.dexterity - 10) / 2} {self.dexterity_modifier()}
<label for="ability-dexterity">{"Dexterity"}</label> <label for="ability-dexterity">{"Dexterity"}</label>
</div> </div>
<div class="field"> <div class="field">
@ -98,9 +232,9 @@ impl yew::Component for AbilityScores {
type="number" type="number"
id="ability-constitution" id="ability-constitution"
value={self.constitution.to_string()} value={self.constitution.to_string()}
onchange=self.link.callback(|e| Message::Constitution(e)) onchange=self.link.callback(Message::ConstitutionScore)
/> />
{(self.constitution - 10) / 2} {self.constitution_modifier()}
<label for="ability-constitution">{"Constitution"}</label> <label for="ability-constitution">{"Constitution"}</label>
</div> </div>
<div class="field"> <div class="field">
@ -108,9 +242,9 @@ impl yew::Component for AbilityScores {
type="number" type="number"
id="ability-intelligence" id="ability-intelligence"
value={self.intelligence.to_string()} value={self.intelligence.to_string()}
onchange=self.link.callback(|e| Message::Intelligence(e)) onchange=self.link.callback(Message::IntelligenceScore)
/> />
{(self.intelligence - 10) / 2} {self.intelligence_modifier()}
<label for="ability-intelligence">{"Intelligence"}</label> <label for="ability-intelligence">{"Intelligence"}</label>
</div> </div>
<div class="field"> <div class="field">
@ -118,9 +252,9 @@ impl yew::Component for AbilityScores {
type="number" type="number"
id="ability-wisdom" id="ability-wisdom"
value={self.wisdom.to_string()} value={self.wisdom.to_string()}
onchange=self.link.callback(|e| Message::Wisdom(e)) onchange=self.link.callback(Message::WisdomScore)
/> />
{(self.wisdom - 10) / 2} {self.wisdom_modifier()}
<label for="ability-wisdom">{"Wisdom"}</label> <label for="ability-wisdom">{"Wisdom"}</label>
</div> </div>
<div class="field"> <div class="field">
@ -128,12 +262,137 @@ impl yew::Component for AbilityScores {
type="number" type="number"
id="ability-charisma" id="ability-charisma"
value={self.charisma.to_string()} value={self.charisma.to_string()}
onchange=self.link.callback(|e| Message::Charisma(e)) onchange=self.link.callback(Message::CharismaScore)
/> />
{(self.charisma - 10) / 2} {self.charisma_modifier()}
<label for="ability-charisma">{"Charisma"}</label> <label for="ability-charisma">{"Charisma"}</label>
</div> </div>
</div> </div>
<div>
<div class="field">
<input type="number" id="inspiration"/>
<label class="label-inline" for="inspiration">
{"Inspiration"}
</label>
</div>
<div class="field">
<input
type="number"
id="proficiency-bonus"
value={self.proficiency_bonus.to_string()}
onchange=self.link.callback(Message::ProficiencyBonus)
/>
<label class="label-inline" for="proficiency-bonus">
{"Proficiency Bonus"}
</label>
</div>
<div>
<div class="field">
<input
type="checkbox"
id="proficiency-strength-saving"
checked=self.proficiency_strength_saving
onchange=self.link.callback(Message::ProficiencyStrengthSaving)
/>
<input
type="number"
disabled=true
value=self.strength_saving().to_string()
id="strength-saving"
/>
<label class="label-inline" for="proficiency-strength-saving">
{"Strength"}
</label>
</div>
<div class="field">
<input
type="checkbox"
id="proficiency-dexterity-saving"
checked=self.proficiency_dexterity_saving
onchange=self.link.callback(Message::ProficiencyDexteritySaving)
/>
<input
type="number"
disabled=true
value=self.dexterity_saving().to_string()
id="dexterity-saving"
/>
<label class="label-inline" for="proficiency-dexterity-saving">
{"Dexterity"}
</label>
</div>
<div class="field">
<input
type="checkbox"
id="proficiency-constitution-saving"
checked=self.proficiency_constitution_saving
onchange=self.link.callback(Message::ProficiencyConstitutionSaving)
/>
<input
type="number"
disabled=true
value=self.constitution_saving().to_string()
id="constitution-saving"
/>
<label class="label-inline" for="proficiency-constitution-saving">
{"Constitution"}
</label>
</div>
<div class="field">
<input
type="checkbox"
id="proficiency-intelligence-saving"
checked=self.proficiency_intelligence_saving
onchange=self.link.callback(Message::ProficiencyIntelligenceSaving)
/>
<input
type="number"
disabled=true
value=self.intelligence_saving().to_string()
id="intelligence-saving"
/>
<label class="label-inline" for="proficiency-intelligence-saving">
{"Intelligence"}
</label>
</div>
<div class="field">
<input
type="checkbox"
id="proficiency-wisdom-saving"
checked=self.proficiency_wisdom_saving
onchange=self.link.callback(Message::ProficiencyWisdomSaving)
/>
<input
type="number"
disabled=true
value=self.wisdom_saving().to_string()
id="wisdom-saving"
/>
<label class="label-inline" for="proficiency-wisdom-saving">
{"Wisdom"}
</label>
</div>
<div class="field">
<input
type="checkbox"
id="proficiency-charisma-saving"
checked=self.proficiency_charisma_saving
onchange=self.link.callback(Message::ProficiencyCharismaSaving)
/>
<input
type="number"
disabled=true
value=self.charisma_saving().to_string()
id="charisma-saving"
/>
<label class="label-inline" for="proficiency-charisma-saving">
{"Charisma"}
</label>
</div>
</div>
</div>
</div>
} }
} }
} }