diff --git a/app/src/main/java/club/clubk/ktag/konfigurator/Device.kt b/app/src/main/java/club/clubk/ktag/konfigurator/Device.kt index a07886d..e715e5f 100644 --- a/app/src/main/java/club/clubk/ktag/konfigurator/Device.kt +++ b/app/src/main/java/club/clubk/ktag/konfigurator/Device.kt @@ -2,6 +2,8 @@ package club.clubk.ktag.konfigurator import java.util.UUID +const val EDIT_DELAY_TIME: Long = 3L + sealed class DeviceState { data object Configurable : DeviceState() data object Ready : DeviceState() @@ -16,24 +18,44 @@ sealed class DeviceConfigureState { data object Failure: DeviceConfigureState() } +enum class DeviceParameter { + PLAYER_ID, TEAM, SECONDARY_COLOR, MAX_HEALTH, SPECIAL_WEAPONS +} + data class Device(val uuid: UUID = UUID.randomUUID(), var name: String = "Unknown Device", var address: String = "00:00:00:00:00:00", var deviceType: UInt? = null, var deviceState: DeviceState? = null, - var deviceConfigureState: DeviceConfigureState = DeviceConfigureState.Discovered, - // All configurable variables have their own dirty flag - var playerID: UInt? = null, - var dirtyPlayerID: Boolean = false, - var team: UInt? = null, - var dirtyTeam: Boolean = false, - var secondaryColor: UInt? = null, - var dirtySecondaryColor: Boolean = false, - var maxHealth: UInt? = null, - var dirtyMaxHealth: Boolean = false, - var specialWeapons: UInt? = null, - var dirtySpecialWeapons: Boolean = false + // All configurable variables + private var _playerID: UInt? = null, + private var _team: UInt? = null, + private var _secondaryColor: UInt? = null, + private var _maxHealth: UInt? = null, + private var _specialWeapons: UInt? = null ) { + var deviceConfigureState: DeviceConfigureState = DeviceConfigureState.Discovered + private set + + var dirtyPlayerID: Boolean = false + private set + var dirtyTeam: Boolean = false + private set + var dirtySecondaryColor: Boolean = false + private set + var dirtyMaxHealth: Boolean = false + private set + var dirtySpecialWeapons: Boolean = false + private set + + var lastEditTime: Long = 0L + private set + + val playerID: UInt? get() = _playerID + val team: UInt? get() = _team + val secondaryColor: UInt? get() = _secondaryColor + val maxHealth: UInt? get() = _maxHealth + val specialWeapons: UInt? get() = _specialWeapons fun deviceTypeName(): String { return when(deviceType) { @@ -55,7 +77,33 @@ data class Device(val uuid: UUID = UUID.randomUUID(), } fun isPastEditTime(): Boolean { - return false + return System.nanoTime() - lastEditTime!! >= EDIT_DELAY_TIME + } + + fun setParameter(parameter: DeviceParameter, value: Any) { + lastEditTime = System.nanoTime() + when (parameter) { + DeviceParameter.PLAYER_ID -> { + _playerID = value as UInt + dirtyPlayerID = true + } + DeviceParameter.TEAM -> { + _team = value as UInt + dirtyTeam = true + } + DeviceParameter.SECONDARY_COLOR -> { + _secondaryColor = value as UInt + dirtySecondaryColor = true + } + DeviceParameter.MAX_HEALTH -> { + _maxHealth = value as UInt + dirtyMaxHealth = true + } + DeviceParameter.SPECIAL_WEAPONS -> { + _specialWeapons = value as UInt + dirtySpecialWeapons = true + } + } } fun checkForDirt() { @@ -63,4 +111,22 @@ data class Device(val uuid: UUID = UUID.randomUUID(), return } } + + fun getDirtyParameters(): List { + val changed = mutableListOf() + if (dirtyPlayerID) changed.add(DeviceParameter.PLAYER_ID) + if (dirtyTeam) changed.add(DeviceParameter.TEAM) + if (dirtySecondaryColor) changed.add(DeviceParameter.SECONDARY_COLOR) + if (dirtyMaxHealth) changed.add(DeviceParameter.MAX_HEALTH) + if (dirtySpecialWeapons) changed.add(DeviceParameter.SPECIAL_WEAPONS) + return changed + } + + fun clearDirt() { + dirtyPlayerID = false + dirtyTeam = false + dirtySecondaryColor = false + dirtyMaxHealth = false + dirtySpecialWeapons = false + } }