-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverPassiveQuery.lua
More file actions
163 lines (146 loc) · 5.97 KB
/
Copy pathserverPassiveQuery.lua
File metadata and controls
163 lines (146 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-- --------------------------
-- Copyright (c) 2026 Alexander L. Penny & Struan D. A. Dundas
-- Licensed under the MIT License - see LICENSE file for details
-- Project Link - https://github.com/AlexanderPenny/mcSkyblockCC
-- --------------------------
--function definitions
local function getOwnerChecksum(id)
--returns checksum that owns id
local dataFile=fs.open("/Data/registeredComputers.txt","r")
local val=dataFile.readLine()
local found=false
local data=nil
while val~=nil and found==false do
if tonumber(string.sub(val,1,val:len()-7))==id then
found=true
data=val
string.sub(data,data:len()-5,data:len())
end
val=dataFile.readLine()
end
if found then
return string.sub(data,data:len()-5,data:len())
else
return -1
end
dataFile.close()
return checksum
end
--protocol defininitons
local function getChecksum(message)
local dataFile=fs.open("/Data/info.txt","r")
for line in dataFile.readLine do
if message==line:sub(line:find("-")+1,line:len()) then
dataFile.close()
return line:sub(0,line:find("-")-1)
end
end
dataFile.close()
return("Invalid Checksum")
end
local function writeValue(id,message) --MAKE ID CHECKS SO CAN ONLY BE DONE BY VERIFIED OFFICIAL BANK COMPS
--banks main account checksum
if "UiQ83O"==getOwnerChecksum(id) then
dataFile=fs.open("/Data/"..string.sub(message,1,6)..".txt","w")
dataFile.write(string.sub(message,8,message:len()))
dataFile.close()
print()
print("Successfully updated Account with checksum "..string.sub(message,1,6).." to value "..string.sub(message,8,message:len()))
rednet.send(id,"Successfully updated Account with checksum "..string.sub(message,1,6).." to value "..string.sub(message,8,message:len()))
else
print("Computer of id "..id.." is not authorised to use this protocol")
rednet.send(id,"Computer of id "..id.." is not authorised to use this protocol")
end
end
local function readValue(id,message)
local dataFile=fs.open("/Data/"..message..".txt","r")
print("/Data/"..message..".txt")
local val=dataFile.readLine()
print("Sending value "..val.." to "..id)
rednet.send(id,val)
dataFile.close()
end
local function updateValue(id,message)
local dataFile=fs.open("/Data/"..string.sub(message,1,6)..".txt","r")
local val=tonumber(dataFile.readLine())
dataFile.close()
dataFile=fs.open("/Data/"..string.sub(message,1,6)..".txt","w")
dataFile.write(tonumber(string.sub(message,8,message:len()))+val)
print("Successfully updated Account with checksum "..string.sub(message,1,6).." incremented value by "..string.sub(message,8,message:len()))
rednet.send(id,("Successfully updated Account with checksum "..string.sub(message,1,6).." incremented value by "..string.sub(message,8,message:len())))
dataFile.close()
end
local function transaction(id,message)
--transactingFromChecksum:transactingToChecksum:Value
local dataFile=fs.open("/Data/"..string.sub(message,1,6)..".txt","r")
local losingAccountValue=tonumber(dataFile.readLine())
dataFile.close()
transactionValue=tonumber(string.sub(message,15,message:len()))
print("losing "..losingAccountValue.." transacting "..transactionValue)
if transactionValue<0 then
print("Sending value must be greater than or equal to 0")
rednet.send(id,"Sending value must be greater than or equal to 0")
else
if losingAccountValue<transactionValue then
print("Insufficient funds")
rednet.send(id,"Insufficient funds")
else
local dataFile=fs.open("/Data/"..string.sub(message,1,6)..".txt","w")
dataFile.write(losingAccountValue-transactionValue)
dataFile.close()
end
local dataFile=fs.open("/Data/"..string.sub(message,8,13)..".txt","r")
local gainingAccountValue=tonumber(dataFile.readLine())
dataFile.close()
local dataFile=fs.open("/Data/"..string.sub(message,8,13)..".txt","w")
dataFile.write(gainingAccountValue+transactionValue)
dataFile.close()
print("Successfully completed transaction with Accounts with checksums "..string.sub(message,1,6).." and "..string.sub(message,8,13)..", transaction value "..transactionValue)
rednet.send(id,"Successfully completed transaction of "..transactionValue)
end
end
local function register(id,message)
--message=checksum
--currently needs you to manually delete empty line after first registeration
local dataFile=fs.open("/Data/registeredComputers.txt","r")
local validRegister=true
local val=dataFile.readLine()
while val~=nil and validRegister==true do
if tonumber(string.sub(val,1,val:len()-7))==id then
print("Computer already has a registered account")
rednet.send(id,"Computer already has a registered account")
validRegister=false
end
val=dataFile.readLine()
end
dataFile.close()
if validRegister then
local dataFile=fs.open("/Data/registeredComputers.txt","a")
dataFile.write(id..":"..message.."\n")
dataFile.close()
end
end
--main loop below
local run=true
rednet.open("top")
rednet.open("left")
while run do
print("Waiting For Query")
local id,message,protocol=rednet.receive()
print("Recieved "..protocol.." Request from ID "..id.." stating "..message)
if protocol=="Read" then
readValue(id,message)
elseif protocol=="Write" then
writeValue(id,message)
elseif protocol=="Update" then
updateValue(id,message)
elseif protocol=="Transaction" then
transaction(id,message)
elseif protocol=="Register" then
register(id,message)
elseif protocol=="getChecksum" then
rednet.send(id,getChecksum(message))
else
rednet.send(id,"Invalid Protocol used in Network")
end
end