Skip to content

Instantly share code, notes, and snippets.

@DarkMentat
Created June 6, 2018 11:22
Show Gist options
  • Save DarkMentat/31f562ac72e67f0c698a3f20c3e06292 to your computer and use it in GitHub Desktop.
Save DarkMentat/31f562ac72e67f0c698a3f20c3e06292 to your computer and use it in GitHub Desktop.
Wod probability Bot
group 'org.darkmentat'
version '0.1'
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = 'org.darkmentat.MainKt'
sourceCompatibility = 1.8
compileKotlin.kotlinOptions.jvmTarget = "1.8"
compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
jar {
manifest.attributes 'Main-Class': mainClassName
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
repositories {
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.telegram:telegrambots:3.6"
}
package org.darkmentat
import org.telegram.telegrambots.ApiContextInitializer
import org.telegram.telegrambots.api.methods.send.SendMessage
import org.telegram.telegrambots.api.objects.Update
import org.telegram.telegrambots.bots.TelegramLongPollingBot
import java.util.Random
import org.telegram.telegrambots.TelegramBotsApi
fun main(args: Array<String>) {
ApiContextInitializer.init()
TelegramBotsApi().registerBot(WodProbabilityBot())
}
class WodProbabilityBot : TelegramLongPollingBot() {
private val rnd = Random()
private val tries = 100_000
override fun getBotToken() = System.getenv("BOT_TOKEN")
override fun getBotUsername() = System.getenv("BOT_NAME")
override fun onUpdateReceived(update: Update?) {
val msg = update?.message ?: return
val txt = update.message?.text ?: return
fun send(text: String) = SendMessage()
.setChatId(msg.chatId)
.setText(text)
.enableMarkdown(true)
.let { execute(it) }
Regex("/roll (\\d+)d10(!)?\\s?>=?\\s?(\\d+)(f1)?([\\s\\S]+)?").matchEntire(txt)
?.destructured
?.let { (dices, reroll, diff, botch, comment) ->
val head = if(comment.isEmpty())
"Rolling **${dices}d10$reroll>=$diff$botch** $tries times"
else
"Rolling **$comment**: ${dices}d10$reroll>=$diff$botch $tries times"
val botches = botch.isNotEmpty()
val rerolls = reroll.isNotEmpty()
var atLeastOneSuccess = 0
var totalSuccesses = 0
var totalBotches = 0
for(i in 1..tries){
val res = roll(dices.toInt(), diff.toInt(), botches, rerolls)
if(res>0) {
totalSuccesses += res
atLeastOneSuccess++
}
if(res==-1)
totalBotches++
}
send("$head\nAt least one success: ${100.0*atLeastOneSuccess.toDouble()/tries}%\nBotch: ${100.0*totalBotches.toDouble()/tries}%\nMean successes: ${totalSuccesses.toDouble()/tries}")
}
}
fun roll(dices: Int, diff: Int, botches:Boolean=false, reroll:Boolean=false): Int {
var successes = 0
var haveSuccess = false
for (i in 1..dices) {
val dice = rnd.nextInt(10) + 1
if (dice >= diff) {
haveSuccess = true
successes++
}
if (botches && dice == 1) {
successes--
}
if (reroll && dice == 10){
while (true){
val newroll = rnd.nextInt(10) + 1
if(newroll == 10){
successes++
continue
}
if(newroll >= diff){
successes++
}
break
}
}
}
if (successes < 0) {
successes = if (haveSuccess) 0 else -1
}
return successes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment