{"size": 4479, "ext": "js", "max_stars_repo_path": "avatar-render-node/dist/controllers/index.js", "max_stars_repo_name": "alainbeired/2012-roblox-master", "max_stars_repo_stars_event_min_datetime": "2020-08-05T21:52:59.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-08T21:52:10.000Z", "max_issues_repo_path": "avatar-render-node/dist/controllers/index.js", "max_issues_repo_name": "alainbeired/2012-roblox-master", "max_issues_repo_issues_event_min_datetime": "2020-08-06T00:39:42.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-14T16:18:57.000Z", "max_forks_repo_path": "avatar-render-node/dist/controllers/index.js", "max_forks_repo_name": "alainbeired/2012-roblox-master", "max_forks_repo_forks_event_min_datetime": "2020-08-12T20:15:20.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-16T23:28:59.000Z"}
{"max_stars_count": 6, "max_issues_count": 5, "max_forks_count": 5, "avg_line_length": 40.7181818182, "max_line_length": 201, "alphanum_fraction": 0.5001116321}
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Exceptions_1 = require("../helpers/Exceptions");
const cookie = require("../helpers/Cookies");
const axios_1 = require("axios");
const client = axios_1.default.create({});
let sleep = require('util').promisify(setTimeout);
client.interceptors.response.use(undefined, e => {
    if (e.isAxiosError) {
        let err = e;
        if (err.response && err.config) {
            if (err.response.status === 403 && err.response.headers['x-csrf-token'] && err.config) {
                err.config.headers['x-csrf-token'] = err.response.headers['x-csrf-token'];
                return client.request(err.config);
            }
            else if (err.response.status === 429 || err.response.status >= 500) {
                console.log('[warn] axios got ' + err.response.status + ' for URL:', err.config.url);
                return sleep(500).then(() => {
                    return client.request(err.config);
                });
            }
        }
        else if (!err.response && err.config) {
            return client.request(err.config);
        }
    }
    return Promise.reject(e);
});
/**
 * Handle incoming WS commands
 */
class CommandHandler extends Exceptions_1.default {
    /**
     * Get the status of the web server
     */
    status() {
        return __awaiter(this, void 0, void 0, function* () {
            return {
                status: 'OK',
            };
        });
    }
    /**
     * Generate a user avatar thumbnail
     * @param user
     */
    GenerateThumbnail(user) {
        return __awaiter(this, void 0, void 0, function* () {
            console.log('gen thumb');
            let cook;
            let cookieOk = false;
            while (!cookieOk) {
                console.log('while true');
                cook = cookie.get();
                try {
                    yield client.get('https://users.roblox.com/v1/users/authenticated', {
                        headers: {
                            'cookie': '.ROBLOSECURITY=' + cook.cookie,
                        }
                    });
                    cookieOk = true;
                }
                catch (err) {
                    console.log('[info] bad cookie');
                }
            }
            console.log('[info] cookie ok. rendering');
            try {
                let assets = user.assets.map(val => { return val.id; });
                let colors = user.bodyColors;
                let conf = {
                    headers: {
                        'cookie': '.ROBLOSECURITY=' + cook.cookie,
                    }
                };
                // set to r6
                yield client.post('https://avatar.roblox.com/v1/avatar/set-player-avatar-type', { playerAvatarType: 'R6' }, conf);
                // update colors
                yield client.post('https://avatar.roblox.com/v1/avatar/set-body-colors', colors, conf);
                // wear items 
                let wear = { data: { final: false } };
                while (!wear.data.final) {
                    wear = yield client.get('https://avatar.roblox.com/v1/try-on/2d?assetIds=' + encodeURIComponent(assets.join(',')) + '&width=420&height=420&format=png&addAccoutrements=false', conf);
                    if (!wear.data.final) {
                        console.log('[info] loading thumb...');
                        yield sleep(500);
                    }
                }
                console.log('wear results', wear.data);
                return wear.data.url;
            }
            catch (e) {
                console.error(e);
                cook.done();
                throw e;
            }
        });
    }
}
exports.default = CommandHandler;
//# sourceMappingURL=index.js.map