dofile("screen.lua") dofile("mqtt_config.lua") cls() console("Welcome to pendu!") console("by Lorem Impsum Corp.") client = mqtt.client(client_name, "mqtt.leizour.fr", 8883, true) console("Connecting...") client:connect("student", password) console("Connected !") try ( function () -- connecte l'écran sur les pins 18 et 19 attachscreen(18, 19, 0x3C) end, function () end, function () end ) if neo ~= nil then -- connecte la bande de led sur le pin 21 neo = neopixel.attach(neopixel.WS2812B, pio.GPIO21, 8) end playing = false won = false lives = 10 word = "" letters_played = "" function draw_ascii(lives) end function draw_ui() cls() if playing then draw_ascii(lives) console("mot a deviner: " .. word) console("lettres essayees: " .. letters_played) return end if won then console("Le mot " .. word .. " a ete devine a " .. lives .. " erreurs prêt !") else console("Le mot etait " .. word .. ", dommage !") end console("en attente d'une proposition de mot...") end function is_won() for i=1, #word do if not was_already_played(word[i]) then return false end end return true end function was_already_played(l) for i=1, #letters_played do if letters_played[i] == l then return true end end return false end function is_in_word(l) for i=1, #word do if word[i] == l then return true end end return false 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 lives = 10 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 if was_already_played(message) then draw_ui() console(message .. " a deja ete joue.") return end letters_played = letters_played .. message if not is_in_word(message) then lives = lives - 1 if lives == 0 then playing = false broadcast_state() draw_ui() return end draw_ui() console(message .. " ne fait pas partie du mot...") return end if not is_won() then draw_ui() console(message .. " trouve ! Bien joue !") return end won = true playing = false broadcast_state() draw_ui() end client:subscribe("pendu/letter", mqtt.QOS0, handle_letter)