function checkTf2Offers() {
	// Retrieve our list of open offers from the database.
	tradeHandler.retrieveOpenTf2Offers((err, accepted, failed) => {
		if (err) {
			console.error(err);
		}
		else {
			// We deal with the successful offers here
			accepted.forEach(function (acceptedOffer) {
				database.getTf2OfferById(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 items to give - must be a successful sell offer
						if (!acceptedOffer.itemsToGive.length) {

							// The number of keys in the trade
							let keys = acceptedOffer.itemsToReceive.length;
							// The value of the keys in the trade
							let value = acceptedOffer.itemsToReceive.length * config.rates.tf2SellPrice;

							// First delete it from open offers
							database.deleteOpenTf2Offer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									// If it can't be deleted, throw an error
									assert(result.affectedRows, `Offer not deleted from open offers`);

									// Add the transaction to history
									database.addTf2OfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Sell', customSteamid, err => {
										if (err) {
											console.error(err);
										}
									});
									console.log(`[SUCCESS SELL] ${realSteamid} sold ${keys} TF2 keys ($${value.toFixed(2)})`);
									// Here's where this function comes in handy. We can emit 
									// to a client outside the socket session, which we are.
									emitAlertToClient(realSteamid, `Successfully sold ${keys} TF2 keys ($${value.toFixed(2)})`);

									// The sell was successful, we can update their balance
									database.updateBalanceBySteamId(realSteamid, value, err => {
										if (err) {
											console.error(err);
										}
										else {
											// Emit the new balance so the client doesn't
											// need to refresh the page.
											emitBalance(realSteamid, err => {
												if (err) {
													console.error(err);
												}
											});
										}
									});
								}
							});
						}
						// Items to give - must be a successful buy offer
						else if (acceptedOffer.itemsToGive.length) {
							// The array of keys that were sent
							let keyArr = acceptedOffer.itemsToGive;
							// The number of keys sent
							let keys = acceptedOffer.itemsToGive.length;
							// The site value of keys sent
							let value = acceptedOffer.itemsToGive.length * config.rates.tf2BuyPrice;

							console.log(`[SUCCESS BUY] ${realSteamid} - ${keys} TF2 - $${value.toFixed(2)}`);
							emitAlertToClient(realSteamid, `TF2 Buy Offer Completed!`);

							// First, delete the open offer from database
							database.deleteOpenTf2Offer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									// If it's not deleted, throw error
									assert(result.affectedRows, `Offer not deleted from open offers`);

									// Add it to the history
									database.addTf2OfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Buy', customSteamid, err => {
										if (err) {
											console.error(err);
										}
									});

									// Delete the keys database table, the
									// keys have successfully been traded away
									keyArr.forEach(function (key) {
										database.deleteTf2Key(key.assetid, err => {
											if (err) {
												console.error(err);
											}
										});
									});
								}
							});
						}
					}
				});
				
			});
			// We deal with the failed offers here
			failed.forEach(function (failedOffer) {
				database.getTf2OfferById(failedOffer.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 items to give - must be a failed sell offer
						if (!failedOffer.itemsToGive.length) {
							let keys = failedOffer.itemsToReceive.length;
							let value = failedOffer.itemsToReceive.length * config.rates.tf2SellPrice;
							console.log(`[CANCELLED SELL] ${realSteamid} - ${keys} TF2 - $${value.toFixed(2)}`);
							emitAlertToClient(realSteamid, `TF2 Sell offer cancelled`);

							// Don't to do anything with a failed sell other than 
							// delete it from our open offers table.
							database.deleteOpenTf2Offer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
								}
							});
						}
						// Items to give - must be a failed buy offer
						else if (failedOffer.itemsToGive.length) {
							let keyArr = failedOffer.itemsToGive;
							let keys = failedOffer.itemsToGive.length;
							let value = failedOffer.itemsToGive.length * config.rates.tf2BuyPrice;
							console.log(`[CANCELLED BUY] ${realSteamid} - ${keys} TF2 - $${value}`);
							emitAlertToClient(realSteamid, `TF2 Buy offer cancelled`);

							// For a failed buy offer, we must refund the user, delete
							// the offer, and restore the keys in stock.

							// Delete the offer from the database
							database.deleteOpenTf2Offer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);

									// Remove the keys from 'intrade' in the database,
									// allowing them to be tradable again.
									keyArr.forEach(function (key) {
										database.revertTf2InTrade(key.assetid, err => {
											if (err) {
												console.error(err);
											}
											else {
												updateInStock();
											}
										});
									});
									// Refund the user
									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.
