From b1c3386ca4032ac5925969d759ae2e5bc015d1f8 Mon Sep 17 00:00:00 2001 From: "U-DESKTOP-FOJ6TK1\\Alex" Date: Wed, 12 Feb 2025 17:25:25 -0600 Subject: Final commit --- client/data/net.lua | 296 ++++++++++++++++++++++++++-------------------------- 1 file changed, 150 insertions(+), 146 deletions(-) (limited to 'client/data/net.lua') diff --git a/client/data/net.lua b/client/data/net.lua index c3eec15..b624184 100644 --- a/client/data/net.lua +++ b/client/data/net.lua @@ -1,146 +1,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: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 + +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 -- cgit v1.2.3-70-g09d2