Skip to content

Commit

Permalink
diec roller progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesw98 committed Oct 31, 2023
1 parent 84c97d4 commit 92494d8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
34 changes: 34 additions & 0 deletions Models/RollDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,45 @@ public class RollDetails
public List<int>? Rolls { get; set; } = new();
public int Total { get; set; }
public bool Fixed { get; set; }
public bool ModOnEvery { get; set; } = false;

public string ToSimpleString()
{
var result = "";
if (Rolls == null)
{
return result;
}

var sign = Mod < 0
? "-"
: "+";

var modString = $"{sign} {Math.Abs(Mod ?? 0)} ";

foreach (var r in Rolls)
{
result += $"[{r}] ";
if (ModOnEvery)
{
result += modString;
}
}

if (!ModOnEvery)
{
result += modString;
}

return result;
}

public override string ToString()
{
if (Fixed)
{
return $"Fixed roll, total: {Total}";
}

var rolls = Rolls.Aggregate("", (current, i) => current + $"[{i}] + ");
return $"{Num}d{Die} + {Mod} -> {rolls}{Mod} = {Total}";
Expand Down
62 changes: 62 additions & 0 deletions Pages/DiceRoller.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@page "/dice"
@using dnd_utils.Services
@using dnd_utils.Models

@inject DiceService Dice

<br/>
<MudContainer MaxWidth="MaxWidth.Large">
<MudGrid>
<MudItem xs="12" sm="7">
<MudPaper Class="pa-4">
<div class="d-flex">
<MudTextField T="int" Label="Number of Dice" Required="true" RequiredError="Number of dice is required!" @ref="_numDice"/>
<MudSpacer/>
<MudTextField T="int" Label="Die Type" Required="true" RequiredError="Die type is required!" @ref="_dieType"/>
<MudSpacer/>
<MudTextField T="int" Label="Modifier" Required="true" @ref="_modifier"/>
<MudSpacer/>
<MudButton Variant="Variant.Filled" Color="Color.Success" OnClick="Roll">Roll</MudButton>
</div>
<br/>
<div class="d-flex">
<MudCheckBox @bind-Checked="_modOnEvery" Color="Color.Primary">Use modifier on every die</MudCheckBox>
</div>
</MudPaper>
</MudItem>
<MudItem xs="12" sm="5">
<MudPaper Class="pa-4">
<MudTable T="RollDetails" Items="_rolls" Breakpoint="Breakpoint.Md">
<HeaderContent>
<MudTh>Formula</MudTh>
<MudTh>Result</MudTh>
<MudTh>Dice</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd>@($"{context.Num}d{context.Die}")</MudTd>
<MudTd>
<b>@context.Total</b>
</MudTd>
<MudTd>@context.ToSimpleString()</MudTd>
</RowTemplate>
</MudTable>
</MudPaper>
</MudItem>
</MudGrid>
</MudContainer>

@code {

private MudTextField<int> _numDice;
private MudTextField<int> _modifier;
private MudTextField<int> _dieType;

private bool _modOnEvery = false;

private List<RollDetails> _rolls = new();

private void Roll()
{
_rolls.Insert(0, Dice.RollDetailed(_numDice.Value, _dieType.Value, _modifier.Value, _modOnEvery));
}
}
24 changes: 16 additions & 8 deletions Services/DiceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ namespace dnd_utils.Services;

public class DiceService
{
private Random _rand = new();
private readonly Random _rand = new();

public (RollDetails, RollDetails) RollDetailsNonStandard(int num, int type, int mod, AdvantageState state, bool modOnEvery=false)
{
if (type != 20)
{
throw new Exception("(Dis)Advantage can only be used with d20s");
}

List<RollDetails> rolls = new (new[] { RollDetailed(num, type, mod), RollDetailed(num, type, mod) });

if (state == AdvantageState.Advantage)
rolls = rolls.OrderByDescending(x => x.Total).ToList();
else
rolls = rolls.OrderBy(x => x.Total).ToList();
rolls = state == AdvantageState.Advantage
? rolls.OrderByDescending(x => x.Total).ToList()
: rolls.OrderBy(x => x.Total).ToList();

return (rolls[0], rolls[1]);
}
Expand All @@ -33,7 +34,9 @@ public RollDetails RollDetailed(int num, int type, int mod, bool modOnEvery=fals
rolls.Add(currentRoll);
}

total += modOnEvery ? mod * num : mod;
total += modOnEvery
? mod * num
: mod;

return new RollDetails
{
Expand All @@ -42,7 +45,8 @@ public RollDetails RollDetailed(int num, int type, int mod, bool modOnEvery=fals
Die = type,
Mod = mod,
Total = total,
Fixed = false
Fixed = false,
ModOnEvery = modOnEvery
};
}

Expand All @@ -51,9 +55,13 @@ public int Roll(int num, int type, int mod, bool modOnEvery=false)
var result = 0;

for (var i = 0; i < num; i++)
{
result += _rand.Next(1, type + 1);
}

result += modOnEvery ? mod * num : mod;
result += modOnEvery
? mod * num
: mod;
return result;
}
}

0 comments on commit 92494d8

Please sign in to comment.