2023-12-21 17:08:36 +00:00
|
|
|
import org.eclipse.smarthome.model.script.ScriptServiceUtil
|
|
|
|
// import org.eclipse.smarthome.core.library.items.StringItem
|
|
|
|
|
|
|
|
val mqttActions = getActions("mqtt", "mqtt:broker:mainBroker")
|
|
|
|
val TAG_FileName = "battlefield.rules"
|
2023-12-21 23:21:32 +00:00
|
|
|
val hp_init = 20 as Number
|
|
|
|
val last_roll = 0 as Number
|
|
|
|
val concurent_roll = 0 as Number
|
2023-12-21 17:08:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
rule "Démarrage de openHAB"
|
|
|
|
when
|
|
|
|
System started
|
|
|
|
then
|
|
|
|
logInfo(TAG_FileName, "Rule : Démarrage de openHAB")
|
2023-12-21 23:21:32 +00:00
|
|
|
|
|
|
|
hp.postUpdate(hp_init)
|
2023-12-21 17:08:36 +00:00
|
|
|
mqttActions = getActions("mqtt", "mqtt:broker:mainBroker")
|
|
|
|
|
|
|
|
animation_toggle.sendCommand(ON)
|
|
|
|
end
|
|
|
|
|
|
|
|
rule "light toggle"
|
|
|
|
when
|
|
|
|
Item animation_toggle received command
|
|
|
|
then
|
2023-12-21 23:21:32 +00:00
|
|
|
logInfo(TAG_FileName, "WHAT " + animation_toggle.state)
|
2023-12-21 17:08:36 +00:00
|
|
|
|
|
|
|
mqttActions.publishMQTT("lumiere/degat/toggle",
|
|
|
|
animation_toggle.state.toString(), false)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
rule "light animation"
|
|
|
|
when
|
|
|
|
Item animation received command
|
|
|
|
then
|
|
|
|
logInfo(TAG_FileName, "item animation changed to " +
|
|
|
|
animation.state)
|
|
|
|
|
|
|
|
mqttActions.publishMQTT("lumiere/degat/animation",
|
|
|
|
animation.state.toString(), false)
|
|
|
|
end
|
2023-12-21 23:21:32 +00:00
|
|
|
|
|
|
|
rule "update hp"
|
|
|
|
when
|
|
|
|
Item dmg received update
|
|
|
|
then
|
|
|
|
val current_roll = dmg.state as Number
|
|
|
|
if(current_roll == last_roll){
|
|
|
|
concurent_roll = concurent_roll + 1
|
|
|
|
val Number current_dmg
|
|
|
|
logInfo(TAG_FileName, "HP: IF: concurent_roll = " + concurent_roll)
|
|
|
|
current_dmg = current_roll * Math.pow(2, (concurent_roll as Number).floatValue)
|
|
|
|
val current_hp = hp.state as Number
|
|
|
|
hp.postUpdate(current_hp - current_dmg)
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
last_roll = current_roll
|
|
|
|
concurent_roll = 0
|
|
|
|
logInfo(TAG_FileName, "HP: ELSE")
|
|
|
|
var new_val = hp.state as Number
|
|
|
|
hp.postUpdate(new_val - dmg.state)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
rule "die"
|
|
|
|
when
|
|
|
|
Item hp changed
|
|
|
|
then
|
|
|
|
if((hp.state as Number) <= 0){
|
|
|
|
animation.sendCommand("mort")
|
|
|
|
hp.sendCommand(0)
|
|
|
|
}
|
|
|
|
else if(concurent_roll >= 1){
|
|
|
|
animation.sendCommand("coup critique")
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
animation.sendCommand("degat")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
rule "reset"
|
|
|
|
when
|
|
|
|
Item reset changed to ON
|
|
|
|
then
|
|
|
|
concurent_roll = 0
|
|
|
|
last_roll = 0
|
|
|
|
hp.postUpdate(hp_init)
|
|
|
|
dmg.postUpdate(0)
|
|
|
|
reset.postUpdate(OFF)
|
|
|
|
end
|