· 9 years ago · Jan 31, 2017, 12:38 AM
1local menu_options = { --This is our menu table. It contains basic data about the menu
2 [1] = {text="Connection", color=colors.green},
3 [2] = {text="Cree un compte", color=colors.blue},
4 [3] = {text="Nouvelle carte - 100$", color=colors.white}
5}
6local termX, termY = term.getSize() --The x/y size of the terminal
7local function menuDraw(selected) --Our main draw function
8 local yPos = termY/2 - #menu_options/2 --The initial y position
9 for index, data in pairs(menu_options) do
10 menu_options[index].bounds = { --Create a new table in each option with the boundary data
11 x1 = termX/2 - (#data.text+4)/2;
12 x2 = termX/2 + (#data.text+4)/2;
13 y = yPos;
14 }
15 term.setTextColor(data.color)
16 term.setCursorPos(data.bounds.x1, data.bounds.y)
17
18 local text =
19 index==selected and "[ "..data.text.." ]" or
20 " "..data.text.." " --Essentially an if statement, but in a contracted form
21 term.write(text)
22 yPos = yPos+1 --Increment the initial y pos so we can move on the next line
23 end
24end
25
26local function checkClick(x,y) --Check the mouse click to see if there's a menu option
27 for index, data in pairs(menu_options) do
28 if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
29 return index --Returns the index of the clicked option
30 end
31 end
32 return false --If it went through the entire for loop without success, return false
33end
34
35term.setBackgroundColor(colors.black)
36term.clear()
37term.setTextColor(colors.blue)
38local selector = 1 --Our selector
39while true do
40 menuDraw(selector) --Draw the menu first
41 local e = {os.pullEvent()} --Pull an event and put the returned values into a table
42 if e[1] == "key" then --If it's a key...
43 if e[2] == keys.down then -- ... and it's the down arrow
44 selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
45 elseif e[2] == keys.up then
46 selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
47 elseif e[2] == keys.enter then
48 break --Break out of the loop
49 end
50 elseif e[1] == "mouse_click" then
51 local value = checkClick(e[3], e[4]) --Check the mouse click
52 if value then --If checkClick returns a value and not false
53 selector = value --Set the selector to that value and break out of the loop
54 break
55 end
56 elseif e[1] == "disk" then
57 autoco()
58 end
59end
60
61function autoco()
62 if disk.isPresent("bottom") then
63 term.clear()
64 term.setCursorPos(1,1)
65 print("Code d'acces:")
66 code = read("*")
67 if fs.exists("disk/.card") then
68 if fs.exists("disk/."..code) then
69 profil = fs.open("disk/."..code,"r")
70 profilinfo = textutils.unserialize(profil.readAll())
71 profil.close()
72 name = profilinfo.name
73 else
74 print("Mauvais code ! Ressayer")
75 sleep(1)
76 autoco()
77 end
78 else
79 print("Ce n'est pas une carte de banque !")
80 sleep(1)
81 disk.eject("bottom")
82 os.reboot()
83 end
84 else
85 detectdisk()
86 end
87 return name,code
88end
89
90function detectdisk()
91 local event,side = os.pullEvent()
92 if event == "disk" then
93 autoco()
94 else
95 detectdisk()
96 end
97end
98
99term.clear()
100term.setCursorPos(1,1)
101term.setTextColor(colors.blue)
102rednet.open("back")
103if selector == 1 then
104 print("Veuillez inserer votre carte banquaire...")
105 name,code = autoco()
106 if name == nil then
107 os.reboot()
108 end
109 rednet.send(87,"profil request")
110 rednet.send(87,name)
111 rednet.send(87,code)
112 local id,msg = rednet.receive()
113 if msg == "profil exists" then
114 id,balance = rednet.receive()
115 shell.run("menu")
116 elseif msg == "account not exists" then
117 print("Ce compte n'existe pas, cree en un")
118 sleep(1)
119 os.reboot()
120 elseif msg == "wrongpass" then
121 print("Mauvais code")
122 sleep(1)
123 os.reboot()
124 end
125elseif selector == 2 then
126 print("Nom du compte:")
127 local name = read()
128 print("Code d'acces:")
129 local code = read()
130 rednet.send(87,"profil add")
131 rednet.send(87,name)
132 rednet.send(87,code)
133 rednet.send(92,"createcard")
134 rednet.send(92,name)
135 rednet.send(92,code)
136 local id,msg = rednet.receive()
137 if msg == "profil ok" then
138 print("Compte et carte cree !")
139 sleep(1)
140 os.reboot()
141 elseif msg == "profil already exists" then
142 print("Compte existant...")
143 sleep(1)
144 os.reboot()
145 end
146elseif selector == 3 then
147 print("Nom sur la carte:")
148 name = read()
149 print("Code d'acces:")
150 code = read()
151 rednet.send(87,"profil request")
152 rednet.send(87,name)
153 rednet.send(87,code)
154 local id,msg = rednet.receive()
155 if msg == "profil exists" then
156 local id,accbal = rednet.receive()
157 if tonumber(accbal) >= 100 then
158 print("Creation de la nouvelle carte...")
159 rednet.send(92,"createcard")
160 rednet.send(92,name)
161 rednet.send(92,code)
162 rednet.send(87,"retrait")
163 rednet.send(87,"100")
164 term.clear()
165 term.setCursorPos(1,1)
166 term.setTextColor(colors.green)
167 print("Carte cree !")
168 disk.eject("bottom")
169 sleep(1)
170 os.reboot()
171 else
172 term.setTextColor(colors.red)
173 print("Votre solde est insufisant !")
174 sleep(1)
175 os.reboot()
176 end
177 elseif msg == "profil not exists" then
178 print("Ce compte n'existe pas...")
179 sleep(1)
180 os.reboot()
181 end
182end