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
|
local enet = require"enet"
local words = require"common.words"
local unpack = unpack or table.unpack
math.randomseed(os.time())
local host = enet.host_create("*:8473")
print(host)
-- sequential list of all players
local playerlist = {}
local nextid = 1
-- this is maybe suboptimal
-- but it is simplest for now
local function player_by_id(id)
for i,pl in ipairs(playerlist) do
if pl.id == id then return pl, i end
end
return nil
end
local function player_by_peer(peer)
for i,pl in ipairs(playerlist) do
if pl.peer == peer then return pl, i end
end
return nil
end
local function random_color()
return {math.random(),math.random(),math.random()}
end
local function make_player(peer)
local p = {pos={100,100},color=random_color(),peer=peer,id=nextid}
nextid = nextid + 1
return p
end
-- maybe json could be used for this
-- or something
local function player_info_part(player)
return words.join(
player.id,
player.pos[1],
player.pos[2],
player.color[1],
player.color[2],
player.color[3])
end
local function player_join_packet(player)
return "join "..player_info_part(player)
end
local function player_you_packet(player)
return "you "..player_info_part(player)
end
local function player_leave_packet(player)
return words.join("leave",player.id)
end
local function player_move_packet(player,x,y)
return words.join("move",player.id,x,y)
end
while true do
local ev = host:service(100)
if ev then
if ev.type == "connect" then
local player = make_player(ev.peer)
table.insert(playerlist,player)
print("connect",player.peer,player.id)
player.peer:send(player_you_packet(player))
for i,otherplayer in ipairs(playerlist) do
if otherplayer ~= player then
-- tell new player about each other player
player.peer:send(player_join_packet(otherplayer))
-- tell each other player about new player
otherplayer.peer:send(player_join_packet(player))
end
end
elseif ev.type == "disconnect" then
local player, idx = player_by_peer(ev.peer)
if not player then error("sneeze "..ev.peer) end
table.remove(playerlist,idx)
for i,otherplayer in ipairs(playerlist) do
otherplayer.peer:send(player_leave_packet(player))
end
elseif ev.type == "receive" then
local player = player_by_peer(ev.peer)
if not player then error("sneezey "..ev.peer) end
local w = words.split(ev.data)
local op = w[1]
if op == "ppos" then
local _,x,y,a = unpack(w)
player.pos[1] = tonumber(x)
player.pos[2] = tonumber(y)
print(player.id,"-->",player.pos[1],player.pos[2])
for i,otherplayer in ipairs(playerlist) do
if otherplayer ~= player then
otherplayer.peer:send(player_move_packet(player,x,y))
end
end
end
end
-- for k,v in pairs(ev) do io.write(tostring(k),":",tostring(v)," ") end
-- print()
end
end
|