This repository was archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.lua
78 lines (68 loc) · 1.74 KB
/
connect.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
-- Load luasockets
local socket = require("socket")
-- Class
CHaserConnect = {}
CHaserConnect.__index = CHaserConnect
function CHaserConnect:Init()
-- io ip
print("Please input the IP address:")
self.ip = io.read()
-- io port
print("Please input the port number:")
self.port = io.read()
-- io name
print("What is your name?")
self.name = io.read()
-- create tcp socket
self.socket = socket.tcp()
-- connect
self.socket:connect(self.ip, self.port)
if self.socket then
print("Connection established!")
else
print("Connection failed!")
end
-- create instance
local instance = {}
setmetatable(instance, CHaserConnect)
return instance
end
function CHaserConnect:InitSocket()
-- send name
self.socket:send(self.name .. "\n")
end
function CHaserConnect:GetReady()
-- send ready
self.socket:send("gr\n")
end
function CHaserConnect:Walk(direction)
print(self.name .. " requested to walk " .. direction .. ".")
if direction == "up" then
self.socket:send("wu\n")
elseif direction == "down" then
self.socket:send("wd\n")
elseif direction == "left" then
self.socket:send("wl\n")
elseif direction == "right" then
self.socket:send("wr\n")
end
local response = self.socket:receive()
local results = tonumber(msg)
return results
end
function CHaserConnect:Look(direction)
print(self.name .. " requested to look " .. direction .. ".")
if direction == "up" then
self.socket:send("lu\n")
elseif direction == "down" then
self.socket:send("ld\n")
elseif direction == "left" then
self.socket:send("ll\n")
elseif direction == "right" then
self.socket:send("lr\n")
end
local response = self.socket:receive()
local results = tonumber(msg)
return results
end
return CHaserConnect