summaryrefslogtreecommitdiff
path: root/shop2/disp.lua
blob: eb06625d02c9d90ab165c6618af150541781fa24 (plain)
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
local ourname = "ac.kst"
local stock=require"shop2.stock"

local function printseq(m) return function(q)
	for _,v in ipairs(q) do
		if type(v) == "number" then
			m.setTextColor(v)
		elseif type(v) == "string" then
			m.write(v)
		elseif type(v) == "table" then
			if #v == 2 then
				-- position
				m.setCursorPos(v[1],v[2])
			else
				error("unknown format")
			end
		end
	end
end end

local function pad(s,n)
	s=tostring(s)
	local l = #s
	local p = string.rep(" ",math.max(0,n-l))
	return p..s
end
local function centre(s,w)
	s=tostring(s)
	local l = #s
	local p = math.floor(math.max(0,w-l)/2)
	return string.rep(" ",p)..s
end

local function disp_shopscreen(items)
	-- i don't like how hardcoded all the spacing is in this
	-- but i also don't feel like rewriting it yet.
	local mon = peripheral.find"monitor"

	local si = stock.take_stock()
	
	-- Stock  Price   Adr.  Name
	--  xxxx  xxxkst  dmn   Diamond
	-- 123456789012345678901234567890123456789012345678901234567
	-- 0        1         2         3         4         5

	-- sort alphabetically by address
	-- todo: more intuitive sort, once more things are for sale
	local adrs = {}
	for adr in pairs(items) do table.insert(adrs,adr) end
	table.sort(adrs)


	-- took stock before clear, to avoid flickering
	mon.clear()
	mon.setTextScale(1)
	local w,h = mon.getSize()

	printseq(mon) {
		{1,1}, colors.orange, centre("BEE MART",w),
		{1,2}, colors.lightGray, "Stock  Price   Adr.  Name",
	}

	local function fmt_row(y, idesc, amt)
		colour = idesc.colour or colors.white
		printseq(mon) {
			{1,y}, colors.white, pad(amt,5),
			{8,y}, colors.yellow, pad(idesc.price,3), colors.lightGray, "kst",
			{16,y}, colour, idesc.adr,
			{22,y}, colour, idesc.hname,
		}
	end
	for ix,adr in ipairs(adrs) do
		local idesc = items[adr]
		local amt = stock.amt_of(si,idesc)
		fmt_row(ix+2, idesc,amt)
	end


	printseq(mon) {
		{1,h-1}, colors.blue, centre("/pay <adr>@"..ourname.." <amount>",w)
	}

end

local items = require"shop2.items"
local function run()
	while true do
		disp_shopscreen(items)
		os.sleep(2)
	end
end

return {run=run}