aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/economy/coins.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2024-09-13 14:32:15 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2024-09-13 14:32:15 -0400
commit51723cfd9a7d31643fa7c14cc2df9d8e9e2bd33d (patch)
tree70cc0e0f9d789b4be3d9249c3ecc23608bb9fa10 /economy/coins.go
parent8759523157afb38f8565687ebd0e1f29a1af3e42 (diff)
Add money and head handling functions
Created types to manage the cannonical coinage of DnD. First pass at computing computing values for heads.
Diffstat (limited to 'economy/coins.go')
-rw-r--r--economy/coins.go138
1 files changed, 138 insertions, 0 deletions
diff --git a/economy/coins.go b/economy/coins.go
new file mode 100644
index 0000000..e2d3531
--- /dev/null
+++ b/economy/coins.go
@@ -0,0 +1,138 @@
+package economy
+
+import (
+ "fmt"
+ "strings"
+)
+
+type Coin float64
+
+const (
+ CopperPiece Coin = 1
+ SilverPiece Coin = 10
+ ElectrumPiece Coin = 50
+ GoldPiece Coin = 100
+ PlatinumPiece Coin = 1000
+)
+
+type Money struct {
+ Copper Coin
+ Silver Coin
+ Electrum Coin
+ Gold Coin
+ Platinum Coin
+}
+
+func abs[T int | int32 | int64](x T) T {
+ if x < 0 {
+ return -x
+ }
+
+ return x
+}
+
+// Computes sign(a) * (abs(a) % b)
+func safeMod(dividend int, divisor uint) int {
+ absMod := abs(dividend) % int(divisor)
+ if dividend < 0 {
+ return -absMod
+ }
+ return absMod
+}
+
+func (a *Money) Add(b Money) {
+ a.Copper += b.Copper
+ a.Silver += b.Silver
+ a.Electrum += b.Electrum
+ a.Gold += b.Gold
+ a.Platinum += b.Platinum
+}
+
+func (a *Money) Subtract(b Money) {
+ a.Copper -= b.Copper
+ a.Silver -= b.Silver
+ a.Electrum -= b.Electrum
+ a.Gold -= b.Gold
+ a.Platinum -= b.Platinum
+}
+
+func (a *Money) Multiply(b Money) {
+ a.Copper *= b.Copper
+ a.Silver *= b.Silver
+ a.Electrum *= b.Electrum
+ a.Gold *= b.Gold
+ a.Platinum *= b.Platinum
+}
+
+// Converts coins Money to the smallest amount of largest denomination possible
+func (m *Money) Simplify() {
+ totalValue := m.Value()
+
+ totalCopper := int(totalValue)
+
+ if abs(totalCopper) > int(PlatinumPiece) {
+ m.Platinum = Coin(totalCopper / int(PlatinumPiece))
+ totalCopper = safeMod(totalCopper, uint(PlatinumPiece))
+ }
+ if abs(totalCopper) > int(GoldPiece) {
+ m.Gold = Coin(totalCopper / int(GoldPiece))
+ totalCopper = safeMod(totalCopper, uint(GoldPiece))
+ }
+ if abs(totalCopper) > int(ElectrumPiece) {
+ m.Electrum = Coin(totalCopper / int(ElectrumPiece))
+ totalCopper = safeMod(totalCopper, uint(ElectrumPiece))
+ }
+ if abs(totalCopper) > int(SilverPiece) {
+ m.Silver = Coin(totalCopper / int(SilverPiece))
+ totalCopper = safeMod(totalCopper, uint(SilverPiece))
+ }
+ m.Copper = Coin(totalCopper)
+}
+
+func (m Money) Value() Coin {
+ var value Coin = 0.0
+
+ value += m.Copper
+ value += m.Silver * SilverPiece
+ value += m.Electrum * ElectrumPiece
+ value += m.Gold * GoldPiece
+ value += m.Platinum * PlatinumPiece
+
+ return value
+}
+
+func (m Money) String() string {
+ var b strings.Builder
+
+ if m.Copper != 0 {
+ fmt.Fprintf(&b, "%.2fcp", m.Copper)
+ }
+
+ if m.Silver != 0 {
+ fmt.Fprintf(&b, "%.2fsp", m.Silver)
+ }
+
+ if m.Electrum != 0 {
+ fmt.Fprintf(&b, "%.2fep", m.Electrum)
+ }
+
+ if m.Gold != 0 {
+ fmt.Fprintf(&b, "%.2fgp", m.Gold)
+ }
+
+ if m.Platinum != 0 {
+ fmt.Fprintf(&b, "%.2fpp", m.Platinum)
+ }
+
+ return b.String()
+}
+
+func Sum(amounts []Money) Money {
+ total := Money{}
+
+ for _, amount := range amounts {
+ total.Add(amount)
+ }
+
+ return total
+}