info = { type = "app", title = "Fear & Greed", author = "BRRRO", license = "MIT", desc = "Market sentiment index", category = "btc", api = 43, } txt = { nodata = "NO DATA", scene = "Scene", scene_o = { "speedometer", "weather" }, loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", console = "SEE CONSOLE", cls = { "EXTREME FEAR", "FEAR", "NEUTRAL", "GREED", "EXTREME GREED" } } for k, v in pairs(({ sk = { nodata = "BEZ DAT", scene = "Scéna", scene_o = { "tachometer", "počasie" }, loading = "NACITAVAM", nowifi = "BEZ WIFI", console = "POZRI KONZOLU", cls = { "VEĽKÝ STRACH", "STRACH", "NEUTRÁLNE", "CHAMTIVOSŤ", "VEĽKÁ CHAMTIVOSŤ" } }, cs = { nodata = "BEZ DAT", scene = "Scéna", scene_o = { "tachometr", "počasí" }, loading = "NACITAM", nowifi = "BEZ WIFI", console = "VIZ KONZOLI", cls = { "VELKÝ STRACH", "STRACH", "NEUTRÁLNÍ", "CHAMTIVOST", "VELKÁ CHAMTIVOST" } }, })[lang] or {}) do txt[k] = v end settings = { { key = "scene", type = "int", default = "gauge", label = txt.scene, opts = { keys = { "gauge", "weather" }, table.unpack(txt.scene_o) } }, } sources = { fng = { url = "https://api.alternative.me", limit = 60, fields = { "data[].value", "data[].value_classification" }, demo = [[{"data":[{"value":"78","value_classification":"Extreme Greed"}]}]] }, } function on_settings() net.fetch("fng", "/fng/?limit=1&format=json", 1800) end local W, H = gfx.W, gfx.H local FPS = gfx.FPS local px, rect, blend, clear, clip = gfx.px, gfx.rect, gfx.blend, gfx.clear, gfx.clip local text_center, text_ink = gfx.text_center, gfx.text_ink local F_TITLE = { font = "4x6" } local F_LABEL = { font = "4x6", fit = "3x5" } local F_NUM = { font = "9x15b" } local F_TINY = { font = "3x5" } local blit_rgb = gfx.blit_rgb local floor, sqrt, atan, cos, sin, fmod, abs, min, max = math.floor, math.sqrt, math.atan, math.cos, math.sin, math.fmod, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local PI = math.pi local fmt, char, concat = string.format, string.char, table.concat 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 STOPS = { { 222, 60, 50 }, { 235, 140, 40 }, { 235, 210, 60 }, { 170, 220, 80 }, { 80, 210, 110 } } local function gauge_col(t) if t < 0 then t = 0 elseif t > 1 then t = 1 end local i = min(floor(t * 4), 3) local a, b = STOPS[i + 1], STOPS[i + 2] return mix(a[1], a[2], a[3], b[1], b[2], b[3], t * 4 - i) end local CLS = { ["Extreme Fear"] = 1, ["Fear"] = 2, ["Neutral"] = 3, ["Greed"] = 4, ["Extreme Greed"] = 5 } local last, last_cls = nil, nil local function value() local j = web.fng local d = j and j.data and j.data[1] local v = d and tonumber(d.value) if v and v >= 0 and v <= 100 then last, last_cls = floor(v), CLS[d.value_classification] end return last end local SWEEP_S, MOVE_S = 1.4, 1.2 local from, to, t0, dur, last_f = 0, nil, 0, 0, nil local function ease(p) local q = p - 1 return 1 + q * q * (2.2 * q + 1.2) end local function shown(v, t, sweep) if not last_f or frame < last_f then from, to, t0, dur = sweep and data.arrive and 0 or v, v, t, SWEEP_S end last_f = frame if v ~= to then from, to, t0, dur = from + (to - from) * ease(min(1, (t - t0) / dur)), v, t, MOVE_S end local p = min(1, (t - t0) / dur) return from + (to - from) * ease(p) end local NUM_Y, LABEL_Y = 45, 57 local function draw_value(v, sv, panel, t) if panel then rect(0, NUM_Y - 2, W, H - NUM_Y + 2, 14, 16, 30) end local r, g, b = gauge_col(sv / 100) local s = fmt("%d", floor(sv + 0.5)) local top = text_ink(s, F_NUM) text_center(NUM_Y - top, s, r, g, b, F_NUM) if last_cls and (from == to or t - t0 >= dur) then text_center(LABEL_Y, txt.cls[last_cls], 220, 222, 232, F_LABEL) end end local GX, GY, R_OUT, R_IN = 31.5, 36.5, 27, 20 local G_BOT = floor(GY) local G_TOP = floor(GY - R_OUT - 2.5) local ring do local rows = {} for y = G_TOP, G_BOT do local row = {} for x = 0, W - 1 do local dx, dy = x - GX, y - GY local d = sqrt(dx * dx + dy * dy) local ang = atan(-dy, dx) local r, g, b = gauge_col(1 - ang / PI) local a = 0 if d >= R_IN - 0.5 and d <= R_OUT + 0.5 then a = min(1, d - (R_IN - 0.5), (R_OUT + 0.5) - d) for _, f in ipairs({ 0.25, 0.5, 0.75 }) do if abs(ang - PI * (1 - f)) * d < 0.7 then a = a * 0.35 end end elseif d > R_OUT + 0.5 and d < R_OUT + 3 then a = 0.3 * (1 - (d - R_OUT - 0.5) / 2.5) end row[x + 1] = char(floor(r * a), floor(g * a), floor(b * a)) end rows[#rows + 1] = concat(row) end ring = concat(rows) end local function draw_needle(frac) local ang = PI * (1 - frac) local ca, sa = cos(ang), -sin(ang) for r = 4, R_OUT - 1, 0.5 do local x, y = GX + r * ca, GY + r * sa local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local k = 1 - 0.3 * (r - 4) / (R_OUT - 5) blend(ix, iy, 250, 250, 245, k * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 250, 250, 245, k * fx * (1 - fy)) blend(ix, iy + 1, 250, 250, 245, k * (1 - fx) * fy) blend(ix + 1, iy + 1, 250, 250, 245, k * fx * fy) end for y = G_BOT - 3, G_BOT + 4 do for x = 28, 35 do local dx, dy = x - GX, y - GY local d = sqrt(dx * dx + dy * dy) if d <= 3.2 then local c = d > 2.2 and 150 or 60 px(x, y, c, c, c + 10) end end end end local function draw_gauge(v, t) clear(0, 0, 0) blit_rgb(ring, 0, G_TOP, W, G_BOT - G_TOP + 1, 0, 0, 0) local sv = shown(v, t, true) draw_needle(sv / 100) return sv end local function draw_ends() local mid = (R_OUT + R_IN) / 2 for _, e in ipairs({ { "0", GX - mid }, { "100", GX + mid } }) do local lo, hi = gfx.text_bounds(e[1], F_TINY) gfx.text(floor(e[2] - (lo + hi) / 2 + 0.5), G_BOT + 3, e[1], 150, 155, 165, F_TINY) end end local SKY_H = 43 local SKY = { top = { { 40, 42, 66 }, { 96, 112, 150 }, { 60, 140, 235 } }, bot = { { 72, 66, 92 }, { 150, 158, 178 }, { 170, 214, 250 } } } local function pal3(c, f) if f <= 0.5 then return mix(c[1][1], c[1][2], c[1][3], c[2][1], c[2][2], c[2][3], f * 2) end return mix(c[2][1], c[2][2], c[2][3], c[3][1], c[3][2], c[3][3], (f - 0.5) * 2) end local HILL = {} for x = 0, W - 1 do HILL[x] = floor(37 + 2.5 * sin(x * 0.15) + 1.5 * sin(x * 0.07 + 1)) end local CLOUD_BIG = { w = 18, h = 8, c = { { 5, 5, 3.5 }, { 10, 3.5, 4 }, { 14, 5.5, 3 } } } local CLOUD_SMALL = { w = 12, h = 6, c = { { 4, 4, 2.6 }, { 8, 3, 3 } } } local stage, sky_rows, hill_c, clouds = nil, {}, nil, {} local function cloud_raster(k, f) local lr, lg, lb = mix(92, 96, 116, 248, 250, 255, f) local dr, dg, db = mix(64, 66, 84, 196, 206, 226, f) local rows = {} for y = 0, k.h - 1 do local row = {} for x = 0, k.w - 1 do local on = false for _, c in ipairs(k.c) do local dx, dy = x + 0.5 - c[1], y + 0.5 - c[2] if dx * dx + dy * dy <= c[3] * c[3] then on = true end end if on and y < k.h - 1 then row[x + 1] = char(mix(lr, lg, lb, dr, dg, db, y / (k.h - 1))) else row[x + 1] = char(0, 0, 0) end end rows[y + 1] = concat(row) end return concat(rows) end local function build_weather(st) stage = st local f = st / 5 local tr, tg, tb = pal3(SKY.top, f) local br, bg, bb = pal3(SKY.bot, f) for y = 0, SKY_H - 1 do sky_rows[y] = { mix(tr, tg, tb, br, bg, bb, y / (SKY_H - 1)) } end hill_c = { mix(50, 92, 70, 76, 156, 84, f) } clouds = { big = cloud_raster(CLOUD_BIG, f), small = cloud_raster(CLOUD_SMALL, f) } end local function win_at(len) local t = data.time return t.unix // len, t.unix % len + t.ms / 1000 end local ROCKET = { ".r.", ".w.", "www", ".w.", "r.r" } local ROCKET_C = { r = { 230, 60, 50 }, w = { 235, 235, 240 } } local function draw_rocket(f) if f <= 0.55 then return end local win, fw = win_at(60) local h = hash32(win * 0x9E3779B1 + 91) if umod(h, 2) == 0 then return end local v = 20 local dur = (SKY_H + 26) / v local e = fw - umod(h >> 4, floor(60 - dur)) if e < 0 or e > dur then return end local drift = umod(h >> 16, 2) == 0 and 0.5 or -0.5 local x0 = drift < 0 and 8 + umod(h >> 10, 11) or 45 + umod(h >> 10, 11) local y = SKY_H + 2 - e * v for k = 1, 8 do local yk = y + 5 + k * 2 if yk < SKY_H then local a = 0.45 * (1 - k / 9) blend(floor(x0 + (SKY_H + 2 - yk) * drift + 0.5), floor(yk), 200, 200, 210, a) end end local x, yi = floor(x0 + (SKY_H + 2 - y) * drift + 0.5) - 1, floor(y) local sh = drift < 0 and 1 or -1 for r = 1, 5 do for c = 1, 3 do local col = ROCKET_C[ROCKET[r]:sub(c, c)] if col then px(x + c - 1 + sh * ((r - 1) // 2), yi + r - 1, col[1], col[2], col[3]) end end end local fl = floor(e * 12) % 2 == 0 px(x + 1 + sh * 2, yi + 5, 255, fl and 220 or 150, 60) blend(x + 1 + sh * 3, yi + 6, 255, 120, 30, fl and 0.5 or 0.8) end local COIN = { ".###.", "#####", "##o##", "#####", ".###." } local COIN_W = { 5, 3, 1, 3 } local function draw_weather(v, t) local sv = shown(v, t, false) local f = sv / 100 local st = floor(f * 5 + 0.5) if st ~= stage then build_weather(st) end clip(0, 0, W, SKY_H) for y = 0, SKY_H - 1 do local c = sky_rows[y]; rect(0, y, W, 1, c[1], c[2], c[3]) end if f > 0.45 then local a = min(1, (f - 0.45) / 0.3) local sx, sy = 51, 16 for i = 0, 7 do local ang = i * PI / 4 + t * 0.4 for r = 7, 9 do blend(floor(sx + r * cos(ang) + 0.5), floor(sy + r * sin(ang) + 0.5), 255, 220, 90, a * (10 - r) / 4) end end for y = -5, 5 do for x = -5, 5 do local d = sqrt(x * x + y * y) if d <= 5.3 then blend(sx + x, sy + y, 255, 226 - floor(d * 6), 110 - floor(d * 10), a) end end end end local n = 2 + floor((1 - f) * 5) for i = 1, n do local big = i % 2 == 1 local k = big and CLOUD_BIG or CLOUD_SMALL local x = floor(fmod(umod(hash32(i * 31), 80) + t * (2 + i % 3), W + k.w)) - k.w blit_rgb(big and clouds.big or clouds.small, x, 6 + (i * 5) % 14, k.w, k.h, 0, 0, 0) end if f < 0.45 then local drops = floor((0.45 - f) / 0.45 * 34) + 4 for i = 1, drops do local h = hash32(i * 0x9E3779B1 + 31) local near = i % 3 == 0 local sp = t * ((near and 54 or 40) + umod(h, 10)) + umod(h >> 5, 64) local n = floor(sp / 30) local y = 12 + sp - n * 30 local x = floor(umod(hash32(h + n * 0x85EBCA6B), W + 10) - (y - 12) / 3) local yy = floor(y) if x >= 0 and x < W and yy < HILL[x] then if near then blend(x, yy, 170, 190, 230, 0.85); blend(x, yy - 1, 170, 190, 230, 0.45) else blend(x, yy, 170, 190, 230, 0.5); blend(x, yy - 1, 170, 190, 230, 0.2) end end end end if f < 0.25 then local cyc, fw = win_at(3) local hb = hash32(cyc * 0x9E3779B1 + 5) local ph = fw - umod(hb >> 4, 250) / 100 if umod(hb, 100) < 35 + floor((0.25 - f) / 0.25 * 50) and ph >= 0 and ph < 0.35 then local a = (0.35 - ph) / 0.35 gfx.blend_rect(0, 0, W, SKY_H, 230, 230, 255, 0.35 * a) local x, y, side = 12 + umod(hash32(cyc * 97), 40), 12, 1 while y < 36 do local nx = x + side * (2 + umod(hash32(cyc * 13 + y), 2)) for yy = y, y + 2 do local bx = x + floor((nx - x) * (yy - y) / 3 + 0.5) blend(bx, yy, 255, 252, 210, a); blend(bx + 1, yy, 200, 200, 255, a * 0.4) end x, y, side = nx, y + 3, -side end end end draw_rocket(f) local hc = hill_c for x = 0, W - 1 do rect(x, HILL[x], 1, SKY_H - HILL[x], hc[1], hc[2], hc[3]) end if f > 0.75 then local coins = 4 + floor((f - 0.75) / 0.25 * 8) for i = 1, coins do local h = hash32(i * 4241) local sp = t * (12 + umod(h, 5)) + umod(h >> 5, 49) local n = floor(sp / (SKY_H + 6)) local x = 4 + umod(hash32(h + n * 0x85EBCA6B), W - 12) local y = floor(sp - n * (SKY_H + 6)) - 6 local w = COIN_W[floor(t * 6 + i) % 4 + 1] local o = (5 - w) // 2 for r = 1, 5 do for c = 1 + o, o + w do local ch = COIN[r]:sub(c, c) if ch == "#" then px(x + c - 1, y + r - 1, 255, 200, 60) elseif ch == "o" then px(x + c - 1, y + r - 1, 200, 140, 20) end end end end end clip() return sv 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 local gauge = cfg.scene ~= "weather" if not data.src.fng.ok then clear(0, 0, 0) gate(data.src.fng, 30) return end local v = value() if not v then clear(0, 0, 0); text_center(28, txt.nodata, 120, 120, 130, F_TINY) return end local sv if gauge then sv = draw_gauge(v, t) draw_ends() else clear(0, 0, 0) sv = draw_weather(v, t) end text_center(1, "FEAR & GREED", 235, 236, 242, F_TITLE) draw_value(v, sv, not gauge, t) end