info = { type = "app", title = "Difficulty", author = "BRRRO", license = "MIT", desc = "Mining difficulty change", category = "btc", api = 43, } permissions = { "location" } txt = { own = "Own mempool server", own_h = "e.g. http://umbrel.local:3006 - empty = mempool.space", nodata = "NO DATA", title = "DIFFICULTY", scene = "Scene", scene_o = { "mountains", "mine" }, loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", console = "SEE CONSOLE" } for k, v in pairs(({ sk = { own = "Vlastný mempool server", own_h = "napr. http://umbrel.local:3006 - prázdne = mempool.space", nodata = "BEZ DAT", title = "OBTIAZNOST", scene = "Scéna", scene_o = { "hory", "baňa" }, loading = "NACITAVAM", nowifi = "BEZ WIFI", console = "POZRI KONZOLU" }, cs = { own = "Vlastní mempool server", own_h = "např. http://umbrel.local:3006 - prázdné = mempool.space", nodata = "BEZ DAT", title = "OBTIZNOST", scene = "Scéna", scene_o = { "hory", "důl" }, loading = "NACITAM", nowifi = "BEZ WIFI", console = "VIZ KONZOLI" }, })[lang] or {}) do txt[k] = v end local DAYS = ({ sk = { "DEŇ", "DNI", "DNÍ" }, cs = { "DEN", "DNY", "DNÍ" } })[lang] or { "DAY", "DAYS", "DAYS" } local function days_word(n) if n == 1 then return DAYS[1] elseif n >= 2 and n <= 4 then return DAYS[2] end return DAYS[3] end settings = { { key = "scene", type = "int", default = "mine", label = txt.scene, opts = { keys = { "peaks", "mine" }, table.unpack(txt.scene_o) } }, { key = "node", type = "server", label = txt.own, hint = txt.own_h }, } sources = { adj = { url = "https://mempool.space", limit = 60, own = "node", fields = { "difficultyChange", "remainingTime" }, demo = [[{"difficultyChange":2.7,"remainingTime":793327000}]] }, } function on_settings() net.fetch("adj", "/api/v1/difficulty-adjustment", 900) end local W, H = gfx.W, gfx.H local FPS = gfx.FPS local px, rect, blend, clip = gfx.px, gfx.rect, gfx.blend, gfx.clip local text, text_center, text_width, text_ink = gfx.text, gfx.text_center, gfx.text_width, gfx.text_ink local F_SMALL = { font = "3x5" } local CELL = { cell = true } local F_MID = { font = "8x13b" } local blit_rgb, blend_mask = gfx.blit_rgb, gfx.blend_mask local floor, sin, sqrt, fmod, abs, min, max = math.floor, math.sin, math.sqrt, math.fmod, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local fmt, char, concat = string.format, string.char, table.concat local function lround(v) if v >= 0 then return floor(v + 0.5) else return -floor(-v + 0.5) end end local function mix(ar, ag, ab, br, bg, bb, t) return floor(ar + (br - ar) * t), floor(ag + (bg - ag) * t), floor(ab + (bb - ab) * t) end local GOLD_R, GOLD_G, GOLD_B = 255, 196, 64 local WHT_R, WHT_G, WHT_B = 255, 250, 232 local CLK_R, CLK_G, CLK_B = 200, 190, 210 local HARD = { 235, 90, 90 } local EASY = { 90, 220, 130 } local TICK_R, TICK_G, TICK_B = 150, 150, 165 local TRK_R, TRK_G, TRK_B = 38, 38, 44 local SEG_PAL = { { 90, 220, 130 }, { 90, 220, 130 }, { 215, 210, 70 }, { 215, 210, 70 }, { 235, 150, 40 }, { 235, 150, 40 }, { 235, 90, 90 }, { 235, 90, 90 } } local SEG_W, SEG_H, N_SEG, SEG_GAP = 4, 8, 8, 2 local METER_W = N_SEG * SEG_W + (N_SEG - 1) * SEG_GAP local METER_X, METER_Y = (W - METER_W) // 2, 48 local HERO_H = 18 local CX, CY, R = 32, 6, 6 local COIN_X, COIN_Y, COIN_W, COIN_H = CX - R, CY - R, 2 * R + 1, 2 * R + 1 local BTC7 = { "0010100", "1111110", "0110011", "0110011", "0111110", "0110011", "0110011", "1111110", "0010100" } local BG_LOW = { 10, 12, 28 } local PAL = { sky_t = { { 110, 175, 235 }, { 90, 80, 160 }, { 16, 22, 80 } }, sky_b = { { 190, 220, 245 }, { 245, 150, 110 }, { 70, 70, 150 } }, back = { { 112, 122, 178 }, { 96, 74, 126 }, { 62, 58, 118 } }, front = { { 80, 92, 150 }, { 66, 52, 98 }, { 42, 40, 92 } }, } local function pal(name, k) local c = PAL[name] if k <= 0.5 then return mix(c[1][1], c[1][2], c[1][3], c[2][1], c[2][2], c[2][3], k * 2) end return mix(c[2][1], c[2][2], c[2][3], c[3][1], c[3][2], c[3][3], (k - 0.5) * 2) end local H1, H2 = {}, {} for x = 0, W - 1 do H1[x] = 13 + lround(4 * sin(x * 0.11 - 1.95)) H2[x] = 15 + lround(3 * sin(x * 0.19 + 2)) end local RAYS = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0.7, 0.7 }, { -0.7, 0.7 }, { 0.7, -0.7 }, { -0.7, -0.7 } } local STAR_AT = { { 3, 2 }, { 12, 5 }, { 19, 1 }, { 24, 7 }, { 42, 2 }, { 47, 6 }, { 53, 1 }, { 60, 4 } } local hero_bg, coin, hero_stage, hero_k, stars local function build_hero(stage) local k = stage / 5 local tr, tg, tb = pal("sky_t", k) local br_, bg_, bb_ = pal("sky_b", k) local back, front = char(pal("back", k)), char(pal("front", k)) local function at(x, y) if y >= H2[x] then return false, front end if y >= H1[x] then return false, back end local bx, by = x - (CX - 3) + 1, y - (CY - 4) + 1 if bx >= 1 and bx <= 7 and by >= 1 and by <= 9 and BTC7[by]:sub(bx, bx) == "1" then return true, char(120, 52, 12) end local dx, dy = x - CX, y - CY local dd = sqrt(dx * dx + dy * dy) if dd <= R then return true, char(mix(255, 210, 120, 235, 140, 40, dd / R)) end return false, nil end local none = char(0, 0, 0) local rows, crows = {}, {} for y = 0, HERO_H - 1 do local row, crow = {}, {} local sky = char(mix(tr, tg, tb, br_, bg_, bb_, y / (HERO_H - 1))) for x = 0, W - 1 do local is_coin, c = at(x, y) row[x + 1] = is_coin and none or (c or sky) if y >= COIN_Y and y < COIN_Y + COIN_H and x >= COIN_X and x < COIN_X + COIN_W then crow[x - COIN_X + 1] = is_coin and c or none end end rows[y + 1] = concat(row) if crow[1] then crows[#crows + 1] = concat(crow) end end hero_bg, coin = concat(rows), concat(crows) stars = {} for _, st in ipairs(STAR_AT) do local is_coin, c = at(st[1], st[2]) if not is_coin and not c then stars[#stars + 1] = st end end hero_stage, hero_k = stage, k end local function night_k() local tm, sun = data.time, data.sun if not (tm and tm.ok and sun) then return 0 end if sun.polar then return sun.up and 0 or 1 end local h = tm.h + tm.m / 60 local k = 0.5 - min(h - sun.rise, sun.set - h) / 1.5 if k < 0 then return 0 elseif k > 1 then return 1 end return k end local function win_at(len) local t = data.time return t.unix // len, t.unix % len + t.ms / 1000 end local GLIDER = { ".ooo.", "o...o", ".l.l.", "..p.." } local GLIDER_C = { o = { 255, 150, 40 }, l = { 160, 160, 170 }, p = { 50, 50, 70 } } local function draw_guest(t, k) if k < 0.4 then local win, fw = win_at(60) local h = hash32(win * 0x9E3779B1 + 91) if umod(h, 3) == 0 then return end local right, v = umod(h >> 8, 2) == 0, 4 local dur = (W + 10) / v local e = fw - umod(h >> 4, max(1, floor(60 - dur))) if e < 0 or e > dur then return end local x = floor(right and -5 + e * v or W + 5 - e * v) local y = floor(1 + umod(h >> 12, 3) + e * 0.12 + 0.6 * sin(t * 1.3) + 0.5) for r = 1, 4 do for c = 1, 5 do local col = GLIDER_C[GLIDER[r]:sub(c, c)] if col then px(x + c - 1, y + r - 1, col[1], col[2], col[3]) end end end elseif k > 0.6 then local win, fw = win_at(20) local h = hash32(win * 0x9E3779B1 + 7) if umod(h, 3) ~= 0 then return end local e = fw - umod(h >> 4, 19) if e < 0 or e > 0.5 then return end local q = e / 0.5 local dir = umod(h >> 20, 2) == 0 and 1 or -1 local hx, hy = 6 + umod(h >> 10, W - 12) + dir * q * 14, umod(h >> 16, 3) + q * 6 for i = 0, 5 do blend(floor(hx - dir * i + 0.5), floor(hy - i * 0.45 + 0.5), 255, 250, 220, (1 - i / 6) * (1 - q * 0.6)) end end end local function draw_hero(t, p) local stage = floor(night_k() * 5 + 0.5) if stage ~= hero_stage then build_hero(stage) end blit_rgb(hero_bg, 0, 0, W, HERO_H) if hero_k > 0.5 then local a = (hero_k - 0.5) * 2 for i, st in ipairs(stars) do blend(st[1], st[2], 230, 225, 255, a * (0.55 + 0.45 * sin(t * 6 + i * 1.7))) end end draw_guest(t, hero_k) blit_rgb(coin, COIN_X, COIN_Y, COIN_W, COIN_H, 0, 0, 0, 0.9 + 0.1 * sin(fmod(t * 3.0, 6.28318))) if p then for k = 0, 1 do local d = R + 2 + k + floor(p * 4) for _, v in ipairs(RAYS) do blend(CX + floor(v[1] * d + 0.5), CY + floor(v[2] * d + 0.5), 255, 200, 60, 1 - p) end end end end local RAIL_Y, RISE_MAX, MINE_V = 13, 6, 12 local POST_EVERY = 32 local MINER_X, CART_X = 24, 30 local M_CEIL, M_MORTAR = { 80, 62, 52 }, { 74, 56, 46 } local M_STONE = { { 112, 84, 64 }, { 100, 76, 58 }, { 122, 92, 70 } } local M_POST, M_POST_D, M_CAP = { 156, 104, 56 }, { 120, 80, 44 }, { 132, 88, 48 } local M_RAIL, M_SLEEPER = { 176, 182, 196 }, { 104, 70, 44 } local M_GRAVEL, M_SPECK = { 96, 82, 70 }, { 124, 108, 94 } local M_FIX, M_BULB, M_GLOW = { 110, 110, 120 }, { 255, 228, 160 }, { 255, 180, 90 } local M_WHEEL = { { 64, 66, 78 }, { 176, 182, 198 } } local CART = { "..g.Gg.g..", ".gGggGgGg.", "RRRRRRRRRR", ".BBvBBvBB.", "..BBBBBB.." } local CART_PAL = { g = { 214, 150, 40 }, G = { 255, 206, 80 }, R = { 176, 184, 200 }, B = { 112, 120, 140 }, v = { 156, 164, 182 } } local NUGGETS = {} for r = 1, 2 do for c = 1, 10 do if CART[r]:sub(c, c) == "G" then NUGGETS[#NUGGETS + 1] = { CART_X + c - 1, r - 1 } end end end local MAN = { [0] = { ".+++..", "++++L.", ".sss..", "#####h", "#####." }, [1] = { "..+++.", ".++++L", "..sss.", ".####h", "####.." } } local LEGS = { [0] = { ".l.l..", "b...b." }, [1] = { ".ll...", ".bb..." } } local MAN_PAL = { ["+"] = { 255, 190, 50 }, L = { 255, 250, 220 }, s = { 232, 196, 156 }, h = { 232, 196, 156 }, ["#"] = { 58, 92, 180 }, l = { 40, 56, 120 }, b = { 120, 76, 40 } } local COIN5 = { ".###.", "#####", "##o##", "#####", ".###." } local SPECKS = {} for wx = 0, W - 1 do if umod(hash32(wx * 7 + 3), 6) == 0 then SPECKS[#SPECKS + 1] = wx end end local mine_wall, glow, man, cart_col local world, last_f, tilt = 0, nil, nil local RY, ry_tilt = {}, nil local function sprite(rows, pal) local out = {} for _, row in ipairs(rows) do for c = 1, #row do local k = pal[row:sub(c, c)] out[#out + 1] = k and char(k[1], k[2], k[3]) or char(0, 0, 0) end end return concat(out) end local function build_mine() local ceil, mortar = char(M_CEIL[1], M_CEIL[2], M_CEIL[3]), char(M_MORTAR[1], M_MORTAR[2], M_MORTAR[3]) local stone = {} for i, c in ipairs(M_STONE) do stone[i] = char(c[1], c[2], c[3]) end local rows = {} for y = 0, HERO_H - 1 do local band, r = (y - 2) // 4, (y - 2) % 4 local edge, id, u, n = {}, {}, 0, 0 local x0 = umod(hash32(band * 977 + 13), W) while u < W do n = n + 1 local w = 5 + umod(hash32(band * 131 + n * 7919), 5) for k = 0, min(w, W - u) - 1 do local xx = (x0 + u + k) % W edge[xx], id[xx] = k == 0, n end u = u + w end local row = {} for x = 0, W - 1 do if y < 2 or (y == 2 and umod(hash32(x * 31 + 5), 3) == 0) then row[x + 1] = ceil elseif r == 3 or edge[x] then row[x + 1] = mortar else row[x + 1] = stone[umod(hash32(band * 53 + id[x] * 17), 3) + 1] end end rows[y + 1] = concat(row) end mine_wall = concat(rows) local g = {} for y = 0, 11 do for x = 0, 15 do local dx, dy = x - 7.5, (y - 4.5) * 1.3 local f = 1 - sqrt(dx * dx + dy * dy) / 8 g[#g + 1] = char(f > 0 and floor(255 * f * f) or 0) end end glow = concat(g) man = {} for lean = 0, 1 do man[lean] = {} for step = 0, 1 do man[lean][step] = sprite({ MAN[lean][1], MAN[lean][2], MAN[lean][3], MAN[lean][4], MAN[lean][5], LEGS[step][1], LEGS[step][2] }, MAN_PAL) end end cart_col = {} for c = 1, 10 do local col = {} for r = 1, #CART do col[r] = CART[r]:sub(c, c) end cart_col[c - 1] = sprite(col, CART_PAL) end end local function draw_mine(t, change, p) if not mine_wall then build_mine() end local target = change and max(-1, min(1, change / 5)) * RISE_MAX or 0 local dt = last_f and (frame - last_f) / FPS or 0 if dt < 0 or dt > 1 then dt = 0 end last_f = frame tilt = tilt and tilt + (target - tilt) * min(1, dt * 2) or target if abs(target - tilt) < 0.01 then tilt = target end if tilt ~= ry_tilt then ry_tilt = tilt for x = -POST_EVERY, W + 1 do RY[x] = RAIL_Y - lround(tilt * (x - 31.5) / W) end end world = fmod(world + dt * MINE_V * (1 - 0.45 * tilt / RISE_MAX), 192) local o = floor(world) clip(0, 0, W, HERO_H) blit_rgb(mine_wall, -(o % W), 0, W, HERO_H) blit_rgb(mine_wall, W - o % W, 0, W, HERO_H) for xp = -(o % POST_EVERY), W, POST_EVERY do rect(xp, 2, 1, RY[xp] - 1, M_POST[1], M_POST[2], M_POST[3]) rect(xp + 1, 2, 1, RY[xp + 1] - 1, M_POST_D[1], M_POST_D[2], M_POST_D[3]) rect(xp - 1, 1, 4, 2, M_CAP[1], M_CAP[2], M_CAP[3]) local lx, wk = xp + POST_EVERY // 2, (xp + o) // POST_EVERY local f = 0.5 + 0.08 * sin(t * 9 + wk * 2.1) if umod(hash32(floor(t * 8) * 7 + wk), 60) == 0 then f = 0.25 end blend_mask(glow, M_GLOW[1], M_GLOW[2], M_GLOW[3], lx - 7, -1, 16, 12, 16, 12, f) rect(lx, 2, 2, 1, M_FIX[1], M_FIX[2], M_FIX[3]) rect(lx, 3, 2, 2, M_BULB[1], M_BULB[2], M_BULB[3]) end local x0 = 0 for x = 1, W do if x == W or RY[x] ~= RY[x0] then local y = RY[x0] rect(x0, y, x - x0, 1, M_RAIL[1], M_RAIL[2], M_RAIL[3]) rect(x0, y + 1, x - x0, HERO_H - y - 1, M_GRAVEL[1], M_GRAVEL[2], M_GRAVEL[3]) x0 = x end end for x = -(o % 4), W - 1, 4 do for xx = max(x, 0), min(x + 1, W - 1) do px(xx, RY[xx] + 1, M_SLEEPER[1], M_SLEEPER[2], M_SLEEPER[3]) end end for _, wx in ipairs(SPECKS) do local x = (wx - o) % W px(x, RY[x] + 2 + wx % 2, M_SPECK[1], M_SPECK[2], M_SPECK[3]) end local lean = tilt >= 2 and 1 or 0 local top = RY[MINER_X + 2] - 7 blit_rgb(man[lean][floor(world / 3) % 2], MINER_X, top, 6, 7, 0, 0, 0) if tilt >= 3 then local q = fmod(t, 1.4) / 0.45 if q < 1 then px(MINER_X + lean - floor(q * 2), top + 1 + floor(q * 3), 150, 205, 255) end end local spin = floor(world / 2) % 2 for c = 0, 9 do local x = CART_X + c local yb = RY[x] - 1 blit_rgb(cart_col[c], x, yb - #CART, 1, #CART, 0, 0, 0) if c == 2 or c == 3 or c == 6 or c == 7 then local w = M_WHEEL[(c + spin) % 2 + 1] px(x, yb, w[1], w[2], w[3]) end end local nu = NUGGETS[floor(t / 0.9) % #NUGGETS + 1] blend(nu[1], RY[nu[1]] - 1 - #CART + nu[2], 255, 255, 240, sin(fmod(t, 0.9) / 0.9 * 3.14159)) if p then for _, n in ipairs(NUGGETS) do blend(n[1], RY[n[1]] - 1 - #CART + n[2], 255, 255, 240, 1 - p) end local cy, a = RY[34] - 1 - #CART - 5 - floor(sin(p * 3.14159) * 3), p < 0.6 and 1 or (1 - p) / 0.4 for r = 1, 5 do for c = 1, 5 do local ch = COIN5[r]:sub(c, c) if ch == "#" then blend(CART_X + 2 + c, cy + r - 1, 255, 200, 60, a) elseif ch == "o" then blend(CART_X + 2 + c, cy + r - 1, 200, 140, 20, a) end end end end clip() end local function draw_scale_ends() local y = METER_Y + SEG_H // 2 - 1 rect(METER_X - SEG_GAP - 6, y, 6, 2, EASY[1], EASY[2], EASY[3]) local x = METER_X + METER_W + SEG_GAP rect(x, y, 6, 2, HARD[1], HARD[2], HARD[3]) rect(x + 2, y - 2, 2, 6, HARD[1], HARD[2], HARD[3]) end local function draw_meter(frac) local zx = METER_X + 4 * (SEG_W + SEG_GAP) - SEG_GAP rect(zx, METER_Y - 1, SEG_GAP, SEG_H + 2, TICK_R, TICK_G, TICK_B) for i = 0, N_SEG - 1 do local sx = METER_X + i * (SEG_W + SEG_GAP) if frac > i / N_SEG then local c = SEG_PAL[i + 1] rect(sx, METER_Y, SEG_W, SEG_H, c[1], c[2], c[3]) else rect(sx, METER_Y, SEG_W, SEG_H, TRK_R, TRK_G, TRK_B) end end end local seen_rt, epoch_at = nil, nil local EPOCH_S = 1.5 local F_GATE = { font = "4x6" } local function gate(s, y, r, g, b) if s.ok then return true end r, g, b = r or 255, g or 250, b or 232 text_center(y, data.net.mode ~= "online" and txt.offline or not data.net.up and txt.nowifi or s.code ~= 0 and "ERR " .. s.code or txt.loading, r, g, b) if s.code ~= 0 then text_center(y + 10, txt.console, r, g, b, F_GATE) end return false end function render() local t = frame / FPS local j = web.adj local left = j and j.remainingTime if left then if seen_rt and left > seen_rt + 86400000 then epoch_at = t end seen_rt = left end if epoch_at and (t < epoch_at or t - epoch_at >= EPOCH_S) then epoch_at = nil end local p = epoch_at and (t - epoch_at) / EPOCH_S if cfg.scene == "mine" then draw_mine(t, j and j.difficultyChange, p) else draw_hero(t, p) end rect(0, HERO_H, W, H - HERO_H, BG_LOW[1], BG_LOW[2], BG_LOW[3]) text_center(20, txt.title, WHT_R, WHT_G, WHT_B, F_SMALL) if not gate(data.src.adj, 30) then return end if not left then text_center(30, txt.nodata, 120, 120, 130, F_SMALL) return end local change = j.difficultyChange or 0 local seconds = left // 1000 - floor(data.src.adj.age) if seconds < 0 then seconds = 0 end local days, rem = seconds // 86400, seconds % 86400 local hh = rem // 3600; rem = rem % 3600 local mm, ss = rem // 60, rem % 60 local ds, word = fmt("%d", days), days_word(days) local digits_w, label_w = text_width(ds, F_MID), text_width(word) local day_x = (W - (digits_w + 3 + label_w)) // 2 text(day_x, 27, ds, GOLD_R, GOLD_G, GOLD_B, F_MID) local _, bot = text_ink(ds, F_MID) text(day_x + digits_w + 3, 27 + bot + 1 - 7, word, WHT_R, WHT_G, WHT_B) text_center(39, fmt("%02d:%02d:%02d", hh, mm, ss), CLK_R, CLK_G, CLK_B, CELL) local harder = change >= 0 local tr = harder and HARD or EASY local frac = (change + 5.0) / 10.0 if frac < 0 then frac = 0 elseif frac > 1 then frac = 1 end draw_scale_ends() draw_meter(frac) text_center(57, fmt("%s%.1f%%", harder and "+" or "-", abs(change)), tr[1], tr[2], tr[3]) end