· 3 years ago · Nov 20, 2021, 10:30 PM
1-- Variables
2
3local w,h = term.getSize()
4local modem = "back"
5local InvFile = "Inv/Inv.cfg"
6local ShopServer = 4
7local BankServer = 5
8local ResponseTime = 5
9local CurrentPage = 1
10local CartPage = 1
11
12local Mode = "Inventory"
13local LoginPage = "Login"
14
15local List = {}
16local Amt = {}
17local Price = {}
18
19local Cart = {
20 ["name"] = {},
21 ["amount"] = {},
22}
23
24local Error404 = "Error404: Unable to communicate with server"
25local Error1 = "Error1: Account doesn't exists"
26local Error2 = "Error2: Account already exists"
27local Error3 = "Error2: table expected, got nil"
28local Error4 = "Error3: Passwords dont match"
29
30-- Rednet
31
32rednet.open(modem)
33
34
35-- API
36
37os.loadAPI("screen.api")
38os.loadAPI("B64.api")
39_G["screen"] = _G["screen.api"]
40_G["B64"] = _G["B64.api"]
41-- Function
42
43function ReadInv()
44 myInv = fs.open(InvFile, "r")
45 Inv = textutils.unserialise(myInv.readAll())
46 myInv.close()
47end
48
49function WriteInv()
50 myInv = fs.open(InvFile, "w")
51 myInv.write(textutils.serialise(Inv))
52 myInv.close()
53end
54
55function RednetSend(toID, Sendmsg)
56 Count = 0
57 msg = nil
58 while (Count <= ResponseTime and msg == nil) do
59 rednet.send(toID,Sendmsg)
60 id, msg = rednet.receive(1)
61
62 Count = Count + 1
63 end
64 if msg == nil and id == nil then
65 return Error404
66 elseif msg == 1 then
67 return Error1
68 elseif msg == 2 then
69 return Error2
70 elseif msg then
71 return msg
72 else
73 return Error3
74 end
75end
76
77function DrawMenu()
78 screen.Clear(colors.lightGray)
79 screen.clearLine(1,colors.gray)
80 screen.drawText(1,1,User)
81 screen.drawText((Bank.Balance < 10 and w-5) or (Bank.Balance < 100 and w-6) or (Bank.Balance < 1000 and w-7) or (Bank.Balance < 10000 and w-8) or (Bank.Balance < 100000 and w-9) or (Bank.Balance < 1000000 and w-10) or w-11,1,"$ "..Bank.Balance)
82 screen.drawRect(4,3,w-6,h-5,colors.gray,false,colors.lightGray)
83 screen.drawText((w-string.len("CCShop"))/2,1,"CCShop",colors.gray)
84 screen.clearLine(h,colors.gray)
85 screen.drawText(2,h,"Cart ("..#Cart["name"]..")")
86 screen.drawText(w-10,h,"refresh")
87 if Mode == "Inventory" then
88 NRInv = 0
89 for name, table in pairs( Inv ) do
90 NRInv = NRInv + 1
91 end
92 i = 1
93 for name, table in pairs( Inv ) do
94 if i <= ((h-7)+((CurrentPage-1)*(h-7))) and i > ((CurrentPage-1)*(h-7)) then
95 screen.drawText(5,4+(i-((CurrentPage-1)*(h-7))-1),name,colors.lightGray)
96 screen.drawText(w-11,4+(i-((CurrentPage-1)*(h-7))-1),"[ ]")
97 screen.drawText((table[1] < 10 and w-5) or (table[1] < 100 and w-6) or (table[1] < 1000 and w-7) or (table[1] <10000 and w-8) or (table[1] <100000 and w-9) or w-10,4+(i-((CurrentPage-1)*(h-7))-1),table[1])
98 screen.drawText((table[2] == 0 and w-14) or (table[2] < 1 and w-17) or (table[2] < 10 and w-14) or (table[2] < 100 and w-15) or (table[2] < 1000 and w-16) or (table[2] <10000 and w-17) or (table[2] <100000 and w-18) or w-19,4+(i-((CurrentPage-1)*(h-7))-1),(math.fmod(table[2]*100,10) == 0 and table[2] < 1 and table[2] ~= 0 and "$"..table[2].."0") or "$"..table[2])
99 end
100 List[i] = name
101 Amt[i] = table[1]
102 Price[i] = table[2]
103 i = i + 1
104 end
105 screen.drawText(5,3,"Page: ("..CurrentPage.."/"..math.ceil(NRInv/(h-7))..")",colors.gray)
106 screen.drawText((w-string.len("Inventory"))/2,3,"Inventory",colors.gray)
107 screen.drawText(w-20,3,"[ PREV ] [ NEXT ]")
108 elseif Mode == "Cart" then
109 screen.drawText(5,3,"Page: ("..CartPage.."/"..math.ceil(#Cart["name"]/(h-7))..")",colors.gray)
110 screen.drawText((w-string.len("Cart"))/2,3,"Cart",colors.gray)
111 screen.drawText(w-20,3,"[ PREV ] [ NEXT ]")
112 for i = 1, #Cart["name"] do
113 if i <= ((h-7)+((CartPage-1)*(h-7))) and i > ((CartPage-1)*(h-7)) then
114 screen.drawText(5,4+(i-((CartPage-1)*(h-7))-1),Cart["name"][i],colors.lightGray)
115 screen.drawText(w-6, 4+(i-((CartPage-1)*(h-7))-1),"[X]")
116 screen.drawText((Cart["amount"][i] < 10 and w - 8) or (Cart["amount"][i] < 100 and w - 9) or (Cart["amount"][i] < 1000 and w - 10) or (Cart["amount"][i] < 10000 and w - 11),4+(i-((CartPage-1)*(h-7))-1),Cart["amount"][i])
117 end
118 end
119 end
120end
121
122function Touch()
123 while true do
124 event, key, x, y = os.pullEvent()
125 if event == "mouse_click" then
126 if x >= w-20 and x <= w-13 and y == 3 then
127 if Mode == "Inventory" then
128 if CurrentPage > 1 then
129 CurrentPage = CurrentPage - 1
130 DrawMenu()
131 end
132 elseif Mode == "Cart" then
133 if CartPage < math.ceil(#Cart["name"]/(h-7)) then
134 CartPage = CartPage - 1
135 DrawMenu()
136 end
137 end
138 elseif x >= w-11 and x <= w-4 and y == 3 then
139 if Mode == "Inventory" then
140 if CurrentPage < math.ceil(NRInv/(h-7)) then
141 CurrentPage = CurrentPage + 1
142 DrawMenu()
143 end
144 elseif Mode == "Cart" then
145 if CartPage < math.ceil(#Cart["name"]/(h-7)) then
146 CartPage = CartPage + 1
147 DrawMenu()
148 end
149 end
150 elseif x >= 2 and x <= 9 and y == h then
151 if Mode == "Inventory" then
152 Mode = "Cart"
153 elseif Mode == "Cart" then
154 Mode = "Inventory"
155 end
156 DrawMenu()
157 elseif x >= w-10 and x <= w-4 and y == h then
158 if Mode == "Inventory" then
159 Inv = RednetSend(ShopServer,"request_inv")
160 Bank = textutils.unserialise(RednetSend(BankServer, "request_account_"..User))
161 elseif Mode == "Cart" then
162 DrawMenu()
163 end
164 end
165
166 if Mode == "Inventory" then
167 if x >= w-10 and x <= w-4 and y == h then
168 Inv = RednetSend(ShopServer,"request_inv")
169 Bank = textutils.unserialise(RednetSend(BankServer, "request_account_"..User))
170 DrawMenu()
171 elseif x >= 1 and x <= string.len(User) and y == 1 then
172 Login()
173 end
174
175 for i = 1, (h-7) do
176 if x >= w-10 and x<= w-4 and y == i+3 then
177 Mode = "Order"
178 Item = List[i+((CurrentPage-1)*(h-7))]
179 amount = Amt[i+((CurrentPage-1)*(h-7))]
180 request_amount = 0
181 button = nil
182 screen.drawRect(8,5,w-15,h-9,colors.blue,false, colors.gray)
183 screen.drawText((w-string.len("Order: "..Item))/2,5,"Order: "..Item,colors.blue)
184 screen.drawText((w-string.len("In Stock: "..amount))/2,7,"In Stock: "..amount,colors.gray)
185 screen.drawText((w-string.len(request_amount))/2,9,request_amount,colors.gray)
186 screen.drawText((w-string.len("-10 -1 +1 +10"))/2,10,"-10 -1 +1 +10",colors.gray)
187 screen.drawText((w-string.len("Enter"))/2,5+(h-12),"Enter",colors.gray)
188 end
189 end
190
191 elseif Mode == "Order" then
192 if x >= (w-string.len("-10 -1 +1 +10"))/2 and x<= (w-string.len("-10 -1 +1 +10"))/2 + 2 and y == 10 then
193 if request_amount >= 10 then
194 request_amount = request_amount - 10
195 screen.drawText(9,9,string.rep(" ",w-17))
196 screen.drawText((w-string.len(request_amount))/2,9,request_amount,colors.gray)
197 end
198 elseif x >= (w-string.len("-10 -1 +1 +10"))/2 + 6 and x<= (w-string.len("-10 -1 +1 +10"))/2 + 7 and y == 10 then
199 if request_amount >= 1 then
200 request_amount = request_amount - 1
201 screen.drawText(9,9,string.rep(" ",w-17))
202 screen.drawText((w-string.len(request_amount))/2,9,request_amount,colors.gray)
203 end
204 elseif x >= (w-string.len("-10 -1 +1 +10"))/2 + 13 and x<= (w-string.len("-10 -1 +1 +10"))/2 + 14 and y == 10 then
205 request_amount = request_amount + 1
206 screen.drawText(9,9,string.rep(" ",w-17))
207 screen.drawText((w-string.len(request_amount))/2,9,request_amount,colors.gray)
208 elseif x >= (w-string.len("-10 -1 +1 +10"))/2 + 18 and x<= (w-string.len("-10 -1 +1 +10"))/2 + 20 and y == 10 then
209 request_amount = request_amount + 10
210 screen.drawText(9,9,string.rep(" ",w-17))
211 screen.drawText((w-string.len(request_amount))/2,9,request_amount,colors.gray)
212 elseif x >= (w-string.len("Enter"))/2 and x <= (w-string.len("Enter"))/2 + 4 and y == 5+(h-12) then
213 Mode = "Inventory"
214 Found = false
215 for index, name in pairs(Cart["name"]) do
216 if Item == name then
217 Cart["amount"][index] = Cart["amount"][index] + request_amount
218 Found = true
219 end
220 end
221 if Found == false then
222 Cart["name"][#Cart["name"]+1] = Item
223 Cart["amount"][#Cart["amount"]+1] = request_amount
224 end
225 term.setCursorPos(1,1)
226 sleep(1)
227 DrawMenu()
228 end
229 elseif Mode == "Cart" then
230 for i = 1, (h-7) do
231 if x >= w-6 and x <= w-4 and y == i+3 then
232 for j = i+((CartPage-1)*(h-7)),#Cart["name"] do
233 Cart["name"][j] = Cart["name"][j+1]
234 Cart["amount"][j] = Cart["amount"][j+1]
235 end
236 DrawMenu()
237 end
238 end
239 end
240 end
241 end
242end
243
244function Rednet()
245 Inv = RednetSend(ShopServer,"request_inv")
246 --Bank = textutils.unserialise(RednetSend(BankServer, "request_account_Jetro"))
247 DrawMenu()
248 Touch()
249end
250
251function Login()
252 if LoginPage == "Login" then
253 screen.Clear(colors.lightGray)
254 screen.clearLine(1,colors.gray)
255 screen.drawRect(4,3,w-6,h-5,colors.gray,false,colors.lightGray)
256 screen.drawText((w-string.len("CCShop"))/2,1,"CCShop",colors.gray)
257 screen.drawText((w-string.len("Login"))/2,3,"Login",colors.gray)
258 screen.drawText(10,(h/2)-1, "Username: ",colors.lightGray)
259 screen.drawText(10,(h/2)+1, "Password: ",colors.lightGray)
260 screen.clearLine(h,colors.gray)
261 term.setBackgroundColor(colors.lightGray)
262 term.setCursorPos(20,(h/2)-1)
263 User = read()
264 term.setCursorPos(20,(h/2)+1)
265 Pass = read("*")
266 Bank = RednetSend(BankServer, "request_account_"..User)
267 if Bank == Error1 then
268 screen.drawText(10,(h/2)+3,Error1)
269 screen.drawText(10,(h/2)+4,"Do you wish to create an acount? [y/n]")
270 input = read()
271 if input == "y" then
272 LoginPage = "CreateLogin"
273 Login()
274 elseif input == "n" then
275 Login()
276 end
277
278 elseif Bank == Error404 then
279 screen.drawText(10,(h/2)+3,Error404)
280 sleep(1)
281 Login()
282 elseif Bank == Error3 then
283 screen.drawText(10,(h/2)+3,Error3)
284 sleep(1)
285 Login()
286 else
287 Bank = textutils.unserialise(Bank)
288 if Pass == B64.decode(Bank.Password) then
289 screen.drawText(10,(h/2)+3,"Welcome "..User)
290 sleep(1)
291 Rednet()
292 else
293 screen.drawText(10,(h/2)+3,"wrong password")
294 sleep(1)
295 Login()
296 end
297 end
298 elseif LoginPage == "CreateLogin" then
299 screen.Clear(colors.lightGray)
300 screen.clearLine(1,colors.gray)
301 screen.drawRect(4,3,w-6,h-5,colors.gray,false,colors.lightGray)
302 screen.drawText((w-string.len("CCShop"))/2,1,"CCShop",colors.gray)
303 screen.drawText((w-string.len("Login"))/2,3,"Login",colors.gray)
304 screen.drawText(10,(h/2)-1, "Username: ",colors.lightGray)
305 screen.drawText(10,(h/2)+1, "Password: ",colors.lightGray)
306 screen.drawText(10,(h/2)+3, "Repeat password: ",colors.lightGray)
307 term.setCursorPos(20,(h/2)-1)
308 User = read()
309 term.setCursorPos(20,(h/2)+1)
310 Pass = read("*")
311 term.setCursorPos(28,(h/2)+3)
312 RPass = read("*")
313 screen.clearLine(h,colors.gray)
314 if Pass ~= RPass then
315 screen.drawText(10,(h/2)+5,Error4,colors.lightGray)
316 sleep(1)
317 Login()
318 end
319 Create = RednetSend(BankServer,"create_account_"..User.."_"..B64.encode(Pass))
320 if Create == true then
321 screen.drawText(10,(h/2)+5,"Account Succesfully created",colors.lightGray)
322 LoginPage = "Login"
323 sleep(1)
324 Login()
325 elseif Create == Error2 then
326 screen.drawText(10,(h/2)+5,Error2,colors.lightGray)
327 sleep(1)
328 Login()
329 end
330 end
331end
332
333-- Main
334
335Login()
336
337