사용자:Hsl0/연구소: 두 판 사이의 차이

리버티게임, 모두가 만들어가는 자유로운 게임
imported>Hsl0
imported>Hsl0
4번째 줄: 4번째 줄:
* {{새로고침}}
* {{새로고침}}
* {{#invoke:metadata|icon|사용자:hsl0/연구소}}
* {{#invoke:metadata|icon|사용자:hsl0/연구소}}
<source lang="js">
function scrapMetadata(category) {
    $('#gamelist-' + category + ' li').map(function() {
        var data = $(this).find('.old-gameicon-status').data();
        data = {
            progress: data.progress === ""? null : data.progress,
            openness: data.edit === ""? null : data.edit,
            tech: data.tech === ""? null : data.tech,
            rating: data.rating === ""? null : data.rating,
            category
        };
        var nameElement = this.querySelector('.old-gameicon-name a');
        data.title = decodeURI(new URL(nameElement.href).pathname.slice(6));
        data.name = nameElement.innerText;
        var maker = $(this).find('.old-gameicon-maker');
        data.helper = maker.find('.old-gameicon-helper a').map(function() {
            var url = new URL(this.href);
            if(url.searchParams.has('title')) return url.searchParams.get('title').slice(4).split('/')[0].replace(/_/g, ' ');
            else return decodeURI(url.pathname.slice(6)).slice(4).split('/')[0].replace(/_/g, ' ');
        }).toArray();
        if(!data.helper.length) data.helper = null;
        else if(data.helper.length === 1) data.helper = data.helper[0];
        data.maker = maker.find('a:not(.old-gameicon-helper a)').map(function() {
            var url = new URL(this.href);
            if(url.searchParams.has('title')) return url.searchParams.get('title').slice(4).split('/')[0].replace(/_/g, ' ');
            else return decodeURI(url.pathname.slice(6)).slice(4).split('/')[0].replace(/_/g, ' ');
        }).toArray();
        if(data.maker.length === 1) data.maker = data.maker[0];
        switch(data.openness) {
            case 0: data.openness = false; break;
            case 1: data.openness = "partial"; break;
            case 2: data.openness = true; break;
            case 3:
                data.openness = null;
                data.abandon = true;
                break;
        }
        return data;
    });
}
function mergeMetadata(datas) {
    var datamap = {};
    for(const list of datas) {
        for(const data of list) {
            if(datamap[data.title]) {
                let category = datamap[data.title].category;
                if(typeof category === 'string') category = [category, data.category];
                else category.push(data.category);
                datamap[data.title].category = category;
            } else datamap[data.title] = data;
            delete data.title;
        }
    }
    return datamap;
}
function saveMetadata(map) {
    const api = new mw.Api();
    const tasks = [];
    for(const title in map) {
        tasks.push(api.create(title + '/game.json', {
            summary: "게임 메타데이터 생성",
            bot: true,
            watchlist: "nochange",
            contentformat: "application/json",
            contentmodel: "json"
        }, JSON.stringify(map[title]))).then(console.log);
    }
}
function createMetadata() {
    const categories = ["act", "adv", "brd", "cpn", "esc", "liv", "mag", "mlt", "mus", "nax", "prd", "puz", "qiz", "ral", "rnd", "rod", "sht", "wst"];
    const datas = [];
    for(const category of categories) datas.push(scrapMetadata(category));
    saveMetadata(mergeMetadata(datas));
}
</source>

2021년 12월 13일 (월) 17:12 판