diff --git a/server/main.lua b/server/main.lua index 472bc55..4fecb84 100644 --- a/server/main.lua +++ b/server/main.lua @@ -7,4 +7,71 @@ attachscreen(18, 19, 0x3C) neo = neopixel.attach(neopixel.WS2812B, pio.GPIO21, 8) cls() -console("test") +console("Welcome to pendu!") +console("by Lorem Impsum Corp.") + +client = mqtt.client("Server", "mqtt.leizour.fr", 8883, true) +client:connect("student", "O99Rq8$F12NXzhL5caya") + +playing = false +won = false + +word = "" +letters_played = "" + +function draw_ui() + cls() + if playing then + console("mot à deviner: " .. word) + console("lettres essayées: " .. letters_played) + return + end + if won then + console("Le mot " .. word .. " a été deviné !") + console("en attente d'une proposition de mot...") + end +end + +function update_victory() + +end + +function broadcast_state() + if playing then + client:publish("pendu/gamestate", "PLAYING", mqtt.QOS0) + else + client:publish("pendu/gamestate", "WAITING", mqtt.QOS0) + end +end + +function handle_state_query(len, message, topic_len, topic_name) + if topic_name != "pendu/gamestate" or message != "query" then + return + end + broadcast_state() +end + +client:subscribe("pendu/gamestate", mqtt.QOS0, handle_state_query) + +function handle_word_suggestion(len, message, topic_len, topic_name) + if topic_name != "pendu/word" or playing then + return + end + word = message + won = false + playing = true + broadcast_state() +end + +client:subscribe("pendu/word", mqtt.QOS0, handle_word_suggestion) + +function handle_letter(len, message, topic_len, topic_name) + if topic_name != "pendu/letter" or not playing then + return + end + letters_played = letters_played .. message + update_victory() + draw_ui() +end + +client:subscribe("pendu/letter", mqtt.QOS0, handle_letter) \ No newline at end of file