diff --git a/Pennywise.user.js b/Pennywise.user.js index dc8ca2c..371ddec 100644 --- a/Pennywise.user.js +++ b/Pennywise.user.js @@ -1,193 +1,291 @@ // ==UserScript== // @name Pennywise // @namespace hardy.pennywise -// @version 1.3 +// @version 2.0 // @description Records your transactions // @author Hardy[2131687] -// @match https://www.torn.com/imarket.php* -// @match https://www.torn.com/bazaar.php* -// @match https://www.torn.com/item.php* -// @match https://www.torn.com/profiles.php* -// @match https://www.torn.com/sendcash.php* -// @match https://www.torn.com/preferences.php* -// @match https://www.torn.com/index.php* -// @match https://www.torn.com/pmarket.php* +// @match https://www.torn.com/* // @grant GM_setValue // @grant GM_getValue // @grant GM_addStyle // @grant GM_xmlhttpRequest -// @connect script.google.com +// @grant unsafeWindow +// @connect script.google.com // ==/UserScript== (function() { - 'use strict'; - //getting your script's settings - var webapp = GM_getValue('link'); - var bazaar = GM_getValue('bazaar'); - var imarket = GM_getValue('imarket'); - var points = GM_getValue('points'); - var cash = GM_getValue('cash'); - var items = GM_getValue('items'); - var foreign = GM_getValue('foreign'); - - //defining HTML to be added to Preference page so you can have options. - if (bazaar === true) { - var bazaar_inp = ''; - } else { - var bazaar_inp = ''; - } - if (webapp) { - var link_value = ' value ='+webapp; - } else { - var link_value = ''; - } - if (imarket === true) { - var imarket_inp = ''; - } else { - var imarket_inp = ''; - } - if (points === true) { - var points_inp = ''; - } else { - var points_inp = ''; - } - if (cash === true) { - var cash_inp = ''; - } else { - var cash_inp = ''; - } - if (items === true) { - var items_inp = ''; - } else { - var items_inp = ''; - } - if (foreign === true) { - var foreign_inp = ''; - } else { - var foreign_inp = ''; - } - - var html = '

Pennywise





' + bazaar_inp + '

' + imarket_inp + '

'+ points_inp + '

' + cash_inp +'

' + items_inp +'

' + foreign_inp + '



