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
|
local snet = {}
local room = require("room")
local item = require("item")
local house = require("house")
local socket = net.newsocket(net.REQ)
--Some server in the cloud, running the same stuff in server/*
socket:connect("tcp://34.73.161.231:80")
function snet.ask_for_homes(location,index)
socket:send(function(stream)
stream:writestring("get_houses")
stream:writestring(location)
stream:writeint(index)
end)
local stream = socket:block_recv()
local num_homes = stream:readint()
local ret = {}
printf("Found %d homes",num_homes)
for i = 1,num_homes do
local home_name = stream:readstring()
local nice_name = string.match(home_name,"(.*).lua")
print(nice_name)
ret[#ret + 1] = nice_name
end
return ret
--print("got homes:",stream:readstring())
end
function snet.ask_for_all_homes(location)
socket:send(function(stream)
stream:writestring("get_all_houses")
stream:writestring(location)
end)
local stream = socket:block_recv()
local ret = {}
for i = 1,stream:readint() do
local home_name = stream:readstring()
local nice_name = string.match(home_name,"(.*).lua")
ret[#ret + 1] = nice_name
end
return ret
end
function snet.house_exists(location,housename)
socket:send(function(stream)
stream:writestring("house_exists")
stream:writestring(location)
stream:writestring(housename)
end)
local stream = socket:block_recv()
return stream:readint() == 1
end
function snet.settle_at(myid,location,name)
socket:send(function(stream)
stream:writestring("settle")
stream:writestring(location)
stream:writestring(name)
stream:writedouble(myid)
end)
local stream = socket:block_recv()
end
function snet.is_name_available(name,loc_name)
socket:send(function(stream)
stream:writestring("is_name_avaliable")
stream:writestring(name)
stream:writestring(loc_name)
end)
local stream = socket:block_recv()
return stream:readint() == 1
end
function snet.claim_house(name,loc_name,playerid)
socket:send(function(stream)
stream:writestring("claim_house")
stream:writestring(name)
stream:writestring(loc_name)
stream:writestring(tostring(playerid))
end)
local stream = socket:block_recv()
end
function snet.write_house(player_id,housedata)
socket:send(function(stream)
stream:writestring("write_house")
stream:writestring(tostring(player_id))
stream:writestring(housedata)
end)
local stream = socket:block_recv()
print("Done getting stream back")
end
function snet.get_house(loc_name,housename)
socket:send(function(stream)
stream:writestring("get_house")
stream:writestring(loc_name)
stream:writestring(housename)
end)
local stream = socket:block_recv()
print("Got house back")
local housedata = stream:readstring()
local this_house = assert(loadstring(housedata,housename))()
setmetatable(this_house,house.meta)
print("house loaded correctly",house)
for room_name,this_room in pairs(this_house.rooms) do
if this_room ~= "exit" then
print("setting item metas for ",this_room,":",type(this_room),"---",this_room.items)
for i,this_item in pairs(this_room.items) do
print(string.format("Setting item metas for %q %q",tostring(i),tostring(this_item)))
--print("setting item metas for",i,":",this_item)
setmetatable(this_item,item.meta)
end
print("Setting meta table for this room")
setmetatable(this_room,room.meta)
print("done setting meta table")
end
end
print("returning house")
return this_house
end
function snet.do_i_own(loc_name,housename,playerid)
print("doing do_i_own")
socket:send(function(stream)
print("Starting to write to stream")
stream:writestring("do_i_own")
print("one")
stream:writestring(loc_name)
print("two",housename)
stream:writestring(housename)
print("three",playerid)
--print("three",tostring(playerid))
stream:writestring(tostring(playerid))
print("Wrote everything to send")
end)
print("Done with sending")
local stream = socket:block_recv()
local ret = stream:readint()
print("got back:",ret)
print("Done with do_i_own")
return ret == 1
end
set_hints("look","go","settle","goto")
return snet
|