1

This is part of code (code not mine, I can't understand how it's working, maybe it's called Promise, but I'm not sure).

m = {
    mounted: function() {
        var e = this;
        this.$bus.on("buff-event", (function(t) {
            e.buff(t)
        }))
    },
    methods: {
        buff: function(e) {
            var t = this;
            this.$bus.emit("bfceebecbb-change", "Motivating...");
            var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
            this.requestFaker.fetch(n).then((function(i) {
                i = (i = i.filter((function(t) {
                    return t.requestMethod == e
                }))[0]).responseData.filter((function(e) {
                    return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
                })), n = [], _.forEach(i, (function(e) {
                    n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
                })), n.length ? (n = "[" + n.join(",") + "]", t.requestFaker.fetch(n).then((function() {
                    t.$bus.emit("bfceebecbb-change", "Motivation success")
                }))) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
            }))
        }
    }
},

This code collecting data of users, store in to array and then pass it to the server. So it sending big array of all users in one request (it pushing all entries to array in this part n.push('{"__class__":"ServerRequest"...).

What I want to achieve is to send requests for each user one by one with some delays (for example 1 second delay between requests).

I've tried many ways to achieve it, but all unsuccessfully, I'm lack of knowledge about this programming language.

I've tried to use setTimeout functions in different ways, but all the time unsuccessfully:

methods: {
    buff: function(e) {
        var t = this;
        this.$bus.emit("bfceebecbb-change", "Motivating...");
        var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
        this.requestFaker.fetch(n).then((function(i) {
            i = (i = i.filter((function(t) {
                return t.requestMethod == e
            }))[0]).responseData.filter((function(e) {
                return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
            })), n = [], _.forEach(i, (function(e) {
                n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
            })), n.length ? 
            setTimeout((function() {
            (setTimeout((function() {n = "[" + n.join(",") + "]"}), 1000) , t.requestFaker.fetch(n).then((function() {
                t.$bus.emit("bfceebecbb-change", "Motivation success")
            })))}), 1000)  : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
        }))
    }
}

Also tried like this:

methods: {
    buff: function(e) {
        var t = this;
        this.$bus.emit("bfceebecbb-change", "Motivating...");
        var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
        this.requestFaker.fetch(n).then((function(i) {
            i = (i = i.filter((function(t) {
                return t.requestMethod == e
            }))[0]).responseData.filter((function(e) {
                return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
            })), n = [], _.forEach(i, (function(e) {
                n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
            })), n.length ? 
            setTimeout((function() {
            (n = "[" + n.join(",") + "]", t.requestFaker.fetch(n).then((function() {
                t.$bus.emit("bfceebecbb-change", "Motivation success")
            })))}), 1000)  : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
        }))
    }
}

UPDATED

As per comment asked what is %%requestId%% - I found this part:

{
    key: "fetch",
    value: function(e) {
        function t(t) {
            return e.apply(this, arguments)
        }
        return t.toString = function() {
            return e.toString()
        }, t
    }((function(e) {
        for (var t = e;
            (t = e.replace("%%requestId%%", this.requestId)) !== e;) e = t, this.incRequestId();
        return fetch(gameVars.gatewayUrl, this.getHeadForFetchQuery(e)).then((function(e) {
            return e.json()
        }))
    }))
}
8
  • Are you using some templating engine? What is %%requestId%%? Commented Apr 30, 2022 at 20:16
  • @trincot thank you for your comment. I've updated question with details about %%requestId%%". I have no idea what it means and why it's used here. I copy > pasted code between { }, but there is much bigger function, if this part is not enough I can paste here entire function. Commented Apr 30, 2022 at 20:23
  • What is this spaghetti code? Commented Apr 30, 2022 at 20:47
  • @DanZuzevich I can't understand it too, I thought maybe more experienced developers will understand and can help me to sort it out. Commented Apr 30, 2022 at 20:54
  • 2
    it's minified/uglified. where is it running, on the browser.? Commented Apr 30, 2022 at 20:55

1 Answer 1

1

try this

in general, forEach should be replaced with for..of loop with using async/await helper function which will delay the request (in await delaySome(1000); part)

it should send data in the same format, and if it fails at the server, that's probably where it needs tweaking.. or everywhere else..

m = {
    mounted: function() {
        var e = this;
        this.$bus.on("buff-event", function(t) {
            e.buff(t);
        });
    },
    methods: {
        buff: function(e) {
            var t = this;


            function delaySome(delay) {
                return new Promise((resolve) => {
                    setTimeout(() => {
                        resolve();
                    }, delay);
                });
            }

            this.$bus.emit("bfceebecbb-change", "Motivating...");
            var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
            this.requestFaker.fetch(n).then(async function(i) {
                (i = (i = i.filter(function(t) {
                    return t.requestMethod == e;
                })[0]).responseData.filter(function(e) {
                    return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted);
                })),
                (n = []);


                if (i.length) {
                    for (const e of i) {

                        await delaySome(1000);

                        t.requestFaker.fetch('[{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}]').then(function() {
                            t.$bus.emit("bfceebecbb-change", "Motivation success");
                        })

                    }
                } else {
                    t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
                }
            });
        },
    },
};
Sign up to request clarification or add additional context in comments.

2 Comments

Unbelievable! It worked! Thank you so much! You managed to figure out this puzzle! :)
@Infinity great. minification is a one way road, you can't reverse it and make the code "pretty" again..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.