From 6f3f9263323da23ffb08ef9eac3cf279c40ab5a1 Mon Sep 17 00:00:00 2001 From: wstathis Date: Thu, 19 Mar 2020 15:24:05 -0400 Subject: [PATCH 1/6] updates to not commit txt files --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 00cbbdf..8399861 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,7 @@ typings/ # dotenv environment variables file .env +#keep tokens private +data/token.txt +#keep price local +data/price.txt \ No newline at end of file From 225a739f24aeb6336abe4afcd54d6fb16bfa7e62 Mon Sep 17 00:00:00 2001 From: wstathis Date: Thu, 19 Mar 2020 22:07:46 -0400 Subject: [PATCH 2/6] Added market parsing and printing --- data/meatMarket.txt | 0 index.js | 90 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 data/meatMarket.txt diff --git a/data/meatMarket.txt b/data/meatMarket.txt new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js index 9cbdfd0..f7854f6 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const fee = 0.05; // user data var ledger = {}; +var market = []; var betTable = {}; var diceTable = {}; @@ -21,6 +22,7 @@ var volume = { }; var miners = 0; var registered = 0; +var marketSize = 0; // setup init(); @@ -75,6 +77,8 @@ bot.on('message', (message) => { victory(message); else if (command == '!prize') prize(message); + else if (command == '!market') + printMarket(message); } else if (splitMessage.length == 2) { const command = splitMessage[0]; @@ -99,6 +103,8 @@ bot.on('message', (message) => { if (command == '!flip') flip(message, directive, coinage); + if (command == '!post') //!post buy/sell x + post(message, directive, coinage) else if (message == '!hall of fame') hallOfFame(message); } @@ -139,6 +145,61 @@ function parseToken() { return buffer[0] } +function parseSupply(message) { + var path = process.cwd() + var buffer = fs.readFileSync(path + "\\data\\meatMarket.txt"); + var lines = buffer.toString().split("\n"); + var buyerData = {}; + var sellerData = {}; + for (var i = 0; i < lines.length; i++) { + var line = lines[i].replace('\r', ''); + + if (line.length > 0) { + var data = line.split('\t'); + if(data[2]=='B'){ + buyerData[data[0]]={ + username: data[1], + price: parseFloat(data[3]), + quantity: parseFloat(data[4]) + }; + } + else if(data[2]=='S'){ + sellerData[data[0]]={ + username: data[1], + price: parseFloat(data[3]), + quantity: parseFloat(data[4]) + }; + } + } + marketSize += 1; + } + market[0]=buyerData; + market[1]=sellerData; + +} + +function printMarket(message) { +parseSupply(); +var response = '```glsl\nGET YER FLESH HERE!\n'; +// create array of unsorted users +var buyerData = market[0]; +var sellerData = market[1]; +if (Object.keys(sellerData).length > 0) { + response += 'Seller\tQUANTITY\tPRICE\n' + Object.keys(sellerData).forEach(function(key) { + response += sellerData[key].username + '\t' + sellerData[key].quantity + '\t' + sellerData[key].price + '\n'; + }); +} +if (Object.keys(buyerData).length > 0) { + response += 'BUYER\tQUANTITY\tPRICE\n' + Object.keys(buyerData).forEach(function(key) { + response += buyerData[key].username + '\t' + buyerData[key].quantity + '\t' + buyerData[key].price + '\n'; + }); +} +response += '```'; +message.channel.send(response); +} + function parseLedger() { var path = process.cwd(); var buffer = fs.readFileSync(path + "\\data\\ledger.txt"); @@ -847,6 +908,35 @@ function flip(message, side, coinage) { message.channel.send(response); } +function post(message, command, coinage){ + + const id = message.member.user.id; + var response = '<@' + id + '>'; + + // check user registration + if (!(id in ledger)) { + message.channel.send(response + ', you are not registered.'); + return; + } + + // update user channel + var userData = ledger[id]; + userData.channel = message.channel; + + if (command == 'buy'){ + parseSupply(); + response += ', is buyin\' who\'s sellin\'?'; + } + else if (command == 'sell'){ + response += ', is looking to unload some meat, get it while it\'s hot'; + } + else{ + response += ', ' + command + ' is not a valid command. ya dingus!'; + } + + message.channel.send(response); +} + function hallOfFame(message) { var response = '```glsl\n'; response += 'Winner of MeatCoin 2.2: The old razzmataz'; From 8d3b3e7a62e764da77ce05a36213bbbdd6ccb66e Mon Sep 17 00:00:00 2001 From: wstathis Date: Fri, 20 Mar 2020 16:44:06 -0400 Subject: [PATCH 3/6] Added full functionality for the meat market, just the actual trading left --- index.js | 288 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 260 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index f7854f6..214d678 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const bot = new Discord.Client(); // constants const fee = 0.05; +const postFee = 0.07; // user data var ledger = {}; @@ -31,6 +32,9 @@ var isReady = true; // handle user commands bot.on('message', (message) => { // idk why this is necessary + if (!message.member) + return; + // idk why this is necessary if (!message.member.user) return; @@ -103,23 +107,31 @@ bot.on('message', (message) => { if (command == '!flip') flip(message, directive, coinage); - if (command == '!post') //!post buy/sell x - post(message, directive, coinage) else if (message == '!hall of fame') hallOfFame(message); } else if (splitMessage.length > 3) { - const command = splitMessage[0]; - const directive = splitMessage[1]; - const username = splitMessage.slice(2, splitMessage.length - 1).join(' '); - const coinage = splitMessage[splitMessage.length - 1]; - if (command == '!dice' && directive == 'challenge') - diceChallenge(message, username, coinage); - else if (command == '!dice' && directive == 'accept') - diceAccept(message, username, coinage); - else if (command == '!send' && directive == 'user') - sendUser(message, username, coinage); + const command = splitMessage[0]; + if(splitMessage.length == 4 && command == '!post'){ + const directive = splitMessage[1]; + const price = splitMessage[2] + const coinage = splitMessage[3]; + post(message, directive, price, coinage) + } + else{ + const directive = splitMessage[1]; + const username = splitMessage.slice(2, splitMessage.length - 1).join(' '); + const price = splitMessage[2] + const coinage = splitMessage[splitMessage.length - 1]; + + if (command == '!dice' && directive == 'challenge') + diceChallenge(message, username, coinage); + else if (command == '!dice' && directive == 'accept') + diceAccept(message, username, coinage); + else if (command == '!send' && directive == 'user') + sendUser(message, username, coinage); + } } isReady = true; }); @@ -127,6 +139,7 @@ bot.on('message', (message) => { function init() { const token = parseToken(); parseLedger(); + parseSupply(); parsePrice(); parseTime(); meatState(); @@ -179,27 +192,49 @@ function parseSupply(message) { } function printMarket(message) { -parseSupply(); var response = '```glsl\nGET YER FLESH HERE!\n'; -// create array of unsorted users -var buyerData = market[0]; -var sellerData = market[1]; -if (Object.keys(sellerData).length > 0) { - response += 'Seller\tQUANTITY\tPRICE\n' - Object.keys(sellerData).forEach(function(key) { - response += sellerData[key].username + '\t' + sellerData[key].quantity + '\t' + sellerData[key].price + '\n'; +if (Object.keys(market[1]).length > 0) { + response += 'SELLERS'.padEnd(18,'-') + '+' + 'QUANTITY'.padStart(20,'-') + '+' + 'PRICE'.padStart(14,'-') + '+\n'; + Object.keys(market[1]).forEach(function(key) { + response += market[1][key].username.padEnd(18,' ') + '|' + String(market[1][key].quantity).padStart(20,' ') + '|' + String(market[1][key].price).padStart(14,' ') + '|\n'; }); } -if (Object.keys(buyerData).length > 0) { - response += 'BUYER\tQUANTITY\tPRICE\n' - Object.keys(buyerData).forEach(function(key) { - response += buyerData[key].username + '\t' + buyerData[key].quantity + '\t' + buyerData[key].price + '\n'; +if (Object.keys(market[0]).length > 0) { + response += 'BUYERS'.padEnd(18,'-') + '+' + 'QUANTITY'.padStart(20,'-') + '+' + 'PRICE'.padStart(14,'-') + '+\n'; + Object.keys(market[0]).forEach(function(key) { + response += market[0][key].username.padEnd(18,' ') + '|' + String(market[0][key].quantity).padStart(20,' ') + '|' + String(market[0][key].price).padStart(14,' ') + '|\n'; }); } response += '```'; message.channel.send(response); } +function saveMarketData(path) { + var postingDataBuffer = ''; + var postingData; + for (var i = market.length - 1; i >= 0; i--) { + if (Object.keys(market[i]).length > 0) { + Object.keys(market[i]).forEach(function(key) { + postingData = market[i][key]; + postingDataBuffer += key; + postingDataBuffer += '\t'; + postingDataBuffer += postingData.username; + postingDataBuffer += '\t'; + postingDataBuffer += (i ? 'S' : 'B'); + postingDataBuffer += '\t'; + postingDataBuffer += parseFloat(postingData.price).toFixed(4); + postingDataBuffer += '\t'; + postingDataBuffer += parseFloat(postingData.quantity).toFixed(4); + postingDataBuffer += '\n'; + }); + + postingDataBuffer = postingDataBuffer.substring(0, postingDataBuffer.length); + fs.writeFileSync(path + "\\data\\meatMarket.txt", postingDataBuffer, function(err) { + }); + } + } +} + function parseLedger() { var path = process.cwd(); var buffer = fs.readFileSync(path + "\\data\\ledger.txt"); @@ -398,6 +433,7 @@ process.on('SIGINT', function() { saveUserData(path); savePriceData(path); saveTimeData(path); + saveMarketData(path); process.exit(); }); @@ -406,6 +442,7 @@ process.on('uncaughtException', function(err) { var path = process.cwd(); saveUserData(path); savePriceData(path); + saveMarketData(path); process.exit(); }); @@ -430,6 +467,7 @@ function help(message) { response += '\n\t\t!mine stop'; response += '\n\t\t!buy '; response += '\n\t\t!sell '; + response += '\n\t\t!post ' response += '\n\n\t#gamble'; response += '\n\t\t!flip rules'; response += '\n\t\t!flip '; @@ -512,7 +550,7 @@ function printPrice(message) { } function printFee(message) { - const response = '```glsl\nThe fee to buy/sell MeatCoin is 5 %.```'; + const response = '```glsl\nThe fee to buy/sell MeatCoin is 5 %.\nThere is NO FEE for posting on the meatMarket, but you will lose 7% for adjustments! (incl. cancellations)```'; message.channel.send(response); } @@ -908,7 +946,7 @@ function flip(message, side, coinage) { message.channel.send(response); } -function post(message, command, coinage){ +function post(message, command, price, coinage){ const id = message.member.user.id; var response = '<@' + id + '>'; @@ -923,12 +961,206 @@ function post(message, command, coinage){ var userData = ledger[id]; userData.channel = message.channel; + // check valid number + if (isNaN(coinage) || isNaN(price)) { + message.channel.send(response + ', that is an invalid number.'); + return; + } + + amount = parseFloat(coinage); + floatPrice = parseFloat(price); + if (command == 'buy'){ - parseSupply(); - response += ', is buyin\' who\'s sellin\'?'; + // check valid amount + if (amount <= 0.0) { + message.channel.send(response + ', that is an invalid amount.'); + return; + } + // check user funds + const value = (amount * floatPrice); + if (userData.gold < value) { + message.channel.send(response + ', you have insufficient funds. ' + value + ' gold is required.'); + return; + } + //check that the user is not already buying + if (id in market[0]){ + response += ', you can\'t create a buy posting, you are already buying. Use !post adjust.\n'; + message.channel.send(response); + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tB ' + String(market[0][id].quantity).padStart(10,' ') + '\t@' + String(market[0][id].price).padStart(8,' ') + '\n'); + return; + } + //check that the user is not also selling + if (id in market[1]){ + response += ', you can\'t create a buy posting, you are already selling. ya dingus!\n'; + message.channel.send(response); + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tS ' + String(market[1][id].quantity).padStart(10,' ') + '\t@' + String(market[1][id].price).padStart(8,' ') + '\n'); + return; + } + response += ', is buyin\' who\'s sellin\'?'; + + // ledger + userData.gold -= value; + var userData = ledger[id]; + market[0][id] = { + username: ledger[id].username, + price: price, + quantity: coinage + }; + + // history not sure if we want this in history here, probably in processMarket().... + // if (userData.history.length > 9) + // userData.history.pop(); + // userData.history.unshift('b' + ',' + amount.toFixed(2) + ',' + price.toFixed(2)); + + // statistics + //volume.bought += amount; } else if (command == 'sell'){ + // check valid amount + if (amount <= 0.0) { + message.channel.send(response + ', that is an invalid amount.'); + return; + } + // check user funds + if (userData.meatCoin < amount) { + message.channel.send(response + ', you do not have that much MeatCoin.'); + return; + } + // check user funds + const value = amount; + if (userData.meatCoin < value) { + message.channel.send(response + ', you have insufficient meatCoin. ' + value + ' meatCoin is required.'); + return; + } + //check that the user is not already buying + if (id in market[0]){ + response += ', you can\'t create a sell posting, you are already buying. ya dingus!\n'; + message.channel.send(response); + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tB ' + String(market[0][id].quantity).padStart(10,' ') + '\t@' + String(market[0][id].price).padStart(8,' ') + '\n'); + return; + } + //check that the user is not also selling + if (id in market[1]){ + response += ', you can\'t create a sell posting, you are already selling. Use !post adjust.\n'; + message.channel.send(response); + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tS ' + String(market[1][id].quantity).padStart(10,' ') + '\t@' + String(market[1][id].price).padStart(8,' ') + '\n'); + return; + } response += ', is looking to unload some meat, get it while it\'s hot'; + + // ledger + userData.meatCoin -= value; + market[1][id] = { + username: ledger[id].username, + price: price, + quantity: coinage + }; + + // history not sure if we want this in history here, probably in processMarket().... + // if (userData.history.length > 9) + // userData.history.pop(); + // userData.history.unshift('b' + ',' + amount.toFixed(2) + ',' + price.toFixed(2)); + + // statistics + //volume.bought += amount; + } + else if (command == 'adjust'){ + //adjusting a buy + if (id in market[0]){ + //remvoing post + if (amount <= 0.0 || floatPrice <= 0.0){ + var postingData = market[0][id]; + var refund = (1.0-postFee) * (postingData.price * postingData.quantity); + delete market[0][id]; + response += ', your posting has been removed\n' + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + ledger[id].gold += refund; + message.author.send('Has been cancelled. You have been credited ' + refund + ' gold\n'); + } + else{ + var postingData = market[0][id]; + var xactFee = (postFee) * (postingData.price * postingData.quantity); + var difference = (postingData.price * postingData.quantity) - (amount * floatPrice); + var refund = difference - xactFee; + if(refund >= 0){ + ledger[id].gold += refund; + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + postingData.price = price; + postingData.quantity = coinage; + response += ', your posting has been updated'; + message.author.send('Has been updated to:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + message.author.send('For a refund of ' + Math.abs(refund).toFixed(2) + ' gold'); + } + else{ + //need to pay more check if you have enough + if(ledger[id].gold < Math.abs(refund)){ + response += ', you dont have enough money to alter your posting, you need ' + Math.abs(refund) + ' gold.'; + message.channel.send(response); + return; + } + //take the extra + else{ + ledger[id].gold += refund; + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + postingData.price = price; + postingData.quantity = coinage; + response += ', your posting has been updated'; + message.author.send('Has been updated to:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + message.author.send('For a price of ' + Math.abs(refund).toFixed(2) + ' gold'); + } + } + } + } + //adjusting a sell + else if (id in market[1]){ + //removing post + if (amount <= 0.0 || floatPrice <= 0.0){ + var postingData = market[1][id]; + var refund = (1.0-postFee) * (postingData.quantity); + delete market[1][id]; + response += ', your posting has been removed\n' + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tS ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + ledger[id].meatCoin += refund; + message.author.send('Has been cancelled. You have been credited ' + refund + ' meatCoin\n'); + } + else{ + var postingData = market[1][id]; + var xactFee = (postFee) * postingData.quantity; + var difference = postingData.quantity - amount; + var refund = difference - xactFee; + if(refund >= 0){ + ledger[id].meatCoin += refund; + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tS ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + postingData.price = price; + postingData.quantity = coinage; + response += ', your posting has been updated'; + message.author.send('Has been updated to:\n' + id.padStart(18,'0') + '\tS ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + message.author.send('For a refund of ' + Math.abs(refund).toFixed(2) + ' meatCoin'); + } + else{ + //need to pay more check if you have enough + if(ledger[id].meatCoin < Math.abs(refund)){ + response += ', you dont have enough money to alter your posting, you need ' + Math.abs(meatCoin) + ' meatCoin.'; + message.channel.send(response); + return; + } + //take the extra + else{ + ledger[id].meatCoin += refund; + message.author.send('Your posting:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + postingData.price = price; + postingData.quantity = coinage; + response += ', your posting has been updated'; + message.author.send('Has been updated to:\n' + id.padStart(18,'0') + '\tB ' + String(postingData.quantity).padStart(10,' ') + '\t@' + String(postingData.price).padStart(8,' ') + '\n'); + message.author.send('For a price of ' + Math.abs(refund).toFixed(2) + ' meatCoin'); + } + } + } + } + else{ + response += ', you have no postings!'; + } + } else{ response += ', ' + command + ' is not a valid command. ya dingus!'; From 4cda5714f3cd4584572c8ff1a05399bb4c974c25 Mon Sep 17 00:00:00 2001 From: wstathis Date: Fri, 20 Mar 2020 18:30:19 -0400 Subject: [PATCH 4/6] added transaction functionality, the meat market is open! --- index.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 214d678..f06f5c0 100644 --- a/index.js +++ b/index.js @@ -99,6 +99,8 @@ bot.on('message', (message) => { diceRules(message); else if (command == '!flip' && directive == 'rules') flipRules(message); + else if (command == '!market' && directive == 'rules') + marketRules(message); } else if (splitMessage.length == 3) { const command = splitMessage[0]; @@ -115,8 +117,8 @@ bot.on('message', (message) => { const command = splitMessage[0]; if(splitMessage.length == 4 && command == '!post'){ const directive = splitMessage[1]; - const price = splitMessage[2] - const coinage = splitMessage[3]; + const coinage = splitMessage[2]; + const price = splitMessage[3] post(message, directive, price, coinage) } else{ @@ -209,6 +211,11 @@ response += '```'; message.channel.send(response); } +function marketRules(message){ + var response = '```!post buy/sell and set quantity then price, buyers lose out on price differential so be careful!\nNo fee for posting/matching, but 7% per adjustment incl. cancellations.```'; + message.channel.send(response); +} + function saveMarketData(path) { var postingDataBuffer = ''; var postingData; @@ -468,6 +475,8 @@ function help(message) { response += '\n\t\t!buy '; response += '\n\t\t!sell '; response += '\n\t\t!post ' + response += '\n\t\t!market' + response += '\n\t\t!market rules' response += '\n\n\t#gamble'; response += '\n\t\t!flip rules'; response += '\n\t\t!flip '; @@ -1003,8 +1012,8 @@ function post(message, command, price, coinage){ var userData = ledger[id]; market[0][id] = { username: ledger[id].username, - price: price, - quantity: coinage + price: parseFloat(price), + quantity: parseFloat(coinage) }; // history not sure if we want this in history here, probably in processMarket().... @@ -1052,8 +1061,8 @@ function post(message, command, price, coinage){ userData.meatCoin -= value; market[1][id] = { username: ledger[id].username, - price: price, - quantity: coinage + price: parseFloat(price), + quantity: parseFloat(coinage) }; // history not sure if we want this in history here, probably in processMarket().... @@ -1167,6 +1176,80 @@ function post(message, command, price, coinage){ } message.channel.send(response); + processMarket(); +} + +function processMarket(){ + var buyers = market[0]; + var sellers = market[1]; + //iterate through buyers + if (Object.keys(buyers).length > 0) { + Object.keys(buyers).forEach(function(Bkey) { + //iterate through sellers + if (Object.keys(sellers).length > 0) { + Object.keys(sellers).forEach(function(Skey) { + // check if we can make a deal + if(buyers[Bkey].price >= sellers[Skey].price){ //price match + var negPrice = buyers[Bkey].price; //probably won't happen too much, but good to be safe, buyers lose + //Buying > Selling + if(buyers[Bkey].quantity > sellers[Skey].quantity){ + var buyerData = ledger[Bkey]; + var sellerData = ledger[Skey]; + //credit both parties as they have already paid + buyerData.meatCoin += parseFloat(sellers[Skey].quantity); + volume.bought += parseFloat(sellers[Skey].quantity); + sellerData.gold += sellers[Skey].quantity * negPrice; + if (buyerData.history.length > 9) + buyerData.history.pop(); + buyerData.history.unshift('b' + ',' + parseFloat(sellers[Skey].quantity).toFixed(2) + ',' + negPrice.toFixed(2)); + if (sellerData.history.length > 9) + sellerData.history.pop(); + sellerData.history.unshift('s' + ',' + parseFloat(sellers[Skey].quantity).toFixed(2) + ',' + negPrice.toFixed(2)); + //decrease buyer quantity by amount sold, remove seller posting + buyers[Bkey].quantity -= sellers[Skey].quantity; + delete sellers[Skey]; + } + //Selling > Buying + else if (sellers[Skey].quantity > buyers[Bkey].quantity){ + var buyerData = ledger[Bkey]; + var sellerData = ledger[Skey]; + //credit both parties as they have already paid + buyerData.meatCoin += parseFloat(buyers[Bkey].quantity); + volume.bought += parseFloat(buyers[Bkey].quantity); + sellerData.gold += buyers[Bkey].quantity * negPrice; + if (buyerData.history.length > 9) + buyerData.history.pop(); + buyerData.history.unshift('b' + ',' + parseFloat(buyers[Bkey].quantity).toFixed(2) + ',' + negPrice.toFixed(2)); + if (sellerData.history.length > 9) + sellerData.history.pop(); + sellerData.history.unshift('s' + ',' + parseFloat(buyers[Bkey].quantity).toFixed(2) + ',' + negPrice.toFixed(2)); + //decrease seller quantity by amount sold, remove buyer posting + sellers[Skey].quantity -= buyers[Bkey].quantity; + delete buyers[Bkey]; + } + //equal + else{ + var buyerData = ledger[Bkey]; + var sellerData = ledger[Skey]; + //credit both parties as they have already paid + buyerData.meatCoin += parseFloat(buyers[Bkey].quantity); + volume.bought += parseFloat(buyers[Bkey].quantity); + sellerData.gold += buyers[Bkey].quantity * negPrice; + if (buyerData.history.length > 9) + buyerData.history.pop(); + buyerData.history.unshift('b' + ',' + parseFloat(buyers[Bkey].quantity).toFixed(2) + ',' + negPrice.toFixed(2)); + if (sellerData.history.length > 9) + sellerData.history.pop(); + sellerData.history.unshift('s' + ',' + parseFloat(buyers[Bkey].quantity).toFixed(2) + ',' + negPrice.toFixed(2)); + //decrease seller quantity by amount sold, remove buyer posting + delete sellers[Skey]; + delete buyers[Bkey]; + } + } + }) + } + }); + } } function hallOfFame(message) { From bb8048d430aae0ac5434a700cb78963439b3108b Mon Sep 17 00:00:00 2001 From: wstathis Date: Fri, 20 Mar 2020 18:33:58 -0400 Subject: [PATCH 5/6] help text fix --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f06f5c0..af3c4f9 100644 --- a/index.js +++ b/index.js @@ -474,7 +474,7 @@ function help(message) { response += '\n\t\t!mine stop'; response += '\n\t\t!buy '; response += '\n\t\t!sell '; - response += '\n\t\t!post ' + response += '\n\t\t!post ' response += '\n\t\t!market' response += '\n\t\t!market rules' response += '\n\n\t#gamble'; From f94efc390dce55124fb2498b477bca73f84a5af3 Mon Sep 17 00:00:00 2001 From: wstathis Date: Sat, 21 Mar 2020 17:00:06 -0400 Subject: [PATCH 6/6] update to include volume.traded, removed comments --- index.js | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index af3c4f9..74b5cc0 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,8 @@ var meatTotalLast = 0; var volume = { bought: 0.0, sold: 0.0, - gambled: 0.0 + gambled: 0.0, + traded: 0.0 }; var miners = 0; var registered = 0; @@ -567,7 +568,8 @@ function printVolume(message) { var response = '```glsl\nTrading Session\n' response += '\t' + volume.bought.toFixed(2) + ' MeatCoin bought.\n'; response += '\t' + volume.sold.toFixed(2) + ' MeatCoin sold.\n'; - response += '\t' + volume.gambled.toFixed(2) + ' MeatCoin gambled.```'; + response += '\t' + volume.gambled.toFixed(2) + ' MeatCoin gambled.\n'; + response += '\t' + volume.traded.toFixed(2) + ' MeatCoin traded.```'; message.channel.send(response); } @@ -1015,14 +1017,6 @@ function post(message, command, price, coinage){ price: parseFloat(price), quantity: parseFloat(coinage) }; - - // history not sure if we want this in history here, probably in processMarket().... - // if (userData.history.length > 9) - // userData.history.pop(); - // userData.history.unshift('b' + ',' + amount.toFixed(2) + ',' + price.toFixed(2)); - - // statistics - //volume.bought += amount; } else if (command == 'sell'){ // check valid amount @@ -1064,14 +1058,6 @@ function post(message, command, price, coinage){ price: parseFloat(price), quantity: parseFloat(coinage) }; - - // history not sure if we want this in history here, probably in processMarket().... - // if (userData.history.length > 9) - // userData.history.pop(); - // userData.history.unshift('b' + ',' + amount.toFixed(2) + ',' + price.toFixed(2)); - - // statistics - //volume.bought += amount; } else if (command == 'adjust'){ //adjusting a buy @@ -1197,7 +1183,7 @@ function processMarket(){ var sellerData = ledger[Skey]; //credit both parties as they have already paid buyerData.meatCoin += parseFloat(sellers[Skey].quantity); - volume.bought += parseFloat(sellers[Skey].quantity); + volume.traded += parseFloat(sellers[Skey].quantity); sellerData.gold += sellers[Skey].quantity * negPrice; if (buyerData.history.length > 9) buyerData.history.pop(); @@ -1215,7 +1201,7 @@ function processMarket(){ var sellerData = ledger[Skey]; //credit both parties as they have already paid buyerData.meatCoin += parseFloat(buyers[Bkey].quantity); - volume.bought += parseFloat(buyers[Bkey].quantity); + volume.traded += parseFloat(buyers[Bkey].quantity); sellerData.gold += buyers[Bkey].quantity * negPrice; if (buyerData.history.length > 9) buyerData.history.pop(); @@ -1233,7 +1219,7 @@ function processMarket(){ var sellerData = ledger[Skey]; //credit both parties as they have already paid buyerData.meatCoin += parseFloat(buyers[Bkey].quantity); - volume.bought += parseFloat(buyers[Bkey].quantity); + volume.traded += parseFloat(buyers[Bkey].quantity); sellerData.gold += buyers[Bkey].quantity * negPrice; if (buyerData.history.length > 9) buyerData.history.pop();