11package de.joshua.dnd
22
3+ import com.beust.klaxon.JsonReader
4+ import com.beust.klaxon.Klaxon
5+ import de.joshua.DiscordBot
6+ import de.joshua.dnd.buttons.BodyPartButton
7+ import de.joshua.dnd.buttons.CritButton
8+ import de.joshua.dnd.buttons.DiceButton
9+ import de.joshua.dnd.buttons.d10Button
10+ import net.dv8tion.jda.api.interactions.components.ItemComponent
311import java.awt.Color
12+ import java.io.StringReader
13+ import java.net.URL
14+
415
516class Dice {
617 companion object {
7- fun rollDice (max : Int ): Int {
8- return (Math .random() * max).toInt()
18+ fun rollDice (max : Int = 100 ): Int {
19+ return (Math .random() * max + 1 ).toInt()
920 }
1021
22+
23+
1124 fun getColor (per : Double ): Color {
1225 val percent = per / 100
1326 val best = Color .decode(" #066e32" )
@@ -25,6 +38,80 @@ class Dice {
2538 if (list.size < 2 ) return false
2639 return list.size == 3 || list[0 ] == list[1 ]
2740 }
41+
42+ fun getCrit (location : Int = this.rollDice(), intensity : Int = this.rollDice()): CritEntry {
43+
44+ var file: URL ? = null
45+
46+ if (location <= 9 ){
47+ file = DiscordBot ::class .java.getResource(" /crit/head.json" )
48+ }else if (location <= 24 ){
49+ file = DiscordBot ::class .java.getResource(" /crit/arm.json" )
50+ }else if (location <= 44 ){
51+ file = DiscordBot ::class .java.getResource(" /crit/arm.json" )
52+ }else if (location <= 79 ){
53+ file = DiscordBot ::class .java.getResource(" /crit/body.json" )
54+ }else if (location <= 89 ){
55+ file = DiscordBot ::class .java.getResource(" /crit/leg.json" )
56+ }else if (location <= 100 ){
57+ file = DiscordBot ::class .java.getResource(" /crit/leg.json" )
58+ }
59+
60+ if (file == null ) return CritEntry (0 , " " , " Error" , " " , " " )
61+
62+
63+ val klaxon = Klaxon ()
64+ val result = arrayListOf<TableEntry >()
65+ JsonReader (StringReader (file.readText())).use { reader ->
66+ reader.beginArray {
67+ while (reader.hasNext()) {
68+ /* val person = klaxon.parse<TableEntry>(reader)
69+ if (person != null) {
70+ result.add(person)
71+ }*/
72+ klaxon.parse<TableEntry >(reader)?.let { result.add(it) }
73+ }
74+ }
75+ }
76+
77+
78+ var index = - 1
79+ for (i in result.size - 1 downTo 0 ) {
80+ if (result.get(i).max <= intensity) {
81+ index = i
82+ break
83+ }
84+ }
85+
86+
87+ return CritEntry (location, intensity.toString(), result[index].description, result[index].wounds, result[index].additionalEffects)
88+ }
89+
90+ fun getBodyPart (number : Int ): String {
91+ if (number <= 9 ){
92+ return " Head"
93+ }else if (number <= 24 ){
94+ return " Left Arm"
95+ }else if (number <= 44 ){
96+ return " Right Arm"
97+ }else if (number <= 79 ){
98+ return " Body"
99+ }else if (number <= 89 ){
100+ return " Left Leg"
101+ }else if (number <= 100 ){
102+ return " Right Leg"
103+ }
104+ return " Error"
105+ }
106+
107+ fun getActionRow (): List <ItemComponent > {
108+ val list = mutableListOf<ItemComponent >()
109+ list.add(DiceButton ().getButton())
110+ list.add(d10Button().getButton())
111+ list.add(BodyPartButton ().getButton())
112+ list.add(CritButton ().getButton())
113+ return list
114+ }
28115 }
29116}
30117
@@ -37,8 +124,28 @@ fun Int.toDigits(base: Int = 10): List<Int> = sequence {
37124 }
38125}.toList()
39126
40- class TableEntry (
127+ fun getBodyPartNumber (bodyPart : String ): Int {
128+ when (bodyPart){
129+ " head" -> return 0
130+ " arm" -> return 10
131+ " body" -> return 45
132+ " leg" -> return 80
133+ }
134+ return 0
135+ }
136+
137+
138+ data class TableEntry (
139+ val max : Int ,
41140 val description : String ,
42- val wounds : Int ,
43- val additionalEffects : String
141+ val wounds : String ,
142+ val additionalEffects : String ,
44143)
144+
145+ data class CritEntry (
146+ val location : Int ,
147+ val intensity : String ,
148+ val description : String ,
149+ val wounds : String ,
150+ val additionalEffects : String ,
151+ )
0 commit comments