//   ==UserScript==
//   @name            pp alert
//   @author          Chinezu
//   @version         1.1
//   @description     pp alert
//   @match         *://*game.php?*screen=market*
//   @match           https://*.*.*/game.php?*screen=market*
//   @match           https://*.*.*/game.php?*mode=exchange&screen=market
//   @match           https://*.*.*/game.php?*screen=market&mode=exchange
//   @grant           window.close
//   @grant           window.focus
//   ==/UserScript==
javascript: {
    (function($, adapter) {
        var data = adapter.Get() || adapter.Set({
             wood: {
                 alert: 64,
                 action: "none"
             },
             stone: {
                 alert: 64,
                 action: "none"
             },
             iron: {
                 alert: 64,
                 action: "none"
             }
        }).Get();
        var type;
        for (type in data) {
             /** @type {string} */
             data[type].action = "none";
        }
        var _post = function(name) {
             var obj = {
                 ro: {
                      alert: "Alerte",
                      sell: "vinde",
                      buy: "cumpara",
                      none: "nimic"
                 },
                 en: {
                      alert: "Alert",
                      sell: "sell",
                      buy: "buy",
                      none: "none"
                 }
             };
             return function(i) {
                 return name in obj ? obj[name][i] : obj.en[i];
             };
        }(game_data.market); /** @type {boolean} */
        var m = false; /**            * @param {!Object} msg        * @param {!
Object} text           * @return {undefined}          */
        var init = function(msg, text) {
            if (!m) {
                 msg = new SpeechSynthesisUtterance(msg); /** @type {number} */
                 msg.rate = 1.5; /** @type {number} */
                 msg.pitch = 2; /** @type {string} */
                 msg.lang = "en-US";
                 msg.addEventListener("start", function(a) {
                     /** @type {boolean} */
                     m = true;
                 });
                 msg.addEventListener("end", function(a) {
                     /** @type {boolean} */
                     m = false;
                 });
                 speechSynthesis.speak(msg);
            }
        }; /**           * @return {?}          */
        var promptConfirmation = function() {
            return new Promise(function(saveNotifs, canCreateDiscussions) {
                 TribalWars.get("market", {
                     ajax: "exchange_data"
                 }, function(notifications) {
                     saveNotifs(notifications);
                 });
            });
        };
        document.querySelector(".premium-
exchange").querySelector("tbody").appendChild(function(label) {
            label.appendChild($("th", {
                 innerText: _post("alert"),
                 style: {
                     textAlign: "left"
                 }
            }));
            ["wood", "stone", "iron"].forEach(function(probe_name) {
                 var group = $("input", {
                     type: "number",
                     min: 64,
                     dataset: {
                          type: probe_name
                     },
                     value: data[probe_name].alert,
                     oninput: function() {
                          /** @type {number} */
                          data[this.dataset.type].alert = 1 * this.value;
                          adapter.Set(data);
                     }
                 }); /** @type
{(ObjectPropertyDescriptor<HTMLInputElement.prototype>|undefined)} */
                 var config =
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value"); /** @type
{function(?): undefined} */
                 var setp = config.set; /**                  * @param {?} mymuted
* @return {undefined}                   */
                 config.set = function(mymuted) {
                     Object.defineProperty(this, "value", {
                          set: setp
                     });
                     this.value = data[probe_name].alert;
                     Object.defineProperty(this, "value", config);
                };
                Object.defineProperty(group, "value", config);
                var node = $("select", {
                     dataset: {
                         type: probe_name
                     },
                     onchange: function() {
                         data[this.dataset.type].action =
this.options[this.selectedIndex].dataset.action;
                     }
                });
                node.add($("option", {
                     dataset: {
                         action: "none"
                     },
                     text: _post("none")
                }));
                node.add($("option", {
                     dataset: {
                         action: "sell"
                     },
                     text: _post("sell")
                }));
                node.add($("option", {
                     dataset: {
                         action: "buy"
                     },
                     text: _post("buy")
                }));
                label.appendChild($("td", {
                     className: "center"
                }).appendChild(group).parentNode.appendChild(node).parentNode);
            });
            return label;
        }($("tr")));
        setInterval(function() {
            promptConfirmation().then(function(d) {
                /** @type {!Object} */
                PremiumExchange.data = d;
                PremiumExchange.updateUI();
                var dp = d.stock;
                var key;
                for (key in data) {
                     var encodedKeyOrRange =
PremiumExchange.calculateRateForOnePoint(key);
                     if ("buy" == data[key].action) {
                         let message = `${game_data.player.name} buy`;
                         if (encodedKeyOrRange >= data[key].alert) {
                              init(message, key);
                         }
                     } else {
                         let message = `${game_data.player.name} sell`;
                         if ("sell" == data[key].action && encodedKeyOrRange <
data[key].alert && 1 <= d.capacity[key] - d.stock[key]) {
                              init(message, key);
                         }
                     }
                }
            });
         }, 1200);
     })(function(node, styles) {
         /** @type {!Element} */
         node = document.createElement(node);
         if (styles) {
             var property;
             for (property in styles) {
                  if (property in node) {
                      if ("object" != typeof styles[property]) {
                           node[property] = styles[property];
                      } else {
                           var i;
                           for (i in styles[property]) {
                               node[property][i] = styles[property][i];
                           }
                      }
                  }
             }
         }
         return node;
     }, function(dbName) {
         return {
             Set: function(data) {
                  if (data) {
                      /** @type {string} */
                      localStorage[dbName] = JSON.stringify(data);
                  } else {
                      delete localStorage[dbName];
                  }
                  return this;
             },
             Get: function() {
                  if (void 0 !== localStorage[dbName]) {
                      return JSON.parse(localStorage[dbName]);
                  }
             }
         };
     }("resourceAlert"));
};