View difference between Paste ID: 2BCqWa2c and 5sY44uVz
SHOW: | | - or go back to the newest paste.
1
local file = fs.open(".server", "r")
2
3
if file == nil then
4
   print("No server configured")
5
   return
6
end
7
8
local server = tonumber(file.readLine())
9
file.close()
10
11
local sid
12
local user = "Guest"
13
local modem = peripheral.find("modem")
14
local event, side, sChannel, id, data, dist
15
local _id = os.getComputerID()
16
local admin = false
17
18
local function idRNG()
19
   modem.close(_id)
20
   _id = math.random(50000, 55000)
21
   modem.open(_id)
22
end
23
24
local function receive()
25
   event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
26
end
27
28
local function send(to, msg)
29
   idRNG()
30
   modem.transmit(to, _id, msg)
31
   receive()
32
end
33
34
local function addmoney(usr, amt)
35
   send(server,"add")
36
   send(id,sid..":"..usr..":"..amt)
37
end
38
39
local function remusr(usr)
40
   send(server,"delete")
41
   send(id,sid..":"..usr)
42
end
43
44
local function chmod(usr,bool)
45
   send(server,"chmod")
46
   send(id,sid..":"..usr..":"..bool)
47
end
48
49
local function PClick()
50
   local evnt, button, x, y = os.pullEvent("mouse_click")
51
   local A = x
52
   local B = y
53
   local C = button
54
   return A, B, C
55
end
56
57
local function start()
58
   while true do
59
      term.setBackgroundColor(8)
60
      term.setTextColor(16)
61
      term.clear()
62
      term.setCursorPos(1, 1)
63
      print("Welcome, " .. user)
64
      if sid ~= nil then
65
         term.setCursorPos(3, 4)
66
         print("[Logout]")
67
         term.setCursorPos(3, 6)
68
         print("[Balance]")
69
         term.setCursorPos(3, 8)
70
         print("[Transfer]")
71
         if admin then
72
            term.setCursorPos(3, 10)
73
            print("[R/A $]")
74
            term.setCursorPos(3,12)
75
            print("[Remove User]")
76
            term.setCursorPos(3,14)
77
            print("[Change User Status]")
78
            x, y, button = PClick()
79
            if button == 1 and x >= 3 and x <= 9 and y == 10 then
80
               term.setBackgroundColor(8)
81
               term.setTextColor(16)
82
               term.clear()
83
               term.setCursorPos(1, 1)
84
               print("Welcome, " .. user)
85
               term.setCursorPos(3, 4)
86
               write("User: ")
87
               USR = read()
88
               term.setCursorPos(3, 5)
89
               write("Amount: $")
90
               ammt = read()
91
               addmoney(USR,ammt)
92
            elseif button == 1 and x >= 3 and x <= 15 and y == 12 then
93
               term.setBackgroundColor(8)
94
               term.setTextColor(16)
95
               term.clear()
96
               term.setCursorPos(1, 1)
97
               print("Welcome, " .. user)
98
               term.setCursorPos(3, 4)
99
               write("User: ")
100
               USR = read()
101
               remusr(USR)
102
            elseif button == 1 and x >= 3 and x <= 22 and y == 14 then
103
               term.setBackgroundColor(8)
104
               term.setTextColor(16)
105
               term.clear()
106
               term.setCursorPos(1, 1)
107
               print("Welcome, " .. user)
108
               term.setCursorPos(3, 4)
109
               write("User: ")
110
               USR = read()
111
               term.setCursorPos(3, 5)
112
               write("Admin (true/false): ")
113
               BOOL = read()
114
               chmod(USR,BOOL)
115
            end
116
         end
117
      else
118
         term.setCursorPos(3, 4)
119
         print("[Login]")
120
         term.setCursorPos(3, 6)
121
         print("[Create Account]")
122
      end
123
      event, button, x, y = os.pullEvent("mouse_click")
124
      if x >= 3 and x <= 10 and y == 4 and sid == nil then
125
         send(server, "login")
126
         term.clear()
127
         term.setCursorPos(3, 4)
128
         write("User: ")
129
         user = read()
130
         term.setCursorPos(3, 5)
131
         write("Pass: ")
132
         pass = read("*")
133
         send(id, user .. ":" .. pass)
134
          if data:find("Login Successful") and data:find(":") then
135
            if data:find(":", data:find(":") + 1) then
136
               admin = true
137
               local c1 = data:find(":")
138
               local c2 = data:find(":", c1 + 1)
139
               sid = data:sub(c1 + 1, c2 - 1)
140
            else
141
               admin = false
142
               sid = data:sub(data:find(":") + 1)
143
            end
144
            print(data:sub(0, data:find(":") - 1))
145
         else
146
            user = "Guest"
147
            print(data)
148
         end
149
         term.setCursorPos(3, 2)
150
         sleep(1)
151
      elseif x >= 3 and x <= 10 and y == 4 and sid ~= nil then
152
         user = "Guest"
153
         send(server, "logout")
154
         send(id, sid)
155
         sid = nil
156
         print(data)
157
         sleep(1)
158
      elseif x >= 3 and x <= 11 and y == 6 and sid ~= nil then
159
         send(server, "balance")
160
         send(id, sid)
161
         term.clear()
162
         term.setCursorPos(1, 1)
163
         print("Balance is $" .. data)
164
         os.pullEvent("mouse_click")
165
      elseif x >= 3 and x <= 12 and y == 8 and sid ~= nil then
166
         send(server, "transfer")
167
         term.clear()
168
         term.setCursorPos(3, 4)
169
         write("Amount: $")
170
         amt = read()
171
         term.setCursorPos(3, 6)
172
         write("To User: ")
173
         user2 = read()
174
         send(id, sid .. ":" .. user2 .. ":" .. amt)
175
         term.setCursorPos(3, 2)
176
         print(data)
177
         os.pullEvent("mouse_click")
178
      elseif x >= 3 and x <= 18 and y == 6 and sid == nil then
179
         term.clear()
180
         term.setCursorPos(3, 4)
181
         write("Username: ")
182
         user = read()
183
         term.clear()
184
         term.setCursorPos(3, 4)
185
         write("Password: ")
186
         pass = read("*")
187
         term.clear()
188
         send(server, "create")
189
         send(id, user .. ":" .. pass)
190
         user = "Guest"
191
         term.setCursorPos(3, 4)
192
         print(data)
193
         os.pullEvent("mouse_click")
194
      end
195
   end
196
end
197
198
start()