Skip to content

Instantly share code, notes, and snippets.

View Tea-Ayataka's full-sized avatar

Ayataka Tea-Ayataka

View GitHub Profile
@Tea-Ayataka
Tea-Ayataka / gist:e3155f91aa700f59377bd43cf1d37dcb
Created October 27, 2019 19:43
The Answer to Perfect Coil Builds for Vaping
var inner_diameter = 2.5; // 2.5mm
var wrap_count = 9; // 9 wraps
var wrap_spacing = 0.01; // the default value for micro coil wrapping
var wire_width = ; // the diameter of the coil (mm) (https://www.steam-engine.org/wirewiz)
var coil_heat_capacity = ; // the heat capacity of the coil (mJ/K)
var coil_width = wire_width * wrap_count + wrap_spacing * (wrap_count - 1);
var inner_heat_capacity = Math.pow(inner_diameter / 10 / 2, 2) * Math.PI * coil_width / 10 * 2285.6225;
var result = inner_heat_capacity / coil_heat_capacity;
fun PipelineContext<*, ApplicationCall>.setSession(id: String, value: String) {
val session = call.sessions.get<Session>() ?: Session()
session.data[id] = value
call.sessions.set(session)
}
fun PipelineContext<*, ApplicationCall>.getSession(id: String): String? {
return call.sessions.get<Session>()?.data?.get(id)
}
@Tea-Ayataka
Tea-Ayataka / Timer.kt
Created September 15, 2018 08:54
Kotlin Coroutine Timer
package net.ayataka.bas.utils
import kotlinx.coroutines.experimental.*
import net.ayataka.bas.LOGGER
import kotlin.system.measureTimeMillis
fun CoroutineScope.timer(interval: Long, fixedRate: Boolean = true, action: suspend TimerScope.() -> Unit): Job {
return launch {
val scope = TimerScope()
@Tea-Ayataka
Tea-Ayataka / TimeUnits.kt
Created September 15, 2018 05:22
Kotlin TimeUnits
fun Int.seconds() = this.toLong() * 1000
fun Int.minutes() = this.toLong() * 1000 * 60
fun Int.hours() = this.toLong() * 1000 * 60 * 60
@Tea-Ayataka
Tea-Ayataka / RateLimiter.kt
Last active September 15, 2018 05:16
Kotlin Simple Rate Limiter
class RateLimiter(private val period: Long, private val rate: Int) {
private val times = LinkedList<Long>()
fun check(): Boolean {
synchronized(times) {
val currentTime = System.currentTimeMillis()
times.removeIf { it < currentTime - period }
val isLimited = times.size >= rate
if (!isLimited) {
const val ENCRYPT_ALGORITHM = "AES"
fun String.encrypt(key: String): String {
val keySpec = SecretKeySpec(key.toByteArray(Charsets.UTF_8), ENCRYPT_ALGORITHM)
val iv = SecureRandom().generateSeed(keySpec.encoded.size)
val encrypted = Cipher.getInstance("$ENCRYPT_ALGORITHM/CBC/PKCS5PADDING").run {
init(Cipher.ENCRYPT_MODE, keySpec, IvParameterSpec(iv))
doFinal(this@encrypt.toByteArray(Charsets.UTF_8))
}
return "${hex(iv)}/${hex(encrypted)}"
@Tea-Ayataka
Tea-Ayataka / WebClient.kt
Created April 5, 2018 14:02
C# WebClient in Kotlin
package net.ayataka.marinetooler.utils.web
import java.io.IOException
import java.net.*
import java.nio.charset.Charset
class WebClient(
val cookie: CookieManager = CookieManager(),
val timeout: Int = 10000, // 10 sec
val encoding: String = "UTF-8"