183 lines
3.9 KiB
Lua
183 lines
3.9 KiB
Lua
dofile("screen.lua")
|
|
|
|
dofile("mqtt_config.lua")
|
|
|
|
attachscreen(18, 19, 0x3C)
|
|
|
|
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 !")
|
|
|
|
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 get_public_word()
|
|
r = ""
|
|
for i=1, #word do
|
|
if was_already_played(word:sub(i, i)) then
|
|
r = r .. word:sub(i, i)
|
|
else
|
|
r = r .. "_"
|
|
end
|
|
end
|
|
return r
|
|
end
|
|
|
|
function set_color(r, g, b)
|
|
for i=0, 7 do
|
|
neo:setPixel(i, r, g, b)
|
|
end
|
|
end
|
|
|
|
function blink(positive)
|
|
thread.start(function()
|
|
if positive then
|
|
set_color(0, 5, 0)
|
|
else
|
|
set_color(5, 0, 0)
|
|
end
|
|
neo:update()
|
|
tmr.delayms(50)
|
|
set_color(0, 0, 0)
|
|
neo:update()
|
|
end)
|
|
end
|
|
|
|
function draw_ui()
|
|
cls()
|
|
if playing then
|
|
console("tentatives: " .. lives)
|
|
console("mot a deviner: ")
|
|
console(get_public_word())
|
|
console("lettres essayees: ")
|
|
console(letters_played)
|
|
return
|
|
end
|
|
if won then
|
|
console("Le mot etait :")
|
|
console(word)
|
|
console("Il a ete devine avec")
|
|
console((10 - lives) .. " erreurs !")
|
|
else
|
|
console("Le mot etait :")
|
|
console(word)
|
|
console("\ndommage !")
|
|
end
|
|
console("En attente d'une")
|
|
console("proposition de mot...")
|
|
end
|
|
|
|
function is_won()
|
|
for i=1, #word do
|
|
if not was_already_played(word:sub(i, i)) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
function was_already_played(l)
|
|
for i=1, #letters_played do
|
|
if letters_played:sub(i, i) == l then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function is_in_word(l)
|
|
for i=1, #word do
|
|
if word:sub(i, 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
|
|
blink(false)
|
|
lives = lives - 1
|
|
if lives == 0 then
|
|
playing = false
|
|
broadcast_state()
|
|
draw_ui()
|
|
return
|
|
end
|
|
draw_ui()
|
|
console(message .. " ne fait pas partie")
|
|
console("du mot...")
|
|
return
|
|
end
|
|
blink(true)
|
|
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)
|
|
|
|
cls()
|
|
console("En attente d'une")
|
|
console("proposition de mot...")
|
|
|
|
--------------------------------------------------------------------
|