From 0aae46ecc38005236210f7e243f02cac39ab1dc3 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sun, 27 Jan 2019 10:32:09 -0500 Subject: Inital commit --- client/data/net.lua | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 client/data/net.lua (limited to 'client/data/net.lua') 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 -- cgit v1.2.3-70-g09d2