Logo Pastebin.fr
Pastebin

Retrouvez, créez et partagez vos snippets en temps réel.

controleur central

local modem = peripheral.find("modem")
modem.open(777)

local accounts = {}

if fs.exists("accounts.db") then
  local f = fs.open("accounts.db", "r")
  accounts = textutils.unserialize(f.readAll())
  f.close()
end

local function save()
  local f = fs.open("accounts.db", "w")
  f.write(textutils.serialize(accounts))
  f.close()
end

while true do
  local _, _, _, _, msg = os.pullEvent("modem_message")
  local card = msg.card
  local pin = msg.pin

  if not accounts[card] or accounts[card].pin ~= pin then
    modem.transmit(777, 777, {status=false})
  else
    if msg.action == "deposit" then
      accounts[card].balance = accounts[card].balance + msg.amount
    elseif msg.action == "withdraw" then
      if accounts[card].balance >= msg.amount then
        accounts[card].balance = accounts[card].balance - msg.amount
      else
        modem.transmit(777,777,{status=false,err="solde"})
        goto continue
      end
    end

    table.insert(accounts[card].history,{
      type=msg.action,
      amount=msg.amount
    })

    save()
    modem.transmit(777,777,{
      status=true,
      balance=accounts[card].balance,
      history=accounts[card].history
    })
  end
  ::continue::
end

Créé il y a 2 semaines.

Rechercher un Pastebin

Aucun paste trouvé.