info = { type = "app", title = "Moscow Time", author = "BRRRO", license = "MIT", desc = "Moscow Time - satoshi per dollar", category = "btc", api = 43, } txt = { nodata = "NO DATA", scene = "Scene", scene_o = { "Kremlin", "St. Basil's" }, loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", console = "SEE CONSOLE" } for k, v in pairs(({ sk = { nodata = "BEZ DAT", scene = "Scéna", scene_o = { "Kremeľ", "Vasilijov chrám" }, loading = "NACITAVAM", nowifi = "BEZ WIFI", console = "POZRI KONZOLU" }, cs = { nodata = "BEZ DAT", scene = "Scéna", scene_o = { "Kreml", "Vasilův chrám" }, loading = "NACITAM", nowifi = "BEZ WIFI", console = "VIZ KONZOLI" }, })[lang] or {}) do txt[k] = v end settings = { { key = "scene", type = "int", default = "basil", label = txt.scene, opts = { keys = { "kremlin", "basil" }, table.unpack(txt.scene_o) } }, } sources = { price = { url = "https://api.binance.com", limit = 15, fields = { "price" }, demo = [[{"symbol":"BTCUSDT","price":"51229.50781250"}]] }, } function on_settings() net.fetch("price", "/api/v3/ticker/price?symbol=BTCUSDT", 60) end local W = gfx.W local FPS = gfx.FPS local clear, px, blend, rect = gfx.clear, gfx.px, gfx.blend, gfx.rect local text_center, text_width, text_ink = gfx.text_center, gfx.text_width, gfx.text_ink local F_SMALL = { font = "3x5" } local F_BIG = { font = "9x15b" } local blit_rgb = gfx.blit_rgb local floor, sin, cos, fmod, min, abs = math.floor, math.sin, math.cos, math.fmod, math.min, math.abs local tan, acos, rad = math.tan, math.acos, math.rad local hash32, umod = math.hash32, math.umod local fmt, char, concat, find = string.format, string.char, table.concat, string.find 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 ACC_R, ACC_G, ACC_B = 255, 140, 40 local DIV_R, DIV_G, DIV_B = 90, 90, 96 local WHT_R, WHT_G, WHT_B = 255, 250, 232 local WIN_R, WIN_G, WIN_B = 255, 205, 90 local GOLD = { 255, 200, 60 } local LINE1, LINE2 = 29, 50 local HERO_H = 18 local MSK_TAN = tan(rad(55.75)) local function night_k() local tm = data.time if not tm.ok then return 1 end local u = (tm.unix - 1767225600) + 3 * 3600 local h = (u % 86400) / 3600 local decl = -0.4091 * cos(6.28318 * (fmod(u // 86400 + 20454, 365.2422) + 10) / 365.2422) local c = -MSK_TAN * tan(decl) if c > 1 then c = 1 elseif c < -1 then c = -1 end local half = acos(c) * 12 / 3.14159 local k = 0.5 - min(h - (12.5 - half), 12.5 + half - h) / 1.5 if k < 0 then return 0 elseif k > 1 then return 1 end return k end local function winter() local tm = data.time return not tm.ok or tm.month >= 11 or tm.month <= 3 end local SKY = { top = { { 110, 175, 235 }, { 90, 80, 160 }, { 16, 22, 80 } }, bot = { { 190, 220, 245 }, { 245, 150, 110 }, { 70, 70, 150 } } } local function pal(c, k) 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 STAR_AT = { { 3, 2 }, { 14, 4 }, { 23, 1 }, { 38, 2 }, { 45, 5 }, { 52, 1 }, { 60, 3 }, { 28, 6 } } local sky, sky_stage, sky_k = {}, nil, 0 local function draw_sky(t) local stage = floor(night_k() * 5 + 0.5) if stage ~= sky_stage then sky_stage, sky_k = stage, stage / 5 local tr, tg, tb = pal(SKY.top, sky_k) local br, bg, bb = pal(SKY.bot, sky_k) for y = 0, HERO_H - 1 do sky[y] = { mix(tr, tg, tb, br, bg, bb, y / (HERO_H - 1)) } end end for y = 0, HERO_H - 1 do local c = sky[y]; rect(0, y, W, 1, c[1], c[2], c[3]) end if sky_k > 0.5 then local a = (sky_k - 0.5) * 2 for i, st in ipairs(STAR_AT) do blend(st[1], st[2], 230, 225, 255, a * (0.55 + 0.45 * sin(t * 2.1 + i * 1.7))) end end end local function draw_snow(t) local ph = fmod(t * 1.2, 6.28318) for i = 0, 13 do local h = hash32(i * 0x9E3779B1 + 57) local sp = 13 + 6 * (i % 3) + umod(h, 4) local s = t * sp + umod(h >> 5, 64) local n = floor(s / (HERO_H + 4)) local y = floor(s - n * (HERO_H + 4)) - 2 local x = umod(hash32(h + n * 0x85EBCA6B), W) + lround(1.8 * sin(ph + i)) if y >= 0 and y < HERO_H then blend(x, y, 235, 244, 255, 0.6 + 0.2 * (i % 3)) end end end local function raster(cells) local g = {} for y = 0, HERO_H - 1 do g[y] = {} end cells(function(x, y, c) if y >= 0 and y < HERO_H and x >= 0 and x < W then g[y][x + 1] = c end end) local none, rows = char(0, 0, 0), {} for y = 0, HERO_H - 1 do local row = g[y] for x = 1, W do row[x] = row[x] or none end rows[y + 1] = concat(row) end return concat(rows) end local K_WALL, K_HI, K_FACE = { 120, 34, 26 }, { 175, 60, 42 }, { 210, 210, 216 } local K_OFF = { 72, 26, 22 } local WALL_Y, CX, FACE_Y = 15, W // 2, 9 local TOWERS = { 9, 49 } local kremlin = raster(function(set) local wall, hi, face = char(K_WALL[1], K_WALL[2], K_WALL[3]), char(K_HI[1], K_HI[2], K_HI[3]), char(K_FACE[1], K_FACE[2], K_FACE[3]) local function box(x, y, w, h, c) for j = y, y + h - 1 do for i = x, x + w - 1 do set(i, j, c) end end end box(0, WALL_Y, W, 3, wall) for x = 1, W - 2, 4 do box(x, WALL_Y - 2, 2, 2, hi) end for _, tx in ipairs(TOWERS) do box(tx, 7, 7, WALL_Y - 7, wall) for i = 0, 2 do box(tx + 1 + i, 6 - i, 5 - i * 2, 1, hi) end end box(CX - 4, 6, 9, WALL_Y - 6, wall) box(CX - 3, 3, 7, 3, wall) for i = 0, 1 do box(CX - 2 + i, 2 - i, 5 - i * 2, 1, hi) end set(CX, 0, char(ACC_R, ACC_G, ACC_B)) for yy = -2, 2 do for xx = -2, 2 do if xx * xx + yy * yy <= 6 then set(CX + xx, FACE_Y + yy, face) end end end end) local function draw_kremlin(t, night) blit_rgb(kremlin, 0, 0, W, HERO_H, 0, 0, 0) local f = floor(t * 2 / 3) for k = 0, 1 do local tx = TOWERS[k + 1] for w = 0, 1 do if night and (f + k * 3 + w * 5) % 7 < 4 then px(tx + 2 + w * 2, 10, WIN_R, WIN_G, WIN_B) else px(tx + 2 + w * 2, 10, K_OFF[1], K_OFF[2], K_OFF[3]) end end end local a = fmod(t * 1.254, 6.28318) px(CX + lround(1.5 * sin(a)), FACE_Y - lround(1.5 * cos(a)), K_WALL[1], K_WALL[2], K_WALL[3]) px(CX, FACE_Y, ACC_R, ACC_G, ACC_B) end local B_BRICK, B_BRICK_D, B_TRIM = { 156, 56, 42 }, { 124, 44, 34 }, { 226, 218, 200 } local B_TENT = { 206, 124, 62 } local B_WIN_OFF = { 84, 30, 26 } local BIG = { "...#...", "...#...", "..###..", ".#####.", "#######", "#######", ".#####.", "..###.." } local SMALL = { "..#..", "..#..", ".###.", "#####", "#####", ".###." } local DOMES = { { 8, 5, SMALL, function(x, y) return (y + abs(x)) % 2 == 0 end, { 220, 56, 48 }, { 236, 232, 222 } }, { 19, 1, BIG, function(x, y) return (x + y) % 3 == 0 end, { 70, 160, 90 }, { 255, 200, 70 } }, { 44, 1, BIG, function(x, y) return (x - y) % 3 == 0 end, { 70, 112, 224 }, { 236, 236, 244 } }, { 55, 5, SMALL, function(x, y) return y % 2 == 0 end, { 220, 56, 48 }, { 80, 176, 96 } }, } local BODY_Y, BODY_X0, BODY_X1 = 13, 3, 60 local B_WINDOWS = { 7, 13, 25, 37, 49, 55 } local B_TIPS, B_DRUM_WIN = {}, {} local basil = raster(function(set) local function c3(c) return char(c[1], c[2], c[3]) end for y = BODY_Y, HERO_H - 1 do for x = BODY_X0, BODY_X1 do set(x, y, c3(y == BODY_Y and B_TRIM or (y >= 16 and B_BRICK_D or B_BRICK))) end end for x = 0, W - 1 do set(x, HERO_H - 1, c3(B_BRICK_D)) end for _, d in ipairs(DOMES) do local cx, top, shape, a, b = d[1], d[2], d[3], c3(d[5]), c3(d[6]) local half, y0 = #shape[1] // 2, top + #shape for y = y0, BODY_Y - 1 do for x = cx - half + 1, cx + half - 1 do set(x, y, c3(y == y0 and B_TRIM or B_BRICK)) end end if BODY_Y - y0 >= 3 then B_DRUM_WIN[#B_DRUM_WIN + 1] = { cx, y0 + 1 } end for r = 1, #shape do for c = 1, #shape[r] do if shape[r]:sub(c, c) == "#" then local x, y = cx - half + c - 1, top + r - 1 if not shape[r]:find("##") then set(x, y, c3(GOLD)) else set(x, y, d[4](x - cx, r) and b or a) end end end end B_TIPS[#B_TIPS + 1] = { cx, top } end local tent = { { 4, 1 }, { 5, 1 }, { 6, 2 }, { 7, 2 }, { 8, 3 }, { 9, 4 }, { 10, 5 } } for _, r in ipairs(tent) do local y, hw = r[1], r[2] for x = 32 - hw, 31 + hw do local dd = abs(2 * x - 63) set(x, y, c3((dd == 3 or dd == 7) and B_TRIM or B_TENT)) end end for x = 27, 36 do set(x, 11, c3(B_TRIM)); set(x, 12, c3(B_BRICK)) end set(31, 0, c3(GOLD)); set(32, 0, c3(GOLD)) for x = 30, 33 do set(x, 1, c3(GOLD)); set(x, 2, c3(GOLD)) end set(31, 3, c3(GOLD)); set(32, 3, c3(GOLD)) B_TIPS[#B_TIPS + 1] = { 31, 0 } end) local function draw_basil(t, night) blit_rgb(basil, 0, 0, W, HERO_H, 0, 0, 0) for i, x in ipairs(B_WINDOWS) do local on = night and umod(hash32(floor(t / 2.5) * 13 + i), 5) > 0 local c = on and { WIN_R, WIN_G, WIN_B } or B_WIN_OFF rect(x, BODY_Y + 1, 2, 2, c[1], c[2], c[3]) end for _, w in ipairs(B_DRUM_WIN) do local c = night and { WIN_R, WIN_G, WIN_B } or B_TRIM rect(w[1], w[2], 1, 2, c[1], c[2], c[3]) end if not night then local tip = B_TIPS[floor(t / 0.8) % #B_TIPS + 1] blend(tip[1], tip[2], 255, 255, 240, sin(fmod(t, 0.8) / 0.8 * 3.14159)) end end local last = nil local function price() local j = web.price local p = j and tonumber(j.price) if p and p > 0 then last = p end return last end local O_CLOCK = { font = "9x15b", hide = nil } local function draw_clock(sats, t) local clock = fmt("%d:%02d", sats // 100, sats % 100) O_CLOCK.font = text_width(clock, F_BIG) > W - 2 and "8x13b" or "9x15b" local tm = data.time local s = tm.ok and tm.s or floor(t) O_CLOCK.hide = s % 2 == 1 and find(clock, ":", 1, true) or nil local top, bot = text_ink(clock, O_CLOCK) text_center(LINE1 + 1 + ((LINE2 - LINE1 - 1) - (bot - top + 1)) // 2 - top, clock, ACC_R, ACC_G, ACC_B, O_CLOCK) end 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 clear(0, 0, 0) draw_sky(t) if winter() then draw_snow(t) end local night = sky_k > 0.5 if cfg.scene == "basil" then draw_basil(t, night) else draw_kremlin(t, night) end text_center(20, "MOSCOWTIME", WHT_R, WHT_G, WHT_B) if not gate(data.src.price, 32) then return end local p = price() if not p then text_center(40, txt.nodata, 120, 120, 130, F_SMALL) return end rect(4, LINE1, W - 8, 1, DIV_R, DIV_G, DIV_B) draw_clock(floor(100000000.0 / p + 0.5), t) rect(4, LINE2, W - 8, 1, DIV_R, DIV_G, DIV_B) text_center(53, "SATS/USD", WHT_R, WHT_G, WHT_B) end