-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdefine.lua
More file actions
145 lines (132 loc) · 4.44 KB
/
define.lua
File metadata and controls
145 lines (132 loc) · 4.44 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
return function(l)
local version = "0.1"
-- Add version info
l:addOp('version')
-- Get version info to client when it requests it
l:addProcessOnServer('version',function(self,peer,arg,storage)
return version
end)
-- Process version information on client side
l:addProcessOnClient('version',function(self,peer,arg,storage)
if version == arg then
return true
else
return "Version mismatch!\n\nClient: "..tostring(arg).."\nServer: "..tostring(version)
end
end)
-- What to do while it waits for version information
l:addDefaultOnClient('version',function(self,peer,arg,storage)
return "Waiting for version information"
end)
local isValidString = function(input)
local utf8 = require("utf8")
local success = utf8.len(input)
return success
end
-- Add a way to name users
l:addOp('whoami')
-- Validate the name argument
l:addValidateOnServer('whoami',{name="string"})
-- Store the user's name in the user data
l:addProcessOnServer('whoami',function(self,peer,arg,storage)
local user = self:getUser(peer)
self:log("event","Rename: "..tostring(user.name).." => "..tostring(arg.name))
if isValidString(arg.name) then
user.name = arg.name
end
end)
-- Add a way to send the current user's position
l:addOp('pos')
-- Validate the x and y arguments as numbers
l:addValidateOnServer('pos',{x="number",y="number"})
-- Store the position of the user in the user data
l:addProcessOnServer('pos',function(self,peer,arg,storage)
local user = self:getUser(peer)
user.x = arg.x
user.y = arg.y
end)
-- Add a way to inform all clients where all players are
l:addOp('p')
-- Create a table containing the name, x and y of each user
l:addProcessOnServer('p',function(self,peer,arg,storage)
local info = {}
for i,v in pairs(self:getUsers()) do
if v.x and v.y then
table.insert(info,{name=v.name,x=v.x,y=v.y})
end
end
-- Return it to the requester
return info
end)
-- Validate that the data is indeed a table containing users with name,x and y
l:addValidateOnClient('p',function(self,peer,arg,storage)
if type(arg) ~= "table" then return false,"root expecting table" end
for i,v in pairs(arg) do
if type(v.name) ~= "string" then return false,"v.name expecting string" end
if type(v.x) ~= "number" then return false,"v.x expecting number" end
if type(v.y) ~= "number" then return false,"v.y expecting number" end
end
return true
end)
-- Provide an empty table by default when a client requests the players
l:addDefaultOnClient('p',function(self,peer,arg,storage)
return {}
end)
-- Get board updates
l:addOp('b')
l:addValidateOnServer('b',"number")
l:addProcessOnServer('b',function(self,peer,arg,storage)
local ret = {}
for x,row in pairs(storage.board or {}) do
for y,val in pairs(row) do
if val.u >= arg then
table.insert(ret,{x=x,y=y,u=val.u,r=val.r,g=val.g,b=val.b})
end
end
end
return ret
end)
l:addOp('draw')
local check_dim = function(data)
if type(data) ~= "number" then return false,"data expecting number" end
if math.floor(data) ~= data then return false,"data expecting int" end
if data < 1 or data > board_size then
return false,"data expecting 1-"..board_size
end
return true
end
local check_color_part = function(data)
if type(data) ~= "number" then return false,"data expecting number" end
if math.floor(data) ~= data then return false,"data expecting int" end
if data < 0 or data > 255 then
return false,"data expecting 0-255"
end
return true
end
l:addValidateOnServer('draw',{
x=check_dim,y=check_dim,
r=check_color_part,g=check_color_part,b=check_color_part,
})
l:addProcessOnServer('draw',function(self,peer,arg,storage)
storage.board = storage.board or {}
storage.board[arg.x] = storage.board[arg.x] or {}
if storage.board[arg.x][arg.y] then
if storage.board[arg.x][arg.y].r == arg.r and
storage.board[arg.x][arg.y].g == arg.g and
storage.board[arg.x][arg.y].b == arg.b then
-- nop
else
storage.board[arg.x][arg.y] = {
r=arg.r,g=arg.g,b=arg.b,
u=storage.draw_index or 0,
}
end
else
storage.board[arg.x][arg.y] = {
r=arg.r,g=arg.g,b=arg.b,
u=storage.draw_index or 0,
}
end
storage.draw_index = ( storage.draw_index or 0 ) + 1
end)
end