Skip to content

Commit

Permalink
Begin redoing values system
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jun 28, 2024
1 parent a2d08c6 commit 21f2d21
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .idea/git_toolbox_blame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values
package dev.kordex.extra.web.oldvalues

import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@file:Suppress("UnnecessaryParentheses", "MagicNumber")

package dev.kordex.extra.web.values
package dev.kordex.extra.web.oldvalues

import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values
package dev.kordex.extra.web.oldvalues

import com.kotlindiscord.kord.extensions.utils.scheduling.Scheduler
import kotlinx.coroutines.launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values
package dev.kordex.extra.web.oldvalues

import com.kotlindiscord.kord.extensions.ExtensibleBot
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values
package dev.kordex.extra.web.oldvalues

import dev.kord.common.entity.Permissions
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.types

import kotlinx.serialization.Serializable

@Serializable(with = IdentifierSerializer::class)
public data class Identifier(public val namespace: String, public val id: String) {
init {
if (":" in namespace || ":" in id) {
error("Namespace and ID must not contain a colon character (:)")
}
}

override fun toString(): String =
"$namespace:$id"

// Generated methods

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Identifier

if (namespace != other.namespace) return false
if (id != other.id) return false

return true
}

override fun hashCode(): Int {
var result = namespace.hashCode()
result = 31 * result + id.hashCode()
return result
}
}

public fun Identifier(identifier: String): Identifier {
if (identifier.count { it == ':' } != 1) {
error("Identifiers must contain exactly one colon character (:), separating the namespace and ID.")
}

val parts = identifier.split(":", limit = 2)

return Identifier(parts.first(), parts.last())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.types

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

public class IdentifierSerializer : KSerializer<Identifier> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("Identifier", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): Identifier =
Identifier(decoder.decodeString())

override fun serialize(encoder: Encoder, value: Identifier) {
encoder.encodeString(value.toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values

import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable

@Serializable
public data class TimedContainer<V : Any?> (
val value: V,
val time: Instant,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values.types

import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.optional
import dev.kordex.extra.web.types.Identifier
import kotlinx.serialization.KSerializer
import kotlinx.serialization.serializer

public class SimpleValue<T : Any> public constructor(
override val identifier: Identifier,
override val writable: Boolean,
override val serializer: KSerializer<T>,
) : Value<T?, T?, T>() {
private var optional: Optional<T?> = Optional.Missing()

public override fun read(): T? =
if (optional is Optional.Missing) {
error("Value has not been set.")
} else {
optional.value
}

public override fun write(value: T?) {
this.optional = value.optional()
}

public fun clear() {
this.optional = Optional.Missing()
}
}

public inline fun <reified T : Any> SimpleValue(
identifier: Identifier,
writable: Boolean
): SimpleValue<T> = SimpleValue(identifier, writable, serializer())
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values.types

import com.kotlindiscord.kord.extensions.utils.collections.FixedLengthQueue
import dev.kordex.extra.web.oldvalues.ValueInterval
import dev.kordex.extra.web.types.Identifier
import dev.kordex.extra.web.values.TimedContainer
import kotlinx.datetime.Clock
import kotlinx.serialization.KSerializer
import kotlinx.serialization.serializer

public class TrackedValue<T : Any>(
override val identifier: Identifier,

public val maxValues: Int = 48,
public val precision: ValueInterval = ValueInterval.HalfHour,

public override val serializer: KSerializer<T>,
) : Value<T?, List<TimedContainer<T?>>, T>() {
private val values: FixedLengthQueue<TimedContainer<T?>> = FixedLengthQueue(maxValues)

public override fun read(): List<TimedContainer<T?>> =
values.getAll()

public override fun write(value: T?) {
values.push(
TimedContainer(value, Clock.System.now())
)
}
}

public inline fun <reified T : Any> TrackedValue(
identifier: Identifier,
maxValues: Int = 48,
precision: ValueInterval = ValueInterval.HalfHour,
): TrackedValue<T> = TrackedValue(identifier, maxValues, precision, serializer())
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.values.types

import dev.kordex.extra.web.types.Identifier
import kotlinx.serialization.KSerializer

public abstract class Value<I : Any?, O : Any?, T : Any> {
public abstract val identifier: Identifier
public abstract val serializer: KSerializer<T>

public open val writable: Boolean = false

public abstract fun write(value: I)
public abstract fun read(): O
}

0 comments on commit 21f2d21

Please sign in to comment.