From da1372cb5d9761cfe3a9b34cf6f7f99a5f6fdb06 Mon Sep 17 00:00:00 2001 From: theBigBlase Date: Thu, 21 Dec 2023 18:21:32 -0500 Subject: [PATCH] last version --- .../openhab_conf/items/light_effects.items | 9 ++- openhab/openhab_conf/rules/battlefield.rules | 60 +++++++++++++++++-- .../openhab_conf/sitemaps/battlefield.sitemap | 15 +++-- .../openhab_conf/things/battlefield.things | 5 +- 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/openhab/openhab_conf/items/light_effects.items b/openhab/openhab_conf/items/light_effects.items index 5cc9f0a..2fe5e65 100644 --- a/openhab/openhab_conf/items/light_effects.items +++ b/openhab/openhab_conf/items/light_effects.items @@ -1,6 +1,3 @@ -Group Room -Group SmartClothes - Group Sensors Group:Number Cam "Last roll" (Sensors) @@ -12,6 +9,12 @@ Number dmg "dernier degat" (Cam, Sensors){ channel="Channel='mqtt:topic:de'" } +Number hp "Points de vie" + +Image dmg_img "Image du de" (Cam, Sensors){ + channel="Channel='mqtt:topic:de'" +} + Switch animation_toggle "toggle animation degat" (Lights){ channel="Channel='mqtt:topic:lumiere'" } diff --git a/openhab/openhab_conf/rules/battlefield.rules b/openhab/openhab_conf/rules/battlefield.rules index 8c25370..263d649 100644 --- a/openhab/openhab_conf/rules/battlefield.rules +++ b/openhab/openhab_conf/rules/battlefield.rules @@ -3,6 +3,9 @@ import org.eclipse.smarthome.model.script.ScriptServiceUtil val mqttActions = getActions("mqtt", "mqtt:broker:mainBroker") val TAG_FileName = "battlefield.rules" +val hp_init = 20 as Number +val last_roll = 0 as Number +val concurent_roll = 0 as Number rule "Démarrage de openHAB" @@ -10,7 +13,8 @@ when System started then logInfo(TAG_FileName, "Rule : Démarrage de openHAB") - // il n'y a personne dans la pièce et l'éclairage est fermé + + hp.postUpdate(hp_init) mqttActions = getActions("mqtt", "mqtt:broker:mainBroker") animation_toggle.sendCommand(ON) @@ -20,9 +24,7 @@ rule "light toggle" when Item animation_toggle received command then - logInfo(TAG_FileName, "Rule : mqtt evts") - logInfo(TAG_FileName, "item animation_toggle changed to " + - animation_toggle.state) + logInfo(TAG_FileName, "WHAT " + animation_toggle.state) mqttActions.publishMQTT("lumiere/degat/toggle", animation_toggle.state.toString(), false) @@ -33,10 +35,58 @@ rule "light animation" when Item animation received command then - logInfo(TAG_FileName, "Rule : mqtt evts") logInfo(TAG_FileName, "item animation changed to " + animation.state) mqttActions.publishMQTT("lumiere/degat/animation", animation.state.toString(), false) end + +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 diff --git a/openhab/openhab_conf/sitemaps/battlefield.sitemap b/openhab/openhab_conf/sitemaps/battlefield.sitemap index 4cb370c..d6e4e86 100644 --- a/openhab/openhab_conf/sitemaps/battlefield.sitemap +++ b/openhab/openhab_conf/sitemaps/battlefield.sitemap @@ -1,7 +1,12 @@ -sitemap battlefield label="My home automation" { - Frame label="Battlefield" { - Text item=dmg label="Last Dmg [%s]" - Group item=Actuators icon="light" - Switch item=reset +sitemap battlefield label="Battlefield" { + Frame label="Sensors" { + Text item=dmg label="Dernier dégat infligé [%s]" icon="fire" + Text item=hp label="Point de vie restant : [%s]" icon="fire" + Switch item=reset label="Forcer le prochain tour" + Image item=dmg_img icon="light" label="Dernier lancé :" + } + Frame label="Actuators" { + Text item=animation label="Dernière animation [%s]" icon="siren" + Switch item=animation_toggle label="Activer les animations" } } diff --git a/openhab/openhab_conf/things/battlefield.things b/openhab/openhab_conf/things/battlefield.things index 839774e..5f93a8b 100644 --- a/openhab/openhab_conf/things/battlefield.things +++ b/openhab/openhab_conf/things/battlefield.things @@ -14,7 +14,10 @@ Thing mqtt:topic:lumiere "lumiere" (mqtt:broker:mainBroker) { Thing mqtt:topic:de "lecteur de" (mqtt:broker:mainBroker) { Channels: Type number : de "effecteur degat" [ - stateTopic="capteur/de/degat/dernierLance" + stateTopic="capteur/de/degat/dernierLanceDmg" + ] + Type string : img "nouvelle image" [ + stateTopic="capteur/de/degat/dernierLanceImg" ] }