public ServerNotificationMessage BuyDrugs(string seller, string buyer) { if (seller == null) throw new ArgumentException("parameter user is null"); if (buyer == null) throw new ArgumentException("parameter user2 is null"); ServerNotificationMessage message = new ServerNotificationMessage(); message.MessageType = EMessageType.error; message.Content = "Error"; using (SqlConnection conn = SqlConnectionService.GetConnection()) { int idSeller = ServiceProvider.GetPlayerService().GetPlayer(seller).UserId; int idBuyer = ServiceProvider.GetPlayerService().GetPlayer(buyer).UserId; int moneyBuyer = ServiceProvider.GetUserService().GetUserBankAccount(buyer); int moneySeller = ServiceProvider.GetUserService().GetUserBankAccount(seller); string DealerJob = ServiceProvider.GetJobService().GetUserJobInfos(seller).JobDescription; if (DealerJob == "Dealer" && moneyBuyer > 30) { int remainingMoneySeller = moneySeller + 30; int remainingMoneyBuyer = moneyBuyer - 30; ServiceProvider.GetUserService().SetUserBankAccount(seller, remainingMoneySeller); ServiceProvider.GetUserService().SetUserBankAccount(buyer, remainingMoneyBuyer); message.MessageType = EMessageType.success; message.Content = "Success buying drugs"; } else if (moneyBuyer < 30) { message.MessageType = EMessageType.error; message.Content = "You do not have enough money"; } } return message; }
public ServerNotificationMessage BuyProperty(string connectionId, ServerPropertyModel property) { if (connectionId == null) throw new ArgumentException("parameter user is null"); if (property == null) throw new ArgumentException("parameter property is null"); ServerNotificationMessage message = new ServerNotificationMessage() { Content = "error", MessageType = EMessageType.error }; int count = 0, threshold = -1; using (SqlConnection conn = SqlConnectionService.GetConnection()) { string query = string.Format("SELECT COUNT (u.ListPropertyId) as count, l.Threshold FROM dbo.ListProperties l JOIN UserProperties u ON u.ListPropertyId = l.ListPropertyId WHERE u.ListPropertyId = @PropertyId GROUP BY Threshold"); using (SqlCommand cmd = new SqlCommand(query, conn)) { conn.Open(); cmd.Parameters.AddWithValue("@PropertyId", property.PropertyId); var reader = cmd.ExecuteReader(); while (reader.Read()) { count = Convert.ToInt32(reader["count"]); threshold = Convert.ToInt32(reader["Threshold"]); } conn.Close(); } } if (count < threshold || (count == 0 && threshold == -1) || threshold == 0) { var properties = GetUserProperties(connectionId); bool alreadyHas = properties.FirstOrDefault(x => x.PropertyId == property.PropertyId) != null; int money = ServiceProvider.GetUserService().GetUserBankAccount(connectionId); int id = ServiceProvider.GetPlayerService().GetPlayer(connectionId).UserId; if (!alreadyHas && money >= property.Price) { using (SqlConnection conn = SqlConnectionService.GetConnection()) { string query = "INSERT INTO dbo.UserProperties (UserId, ListPropertyId) values ( @UserId, @ListPropertyId) "; using (SqlCommand cmd = new SqlCommand(query, conn)) { conn.Open(); cmd.Parameters.AddWithValue("@UserId", id); cmd.Parameters.AddWithValue("@ListPropertyId", property.PropertyId); cmd.ExecuteNonQuery(); conn.Close(); } } int remainingMoney = money - property.Price; ServiceProvider.GetUserService().SetUserBankAccount(connectionId, remainingMoney); message.Content = "Success"; message.MessageType = EMessageType.success; } else { if (alreadyHas) { message.Content = "You already have this property"; } else { message.Content = "You dont have enough money"; } } } else { message.Content = "Error, this propery reached its limit"; } return message; }
public ServerNotificationMessage MakePropertyInSell(string connectionId, ServerPropertyModel property, int price) { if (connectionId == null) throw new ArgumentException("parameter user is null"); if (property == null) throw new ArgumentException("parameter property is null"); if (price <= 0) throw new ArgumentException("price must be greater than 0"); ServerNotificationMessage message = new ServerNotificationMessage() { Content = "error", MessageType = EMessageType.error }; using (SqlConnection conn = SqlConnectionService.GetConnection()) { var properties = GetUserProperties(connectionId); bool alreadyHas = properties.FirstOrDefault(x => x.PropertyId == property.PropertyId) != null; bool alreadyInSell = GetPropertiesInSell().FirstOrDefault(x => x.PropertyId == property.PropertyId) != null; int id = ServiceProvider.GetPlayerService().GetPlayer(connectionId).UserId; if (alreadyInSell) { message.Content = "This property is already in sell"; message.MessageType = EMessageType.error; return message; } if (alreadyHas) { { string query = "INSERT INTO dbo.PropertiesToSell (UserId, ListPropertyId, Price) values ( @UserId, @ListPropertyId, @Price) "; using (SqlCommand cmd = new SqlCommand(query, conn)) { conn.Open(); cmd.Parameters.AddWithValue("@UserId", id); cmd.Parameters.AddWithValue("@ListPropertyId", property.PropertyId); cmd.Parameters.AddWithValue("@Price", price); cmd.ExecuteNonQuery(); conn.Close(); } message.Content = "Success ! "; message.MessageType = EMessageType.success; } } else { message.Content = "You already own this property !"; } } return message; }