'; - - - //Creating an Options box on Preferences page by adding the above defined HTML - if (window.location.href.includes("/preferences.php")) { - $(".content-wrapper").append(html); - } - - //saving settings when the "Save" button is clicked by the user - $("#savehardy_options").on("click", function () { - GM_setValue('link', document.getElementById("weblink").value); - GM_setValue('bazaar', document.getElementById("bazaar_").checked); - GM_setValue('imarket', document.getElementById("imarket_").checked); - GM_setValue('points', document.getElementById("points_").checked); - GM_setValue('cash', document.getElementById("cash_").checked); - GM_setValue('items', document.getElementById("items_").checked); - GM_setValue('foreign', document.getElementById("foreign_").checked); - - location.reload(); //before you come at me with pitchforks for reloading the page, this sort of reload has been given clearance in the past by IceBlueFire. As it is not automatically reloading and only reloads when you have to save the settings. - }); - - //defining a function to send data to your Webapp - function sendData(x) { - if (webapp == '') { - console.log("Webapp is not set.") - } else { - GM_xmlhttpRequest({ - method: "POST", - data: x, - url: webapp, - onload: function(e) { - console.log("Data has been sent to your Webapp."); - } - }); - } - } - -//This function is to get the name of country from where you buy stuff from. - function getcountry() { - var country_array = ["Mexico", "Switzerland", "UAE", "Cayman Islands", "Canada", "Hawaii", "United Kingdom", "Argentina", "Japan", "China", "South Africa"]; - var country = document.getElementsByClassName('msg right-round')[1].childNodes[1].childNodes[0].data; - if (country_array.indexOf(country) !== -1) { - sendData(JSON.stringify({"country": country})); - } - } -//executing the above defined function to get the name of country - if (window.location.href.includes("index.php")) { - getcountry(); - } - //Main part of the script. It catches AJAX responses and parses them. It sends the responses to WebApp, only if they meet a certain criteria. - $(document).ajaxComplete(function (event, jqXHR, ajaxObj) { - //bazaar Log. - if (window.location.href.includes("/bazaar.php")) { - if (ajaxObj.url.split("?")[0] == "bazaar.php") { - if (ajaxObj.data.split("&")[0] == "step=buyItem") { - if (bazaar === true) { - sendData(jqXHR.responseText); - - } - } - } // Items sent Log - } else if (window.location.href.includes("/item.php")) { - if (ajaxObj.url.split("?")[0] == "item.php") { - if (ajaxObj.data.split("&")[0] == "step=sendItemAction") { - if (ajaxObj.data.split("&")[1] == "confirm=1") { - if (items === true) { - sendData(jqXHR.responseText); - } - } - } - } //Item Market Log - } else if (window.location.href.includes("/imarket.php")) { - if (ajaxObj.url.split("?")[0] == "imarket.php") { - if (ajaxObj.data.split("&")[0] == "step=buyItemConfirm") { - if (imarket === true) { - sendData(jqXHR.responseText); - } - } - } //Cash Sent Log, sent through the profile page of another player. - } else if (window.location.href.includes("/profiles.php")) { - if (ajaxObj.url.split("?")[0] == "/sendcash.php") { - if (ajaxObj.data.split("&")[0] == "step=cash1") { - if (cash === true) { - sendData(jqXHR.responseText); - } - } - } //Cash Sent Log, sent through www.torn.com/sendcash.php - } else if (window.location.href.includes("/sendcash.php")) { - if (ajaxObj.url.split("?")[0] == "sendcash.php") { - if (ajaxObj.data.split("&")[4] == "step=cash1") { - if (cash === true) { - sendData(jqXHR.responseText); - } - } - } //Points Bought Log - } else if (window.location.href.includes("/pmarket.php")) { - if (ajaxObj.url.split("?")[0] == "pmarket.php") { - if (ajaxObj.data) { - if (ajaxObj.data.split("&")[0] == "ajax_action=buy1") { - if (points === true) { - sendData(jqXHR.responseText); - } - } - } - } //foreign items log - } else if (window.location.href.includes("/index.php")) { - if (ajaxObj.url.split("?")[0] == "shops.php") { - if (ajaxObj.data.split("&")[0] == "step=buyShopItem") { - if (foreign === true) { - sendData(jqXHR.responseText) - } - } - } - } - }); - //Adding CSS to options page - GM_addStyle(` -.accounting_save { padding: 5px 15px; font-size: 20px; } -.hardy_box { border-radius: 8px; background-color: rgb(242, 242, 242); box-shadow: 0px 4px 9px 3px rgba(119, 119, 119, 0.64); -moz-box-shadow: 0px 4px 9px 3px rgba(119, 119, 119, 0.64); -webkit-box-shadow: 0px 4px 9px 3px rgba(119, 119, 119, 0.64); } -.accounting_text { padding: 10px; padding-top: 10px; padding-right: 30px; padding-bottom: 10px; padding-left: 30px; } -.accounting_link { border-color:#cccccc; font-size:16px; width: 90%; border-radius:8px; border-width:3px; border-style:ridge; padding: 10px; box-shadow: 1px 3px 5px 0px rgba(42,42,42,.39); text-shadow:0px 0px 1px rgba(42,42,42,.75); font-family:monospace; } -.accounting_link:focus { outline:none; } -.css-label { font-size: medium; }`) -})(); + 'use strict'; + + //Special Thanks to Helcostr for all his help with the code. Love ya <3 #NoHomo. (っ.❛ ᴗ ❛.)っ + + //getting your script's settings + var penny_webapp = GM_getValue('link'); + var penny_bazaar = GM_getValue('bazaar'); + var penny_imarket = GM_getValue('imarket'); + var penny_points = GM_getValue('points'); + var penny_cash = GM_getValue('cash'); + var penny_items = GM_getValue('items'); + var penny_foreign = GM_getValue('foreign'); + + //defining HTML to be added to Preference page so you can have options. + if (penny_bazaar === true) { + var penny_bazaar_inp = ' checked'; + } else { + var penny_bazaar_inp = ''; + } + if (penny_webapp) { + var penny_link_value = ' value ='+ penny_webapp; + } else { + var penny_link_value = ''; + } + if (penny_imarket === true) { + var penny_imarket_inp = ' checked'; + } else { + var penny_imarket_inp = ''; + } + if (penny_points === true) { + var penny_points_inp = ' checked'; + } else { + var penny_points_inp = ''; + } + if (penny_cash === true) { + var penny_cash_inp = ' checked'; + } else { + var penny_cash_inp = ''; + } + if (penny_items === true) { + var penny_items_inp = ' checked'; + } else { + var penny_items_inp = ''; + } + if (penny_foreign === true) { + var penny_foreign_inp = ' checked'; + } else { + var penny_foreign_inp = ''; + } + + var penny_html = '

