public void sendMessageTORaffleWinner(int saleId) { Sale s = SalesManager.getInstance().getSale(saleId); ProductInStore p = ProductManager.getInstance().getProductInStore(s.ProductInStoreId); LinkedList <RaffleSale> relevant = new LinkedList <RaffleSale>(); double realPrice = p.price; double acc = 0; foreach (RaffleSale rs in raffleSales) { if (rs.SaleId == saleId) { acc += rs.Offer; relevant.AddLast(rs); } } if (acc == realPrice) { int index = 1; Random rand = new Random(); int winner = rand.Next(1, (int)realPrice); RaffleSale winnerS = null; foreach (RaffleSale r in relevant) { if (winner <= r.Offer + index && winner >= index) { string message = r.UserName + " WON THE RAFFLE SALE ON PRODUCT: " + getProductNameFromSaleId(r.SaleId); NotificationPublisher.getInstance().publish(NotificationPublisher.NotificationCategories.RaffleSale, message, p.getStore().storeId); StoreRole sR = StoreRole.getStoreRole(p.getStore(), UserManager.getInstance().getUser(r.UserName)); NotificationPublisher.getInstance().removeAllNotificationSubscriptionsOfAStoreRole(sR); //NotificationManager.getInstance().notifyUser(r.UserName, message); winnerS = r; break; } else { index += (int)r.Offer; } } if (winnerS != null) { RSDB.Remove(winnerS); raffleSales.Remove(winnerS); relevant.Remove(winnerS); } foreach (RaffleSale r in relevant) { string message = r.UserName + " LOST THE RAFFLE SALE ON PRODUCT: " + getProductNameFromSaleId(r.SaleId); NotificationPublisher.getInstance().publish(NotificationPublisher.NotificationCategories.RaffleSale, message, p.getStore().storeId); StoreRole sR = StoreRole.getStoreRole(p.getStore(), UserManager.getInstance().getUser(r.UserName)); NotificationPublisher.getInstance().removeAllNotificationSubscriptionsOfAStoreRole(sR); //NotificationManager.getInstance().notifyUser(r.UserName, message); RSDB.Remove(winnerS); raffleSales.Remove(r); } } }
public Boolean addRaffleSale(int saleId, String userName, double offer, String dueDate) { RaffleSale toAdd = new RaffleSale(saleId, userName, offer, dueDate); ProductInStore pis = ProductManager.getInstance().getProductInStore(SalesManager.getInstance().getSale(saleId).ProductInStoreId); StoreRole sR = StoreRole.getStoreRole(pis.store, UserManager.getInstance().getUser(userName)); NotificationPublisher.getInstance().signToCategory(sR, NotificationPublisher.NotificationCategories.RaffleSale); RSDB.Add(toAdd); raffleSales.AddLast(toAdd); return(true); }