function checkVgoOffers() {
	// Get the open offers
	tradeHandler.retrieveOpenVgoOffers((err, accepted, failed) => {
		if (err) {
			console.error(err);
		}
		else {
			// Deal with the accepted VGO offers here
			accepted.forEach(function (acceptedOffer) {
				// First retrieve the offer from the database.
				database.getVgoOfferById(acceptedOffer.id, (err, localOffer) => {
					if (err) {
						console.error(err);
					}
					else {
						// We separate the steamid of the account, and the
						// custom steamid if present
						let realSteamid = localOffer.steamid;
						let customSteamid = localOffer.custom;
						
						// No sender items - must be a successful sell
						if (!acceptedOffer.sender.items.length) {
							let keyArr = acceptedOffer.recipient.items;
							let keys = acceptedOffer.recipient.items.length;
							let value = acceptedOffer.recipient.items.length * config.rates.vgoSellPrice;
							
							// Delete the open offer
							database.deleteOpenVgoOffer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									// Insert the transaction into the offer history
									database.addVgoOfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Sell', customSteamid, err => {
										if (err) {
											console.error(err);
										}
									});
									// Unlike Steam, up-to-date assetids are provided in the 
									// offer receipt, so we'll use them now.
									keyArr.forEach(function (key) {
										database.insertVgoKey(key.id, err => {
											if (err) {
												console.error(err);
											} else {
												updateInStock();
											}
										});
									});
									console.log(`[SUCCESS SELL] ${realSteamid} sold ${keys} VGO keys ($${value.toFixed(2)})`);
									emitAlertToClient(realSteamid, `Successfully sold ${keys} VGO keys ($${value.toFixed(2)})`);
									// Update the balance of the real account
									database.updateBalanceBySteamId(realSteamid, value, err => {
										if (err) {
											console.error(err);
										}
										else {
											emitBalance(realSteamid, err => {
												if (err) {
													console.error(err);
												}
											});
										}
									});
								}
							});
						}
						// Sender items - must be a successful buy
						else if (acceptedOffer.sender.items.length) {
							let keyArr = acceptedOffer.sender.items;
							let keys = acceptedOffer.sender.items.length;
							let value = acceptedOffer.sender.items.length * config.rates.vgoBuyPrice;
							console.log(`[SUCCESS BUY] ${realSteamid} - ${keys} VGO - $${value.toFixed(2)}`);
							emitAlertToClient(realSteamid, `VGO buy offer completed!`);
							
							// Add the offer to history
							database.addVgoOfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Buy', customSteamid, err => {
								if (err) {
									console.error(err);
								}
							});
							// Delete the offer from open
							database.deleteOpenVgoOffer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
								}
							});
							
							// We can delete the keys from the database - they
							// have been successfully traded away
							keyArr.forEach(function (key) {
								database.deleteVgoKey(key.id, err => {
									if (err) {
										console.error(err);
									}
								});
							});
						}
					}
				});
			});
			// Deal with the failed VGO offers here
			failed.forEach(function (failedOffer) {
				// First retrieve the offer from the database.
				database.getVgoOfferById(failedOffer.id, (err, localOffer) => {
					if (err) {
						console.error(err);
					}
					else {
						let realSteamid = localOffer.steamid;
						let customSteamid = localOffer.custom;
						// No sender items - must be a failed sell offer
						if (!failedOffer.sender.items.length) {
							console.log(`[CANCELLED SELL] ${realSteamid} - ${failedOffer.recipient.items.length} VGO - $${failedOffer.recipient.items.length * config.rates.vgoSellPrice}`);
							emitAlertToClient(realSteamid, `VGO Sell offer cancelled`);
							
							// Delete the open offer from the database
							database.deleteOpenVgoOffer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
								}
							});
						}
						// Sender items - must be a failed buy offer
						else if (failedOffer.sender.items.length) {
							let keyArr = failedOffer.sender.items;
							let keys = failedOffer.sender.items.length;
							let value = failedOffer.sender.items.length * config.rates.vgoBuyPrice;
							console.log(`[CANCELLED BUY] ${realSteamid} - ${keys} VGO - $${value}`);
							emitAlertToClient(realSteamid, `VGO Buy offer cancelled`);
							
							// This adds the keys back to the site
							// stock database, tracked by the
							// 'intrade' column
							keyArr.forEach(function (key) {
								database.revertVgoInTrade(key.id, err => {
									if (err) {
										console.error(err);
									}
									else {
										updateInStock();
									}
								});
							});
							// Delete the open offer from database
							database.deleteOpenVgoOffer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									// Refund the user
									database.updateBalanceBySteamId(realSteamid, value, err => {
										if (err) {
											console.error(err);
										}
										else {
											// Update balance so they don't have to
											// refresh the page
											emitBalance(realSteamid, err => {
												if (err) {
													console.error(err);
												}
											});
										}
									});
								}
							});
						}
					}
				});
			});
		}
	});
}
// This function periodically checks open tf2 offers.
This function periodically checks open tf2 offers.
This function periodically checks open tf2 offers .
function checkVgoOffers() {
	tradeHandler.retrieveOpenVgoOffers((err, accepted, failed) => {
		if (err) {
			console.error(err);
		}
		else {
			accepted.forEach(function (acceptedOffer) {
				database.getVgoOfferById(acceptedOffer.id, (err, localOffer) => {
					if (err) {
						console.error(err);
					}
					else {
						let realSteamid = localOffer.steamid;
						let customSteamid = localOffer.custom;
						if (!acceptedOffer.sender.items.length) {
							let keyArr = acceptedOffer.recipient.items;
							let keys = acceptedOffer.recipient.items.length;
							let value = acceptedOffer.recipient.items.length * config.rates.vgoSellPrice;
							database.deleteOpenVgoOffer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									database.addVgoOfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Sell', customSteamid, err => {
										if (err) {
											console.error(err);
										}
									});
									keyArr.forEach(function (key) {
										database.insertVgoKey(key.id, err => {
											if (err) {
												console.error(err);
											} else {
												updateInStock();
											}
										});
									});
									console.log(`[SUCCESS SELL] ${realSteamid} sold ${keys} VGO keys ($${value.toFixed(2)})`);
									emitAlertToClient(realSteamid, `Successfully sold ${keys} VGO keys ($${value.toFixed(2)})`);
									database.updateBalanceBySteamId(realSteamid, value, err => {
										if (err) {
											console.error(err);
										}
										else {
											emitBalance(realSteamid, err => {
												if (err) {
													console.error(err);
												}
											});
										}
									});
								}
							});
						}
						else if (acceptedOffer.sender.items.length) {
							let keyArr = acceptedOffer.sender.items;
							let keys = acceptedOffer.sender.items.length;
							let value = acceptedOffer.sender.items.length * config.rates.vgoBuyPrice;
							console.log(`[SUCCESS BUY] ${realSteamid} - ${keys} VGO - $${value.toFixed(2)}`);
							emitAlertToClient(realSteamid, `VGO buy offer completed!`);
							database.addVgoOfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Buy', customSteamid, err => {
								if (err) {
									console.error(err);
								}
							});
							database.deleteOpenVgoOffer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
								}
							});
							keyArr.forEach(function (key) {
								database.deleteVgoKey(key.id, err => {
									if (err) {
										console.error(err);
									}
								});
							});
						}
					}
				});
			});
			failed.forEach(function (failedOffer) {
				database.getVgoOfferById(failedOffer.id, (err, localOffer) => {
					if (err) {
						console.error(err);
					}
					else {
						let realSteamid = localOffer.steamid;
						let customSteamid = localOffer.custom;
						if (!failedOffer.sender.items.length) {
							console.log(`[CANCELLED SELL] ${realSteamid} - ${failedOffer.recipient.items.length} VGO - $${failedOffer.recipient.items.length * config.rates.vgoSellPrice}`);
							emitAlertToClient(realSteamid, `VGO Sell offer cancelled`);
							 from the database
							database.deleteOpenVgoOffer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
								}
							});
						}
						else if (failedOffer.sender.items.length) {
							let keyArr = failedOffer.sender.items;
							let keys = failedOffer.sender.items.length;
							let value = failedOffer.sender.items.length * config.rates.vgoBuyPrice;
							console.log(`[CANCELLED BUY] ${realSteamid} - ${keys} VGO - $${value}`);
							emitAlertToClient(realSteamid, `VGO Buy offer cancelled`);
							keyArr.forEach(function (key) {
								database.revertVgoInTrade(key.id, err => {
									if (err) {
										console.error(err);
									}
									else {
										updateInStock();
									}
								});
							});
							 from database
							database.deleteOpenVgoOffer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									database.updateBalanceBySteamId(realSteamid, value, err => {
										if (err) {
											console.error(err);
										}
										else {
											emitBalance(realSteamid, err => {
												if (err) {
													console.error(err);
												}
											});
										}
									});
								}
							});
						}
					}
				});
			});
		}
	});
}
function checkVgoOffers ( ) { tradeHandler . retrieveOpenVgoOffers ( ( err , accepted , failed ) => { if ( err ) { console . error ( err ) ; } else { accepted . forEach ( function ( acceptedOffer ) { database . getVgoOfferById ( acceptedOffer . id , ( err , localOffer ) => { if ( err ) { console . error ( err ) ; } else { let realSteamid = localOffer . steamid ; let customSteamid = localOffer . custom ; if ( ! acceptedOffer . sender . items . length ) { let keyArr = acceptedOffer . recipient . items ; let keys = acceptedOffer . recipient . items . length ; let value = acceptedOffer . recipient . items . length * config . rates . vgoSellPrice ; database . deleteOpenVgoOffer ( acceptedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; database . addVgoOfferHistory ( realSteamid , acceptedOffer . id , keys , value , utils . getIsoString ( ) , 'Sell' , customSteamid , err => { if ( err ) { console . error ( err ) ; } } ) ; keyArr . forEach ( function ( key ) { database . insertVgoKey ( key . id , err => { if ( err ) { console . error ( err ) ; } else { updateInStock ( ) ; } } ) ; } ) ; console . log ( ` ${ realSteamid } ${ keys } ${ value . toFixed ( 2 ) } ` ) ; emitAlertToClient ( realSteamid , ` ${ keys } ${ value . toFixed ( 2 ) } ` ) ; database . updateBalanceBySteamId ( realSteamid , value , err => { if ( err ) { console . error ( err ) ; } else { emitBalance ( realSteamid , err => { if ( err ) { console . error ( err ) ; } } ) ; } } ) ; } } ) ; } else if ( acceptedOffer . sender . items . length ) { let keyArr = acceptedOffer . sender . items ; let keys = acceptedOffer . sender . items . length ; let value = acceptedOffer . sender . items . length * config . rates . vgoBuyPrice ; console . log ( ` ${ realSteamid } ${ keys } ${ value . toFixed ( 2 ) } ` ) ; emitAlertToClient ( realSteamid , ` ` ) ; database . addVgoOfferHistory ( realSteamid , acceptedOffer . id , keys , value , utils . getIsoString ( ) , 'Buy' , customSteamid , err => { if ( err ) { console . error ( err ) ; } } ) ; database . deleteOpenVgoOffer ( acceptedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; } } ) ; keyArr . forEach ( function ( key ) { database . deleteVgoKey ( key . id , err => { if ( err ) { console . error ( err ) ; } } ) ; } ) ; } } } ) ; } ) ; failed . forEach ( function ( failedOffer ) { database . getVgoOfferById ( failedOffer . id , ( err , localOffer ) => { if ( err ) { console . error ( err ) ; } else { let realSteamid = localOffer . steamid ; let customSteamid = localOffer . custom ; if ( ! failedOffer . sender . items . length ) { console . log ( ` ${ realSteamid } ${ failedOffer . recipient . items . length } ${ failedOffer . recipient . items . length * config . rates . vgoSellPrice } ` ) ; emitAlertToClient ( realSteamid , ` ` ) ; database . deleteOpenVgoOffer ( failedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; } } ) ; } else if ( failedOffer . sender . items . length ) { let keyArr = failedOffer . sender . items ; let keys = failedOffer . sender . items . length ; let value = failedOffer . sender . items . length * config . rates . vgoBuyPrice ; console . log ( ` ${ realSteamid } ${ keys } ${ value } ` ) ; emitAlertToClient ( realSteamid , ` ` ) ; keyArr . forEach ( function ( key ) { database . revertVgoInTrade ( key . id , err => { if ( err ) { console . error ( err ) ; } else { updateInStock ( ) ; } } ) ; } ) ; database . deleteOpenVgoOffer ( failedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; database . updateBalanceBySteamId ( realSteamid , value , err => { if ( err ) { console . error ( err ) ; } else { emitBalance ( realSteamid , err => { if ( err ) { console . error ( err ) ; } } ) ; } } ) ; } } ) ; } } } ) ; } ) ; } } ) ; }
This function periodically checks open tf2 offers.
This function periodically checks open tf2 offers .
// Get the open offers // Deal with the accepted VGO offers here // First retrieve the offer from the database. // We separate the steamid of the account, and the // custom steamid if present // No sender items - must be a successful sell // Delete the open offer // Insert the transaction into the offer history // Unlike Steam, up-to-date assetids are provided in the  // offer receipt, so we'll use them now. // Update the balance of the real account // Sender items - must be a successful buy // Add the offer to history // Delete the offer from open // We can delete the keys from the database - they // have been successfully traded away // Deal with the failed VGO offers here // First retrieve the offer from the database. // No sender items - must be a failed sell offer // Delete the open offer from the database // Sender items - must be a failed buy offer // This adds the keys back to the site // stock database, tracked by the // 'intrade' column // Delete the open offer from database // Refund the user // Update balance so they don't have to // refresh the page