mirror of
https://github.com/asphyxia-core/plugins.git
synced 2026-12-04 19:20:29 -06:00
746 lines
32 KiB
Plaintext
746 lines
32 KiB
Plaintext
//DATA//
|
|
trbitems: JSON.parse(require("fs").readFileSync(process.cwd() + "/plugins/gitadora@asphyxia/data/trbitems.json", "utf8"))
|
|
info: DB.Find(refid, { collection: 'playerinfo' })
|
|
profile: DB.Find(refid, { collection: 'profile' })
|
|
rivals: DB.Find(refid, { collection: 'rival' })
|
|
all_players: DB.Find(null, { collection: 'playerinfo' })
|
|
-
|
|
|
|
-
|
|
function getFullGameName(shortName) {
|
|
switch (shortName) {
|
|
case "gf":
|
|
return "GuitarFreaks"
|
|
case "dm" :
|
|
return "DrumMania"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|
|
function getFullGameVersion(shortVer) {
|
|
switch (shortVer) {
|
|
case "re" :
|
|
return "Tri-Boost Re:EVOLVE"
|
|
case "matixx":
|
|
return "Matixx"
|
|
case "exchain":
|
|
return "EXCHAIN"
|
|
case "nextage":
|
|
return "NEX+AGE"
|
|
case "highvoltage":
|
|
return "HIGH-VOLTAGE"
|
|
case "fuzzup":
|
|
return "FUZZ-UP"
|
|
case "galaxywave":
|
|
return "GALAXY WAVE"
|
|
case "galaxywave_delta":
|
|
return "GALAXY WAVE DELTA"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|
|
|
|
const versionOrder = ["re", "matixx", "exchain", "nextage", "highvoltage", "fuzzup", "galaxywave", "galaxywave_delta"]
|
|
const gameOrder = ["gf", "dm"]
|
|
|
|
// Versions that have either a playerinfo or a profile document, in canonical order.
|
|
function getVersionsPresent(info, profile) {
|
|
const present = new Set()
|
|
for (const i of info) present.add(i.version)
|
|
for (const p of profile) present.add(p.version)
|
|
return versionOrder.filter(v => present.has(v))
|
|
}
|
|
|
|
// ---- Playerboard: mirrors models/playerboard.ts (fixed 3x5 grid, two sizes). ----
|
|
const PB = { CENTER_X: 160, CENTER_Y: 235, STEP: 96, SLOTS: 10, ROTATE_STEP: 45 }
|
|
const PB_COLUMNS = [PB.CENTER_X - PB.STEP, PB.CENTER_X, PB.CENTER_X + PB.STEP]
|
|
const PB_ROWS = [-2, -1, 0, 1, 2].map(n => PB.CENTER_Y + n * PB.STEP)
|
|
const catalogById = new Map((trbitems || []).map(item => [item.id, item]))
|
|
const sheetItems = (trbitems || []).filter(item => item.category === 'Sheet')
|
|
const stickerCatalog = (trbitems || []).filter(item => item.category !== 'Sheet').map(item => [item.id, item.name, item.category])
|
|
|
|
function closestIndex(value, options) {
|
|
return options.reduce((best, option, idx) => Math.abs(option - value) < Math.abs(options[best] - value) ? idx : best, 0)
|
|
}
|
|
|
|
// Turns any stored board shape into the editor's grid presets:
|
|
// - current editor: playerboard.sheet + playerboard.sticker (with slot)
|
|
// - older free-placement editor: playerboard.sticker, background first
|
|
// - legacy: sheet + card
|
|
// Off-grid coordinates snap to the nearest grid cell / size.
|
|
function toPickerBoard(i) {
|
|
const pb = i && i.playerboard
|
|
let sheet = null
|
|
let list = []
|
|
if (pb && Array.isArray(pb.sticker)) {
|
|
list = pb.sticker
|
|
if (Object.prototype.hasOwnProperty.call(pb, 'sheet')) {
|
|
sheet = pb.sheet == null ? null : Number(pb.sheet)
|
|
} else if (list.length && (catalogById.get(Number(list[0].id)) || {}).category === 'Sheet') {
|
|
sheet = Number(list[0].id)
|
|
list = list.slice(1)
|
|
}
|
|
} else {
|
|
sheet = i && i.sheet != null ? Number(i.sheet) : null
|
|
list = Array.isArray(i && i.card) ? i.card : []
|
|
}
|
|
|
|
const num = (value, fallback) => Number.isFinite(Number(value)) ? Number(value) : fallback
|
|
const used = new Set()
|
|
const stickers = []
|
|
list.forEach((item, index) => {
|
|
if (!item || !Number.isFinite(Number(item.id))) return
|
|
const x = num(item.pos_x !== undefined ? item.pos_x : (item.position || [])[0], PB.CENTER_X)
|
|
const y = num(item.pos_y !== undefined ? item.pos_y : (item.position || [])[1], PB.CENTER_Y)
|
|
const scale = num(item.scale_x !== undefined ? item.scale_x : (item.scale || [])[0], 1)
|
|
const rotate = num(item.rotate !== undefined ? item.rotate : item.rotation, 0)
|
|
let slot = Number.isInteger(item.slot) ? item.slot : index
|
|
if (slot < 0 || slot >= PB.SLOTS || used.has(slot)) {
|
|
slot = [...Array(PB.SLOTS).keys()].find(s => !used.has(s))
|
|
}
|
|
if (slot === undefined) return
|
|
used.add(slot)
|
|
const snapped = Math.round(rotate / PB.ROTATE_STEP) * PB.ROTATE_STEP
|
|
stickers.push({
|
|
id: Math.trunc(Number(item.id)),
|
|
position: closestIndex(y, PB_ROWS) * 3 + closestIndex(x, PB_COLUMNS) + 1,
|
|
size: scale < 0.8 ? 2 : 1,
|
|
rotate: ((snapped % 360) + 360) % 360,
|
|
slot
|
|
})
|
|
})
|
|
return { sheet, stickers }
|
|
}
|
|
-
|
|
const versionsPresent = getVersionsPresent(info, profile)
|
|
|
|
mixin userCard(i)
|
|
.card.user-card
|
|
.card-header
|
|
p.card-header-title
|
|
span.icon
|
|
i.mdi.mdi-account-edit
|
|
| User Detail (#{getFullGameVersion(i.version)})
|
|
a.button.is-link.is-small(href=('/plugin/gitadora@asphyxia/profile?refid=' + encodeURIComponent(refid) + '&version=' + encodeURIComponent(i.version) + '&view=playerboard') style="margin-left:auto")
|
|
span.icon
|
|
i.mdi.mdi-view-dashboard
|
|
span PlayerBoard
|
|
.card-content
|
|
form.ajax-form(method="post" action="/emit/updatePlayerInfo")
|
|
.field
|
|
label.label ID
|
|
.control
|
|
input.input(type="text" name="refid", value=refid readonly)
|
|
.field
|
|
label.label Version
|
|
.control
|
|
input.input(type="text" name="version", value=i.version readonly)
|
|
.field
|
|
label.label Name
|
|
.control
|
|
input.input(type="text" name="name", value=i.name)
|
|
.field
|
|
label.label Title
|
|
.control
|
|
input.input(type="text" name="title", value=i.title)
|
|
.field.is-grouped.is-align-items-center
|
|
.control
|
|
button.button.is-primary(type="submit")
|
|
span.icon
|
|
i.mdi.mdi-check
|
|
span Submit
|
|
.control
|
|
span.help(data-role="form-status")
|
|
|
|
mixin boardCard(i)
|
|
.card.board-card
|
|
.card-header
|
|
p.card-header-title
|
|
span.icon
|
|
i.mdi.mdi-view-dashboard
|
|
| PlayerBoard (#{getFullGameVersion(i.version)})
|
|
a.button.is-small(href=('/plugin/gitadora@asphyxia/profile?refid=' + encodeURIComponent(refid) + '&version=' + encodeURIComponent(i.version)) style="margin-left:auto")
|
|
span.icon
|
|
i.mdi.mdi-arrow-left
|
|
span Back to Profile
|
|
.card-content
|
|
form.ajax-form(method="post" action="/emit/updatePlayerBoard")
|
|
input(type="hidden" name="refid" value=refid)
|
|
input(type="hidden" name="version" value=i.version)
|
|
textarea.is-hidden(name="board" data-pb-json)= JSON.stringify(toPickerBoard(i))
|
|
.pb-editor(data-pb-editor tabindex="0")
|
|
.pb-col.pb-left
|
|
.pb-heading Sheet
|
|
.select.is-fullwidth.is-small
|
|
select(data-pb-sheet)
|
|
option(value="") (none)
|
|
each item in sheetItems
|
|
option(value=item.id)= item.name
|
|
.pb-heading Stickers
|
|
.pb-slots(data-pb-slots)
|
|
.pb-col.pb-center
|
|
.pb-board
|
|
.pb-header
|
|
.pb-nameplate
|
|
.pb-title= i.title
|
|
.pb-name= i.name
|
|
.pb-skill SKILL
|
|
.pb-area(data-pb-area)
|
|
.pb-col.pb-right
|
|
.pb-heading(data-pb-selected-label) Sticker
|
|
.pb-picker
|
|
.select.is-fullwidth.is-small
|
|
select(data-pb-category)
|
|
input.input.is-small(type="search" placeholder="Filter by name" data-pb-filter)
|
|
.select.is-fullwidth.is-small
|
|
select(data-pb-item)
|
|
button.button.is-small.is-danger.is-light.is-fullwidth(type="button" data-pb-action="remove")
|
|
span.icon
|
|
i.mdi.mdi-delete
|
|
span Remove sticker
|
|
.pb-heading Move
|
|
.pb-dpad
|
|
button.pb-btn.pb-up(type="button" data-pb-action="up" title="Up")
|
|
i.mdi.mdi-chevron-up
|
|
button.pb-btn.pb-left-btn(type="button" data-pb-action="left" title="Left")
|
|
i.mdi.mdi-chevron-left
|
|
button.pb-btn.pb-right-btn(type="button" data-pb-action="right" title="Right")
|
|
i.mdi.mdi-chevron-right
|
|
button.pb-btn.pb-down(type="button" data-pb-action="down" title="Down")
|
|
i.mdi.mdi-chevron-down
|
|
.pb-heading Rotate
|
|
.pb-row
|
|
button.pb-btn(type="button" data-pb-action="rotate-ccw" title="Rotate left")
|
|
i.mdi.mdi-rotate-left
|
|
button.pb-btn(type="button" data-pb-action="rotate-cw" title="Rotate right")
|
|
i.mdi.mdi-rotate-right
|
|
.pb-heading Size
|
|
.pb-row
|
|
button.pb-btn(type="button" data-pb-action="size-small" title="Small")
|
|
i.mdi.mdi-arrow-collapse-all
|
|
button.pb-btn(type="button" data-pb-action="size-normal" title="Normal")
|
|
i.mdi.mdi-checkbox-blank-outline
|
|
.pb-heading Layer
|
|
.pb-row
|
|
button.pb-btn(type="button" data-pb-action="layer-up" title="Bring forward (move to previous slot)")
|
|
i.mdi.mdi-arrange-bring-forward
|
|
button.pb-btn(type="button" data-pb-action="layer-down" title="Send backward (move to next slot)")
|
|
i.mdi.mdi-arrange-send-backward
|
|
button.pb-save(type="submit") Save
|
|
p.help.pb-status(data-role="form-status")
|
|
|
|
mixin profileCard(pr)
|
|
.card.profile-card
|
|
.card-header
|
|
p.card-header-title
|
|
span.icon
|
|
i.mdi.mdi-account-details
|
|
| Profile Detail (#{getFullGameName(pr.game)} #{getFullGameVersion(pr.version)})
|
|
.card-content
|
|
form(method="post")
|
|
.field
|
|
label.label Skill
|
|
.control
|
|
input.input(type="text" name="skill", value=(pr.skill/100) readonly)
|
|
.field
|
|
label.label Skill (All Songs)
|
|
.control
|
|
input.input(type="text" name="all_skill", value=(pr.all_skill/100) readonly)
|
|
.field
|
|
label.label Songs Cleared
|
|
.control
|
|
input.input(type="text" name="clear_num", value=pr.clear_num readonly)
|
|
.field
|
|
label.label Full Combos
|
|
.control
|
|
input.input(type="text" name="full_num", value=pr.full_num readonly)
|
|
.field
|
|
label.label Excellent Full Combos
|
|
.control
|
|
input.input(type="text" name="exce_num", value=pr.exce_num readonly)
|
|
.field
|
|
label.label Highest Difficulty Cleared
|
|
.control
|
|
input.input(type="text" name="max_clear_diff", value=(pr.max_clear_diff/100) readonly)
|
|
.field
|
|
label.label Highest Difficulty Full Combo
|
|
.control
|
|
input.input(type="text" name="max_full_diff", value=(pr.max_full_diff/100) readonly)
|
|
.field
|
|
label.label Highest Difficulty Excellent Full Combo
|
|
.control
|
|
input.input(type="text" name="max_exce_diff", value=(pr.max_exce_diff/100) readonly)
|
|
.field
|
|
label.label Sessions
|
|
.control
|
|
input.input(type="text" name="session_cnt", value=pr.session_cnt readonly)
|
|
|
|
mixin rivalCard(pr)
|
|
.card.rival-card
|
|
.card-header
|
|
p.card-header-title
|
|
span.icon
|
|
i.mdi.mdi-account-multiple
|
|
| Rivals (#{getFullGameName(pr.game)} #{getFullGameVersion(pr.version)})
|
|
.card-content
|
|
-
|
|
const selectedRivals = (rivals || [])
|
|
.filter(r => r.game === pr.game && r.version === pr.version)
|
|
.sort((a, b) => (a.slot || 0) - (b.slot || 0))
|
|
.map(r => r.rival_refid);
|
|
const candidates = (all_players || []).filter(p =>
|
|
p.__refid !== refid && p.version === pr.version);
|
|
form.ajax-form(method="post" action="/emit/updateRival")
|
|
input(type="hidden" name="refid" value=refid)
|
|
input(type="hidden" name="version" value=pr.version)
|
|
input(type="hidden" name="game" value=pr.game)
|
|
p.help Select up to five distinct players. Only players with the same game version are shown.
|
|
each slot in [1, 2, 3, 4, 5]
|
|
.field
|
|
label.label Rival #{slot}
|
|
.control
|
|
.select.is-fullwidth
|
|
select(name=`rival${slot}`)
|
|
option(value="") None
|
|
each candidate in candidates
|
|
option(value=candidate.__refid selected=(selectedRivals[slot - 1] === candidate.__refid)) #{candidate.name} (DID: #{candidate.id})
|
|
.field.is-grouped.is-align-items-center
|
|
.control
|
|
button.button.is-primary(type="submit")
|
|
span.icon
|
|
i.mdi.mdi-check
|
|
span Save rivals
|
|
.control
|
|
span.help(data-role="form-status")
|
|
|
|
link(rel="stylesheet", href="static/css/webui_util.css", type="text/css")
|
|
script(src = "static/js/webui_util.js")
|
|
script(src = "static/js/ajax-form.js")
|
|
|
|
if versionsPresent.length == 0
|
|
p.has-text-grey No profile data available yet.
|
|
else
|
|
.tabs.is-toggle.is-toggle-rounded.is-small.version-tabs#tabs
|
|
ul
|
|
each version, vIdx in versionsPresent
|
|
li(tab-group="profile-version", tab-index=vIdx, data-version=version, class=(vIdx == 0) ? "is-active" : "")
|
|
a #{getFullGameVersion(version)}
|
|
|
|
each version, vIdx in versionsPresent
|
|
#tab-content(tab-group="profile-version", tab-index=vIdx, data-version=version, class=(vIdx == 0) ? "is-active" : "")
|
|
-
|
|
const infoForVersion = info.find(i => i.version === version)
|
|
const profilesForVersion = profile
|
|
.filter(p => p.version === version)
|
|
.sort((a, b) => gameOrder.indexOf(a.game) - gameOrder.indexOf(b.game))
|
|
|
|
if infoForVersion
|
|
+userCard(infoForVersion)
|
|
+boardCard(infoForVersion)
|
|
|
|
each pr in profilesForVersion
|
|
+profileCard(pr)
|
|
+rivalCard(pr)
|
|
|
|
style.
|
|
/* Profile view shows everything except the board editor; board view shows only the editor. */
|
|
.board-card { display: none; }
|
|
.playerboard-fullscreen .board-card { display: block; }
|
|
.playerboard-fullscreen .version-tabs,
|
|
.playerboard-fullscreen .user-card,
|
|
.playerboard-fullscreen .profile-card,
|
|
.playerboard-fullscreen .rival-card { display: none; }
|
|
|
|
.pb-editor {
|
|
display: flex; flex-wrap: wrap; gap: 1.25rem; justify-content: center; align-items: flex-start;
|
|
padding: 1rem 0; outline: none;
|
|
}
|
|
.pb-col { display: flex; flex-direction: column; gap: .5rem; }
|
|
.pb-left, .pb-right { flex: 1 1 200px; max-width: 260px; }
|
|
.pb-center { flex: 0 1 360px; }
|
|
.pb-heading {
|
|
background: #111; color: #fff; text-align: center; font-weight: 600;
|
|
padding: .3rem .5rem; margin-top: .35rem; border-radius: 2px; letter-spacing: .03em;
|
|
}
|
|
.pb-slots { display: flex; flex-direction: column; gap: .35rem; }
|
|
.pb-slot {
|
|
display: flex; align-items: center; gap: .5rem; width: 100%; min-height: 2.6rem;
|
|
padding: .3rem .5rem; border: 2px solid #dbdbdb; border-radius: 3px;
|
|
background: #fff; color: #333; text-align: left; cursor: pointer; font-size: .8rem; line-height: 1.2;
|
|
}
|
|
.pb-slot:hover { background: #f0f0f0; }
|
|
.pb-slot.is-empty { color: #999; }
|
|
.pb-slot.is-selected { border-color: #ffd600; background: #e8e8e8; box-shadow: 0 0 0 2px rgba(255,214,0,.35); }
|
|
.pb-slot-no { flex: 0 0 auto; min-width: 1.6rem; font-weight: 700; color: #6b4fd8; }
|
|
.pb-slot-name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
|
|
.pb-board { width: 100%; max-width: 360px; margin: 0 auto; background: #000; border: 1px solid #555; }
|
|
.pb-header { display: flex; gap: 4px; padding: 6px; background: #000; }
|
|
.pb-nameplate { flex: 1 1 auto; border: 1px solid #888; padding: 2px 6px; min-width: 0; }
|
|
.pb-title { font-size: .7rem; color: #ddd; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.pb-name { font-size: .95rem; font-weight: 700; color: #ff4d4d; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.pb-skill { flex: 0 0 64px; border: 1px solid #888; color: #aaa; font-size: .7rem; padding: 2px 6px; }
|
|
.pb-area {
|
|
position: relative; width: 100%; aspect-ratio: 320 / 470; overflow: hidden; touch-action: none;
|
|
background: linear-gradient(135deg, #20243a, #384d69);
|
|
}
|
|
.pb-area.has-sheet { background: linear-gradient(135deg, #3a3f62, #6a7fa3); }
|
|
.pb-sheet-label {
|
|
position: absolute; inset: 4px 0 auto 0; text-align: center; font-size: .7rem;
|
|
color: rgba(255,255,255,.55); pointer-events: none;
|
|
}
|
|
.pb-cell {
|
|
position: absolute; width: 8px; height: 8px; border-radius: 50%;
|
|
background: rgba(255,255,255,.28); transform: translate(-50%, -50%); pointer-events: none;
|
|
}
|
|
.pb-cell.is-target { background: #ffd600; width: 12px; height: 12px; }
|
|
.pb-sticker {
|
|
position: absolute; display: flex; align-items: center; justify-content: center;
|
|
padding: 2px; border: 1px solid rgba(255,255,255,.7); border-radius: 4px;
|
|
background: rgba(0,0,0,.72); color: #fff; font-size: .62rem; line-height: 1.1; text-align: center;
|
|
overflow: hidden; cursor: grab; user-select: none; transform-origin: center;
|
|
}
|
|
.pb-sticker.is-selected { border: 2px solid #ffd600; box-shadow: 0 0 8px rgba(255,214,0,.7); z-index: 20; }
|
|
.pb-sticker.is-dragging { cursor: grabbing; opacity: .85; z-index: 30; }
|
|
|
|
.pb-picker { display: flex; flex-direction: column; gap: .35rem; }
|
|
.pb-dpad {
|
|
display: grid; grid-template-columns: repeat(3, 3rem); grid-template-rows: repeat(3, 3rem);
|
|
gap: .35rem; justify-content: center;
|
|
}
|
|
.pb-up { grid-column: 2; grid-row: 1; }
|
|
.pb-left-btn { grid-column: 1; grid-row: 2; }
|
|
.pb-right-btn { grid-column: 3; grid-row: 2; }
|
|
.pb-down { grid-column: 2; grid-row: 3; }
|
|
.pb-row { display: flex; gap: .6rem; justify-content: center; }
|
|
.pb-btn {
|
|
width: 3rem; height: 3rem; display: inline-flex; align-items: center; justify-content: center;
|
|
background: #fff; color: #111; border: 3px solid #111; border-radius: 2px;
|
|
box-shadow: 2px 2px 0 rgba(0,0,0,.45); font-size: 1.6rem; cursor: pointer;
|
|
}
|
|
.pb-btn:hover:not(:disabled) { background: #f3f3f3; }
|
|
.pb-btn:active:not(:disabled) { transform: translate(1px, 1px); box-shadow: 1px 1px 0 rgba(0,0,0,.45); }
|
|
.pb-btn:disabled { opacity: .35; cursor: default; }
|
|
.pb-btn.is-current { background: #ffd600; }
|
|
.pb-save {
|
|
margin-top: .9rem; align-self: center; width: 9rem; height: 3.4rem;
|
|
background: #ffe600; color: #555; border: 3px solid #111; border-radius: 2px;
|
|
font-size: 1.15rem; font-weight: 700; cursor: pointer; box-shadow: 2px 2px 0 rgba(0,0,0,.45);
|
|
}
|
|
.pb-save:hover { background: #fff04d; }
|
|
.pb-status { text-align: center; min-height: 1.2em; }
|
|
|
|
script.
|
|
(() => {
|
|
// Page mode: ?version=<v> opens that version tab; adding &view=playerboard
|
|
// switches to the playerboard editor for that version.
|
|
const params = new URLSearchParams(location.search);
|
|
const selectedVersion = params.get('version');
|
|
if (selectedVersion) {
|
|
const tab = document.querySelector('li[tab-group="profile-version"][data-version="' + CSS.escape(selectedVersion) + '"]');
|
|
if (tab) {
|
|
const index = tab.getAttribute('tab-index');
|
|
document.querySelectorAll('[tab-group="profile-version"]').forEach(el => {
|
|
el.classList.toggle('is-active', el.getAttribute('tab-index') === index);
|
|
});
|
|
}
|
|
}
|
|
if (params.get('view') === 'playerboard') document.body.classList.add('playerboard-fullscreen');
|
|
})();
|
|
|
|
script.
|
|
window.GITADORA_STICKER_CATALOG = !{JSON.stringify(stickerCatalog).replace(/</g, '\\u003c')};
|
|
|
|
script.
|
|
(() => {
|
|
// Must match models/playerboard.ts.
|
|
const BOARD_W = 320, BOARD_H = 470;
|
|
const CENTER_X = 160, CENTER_Y = 235, STEP = 96;
|
|
const COLUMNS = 3, ROWS = 5, SLOTS = 10, ROTATE_STEP = 45;
|
|
const SIZE_SCALE = { 1: 1, 2: 0.6 };
|
|
const CATEGORY_ORDER = ['Sticker', 'Seasonal', 'Special'];
|
|
|
|
const catalog = window.GITADORA_STICKER_CATALOG || [];
|
|
const byId = new Map(catalog.map(entry => [entry[0], entry]));
|
|
const byCategory = new Map();
|
|
for (const entry of catalog) {
|
|
if (!byCategory.has(entry[2])) byCategory.set(entry[2], []);
|
|
byCategory.get(entry[2]).push(entry);
|
|
}
|
|
const categories = CATEGORY_ORDER.filter(c => byCategory.has(c))
|
|
.concat([...byCategory.keys()].filter(c => !CATEGORY_ORDER.includes(c)));
|
|
|
|
const positionToXY = position => {
|
|
const row = Math.floor((position - 1) / COLUMNS);
|
|
const col = (position - 1) % COLUMNS;
|
|
return [CENTER_X + (col - 1) * STEP, CENTER_Y + (row - 2) * STEP];
|
|
};
|
|
const xyToPosition = (x, y) => {
|
|
const col = Math.max(0, Math.min(COLUMNS - 1, Math.round((x - CENTER_X) / STEP) + 1));
|
|
const row = Math.max(0, Math.min(ROWS - 1, Math.round((y - CENTER_Y) / STEP) + 2));
|
|
return row * COLUMNS + col + 1;
|
|
};
|
|
const stickerName = id => {
|
|
const entry = byId.get(id);
|
|
return entry ? entry[1] : 'Unknown #' + id;
|
|
};
|
|
|
|
const initialize = editor => {
|
|
const form = editor.closest('form');
|
|
const jsonField = form.querySelector('[data-pb-json]');
|
|
const sheetSelect = editor.querySelector('[data-pb-sheet]');
|
|
const slotList = editor.querySelector('[data-pb-slots]');
|
|
const area = editor.querySelector('[data-pb-area]');
|
|
const categorySelect = editor.querySelector('[data-pb-category]');
|
|
const filterInput = editor.querySelector('[data-pb-filter]');
|
|
const itemSelect = editor.querySelector('[data-pb-item]');
|
|
const selectedLabel = editor.querySelector('[data-pb-selected-label]');
|
|
|
|
// ---- State ----
|
|
let initial = {};
|
|
try { initial = JSON.parse(jsonField.value || '{}'); } catch (_) { initial = {}; }
|
|
const state = { sheet: initial.sheet == null ? null : Number(initial.sheet), slots: Array(SLOTS).fill(null), selected: 0 };
|
|
(Array.isArray(initial.stickers) ? initial.stickers : []).forEach(item => {
|
|
if (item && Number.isInteger(item.slot) && item.slot >= 0 && item.slot < SLOTS) {
|
|
state.slots[item.slot] = { id: item.id, position: item.position || 8, size: item.size || 1, rotate: item.rotate || 0 };
|
|
}
|
|
});
|
|
|
|
if (state.sheet != null && !sheetSelect.querySelector('option[value="' + state.sheet + '"]')) {
|
|
sheetSelect.insertAdjacentHTML('beforeend', '<option value="' + state.sheet + '">Unknown #' + state.sheet + '</option>');
|
|
}
|
|
sheetSelect.value = state.sheet == null ? '' : String(state.sheet);
|
|
|
|
const current = () => state.slots[state.selected];
|
|
const sync = () => {
|
|
jsonField.value = JSON.stringify({
|
|
sheet: state.sheet,
|
|
stickers: state.slots
|
|
.map((s, slot) => s && { id: s.id, position: s.position, size: s.size, rotate: s.rotate, slot })
|
|
.filter(Boolean)
|
|
});
|
|
};
|
|
|
|
// ---- Static preview grid ----
|
|
const sheetLabel = document.createElement('div');
|
|
sheetLabel.className = 'pb-sheet-label';
|
|
area.appendChild(sheetLabel);
|
|
const cells = [];
|
|
for (let position = 1; position <= COLUMNS * ROWS; position++) {
|
|
const [x, y] = positionToXY(position);
|
|
const cell = document.createElement('div');
|
|
cell.className = 'pb-cell';
|
|
cell.style.left = (x / BOARD_W * 100) + '%';
|
|
cell.style.top = (y / BOARD_H * 100) + '%';
|
|
area.appendChild(cell);
|
|
cells.push(cell);
|
|
}
|
|
|
|
// ---- Slot list ----
|
|
const slotButtons = [];
|
|
for (let slot = 0; slot < SLOTS; slot++) {
|
|
const button = document.createElement('button');
|
|
button.type = 'button';
|
|
button.className = 'pb-slot';
|
|
button.innerHTML = '<span class="pb-slot-no"></span><span class="pb-slot-name"></span>';
|
|
button.querySelector('.pb-slot-no').textContent = slot + 1;
|
|
button.addEventListener('click', () => { state.selected = slot; render(); });
|
|
slotList.appendChild(button);
|
|
slotButtons.push(button);
|
|
}
|
|
|
|
// ---- Sticker picker ----
|
|
const fillCategories = () => {
|
|
categorySelect.innerHTML = '';
|
|
categories.forEach(c => {
|
|
const option = document.createElement('option');
|
|
option.value = c;
|
|
option.textContent = c + ' (' + byCategory.get(c).length + ')';
|
|
categorySelect.appendChild(option);
|
|
});
|
|
};
|
|
const fillItems = selectedId => {
|
|
const query = filterInput.value.trim().toLowerCase();
|
|
const items = (byCategory.get(categorySelect.value) || [])
|
|
.filter(entry => !query || entry[1].toLowerCase().includes(query) || String(entry[0]) === query);
|
|
itemSelect.innerHTML = '';
|
|
const placeholder = document.createElement('option');
|
|
placeholder.value = '';
|
|
placeholder.textContent = current() ? '— keep current —' : '— choose a sticker —';
|
|
itemSelect.appendChild(placeholder);
|
|
items.forEach(entry => {
|
|
const option = document.createElement('option');
|
|
option.value = entry[0];
|
|
option.textContent = entry[1];
|
|
itemSelect.appendChild(option);
|
|
});
|
|
itemSelect.value = selectedId != null && items.some(e => e[0] === selectedId) ? String(selectedId) : '';
|
|
};
|
|
const syncPicker = () => {
|
|
const sticker = current();
|
|
const entry = sticker && byId.get(sticker.id);
|
|
if (entry) categorySelect.value = entry[2];
|
|
filterInput.value = '';
|
|
fillItems(sticker ? sticker.id : null);
|
|
};
|
|
fillCategories();
|
|
|
|
// ---- Rendering ----
|
|
const placeSticker = (el, sticker, x, y) => {
|
|
const scale = SIZE_SCALE[sticker.size] || 1;
|
|
el.style.left = (x / BOARD_W * 100) + '%';
|
|
el.style.top = (y / BOARD_H * 100) + '%';
|
|
el.style.width = (STEP * scale / BOARD_W * 100) + '%';
|
|
el.style.height = (STEP * scale / BOARD_H * 100) + '%';
|
|
el.style.transform = 'translate(-50%, -50%) rotate(' + sticker.rotate + 'deg)';
|
|
};
|
|
|
|
const render = () => {
|
|
const selected = current();
|
|
|
|
area.classList.toggle('has-sheet', state.sheet != null);
|
|
sheetLabel.textContent = state.sheet != null ? 'Sheet: ' + (sheetSelect.selectedOptions[0] || {}).textContent : '';
|
|
cells.forEach((cell, index) => cell.classList.toggle('is-target', !!selected && selected.position === index + 1));
|
|
|
|
area.querySelectorAll('.pb-sticker').forEach(el => el.remove());
|
|
state.slots.forEach((sticker, slot) => {
|
|
if (!sticker) return;
|
|
const el = document.createElement('div');
|
|
el.className = 'pb-sticker' + (slot === state.selected ? ' is-selected' : '');
|
|
el.textContent = stickerName(sticker.id);
|
|
el.title = 'Slot ' + (slot + 1) + ': ' + stickerName(sticker.id) + ' — drag to move';
|
|
el.style.zIndex = slot + 1;
|
|
placeSticker(el, sticker, ...positionToXY(sticker.position));
|
|
bindDrag(el, slot);
|
|
area.appendChild(el);
|
|
});
|
|
|
|
slotButtons.forEach((button, slot) => {
|
|
const sticker = state.slots[slot];
|
|
button.classList.toggle('is-selected', slot === state.selected);
|
|
button.classList.toggle('is-empty', !sticker);
|
|
button.querySelector('.pb-slot-name').textContent = sticker ? stickerName(sticker.id) : '(empty)';
|
|
});
|
|
|
|
selectedLabel.textContent = 'Sticker — slot ' + (state.selected + 1);
|
|
const has = !!selected;
|
|
editor.querySelectorAll('[data-pb-action]').forEach(button => { button.disabled = !has; });
|
|
if (has) {
|
|
const row = Math.floor((selected.position - 1) / COLUMNS), col = (selected.position - 1) % COLUMNS;
|
|
editor.querySelector('[data-pb-action="up"]').disabled = row === 0;
|
|
editor.querySelector('[data-pb-action="down"]').disabled = row === ROWS - 1;
|
|
editor.querySelector('[data-pb-action="left"]').disabled = col === 0;
|
|
editor.querySelector('[data-pb-action="right"]').disabled = col === COLUMNS - 1;
|
|
editor.querySelector('[data-pb-action="layer-up"]').disabled = state.selected === 0;
|
|
editor.querySelector('[data-pb-action="layer-down"]').disabled = state.selected === SLOTS - 1;
|
|
}
|
|
editor.querySelector('[data-pb-action="size-small"]').classList.toggle('is-current', has && selected.size === 2);
|
|
editor.querySelector('[data-pb-action="size-normal"]').classList.toggle('is-current', has && selected.size === 1);
|
|
|
|
syncPicker();
|
|
sync();
|
|
};
|
|
|
|
// Free drag for feedback, snapped to the nearest grid cell on release.
|
|
const bindDrag = (el, slot) => {
|
|
let drag = null;
|
|
el.addEventListener('pointerdown', event => {
|
|
event.preventDefault();
|
|
editor.focus({ preventScroll: true });
|
|
el.setPointerCapture(event.pointerId);
|
|
drag = { rect: area.getBoundingClientRect(), moved: false };
|
|
if (state.selected !== slot) {
|
|
state.selected = slot;
|
|
slotButtons.forEach((b, i) => b.classList.toggle('is-selected', i === slot));
|
|
area.querySelectorAll('.pb-sticker').forEach(s => s.classList.remove('is-selected'));
|
|
el.classList.add('is-selected');
|
|
}
|
|
});
|
|
el.addEventListener('pointermove', event => {
|
|
if (!drag) return;
|
|
drag.moved = true;
|
|
el.classList.add('is-dragging');
|
|
const x = Math.max(0, Math.min(BOARD_W, (event.clientX - drag.rect.left) / drag.rect.width * BOARD_W));
|
|
const y = Math.max(0, Math.min(BOARD_H, (event.clientY - drag.rect.top) / drag.rect.height * BOARD_H));
|
|
drag.position = xyToPosition(x, y);
|
|
placeSticker(el, state.slots[slot], x, y);
|
|
cells.forEach((cell, index) => cell.classList.toggle('is-target', drag.position === index + 1));
|
|
});
|
|
const end = event => {
|
|
if (!drag) return;
|
|
try { el.releasePointerCapture(event.pointerId); } catch (_) {}
|
|
if (drag.moved && drag.position) state.slots[slot].position = drag.position;
|
|
drag = null;
|
|
render();
|
|
};
|
|
el.addEventListener('pointerup', end);
|
|
el.addEventListener('pointercancel', end);
|
|
};
|
|
|
|
// ---- Actions ----
|
|
const move = (dRow, dCol) => {
|
|
const s = current();
|
|
if (!s) return;
|
|
const row = Math.floor((s.position - 1) / COLUMNS) + dRow;
|
|
const col = (s.position - 1) % COLUMNS + dCol;
|
|
if (row < 0 || row >= ROWS || col < 0 || col >= COLUMNS) return;
|
|
s.position = row * COLUMNS + col + 1;
|
|
};
|
|
const actions = {
|
|
'up': () => move(-1, 0),
|
|
'down': () => move(1, 0),
|
|
'left': () => move(0, -1),
|
|
'right': () => move(0, 1),
|
|
'rotate-ccw': () => { current().rotate = (current().rotate - ROTATE_STEP + 360) % 360; },
|
|
'rotate-cw': () => { current().rotate = (current().rotate + ROTATE_STEP) % 360; },
|
|
'size-small': () => { current().size = 2; },
|
|
'size-normal': () => { current().size = 1; },
|
|
'remove': () => { state.slots[state.selected] = null; },
|
|
'layer-up': () => swap(state.selected, state.selected - 1),
|
|
'layer-down': () => swap(state.selected, state.selected + 1),
|
|
};
|
|
const swap = (a, b) => {
|
|
if (b < 0 || b >= SLOTS) return;
|
|
[state.slots[a], state.slots[b]] = [state.slots[b], state.slots[a]];
|
|
state.selected = b;
|
|
};
|
|
editor.addEventListener('click', event => {
|
|
const button = event.target.closest('[data-pb-action]');
|
|
if (!button || button.disabled) return;
|
|
if (!current() && button.dataset.pbAction !== 'remove') return;
|
|
actions[button.dataset.pbAction]();
|
|
render();
|
|
});
|
|
editor.addEventListener('keydown', event => {
|
|
if (event.target.matches('input, select, textarea')) return;
|
|
const key = { ArrowUp: 'up', ArrowDown: 'down', ArrowLeft: 'left', ArrowRight: 'right', Delete: 'remove' }[event.key];
|
|
if (!key || !current()) return;
|
|
event.preventDefault();
|
|
actions[key]();
|
|
render();
|
|
});
|
|
|
|
sheetSelect.addEventListener('change', () => {
|
|
state.sheet = sheetSelect.value === '' ? null : Number(sheetSelect.value);
|
|
render();
|
|
});
|
|
categorySelect.addEventListener('change', () => { filterInput.value = ''; fillItems(current() ? current().id : null); });
|
|
filterInput.addEventListener('input', () => fillItems(current() ? current().id : null));
|
|
itemSelect.addEventListener('change', () => {
|
|
if (itemSelect.value === '') return;
|
|
const id = Number(itemSelect.value);
|
|
if (current()) current().id = id;
|
|
else state.slots[state.selected] = { id, position: 8, size: 1, rotate: 0 };
|
|
render();
|
|
});
|
|
// Registered before ajax-form.js binds its handler, so the JSON is current when sent.
|
|
form.addEventListener('submit', sync);
|
|
|
|
const firstUsed = state.slots.findIndex(Boolean);
|
|
state.selected = firstUsed >= 0 ? firstUsed : 0;
|
|
render();
|
|
};
|
|
|
|
document.querySelectorAll('[data-pb-editor]').forEach(initialize);
|
|
})();
|