Skip to content

Commit

Permalink
[client] Record & display adjusted dice rolls
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Prior committed Nov 20, 2021
1 parent b38fb04 commit 0534b0f
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 17 deletions.
157 changes: 151 additions & 6 deletions client/source/Dice.mint
Original file line number Diff line number Diff line change
@@ -1,26 +1,145 @@
enum Adjust.Type {
Low
High
}

record Adjust {
sign : Number,
count : Number,
type : Adjust.Type
}

record Dice {
count : Number,
sides : Number,
constant : Number,
adjust : Maybe(Adjust)
}

record Roll {
dice: Dice,
results: Array(Number)
}

module Adjust {
fun fromObject(object : Object) : Result(Object.Error, Adjust) {
try {
sign = object |> Object.Decode.field("sign", Object.Decode.number)
count = object |> Object.Decode.field("count", Object.Decode.number)
type = case(`#{object}.type`) {
"low" => Adjust.Type::Low
=> Adjust.Type::High
}
Result::Ok({sign = sign, count = count, type = type})
} catch Object.Error => error {
try {
error |> Debug.log
Result::Err(error)
}
}
}

fun toObject(adjust : Adjust) : Object {
try {
sign = (encode adjust.sign) |> Object.Encode.field("sign")
count = (encode adjust.count) |> Object.Encode.field("count")
type = Object.Encode.field("type", `#{adjust.type == Adjust.Type::High} ? "high" : "low"`)
Object.Encode.object([sign, count, type])
}
}
}

module Dice {
fun fromObject(object : Object) : Result(Object.Error, Dice) {
try {
count = object |> Object.Decode.field("count", Object.Decode.number)
sides = object |> Object.Decode.field("sides", Object.Decode.number)
constant = object |> Object.Decode.field("constant", Object.Decode.number)
adjust = if (`!!#{object}.adjust`) {
try {
result =
object
|> Object.Decode.field("adjust", Adjust.fromObject)
Maybe::Just(result)
} catch Object.Error => error {
try {
error |> Debug.log
Maybe::Nothing
}
}
} else {
Maybe::Nothing
}
Result::Ok({count = count, sides = sides, constant = constant, adjust = adjust})
} catch Object.Error => error {
try {
error |> Debug.log
Result::Err(error)
}
}
}

fun toObject(dice : Dice) : Object {
try {
count = (encode dice.count) |> Object.Encode.field("count")
sides = (encode dice.sides) |> Object.Encode.field("sides")
constant = (encode dice.constant) |> Object.Encode.field("constant")
adjust = case(dice.adjust) {
Maybe::Just(adjust) => adjust |> Adjust.toObject
Maybe::Nothing => `null`
} |> Object.Encode.field("adjust")
Object.Encode.object([count, sides, constant, adjust])
}
}
}

module Roll {
fun fromObject(object : Object) : Result(Object.Error, Roll) {
try {
roll = decode object as Roll
Result::Ok(roll)
dice =
object
|> Object.Decode.field("dice", Dice.fromObject)
results =
object
|> Object.Decode.field("results", Object.Decode.number |> Object.Decode.array)
Result::Ok({dice = dice, results = results})
} catch Object.Error => error {
Result::Err(error)
}
}

fun toObject(roll : Roll) : Object {
try {
dice = roll.dice |> Dice.toObject |> Object.Encode.field("dice")
results = (encode roll.results) |> Object.Encode.field("results")
Object.Encode.object([dice, results])
}
}

fun adjustResults(complement : Bool, roll : Roll) : Roll {
case (roll.dice.adjust) {
Maybe::Just(adjust) => {
roll | results = roll.results
|> Array.sort(
case (adjust.type) {
Adjust.Type::High => (a : Number, b : Number) { b - a }
Adjust.Type::Low => (a : Number, b : Number) { a - b }
}
)
|> if(complement) {
Array.take(adjust.count)
} else {
Array.drop(adjust.count)
}
}
=> roll
}
}

fun total(rolls : Array(Roll)) : Number {
rolls |> Array.sumBy((roll : Roll) { roll.dice.constant * (roll.results |> Array.sum) })
rolls
|> Array.map(Roll.adjustResults(false))
|> Array.sumBy((roll : Roll) { roll.dice.constant * (roll.results |> Array.sum) })
}
}

Expand All @@ -38,6 +157,12 @@ component DiceRoll {
vertical-align: baseline;
}

style dropped {
&:before {
content: "";
}
}

style part(constant : Number) {
line-height: 14pt;
&:not(:first-child)::before {
Expand All @@ -58,9 +183,29 @@ component DiceRoll {
<>
"d#{dice.sides}"
if(showDice) {
<span::results>
<{ results |> Array.map(Number.toString) |> String.join(", ") }>
</span>
case(dice.adjust) {
Maybe::Just(adjust) =>
<>
<span::results>
<{
(data |> Roll.adjustResults(false)).results
|> Array.map(Number.toString)
|> String.join(", ")
}>
</span>
<span::results::dropped>
<{
(data |> Roll.adjustResults(true)).results
|> Array.map(Number.toString)
|> String.join(", ")
}>
</span>
</>
Maybe::Nothing =>
<span::results>
<{ results |> Array.map(Number.toString) |> String.join(", ") }>
</span>
}
}
</>
}
Expand Down
2 changes: 1 addition & 1 deletion client/source/Message.mint
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Message.Part {
try {
case(part) {
Message.Part::Text(string) => `#{string}`
Message.Part::Rolls(rolls) => encode rolls
Message.Part::Rolls(rolls) => rolls |> Array.map(Roll.toObject) |> Object.Encode.array
}
}
}
Expand Down
30 changes: 20 additions & 10 deletions client/source/Messages.mint
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ store Messages {
{
count = 1,
sides = 20,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [14]
},
Expand All @@ -30,7 +31,8 @@ store Messages {
{
count = 11,
sides = 1,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [11]
}
Expand All @@ -49,7 +51,8 @@ store Messages {
{
count = 1,
sides = 20,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [20]
},
Expand All @@ -58,7 +61,8 @@ store Messages {
{
count = 1,
sides = 4,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [2]
},
Expand All @@ -67,7 +71,8 @@ store Messages {
{
count = 13,
sides = 1,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [13]
}
Expand All @@ -80,7 +85,8 @@ store Messages {
{
count = 1,
sides = 20,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [1]
},
Expand All @@ -89,7 +95,8 @@ store Messages {
{
count = 1,
sides = 4,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [4]
},
Expand All @@ -98,7 +105,8 @@ store Messages {
{
count = 13,
sides = 1,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [13]
}
Expand All @@ -121,7 +129,8 @@ store Messages {
{
count = 1,
sides = 20,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [1]
}
Expand All @@ -144,7 +153,8 @@ store Messages {
{
count = 12,
sides = 6,
constant = 1
constant = 1,
adjust = Maybe::Nothing
},
results = [6, 1, 1, 2, 6, 3, 1, 1, 4, 6, 5, 6]
}
Expand Down

0 comments on commit 0534b0f

Please sign in to comment.