-
-
Save anonymous/d3ccebdcc0c7d659f70c8cf3eebe2e4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Variablen-- | |
local fs = require("filesystem") | |
local event = require("event") | |
local component = require("component") | |
local modem = component.modem | |
local Tablet = "e0d2bad8-26dc-44b8-a307-592316ea050b" | |
local Channel = 1 | |
local Konto = "Pfad/zu/konto.txt" | |
--Netzwerkkanal öffnen-- | |
modem.open(Channel) | |
--Schleife für Aktionen-- | |
while true do | |
--Warten auf Nachricht mit Absenderadresse und Wert | |
local _, _, Adresse, _, _, Betr = event.pull("modem_message") | |
local Betrag = tostring(Betr) | |
--Prüfen ob der Kontostand nur gelesen werden soll | |
if Betrag == "read" then | |
if fs.exists(Konto) then -- Check if "Konto.txt" exists in Filesystem | |
local file = fs.open(Konto, "r") | |
modem.send(Tablet, Channel, file:read()) | |
file.close() | |
else | |
print("Could not open Konto.txt") -- Could not open "Konto.txt" | |
end | |
--Sonst mit der Überweisung beginnen | |
else | |
--Prüfen ob Der Betrag Positiv ist | |
if Betrag > 0 then | |
--Wenn ja prüfen ob die Nachricht vom User stammt | |
if Adresse == Tablet then | |
--Wenn ja, Überweisung | |
if fs.exists(Konto) then -- Check if "Konto.txt" exists in Filesystem | |
local file = fs.open(Konto, "wb") | |
local Kontostand = tostring(file:read()); | |
--Prüfen ob auf dem Konto genug Geld vorhanden ist | |
if Kontostand >= Betrag then | |
--Überweisung des Geldes | |
modem.send(Tablet, Channel, "Success") | |
local _, _, _, _, _, Empf, Port = event.pull("modem_message") | |
local Emp = tostring(Empf) | |
modem.send(Emp, Port, Betrag) | |
local _, _, _, _, _, _ = event.pull("modem_message") | |
Kontostand = Kontostand - Betrag | |
file:write(Kontostand) | |
modem.send(Tablet, Channel, "Success") | |
file.close() | |
--Sonst Fehler senden | |
else | |
modem.send(Tablet, Channel, "Error2: Not enough money on account") | |
end | |
else | |
print("Error:Could not find 'Konto.txt'") | |
end | |
--Sonst Addition des Geldes | |
else | |
if fs.exists(Konto) then -- Check if "Konto.txt" exists in Filesystem | |
local file = fs.open(Konto, "wb") | |
local Kontostand = tostring(Konto:read()) | |
Kontostand = Kontostand + Betrag | |
file:write(Kontostand) | |
modem.send(Adresse, Channel, "Success") | |
file.close() | |
else | |
print("Could not find Konto.txt") | |
end | |
end | |
--Wenn nicht Positiv Fehler Senden | |
else | |
modem.send(Tablet, Channel, "Error1: Cannot transfer negative values or nil") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment