· 6 years ago · Jan 01, 2020, 02:06 AM
1local version = "Beta 0.2"
2
3local passKey = "Do not edit. Re-install ChamShop Mobile if you do so."
4
5local width, height = term.getSize()
6local id
7
8local opID = 422
9local reID = 425
10
11local connected = false
12
13local function cenLeft(objWidth)
14 return (width - objWidth) / 2 + 1
15end
16
17local function cenTop(objHeight)
18 return (height - objHeight) / 2 + 1
19end
20
21local function startup()
22 term.clear()
23 term.setCursorPos(cenLeft(26),cenTop(1)-1)
24 term.write("Launching ChamShop Mobile!")
25 term.setCursorPos(cenLeft(24),cenTop(1)+1)
26 term.write("Connecting to Servers...")
27 term.setCursorPos(1,1)
28 print("WARNING: Beware of counterfeit programs! Do not trust computers given from other players! Keep your computer on you at all times!")
29end
30
31local function locateRednet()
32 if peripheral.find("modem")==nil then
33 error("Ender modem not found", 2)
34 end
35 local sides = peripheral.getNames()
36
37 for _, side in pairs(sides) do
38 if peripheral.getType(side)=="modem" then
39 rednet.open(side)
40 return
41 end
42 end
43end
44
45local function runMsg(c)
46 local l = #c
47 local msgType = c[1]
48 local msgCat = -1
49 if (msgType=="func" or msgType=="var" or msgType=="impRcl") then
50 msgCat = 0
51 elseif msgType=="impSto" then
52 msgCat = 1
53 end
54 if (msgCat==0 and l<3) or (msgCat==1 and l<4) or msgCat==-1 then
55 return c
56 end
57
58 if msgCat==0 then
59
60 -- Set Implicit Values and Functions
61 local api = c[2]
62 local func = c[3]
63
64 -- Check for things to handle in arguments
65 for i = 4, l do
66 if type(c[i])=="table" then
67 c[i] = runMsg(c[i])
68 end
69 end
70
71 -- Sets up the function/variable
72 local prompt = _G[api][func]
73
74 -- Removes the first 3 values of the table, which are before the arguments
75 for i = 1, 3 do
76 table.remove(c,1)
77 end
78
79 if l==3 then
80 if msgType=="func" then
81 return prompt()
82 elseif msgType=="var" then
83 return prompt
84 elseif msgType=="impRcl" then
85 return _G["csm_"..api].prompt()
86 end
87 elseif l>3 then
88 if msgType=="func" then
89 return prompt(unpack(c))
90 elseif msgType=="impRcl" then
91 return _G["csm_"..api].prompt(unpack(c))
92 end
93 end
94 elseif msgCat==1 and msgType=="impSto" then
95 local var = c[2]
96 local api = c[3]
97 local func = c[4]
98 for i = 5, l do
99 if type(c[i])=="table" then
100 c[i] = runMsg(c[i])
101 end
102 end
103
104 local prompt = _G[api][func]
105
106 for i = 1, 4 do
107 table.remove(c,1)
108 end
109
110 if l==4 then
111 _G["csm_"..var] = prompt()
112 elseif l>4 then
113 _G["csm_"..var] = prompt(unpack(c))
114 end
115 return _G["csm_"..var]
116 end
117end
118
119local function run()
120 while true do
121 local e = {os.pullEvent()}
122 if e[1]=="rednet_message" and e[4]=="csm" and type(e[3])=="table" and (e[3][1]=="func" or e[3][1]=="var") then
123 id = e[2]
124 local output = runMsg(e[3])
125 if (id==reID or id==opID) and output then
126 connected = true
127 end
128 rednet.send(id, output, "csm")
129 elseif id then
130 if e[1]=="key" then
131 local key = e[2]
132 local held = e[3]
133 rednet.send(id,{"key",key,held},"csm")
134 elseif e[1]=="key_up" then
135 local key = e[2]
136 rednet.send(id,{"key_up",key},"csm")
137 elseif e[1]=="mouse_click" then
138 local button = e[2]
139 local x = e[3]
140 local y = e[4]
141 rednet.send(id,{"mouse_click",button,x,y},"csm")
142 elseif e[1]=="terminate" then
143
144 end
145 end
146 end
147end
148
149local function connectExcept()
150 term.setCursorPos(cenLeft(26),cenTop(1)-1)
151 term.write("Cannot connect to servers.")
152 term.setCursorPos(1,cenTop(1)+1)
153 term.clearLine()
154 print("Try reinstalling the program, or ChamShop Mobile Services may be under maintenance. Check updates on twitter.com/ChamShopInfo")
155end
156
157local function disconnect(check)
158 if not check then
159 error()
160 end
161end
162
163local function loadingAnimation()
164 term.setCursorPos(1,cenTop(1))
165 local i = 1
166 local w = width or 20
167 while i <= w and not connected do
168 io.write("-")
169 i = i + 1
170 os.sleep(1/w)
171 end
172 if not connected then
173 connectExcept()
174 error()
175 end
176end
177
178locateRednet()
179startup()
180parallel.waitForAll(loadingAnimation, run)