Pennywise



















'; + + + //Creating an Options box on Preferences page by adding the above defined HTML + if (window.location.href.includes("/preferences.php")) { + $(".content-wrapper").append(penny_html); + } + + //saving settings when the "Save" button is clicked by the user + $("#penny_savehardy_options").on("click", function () { + GM_setValue('link', document.getElementById("penny_weblink").value); + GM_setValue('bazaar', document.getElementById("penny_bazaar_").checked); + GM_setValue('imarket', document.getElementById("penny_imarket_").checked); + GM_setValue('points', document.getElementById("penny_points_").checked); + GM_setValue('cash', document.getElementById("penny_cash_").checked); + GM_setValue('items', document.getElementById("penny_items_").checked); + GM_setValue('foreign', document.getElementById("penny_foreign_").checked); + GM_setValue('version', "2.0"); + location.reload(); //before you come at me with pitchforks for reloading the page, this sort of reload has been given clearance in the past by IceBlueFire. As it is not automatically reloading and only reloads when you have to save the settings. + }); + + //creating a pop-up to remind users to update the Spreadsheet code. + function pennyAlert() { + $(".content-wrapper").prepend(`
+
+

Hi!! You are using Pennywise script, which has recently been updated. Some changes were also made to the Spreadsheet. Please update the Spreadsheet code to make sure that the script works properly.

+

Go to this page for more information.

+
+ +
+
+


