-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
317 lines (292 loc) · 7.55 KB
/
client.lua
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package.cpath = "skynet/luaclib/?.so;luaclib/?.so"
package.path = "skynet/lualib/?.lua;service/?.lua;etc/?.lua;lualib/?.lua;proto/?.lua;"
local socket = require "clientsocket"
local sproto = require "sproto"
local proto = require "pack_proto"
local error = require "error"
local config = require "server_config"
local logind_host = sproto.new(proto.logindmsg):host("package")
local logind_pack_req = logind_host:attach(sproto.new(proto.logindmsg))
local lobby_host = sproto.new(proto.lobbymsg):host("package")
local lobby_pack_req = lobby_host:attach(sproto.new(proto.lobbymsg))
local gated_host = sproto.new(proto.gatedmsg):host("package")
local gated_pack_req = gated_host:attach(sproto.new(proto.gatedmsg))
local battle_host = sproto.new(proto.battlemsg):host("package")
local battle_pack_req = battle_host:attach(sproto.new(proto.battlemsg))
local fd
local session = 1
local last = ""
local logged = false
local command = {}
local status = "Unsign"
local account, password, token
local allow_cmd = {"Unsign", "Signed", "Ready", "Battle"}
allow_cmd.Unsign = {"sign_in", "sign_up", "quit"}
allow_cmd.Signed = {"ready", "sign_out", "quit"}
allow_cmd.Ready = {"cancel_ready"}
allow_cmd.Battle = {"choose_career"}
local function send_pack(msg)
local pack = string.pack(">s2", msg)
socket.send(fd, pack)
end
local function send_to_battle(msg)
local pack = gated_pack_req("message", {
type = "redirect",
msg = msg
}, session)
pack = string.pack(">s2", pack)
socket.send(fd, pack)
session = session + 1
end
local function connect(conf) -- {address,port}
if fd then
socket.close(fd)
end
fd = socket.connect(conf.address, conf.port)
end
local function show_command()
local cmd = allow_cmd[status]
local str = ""
for _, v in ipairs(cmd) do
str = str .. v .. "\t"
end
print(str)
end
local function unpack(str)
local size = #str
if size < 2 then
return nil, str
end
local len = str:byte(1) * 256 + str:byte(2)
if size < len + 2 then
return nil, str
end
return str:sub(3, 2 + len), str:sub(3 + len)
end
local function recv(last) -- recv从socket读取的数据
local result
result, last = unpack(last)
if result then
return result, last
end
local r = socket.recv(fd)
if r == "" then
return nil, last
end
return recv(last .. r)
end
local function get_response(host)
local str
str, last = recv(last)
if not str then
print("Lost connection with server !")
os.exit()
return
end
local _, _, res = host:dispatch(str)
return res
end
local function call_RPC(host, pack_req, func, args)
local msg = pack_req(func, args, session)
session = session + 1
send_pack(msg)
local res = get_response(host)
return res
end
local function bind_gated(res)
local msg = gated_pack_req("bind", {
token = token
}, session)
session = session + 1
local res = call_RPC(gated_host, gated_pack_req, "message", {
type = "bind",
msg = msg
})
if res.result == error.ok then
return true
end
return false
end
local function log_input()
local account, password
while true do
print("Enter your account:")
print("(Whitespace will be ignored)")
::account::
account = io.read("l")
account = string.gsub(account, ' ', '')
if account == "" or account == nil then
print("Account cannot be empty !")
goto account
end
if #account > 12 then
print("Account length cannot be greater than 12 !")
goto account
end
print("Enter your password:")
print("(Whitespace will be ignored)")
::password::
password = io.read("l")
password = string.gsub(password, ' ', '')
if password == "" or password == nil then
print("Password cannot be empty !")
goto password
end
if #password > 12 then
print("Password length cannot be greater than 12 !")
goto password
end
break
end
return account, password
end
local function sign(arg)
account, password = log_input()
local cmd, args
args = {
account = account,
password = password
}
if arg == "in" then
cmd = "sign_in"
elseif arg == "up" then
cmd = "sign_up"
end
local res = call_RPC(logind_host, logind_pack_req, cmd, args)
if not res then -- 失去与服务端的连接
socket.close(fd)
status = "Unsign"
return
end
if res.result ~= error.ok then
error.print_error(res.result)
return
end
status = "Signed"
token = res.token
connect({
address = res.address,
port = res.port
})
res = call_RPC(lobby_host, lobby_pack_req, "bind", {
token = token
})
if res.result == error.Reconnect then
connect({
address = res.conf.address,
port = res.conf.port
})
if bind_gated(res.conf) then
status = "Battle"
else
status = "Unsign"
end
else
command.query_score()
end
end
function command.sign_in()
sign("in")
end
function command.sign_up()
sign("up")
end
function command.sign_out()
call_RPC(lobby_host, lobby_pack_req, "sign_out", {})
fd = socket.connect("127.0.0.1", 6666)
-- connect({
-- address = "127.0.0.1",
-- port = 6666
-- })
status = "Unsign"
end
function command.query_score()
local res = call_RPC(lobby_host, lobby_pack_req, "query_score", {})
if res.result == error.ok then
print("user:" .. account .. "\nscore:" .. res.score)
else
error.print_error(res.result)
end
end
local function check_cmd(cmd)
if not command[cmd] then
print("Command not found !")
return false
end
local command = allow_cmd[status]
for _, v in ipairs(command) do
if v == cmd then
return true
end
end
print("Error command !")
return false
end
function command.ready()
call_RPC(lobby_host, lobby_pack_req, "ready", {})
status = "Ready"
local res = get_response(lobby_host)
connect({
address = res.address,
port = res.port
})
if bind_gated() then
status = "Battle"
else
status = "Unsign"
end
end
function command.cancel_ready()
call_RPC(lobby_host, lobby_pack_req, "cancel_ready", {})
status = "Signed"
end
local function input_career(str)
::input_career::
print(str)
local career = io.read("l")
for k, _ in pairs(config.career) do
if k == career then
return career
end
end
print("Error: Career not exist !")
goto input_career
end
function command.choose_career()
local config = config.career
local str = ""
for k, v in pairs(config) do
str = str .. k .. "\t"
end
local career = input_career(str)
local msg = battle_pack_req("choose_career", {
career = config[career]
}, session)
session = session + 1
send_to_battle(msg)
end
function command.quit()
if fd then
socket.close(fd)
end
end
local function main()
while true do
show_command()
local cmd = io.read("l")
if not check_cmd(cmd) then
goto main_continue
end
local func = command[cmd]
func()
if cmd == "quit" then
return
end
::main_continue::
end
end
connect({
address = "127.0.0.1",
port = 6666
})
main()