public static void AMA_Buy(int commodity, int desiredPrice, int amount) { FLAG_isRunning = true; notOverLoadServer(); MarketClientClass client = new MarketClientClass(); AllMarketRequest all = client.QueryAllMarketRequest(); counter++; foreach (ItemAskBid item in all.MarketInfo) { if (item.Id == commodity && item.Info.Ask <= desiredPrice) { //if item is the right commodity & right price MarketUserData userData = client.SendQueryUserRequest(); counter++; List <int> l = userData.Requests; if (l.Count != 0) //there are open requests in server //if USER dont have enough money, we'll cancel his open buy requests- hoping after that he'll have enough { for (int i = l.Count; i >= 0 & userData.Funds < (item.Info.Ask * amount); i--) //going from end so in delete won't change index of l { notOverLoadServer(); int reqID = l[i]; //saving the ID just for simplicity MarketItemQuery request = client.SendQueryBuySellRequest(l[i]); counter++; if (request.Type.Equals("buy")) //Note: check with roey { client.SendCancelBuySellRequest(reqID); HistoryLogger.WriteHistory("Cancel," + request.Commodity + "," + request.Price + "," + request.Amount + "," + reqID); counter++; } } } if (userData.Funds >= item.Info.Ask * amount) { int ID = client.SendBuyRequest(item.Info.Ask + 1, commodity, amount).Id; HistoryLogger.WriteHistory("Buy," + commodity + "," + (item.Info.Ask + 1) + "," + amount + "," + ID); counter++; } }//bigIf } FLAG_isRunning = false; return; }//AMAbuy
}//AMAbuy public static void AMA_Sell(int commodity, int desiredPrice, int amount) { FLAG_isRunning = true; notOverLoadServer(); MarketClientClass client = new MarketClientClass(); AllMarketRequest all = client.QueryAllMarketRequest(); counter++; MarketUserData userData = client.SendQueryUserRequest(); counter++; foreach (int cmdty in userData.Commodities.Keys) //check if we own that commodity { if (cmdty == commodity & userData.Commodities[cmdty] > 0) { //passing on commodities list, until arriving the wished one foreach (ItemAskBid item in all.MarketInfo) { if (item.Id == commodity && item.Info.Bid >= desiredPrice) { //if item is the right commodity & right price if (amount > userData.Commodities[cmdty] | amount == -1) //we cant sell more than we have OR -1 is our sign to sell ALL { amount = userData.Commodities[cmdty]; } //Note: ask roey about error int ID = client.SendSellRequest(item.Info.Bid - 1, commodity, amount).Id; HistoryLogger.WriteHistory("Sell," + commodity + "," + (item.Info.Bid - 1) + "," + amount + "," + ID); counter++; } } } } FLAG_isRunning = false; return; }//AMAsell