`) + } + //pennyAlert(); + + + //defining a function to send data to your Webapp + function sendDatatoWebapp(x) { + if (GM_getValue('version') !== '2.0') { + pennyAlert(); + } else { + console.log(x); + GM_xmlhttpRequest({ + method: "POST", + data: JSON.stringify({ + "pennywise": x + }), + url: penny_webapp, + onload: function(e) { + console.log("Data has been sent to your Webapp."); + } + }); + } + } + +//for the alert Popup + $(".penny_close-button").on("click", function () { + GM_setValue('version', "2.0"); + var node = document.getElementById("penny_modal"); + node.style.display = "none"; + }); + + //This function is to get the name of country from where you buy stuff from. + function getcountry() { + var penny_country_array = ["Mexico", + "Switzerland", + "UAE", + "Cayman Islands", + "Canada", + "Hawaii", + "United Kingdom", + "Argentina", + "Japan", + "China", + "South Africa"]; + var penny_country = document.getElementsByClassName('msg right-round')[1].childNodes[1].childNodes[0].data; + if (penny_country_array.indexOf(penny_country) !== -1) { + var penny_return = penny_country; + } + return penny_return; + } + + //Bazaar log. Because Ched changed the way Bazaar Requests work. They now use Fetch + let original_fetch = unsafeWindow.fetch; + unsafeWindow.fetch = async (input, init) => { + let response = await original_fetch(input, init); + + let respo = response.clone(); + + respo.json().then((data) => { + if (input == "/bazaar.php?sid=bazaarData&step=buyItem" && data.success) { + let formData = { + itemName: /\sx\s(.*)\sfrom\s/.exec(data.text)[1] + + }; + for (let key of init.body.keys()) + formData[key] = init.body.get(key); + formData.userName = /\sfrom\s(.*)\'/.exec(data.text)[1]; + formData.type = 'bazaar'; + sendDatatoWebapp(formData); + } + }); + return response; + }; + + + //Main part of the script. It catches AJAX responses and parses them. It sends the responses to WebApp, only if they meet a certain criteria. Unlike the old version, I parsed the response in the script and sent it as a JSON string with only relevant data. Due to changes made by Ched, the below method no longer works for Bazaar items. + $(document).ajaxComplete(function (event, jqXHR, ajaxObj) { + var penny_ajax_formData = ajaxObj.data; + var penny_ajax_response = jqXHR.responseText; + var penny_ajax_url = ajaxObj.url; + if (penny_ajax_formData) { + if (penny_ajax_url.includes("item.php") && penny_ajax_formData.includes("step=sendItemAction") && penny_ajax_formData.includes("confirm=1")) { + let penny_parsed = JSON.parse(penny_ajax_response); + if (penny_parsed.success && penny_items == true) { + var penny_obj = {}; + for (let [key, data] of penny_ajax_formData.split("&").map(e=>e.split("="))) + penny_obj[key] = data; + let bold_text = $(penny_parsed["text"]); + console.log(bold_text); + for (var k = 0; k < bold_text.length; k++) { + if (bold_text[k].nodeName == "B") { + let text = bold_text[k].innerText; + if (text.match(/^\d/)) { + {} + } else { + penny_obj.item = text; + } + } else if (bold_text[k].nodeName == "A") { + penny_obj.userName = bold_text[k].innerText; + } + } + + penny_obj.type = "item_sent"; + if (penny_obj.tag !== "") { + penny_obj.tag = penny_parsed["text"].split("with the message: ")[1]; + } else { + penny_obj.tag = "None"; + } + sendDatatoWebapp(penny_obj); + } + } else if (penny_ajax_url.includes("imarket.php") && penny_ajax_formData.includes("step=buyItemConfirm")) { + let penny_parsed = JSON.parse(penny_ajax_response); + if (penny_parsed.success && penny_imarket === true) { + var penny_result = penny_parsed["text"].replace(/<[^>]+>/g, "").replace(/\s+/g, " "); + var imarket_obj = {}; + imarket_obj.item = /bought\sthe\s(.*?)\sfor\s\$/g.exec(penny_result)[1]; + imarket_obj.price = parseInt(/for\s\$(.*?)\sfrom\sthe\smarket/g.exec(penny_result)[1].replace(/,/g, "")); + imarket_obj.type = "imarket"; + sendDatatoWebapp(imarket_obj); + } + } else if (penny_ajax_url.includes("sendcash.php") && penny_ajax_formData.includes("step=cash1")) { + var penny_parsed = JSON.parse(penny_ajax_response); + if (penny_parsed.success && penny_cash === true) { + var penny_cash_obj = {}; + for (let [key, data] of penny_ajax_formData.split("&").map(e=>e.split("="))) + penny_cash_obj[key] = data; + if (penny_ajax_formData.split("&")[0] == "step=cash1") { + penny_cash_obj.user = penny_parsed["text"].match(/(.*?)<\/b>/gm)[1].replace("", "").replace("", ""); + } else { + penny_cash_obj.user = penny_cash_obj.ID.split("+")[0]; + let id = penny_cash_obj.ID.split("+%5B")[1]; + penny_cash_obj.ID = id.substring(0, id.indexOf("%")); + } + if (penny_cash_obj.tag !== "") { + penny_cash_obj.tag = penny_parsed["text"].split("with the message: ")[1]; + } else { + penny_cash_obj.tag = "None"; + } + if (penny_cash_obj.theanon == "true") { + penny_cash_obj.theanon = "Yes"; + } else { + penny_cash_obj.theanon = "No"; + } + penny_cash_obj.type = "cash_sent"; + sendDatatoWebapp(penny_cash_obj); + } + } else if (penny_ajax_url.includes("pmarket.php") && penny_ajax_formData.includes("ajax_action=buy1")) { + let penny_parsed = JSON.parse(penny_ajax_response); + if (penny_parsed.color == "green" && penny_points === true) { + let result = penny_parsed["msg"].split(" "); + let amount = parseInt(result[2].replace(/,/g, "")); + let total = parseInt(result[result.length -1].replace("$", "").replace(/,/g, "").replace(".", "")); + let unit_price = total/amount; + var point_obj = {}; + point_obj.type = "points"; + point_obj.total = total; + point_obj.quantity = amount; + point_obj.unit_price = unit_price; + sendDatatoWebapp(point_obj); + } + } else if (penny_ajax_url.includes("shops.php") && penny_ajax_formData.includes("step=buyShopItem")) { + let penny_parsed = JSON.parse(penny_ajax_response); + if (penny_parsed.success && penny_foreign === true) { + var penny_foreign_obj = {}; + for (let [key, data] of penny_ajax_formData.split("&").map(e=>e.split("="))) + penny_foreign_obj[key] = data; + let penny_text = penny_parsed["text"]; + let total = parseInt(/\sfor\s\$(.*?)\./.exec(penny_text)[1].replace(/,/g, "")); + penny_foreign_obj.total = total; + penny_foreign_obj.item = /x\s(.*?)<\/b>/.exec(penny_text)[1]; + penny_foreign_obj.type = "foreign"; + let amount = penny_foreign_obj.amount; + penny_foreign_obj.per_unit = total/amount; + penny_foreign_obj.country = getcountry(); + sendDatatoWebapp(penny_foreign_obj); + } + } + } + }); + + //Adding CSS to options box and the alert Popup + GM_addStyle(` + .penny_modal { border-radius: 8px; background-color: rgb(242, 242, 242); animation: animate 3s linear infinite;} + @keyframes animate { 0% { box-shadow: 0 0 0 0 rgba(255,109,74,.7), 0 0 0 0 rgba(255,109,74,.7);} 40% { box-shadow: 0 0 0 50px rgba(255,109,74,0), 0 0 0 0 rgba(255,109,74,.7);} 80% { box-shadow: 0 0 0 50px rgba(255,109,74,0), 0 0 0 0 rgba(255,109,74,0);} 100% { box-shadow: 0 0 0 0 rgba(255,109,74,0), 0 0 0 0 rgba(255,109,74,0);} } + .penny_button_container { padding: 8px; padding-left: 30px;} + .hardy_pennywise_header {align-items: center; color: rgb(249, 245, 245); font-size: 15px; background-color: rgb(31, 31, 32); border-radius: 3px; background-size: 4px; padding: 6px;} + .accounting_save, .penny_save-button { padding: 5px 15px; font-size: 20px; display: inline-block; } + .hardy_box { border-radius: 8px; background-color: rgb(242, 242, 242); box-shadow: 0px 4px 9px 3px rgba(119, 119, 119, 0.64); -moz-box-shadow: 0px 4px 9px 3px rgba(119, 119, 119, 0.64); -webkit-box-shadow: 0px 4px 9px 3px rgba(119, 119, 119, 0.64); } + .accounting_text, .penny_line { padding: 10px; padding-top: 10px; padding-right: 30px; padding-bottom: 10px; padding-left: 30px; font-size: 18px } + .accounting_link { border-color:#cccccc; font-size:16px; width: 90%; border-radius:8px; border-width:3px; border-style:ridge; padding: 10px; box-shadow: 1px 3px 5px 0px rgba(42,42,42,.39); text-shadow:0px 0px 1px rgba(42,42,42,.75); font-family:monospace; } + .accounting_link:focus { outline:none; } + .css-label { font-size: medium; } + `) +})(); \ No newline at end of file diff --git a/Pennywise_GoogleAppScript.gs b/Pennywise_GoogleAppScript.gs index a3fe9c4..62a8fe5 100644 --- a/Pennywise_GoogleAppScript.gs +++ b/Pennywise_GoogleAppScript.gs @@ -1,258 +1,94 @@ -//Welcome Stranger -var properties = PropertiesService.getUserProperties(); -//function to format numbers and add commas in them to make them pretty like your mom. -function formatNumber(num) { - return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') +var version = { + "version": "2.0" +}; +//a function to track version of code incase of future updates +function doGet(fg) { + if (fg.parameter.type == "version_penny") { + return ContentService.createTextOutput(JSON.stringify(version)); + } } - -//this function is for Item Market Log. -function market(x) { - - - var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Item Market"); //Get Item Market sheet in your Spreadsheet - ss.insertRowsAfter(1, 1); //Insert a row - - //parsing the response to get required information - var splash = x.split(" "); - var item_array = []; - for (i = 3; i < splash.length - 5; i++) { - item_array.push(splash[i]); - } - var item = item_array.join(" "); - - var price_ = splash[splash.length - 4]; - var price_a = price_.substring(1); - var price = parseInt(price_a.replace(/,/g, '')); - var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); - - var array = [] - array.push([time + " TCT", item, price]); - - //writing data - var range = ss.getRange(2, 1, 1, 3); - range.setValues(array); - - +function doPost(e) { + var inp = e.postData.contents; + var input = JSON.parse(inp); + if (input["pennywise"]) { + var type = input["pennywise"]["type"]; + if (type == 'imarket') { + imarket(inp); + } else if (type == 'bazaar') { + bazaar(inp); + } else if (type == "points") { + points(inp); + } else if (type == 'item_sent') { + item_sent(inp); + } else if (type == "cash_sent") { + cash_sent(inp); + } else if (type == "foreign") { + foreign(inp); + } + } } - -function bazaar(x) { - var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bazaars"); - ss.insertRowsAfter(1, 1); - var splash = x.split(" "); - var item_array = []; - for (i = 4; i < splash.length - 8; i++) { - item_array.push(splash[i]); - var item = item_array.join(" "); - } - var name_ = splash[splash.length - 7]; - var name = name_.substring(0, name_.indexOf("'")); - var total_ = splash[splash.length - 1].substring(1, splash[splash.length - 1].indexOf(".")); - var total = parseInt(total_.replace(/,/g, "")); - var amount_ = splash[2]; - var amount = parseInt(amount_.replace(/,/g, "")); - var unit_price = total / amount; - - - - var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); - - var array = [] - array.push([time + " TCT", item, amount, total, unit_price, name]); - var range = ss.getRange(2, 1, 1, 6); - range.setValues(array); - - +function imarket(args) { + var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Item Market"); + var lock = LockService.getScriptLock(); + lock.tryLock(4*60*1000); + ss.insertRowAfter(1); + var data = JSON.parse(args)["pennywise"]; + ss.getRange(2, 1, 1, 3).setValues([[getTime(), data.item, data.price]]); + lock.releaseLock() } - -function item(x) { - var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sent Items"); - ss.insertRowsAfter(1, 1); - var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); - var splash = x.split("message:"); - var crack = x.split(" "); - - if (splash[1]) { - var message = splash[1]; - var bish = splash[0].split(" "); - var user = bish[bish.length - 4]; - var length = bish.length; - bish.length = length - 5; - bish.splice(0, 3); - var item = bish.join(" "); - - } else { - var message = "None"; - - - var user_ = crack[crack.length - 1]; - var usera = user_.split(""); - usera.splice(-1, 1); - var user = usera.join(""); - crack.splice(-1, 1); - crack.splice(-1, 1); - crack.splice(0, 3); - var item = crack.join(" "); - } - - - var crack_ = x.split(" "); - - if (crack_[2] == "some") { - var amount = "1"; - } else if (crack_[2] == "a") { - var amount = "1"; - } else if (crack_[2] == "an") { - var amount = "1"; - } else { - var amount_ = crack_[2]; - var amount = amount_.substring(0, amount_.indexOf("x")); - } - - - var array = [] - array.push([time + " TCT", user, item, amount.replace(/,/g, ''), message]); - var range = ss.getRange(2, 1, 1, 5); - range.setValues(array); - - - +function bazaar(args) { + var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bazaars"); + var lock = LockService.getScriptLock(); + lock.tryLock(4*60*1000); + ss.insertRowAfter(1); + var data = JSON.parse(args)["pennywise"]; + ss.getRange(2, 1, 1, 6).setValues([[getTime(), data.itemName, data.amount, data.beforeval, data.price, '=HYPERLINK("https://www.torn.com/profiles.php?XID='+data.userID+'", "'+data.userName+'")']]); + lock.releaseLock() } -function cash(x) { - - var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cash Sent"); - ss.insertRowsAfter(1, 1); - var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); - var splash = x.split(" "); - if (splash[1] == "sent") { - var anon = "No"; - var amount_ = splash[2]; - var amount = amount_.substring(1); - - if (splash[7] == "message:") { - var message = x.substring(x.indexOf("message:") + 9); - var user = splash[4]; - } else { - var message = "None"; - var user = splash[4].substring(0, splash[4].indexOf(".")); - } - } else if (splash[1] == "anonymously") { - var anon = "Yes"; - var amount_ = splash[3]; - var amount = amount_.substring(1); - - - if (splash[8] == "message:") { - var message = x.substring(x.indexOf("message:") + 9); - var user = splash[5]; - } else { - var message = "None"; - var user = splash[5].substring(0, splash[5].indexOf(".")); - } - } - - - var array = [] - array.push([time + " TCT", user, amount.replace(/,/g, ''), message, anon]); - var range = ss.getRange(2, 1, 1, 5); - range.setValues(array); - - - +function points(args) { + var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Points"); + var lock = LockService.getScriptLock(); + lock.tryLock(4*60*1000); + ss.insertRowAfter(1); + var data = JSON.parse(args)["pennywise"]; + ss.getRange(2, 1, 1, 4).setValues([[getTime(), data.quantity, data.unit_price, data.total]]); + lock.releaseLock() } -function point(x) { - var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Points"); - ss.insertRowsAfter(1, 1); - var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); - var splash = x.split(" "); - var amount = splash[2]; - var unit_price_ = splash[6] - var unit_price = unit_price_.substring(1); - var total_price_ = splash[12]; - var total_price = total_price_.substring(1, total_price_.indexOf(".")); - - var array = [] - array.push([time + " TCT", amount.replace(/,/g, ''), unit_price.replace(/,/g, ''), total_price.replace(/,/g, '')]); - var range = ss.getRange(2, 1, 1, 4); - range.setValues(array); - - +function cash_sent(args) { + var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cash sent"); + var lock = LockService.getScriptLock(); + lock.tryLock(4*60*1000); + ss.insertRowAfter(1); + var data = JSON.parse(args)["pennywise"]; + ss.getRange(2, 1, 1, 6).setValues([[getTime(), data.user, data.money, data.tag, data.theanon, data.ID]]); + lock.releaseLock() } -function foreign(x) { - var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foreign Items"); - ss.insertRowsAfter(1, 1); - var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); - var splash = x.split(" "); - var total_ = splash[splash.length - 1]; - var total_a = total_.substring(1, total_.indexOf(".")); - var total = parseInt(total_a.replace(/,/g, "")); - var amount = parseInt(splash[3].replace(/,/g, "")); - var unit_price = total/amount; - var length = splash.length; - splash.length = length -3; - splash.splice(0, 5); - var item = splash.join(" "); - array = []; - var country = properties.getProperty("country"); - - array.push([time + " TCT", item, amount, unit_price, total, country]); - var range = ss.getRange(2, 1, 1, 6); - range.setValues(array); - - - +function item_sent(args) { + var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sent Items"); + var lock = LockService.getScriptLock(); + lock.tryLock(4*60*1000); + ss.insertRowAfter(1); + var data = JSON.parse(args)["pennywise"]; + ss.getRange(2, 1, 1, 6).setValues([[getTime(), data.userName, data.item, data.amount, data.tag, data.userID]]); + lock.releaseLock() } - -//the MVP function of the Webapp. It is responsible for the catching the data your script sends and then it executes a function from above according to data. -function doPost(e) { - var inp = e.postData.contents; - var input = JSON.parse(inp); - if (input.success) { - var text = input.text; - var reg = text.replace(/<[^>]+>/g, ""); - var split = reg.split(" "); - var last_term = split[split.length - 1]; - if (split[0] == "You") { - if (split[1] == "bought") { - - if (last_term == "market.") { - market(reg); - } else if (split[split.length - 6] == "bazaar") { - bazaar(reg); - } - } else if (split[1] == "sent") { - if (split[2].slice(0, 1) == "$") { - cash(reg); - } else { - item(reg); - } - } else if (split[1] == "anonymously") { - cash(reg); - - } else if (split[1] == "have") { - if (split[2] == "purchased") { - foreign(reg); - } - } - - - } - - } else if (input.msg) { - var text = input.msg; - var split = text.split(" "); - if (split[0] == "You") { - if (split[1] == "bought") { - point(text); - } - } - } else if (input.country) { - properties.setProperty('country', input.country); - } +function foreign(args) { + var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foreign Items"); + var lock = LockService.getScriptLock(); + lock.tryLock(4*60*1000); + ss.insertRowAfter(1); + var data = JSON.parse(args)["pennywise"]; + ss.getRange(2, 1, 1, 6).setValues([[getTime(), data.item, data.amount, data.per_unit, data.total, data.country]]); + lock.releaseLock(); } +function getTime() { + var time = Utilities.formatDate(new Date(), "GMT", "dd MMMM yyyy hh:mm:ss a"); + return time; +} \ No newline at end of file