From 77b4fbe12db947911fcccd82d0da1eef4b68d9b1 Mon Sep 17 00:00:00 2001 From: cheapie Date: Sat, 28 Mar 2020 01:31:12 -0500 Subject: Add magnetic card reader/writer and cards to go with it --- README | 9 +++ cardreader.lua | 118 ++++++++++++++++++++++++++++++++ init.lua | 1 + textures/digistuff_cardreader_sides.png | Bin 0 -> 6015 bytes textures/digistuff_cardreader_top.png | Bin 0 -> 6094 bytes textures/digistuff_magnetic_card.png | Bin 0 -> 635 bytes 6 files changed, 128 insertions(+) create mode 100644 cardreader.lua create mode 100644 textures/digistuff_cardreader_sides.png create mode 100644 textures/digistuff_cardreader_top.png create mode 100644 textures/digistuff_magnetic_card.png diff --git a/README b/README index 79f68c7..d04d661 100644 --- a/README +++ b/README @@ -71,3 +71,12 @@ As in, [digiline][junction box][dirt][junction box][digiline] will work to trans How to use the I/O expander: After setting a channel, send a table (same format as a Luacontroller's "port" table) to set the output states. A table in this same format will be sent back whenever an input changes or you manually poll it by sending a "GET" message. + +How to use the card reader: +After setting a channel, swiping a card (punch the reader with the card to swipe) will send a message in the following format: + {event = "read",data = "The data that was on the card"} +To write a card, send a command in the following format: + {command = "write",data = "The data to put on the card",description = "A description of what the card is for"} +After sending the write command, swipe the card to be written and the reader will send back the following message: + {event = "write"} +Both blank and previously written cards can be written to. If the card was not blank, it will be overwritten. diff --git a/cardreader.lua b/cardreader.lua new file mode 100644 index 0000000..31ff7e4 --- /dev/null +++ b/cardreader.lua @@ -0,0 +1,118 @@ +local cardreader_rules = { + {x = 1, y = 0,z = 0,}, + {x = 2, y = 0,z = 0,}, + {x = -1, y = 0,z = 0,}, + {x = -2, y = 0,z = 0,}, + {x = 0, y = 1,z = 0,}, + {x = 0, y = 2,z = 0,}, + {x = 0, y = -1,z = 0,}, + {x = 0, y = -2,z = 0,}, + {x = 0, y = 0,z = 1,}, + {x = 0, y = 0,z = 2,}, + {x = 0, y = 0,z = -1,}, + {x = 0, y = 0,z = -2,}, +} + +minetest.register_craftitem("digistuff:card",{ + description = "Blank Magnetic Card", + image = "digistuff_magnetic_card.png", + stack_max = 1, + on_use = function(stack,_,pointed) + local pos = pointed.under + if minetest.get_node(pos).name ~= "digistuff:card_reader" then return end + local meta = minetest.get_meta(pos) + local channel = meta:get_string("channel") + local stackmeta = stack:get_meta() + if meta:get_int("writepending") > 0 then + local data = meta:get_string("writedata") + meta:set_int("writepending",0) + meta:set_string("infotext","Ready to Read") + digiline:receptor_send(pos,cardreader_rules,channel,{event = "write",}) + stackmeta:set_string("data",data) + stackmeta:set_string("description",string.format("Magnetic Card (%s)",meta:get_string("writedescription"))) + return stack + else + local channel = meta:get_string("channel") + local data = stackmeta:get_string("data") + digiline:receptor_send(pos,cardreader_rules,channel,{event = "read",data = data,}) + end + end, +}) + +minetest.register_node("digistuff:card_reader",{ + description = "Digilines Magnetic Card Reader/Writer", + groups = {cracky = 3,digiline_receiver = 1,}, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec","field[channel;Channel;${channel}") + meta:set_int("writepending",0) + meta:set_string("infotext","Ready to Read") + end, + on_receive_fields = function(pos, formname, fields, sender) + local name = sender:get_player_name() + if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then + minetest.record_protection_violation(pos,name) + return + end + local meta = minetest.get_meta(pos) + if fields.channel then meta:set_string("channel",fields.channel) end + end, + paramtype = "light", + paramtype2 = "facedir", + tiles = { + "digistuff_cardreader_sides.png", + "digistuff_cardreader_sides.png", + "digistuff_cardreader_sides.png", + "digistuff_cardreader_sides.png", + "digistuff_cardreader_sides.png", + "digistuff_cardreader_top.png", + }, + drawtype = "nodebox", + node_box = { + type = "fixed", + fixed = { + {-0.08,-0.12,0.4,0.08,0.12,0.5}, + } + }, + digiline = { + receptor = {}, + wire = { + rules = cardreader_rules, + }, + effector = { + action = function(pos,node,channel,msg) + local setchannel = minetest.get_meta(pos):get_string("channel") + if channel ~= setchannel or type(msg) ~= "table" then return end + if msg.command == "write" and (type(msg.data) == "string" or type(msg.data) == "number") then + local meta = minetest.get_meta(pos) + meta:set_string("infotext","Ready to Write") + meta:set_int("writepending",1) + if type(msg.data) ~= "string" then msg.data = tostring(msg.data) end + meta:set_string("writedata",string.sub(msg.data,1,256)) + if type(msg.description == "string") then + meta:set_string("writedescription",string.sub(msg.description,1,64)) + else + meta:set_string("writedescription","no name") + end + end + end, + }, + }, +}) + +minetest.register_craft({ + output = "digistuff:card", + recipe = { + {"basic_materials:plastic_sheet",}, + {"default:iron_lump",}, + } +}) + +minetest.register_craft({ + output = "digistuff:card_reader", + recipe = { + {"basic_materials:plastic_sheet","basic_materials:plastic_sheet","digilines:wire_std_00000000",}, + {"basic_materials:plastic_sheet","basic_materials:copper_wire","mesecons_luacontroller:luacontroller0000",}, + {"basic_materials:plastic_sheet","basic_materials:plastic_sheet","",}, + } +}) diff --git a/init.lua b/init.lua index 4df2e65..90d8ea0 100644 --- a/init.lua +++ b/init.lua @@ -13,6 +13,7 @@ local components = { "detector", "piston", "timer", + "cardreader", } if minetest.get_modpath("mesecons_luacontroller") then table.insert(components,"ioexpander") end diff --git a/textures/digistuff_cardreader_sides.png b/textures/digistuff_cardreader_sides.png new file mode 100644 index 0000000..ede5f18 Binary files /dev/null and b/textures/digistuff_cardreader_sides.png differ diff --git a/textures/digistuff_cardreader_top.png b/textures/digistuff_cardreader_top.png new file mode 100644 index 0000000..3dde6e9 Binary files /dev/null and b/textures/digistuff_cardreader_top.png differ diff --git a/textures/digistuff_magnetic_card.png b/textures/digistuff_magnetic_card.png new file mode 100644 index 0000000..aaa2225 Binary files /dev/null and b/textures/digistuff_magnetic_card.png differ -- cgit v1.2.3