summaryrefslogtreecommitdiff
path: root/client/data/net.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2019-01-27 10:32:09 -0500
committerAlexander Pickering <alex@cogarr.net>2019-01-27 10:32:09 -0500
commit0aae46ecc38005236210f7e243f02cac39ab1dc3 (patch)
treee2fcc9893df4ff03a87e113b3c0ff89357c86ef7 /client/data/net.lua
downloadhome_text_adventure-0aae46ecc38005236210f7e243f02cac39ab1dc3.tar.gz
home_text_adventure-0aae46ecc38005236210f7e243f02cac39ab1dc3.tar.bz2
home_text_adventure-0aae46ecc38005236210f7e243f02cac39ab1dc3.zip
Inital commit
Diffstat (limited to 'client/data/net.lua')
-rw-r--r--client/data/net.lua146
1 files changed, 146 insertions, 0 deletions
diff --git a/client/data/net.lua b/client/data/net.lua
new file mode 100644
index 0000000..c3eec15
--- /dev/null
+++ b/client/data/net.lua
@@ -0,0 +1,146 @@
+
+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:5555")
+
+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
+ 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
+ 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
+
+return snet