This function periodically checks open TF2 offers .
function checkTf2Offers() {
	tradeHandler.retrieveOpenTf2Offers((err, accepted, failed) => {
		if (err) {
			console.error(err);
		}
		else {
			accepted.forEach(function (acceptedOffer) {
				database.getTf2OfferById(acceptedOffer.id, (err, localOffer) => {
					if (err) {
						console.error(err);
					}
					else {
						let realSteamid = localOffer.steamid;
						let customSteamid = localOffer.custom;
						if (!acceptedOffer.itemsToGive.length) {
							let keys = acceptedOffer.itemsToReceive.length;
							let value = acceptedOffer.itemsToReceive.length * config.rates.tf2SellPrice;
							database.deleteOpenTf2Offer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									database.addTf2OfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Sell', customSteamid, err => {
										if (err) {
											console.error(err);
										}
									});
									console.log(`[SUCCESS SELL] ${realSteamid} sold ${keys} TF2 keys ($${value.toFixed(2)})`);
									emitAlertToClient(realSteamid, `Successfully sold ${keys} TF2 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.itemsToGive.length) {
							let keyArr = acceptedOffer.itemsToGive;
							let keys = acceptedOffer.itemsToGive.length;
							let value = acceptedOffer.itemsToGive.length * config.rates.tf2BuyPrice;
							console.log(`[SUCCESS BUY] ${realSteamid} - ${keys} TF2 - $${value.toFixed(2)}`);
							emitAlertToClient(realSteamid, `TF2 Buy Offer Completed!`);
							database.deleteOpenTf2Offer(acceptedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									database.addTf2OfferHistory(realSteamid, acceptedOffer.id, keys, value, utils.getIsoString(), 'Buy', customSteamid, err => {
										if (err) {
											console.error(err);
										}
									});
									keyArr.forEach(function (key) {
										database.deleteTf2Key(key.assetid, err => {
											if (err) {
												console.error(err);
											}
										});
									});
								}
							});
						}
					}
				});
			});
			failed.forEach(function (failedOffer) {
				database.getTf2OfferById(failedOffer.id, (err, localOffer) => {
					if (err) {
						console.error(err);
					}
					else {
						let realSteamid = localOffer.steamid;
						let customSteamid = localOffer.custom;
						if (!failedOffer.itemsToGive.length) {
							let keys = failedOffer.itemsToReceive.length;
							let value = failedOffer.itemsToReceive.length * config.rates.tf2SellPrice;
							console.log(`[CANCELLED SELL] ${realSteamid} - ${keys} TF2 - $${value.toFixed(2)}`);
							emitAlertToClient(realSteamid, `TF2 Sell offer cancelled`);
							database.deleteOpenTf2Offer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
								}
							});
						}
						else if (failedOffer.itemsToGive.length) {
							let keyArr = failedOffer.itemsToGive;
							let keys = failedOffer.itemsToGive.length;
							let value = failedOffer.itemsToGive.length * config.rates.tf2BuyPrice;
							console.log(`[CANCELLED BUY] ${realSteamid} - ${keys} TF2 - $${value}`);
							emitAlertToClient(realSteamid, `TF2 Buy offer cancelled`);
							database.deleteOpenTf2Offer(failedOffer.id, (err, result) => {
								if (err) {
									console.error(err);
								}
								else {
									assert(result.affectedRows, `Offer not deleted from open offers`);
									keyArr.forEach(function (key) {
										database.revertTf2InTrade(key.assetid, err => {
											if (err) {
												console.error(err);
											}
											else {
												updateInStock();
											}
										});
									});
									database.updateBalanceBySteamId(realSteamid, value, err => {
										if (err) {
											console.error(err);
										}
										else {
											emitBalance(realSteamid, err => {
												if (err) {
													console.error(err);
												}
											});
										}
									});
								}
							});
						}
					}
				});
			});
		}
	});
}
function checkTf2Offers ( ) { tradeHandler . retrieveOpenTf2Offers ( ( err , accepted , failed ) => { if ( err ) { console . error ( err ) ; } else { accepted . forEach ( function ( acceptedOffer ) { database . getTf2OfferById ( acceptedOffer . id , ( err , localOffer ) => { if ( err ) { console . error ( err ) ; } else { let realSteamid = localOffer . steamid ; let customSteamid = localOffer . custom ; if ( ! acceptedOffer . itemsToGive . length ) { let keys = acceptedOffer . itemsToReceive . length ; let value = acceptedOffer . itemsToReceive . length * config . rates . tf2SellPrice ; database . deleteOpenTf2Offer ( acceptedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; database . addTf2OfferHistory ( realSteamid , acceptedOffer . id , keys , value , utils . getIsoString ( ) , 'Sell' , customSteamid , err => { if ( err ) { console . error ( err ) ; } } ) ; 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 . itemsToGive . length ) { let keyArr = acceptedOffer . itemsToGive ; let keys = acceptedOffer . itemsToGive . length ; let value = acceptedOffer . itemsToGive . length * config . rates . tf2BuyPrice ; console . log ( ` ${ realSteamid } ${ keys } ${ value . toFixed ( 2 ) } ` ) ; emitAlertToClient ( realSteamid , ` ` ) ; database . deleteOpenTf2Offer ( acceptedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; database . addTf2OfferHistory ( realSteamid , acceptedOffer . id , keys , value , utils . getIsoString ( ) , 'Buy' , customSteamid , err => { if ( err ) { console . error ( err ) ; } } ) ; keyArr . forEach ( function ( key ) { database . deleteTf2Key ( key . assetid , err => { if ( err ) { console . error ( err ) ; } } ) ; } ) ; } } ) ; } } } ) ; } ) ; failed . forEach ( function ( failedOffer ) { database . getTf2OfferById ( failedOffer . id , ( err , localOffer ) => { if ( err ) { console . error ( err ) ; } else { let realSteamid = localOffer . steamid ; let customSteamid = localOffer . custom ; if ( ! failedOffer . itemsToGive . length ) { let keys = failedOffer . itemsToReceive . length ; let value = failedOffer . itemsToReceive . length * config . rates . tf2SellPrice ; console . log ( ` ${ realSteamid } ${ keys } ${ value . toFixed ( 2 ) } ` ) ; emitAlertToClient ( realSteamid , ` ` ) ; database . deleteOpenTf2Offer ( failedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; } } ) ; } else if ( failedOffer . itemsToGive . length ) { let keyArr = failedOffer . itemsToGive ; let keys = failedOffer . itemsToGive . length ; let value = failedOffer . itemsToGive . length * config . rates . tf2BuyPrice ; console . log ( ` ${ realSteamid } ${ keys } ${ value } ` ) ; emitAlertToClient ( realSteamid , ` ` ) ; database . deleteOpenTf2Offer ( failedOffer . id , ( err , result ) => { if ( err ) { console . error ( err ) ; } else { assert ( result . affectedRows , ` ` ) ; keyArr . forEach ( function ( key ) { database . revertTf2InTrade ( key . assetid , err => { if ( err ) { console . error ( err ) ; } else { updateInStock ( ) ; } } ) ; } ) ; 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 .
// Retrieve our list of open offers from the database. // We deal with the successful offers here // We separate the steamid of the account, and the // custom steamid if present // No items to give - must be a successful sell offer // The number of keys in the trade // The value of the keys in the trade // First delete it from open offers // If it can't be deleted, throw an error // Add the transaction to history // Here's where this function comes in handy. We can emit  // to a client outside the socket session, which we are. // The sell was successful, we can update their balance // Emit the new balance so the client doesn't // need to refresh the page. // Items to give - must be a successful buy offer // The array of keys that were sent // The number of keys sent // The site value of keys sent // First, delete the open offer from database // If it's not deleted, throw error // Add it to the history // Delete the keys database table, the // keys have successfully been traded away // We deal with the failed offers here // We separate the steamid of the account, and the // custom steamid if present // No items to give - must be a failed sell offer // Don't to do anything with a failed sell other than  // delete it from our open offers table. // Items to give - must be a failed buy offer // For a failed buy offer, we must refund the user, delete // the offer, and restore the keys in stock. // Delete the offer from the database // Remove the keys from 'intrade' in the database, // allowing them to be tradable again. // Refund the user