private AuctionSniper CreateWinningSniper() { var sniper = new AuctionSniper("bidder", 200); sniper.Process(AuctionEvent.Price(1, 2, "bidder")); return(sniper); }
private AuctionSniper CreateBiddingSniper(string bidder = "some bidder") { var sniper = new AuctionSniper(bidder, 200); sniper.Process(AuctionEvent.Price(1, 2, Guid.NewGuid().ToString())); return(sniper); }
private AuctionSniper CreateLosingSniper() { var sniper = new AuctionSniper("bidder", 20); sniper.Process(AuctionEvent.Price(20, 5, "other bidder")); return(sniper); }
private AuctionSniper CreateFailedSniper() { var sniper = new AuctionSniper("bidder", 20); sniper.Process(AuctionEvent.From("Some corrupted message")); return(sniper); }
public void Close_method_returns_a_close_event() { AuctionEvent auctionEvent = AuctionEvent.Close(); auctionEvent.Type.ShouldEqual(AuctionEventType.Close); auctionEvent.ToString().ShouldEqual("SOLVersion: 1.1; Event: CLOSE;"); }
public void Does_not_parse_events_with_incorrect_type() { string message = "SOLVersion: 1.1; Event: SOME_NONEXISTING_TYPE;"; AuctionEvent auctionEvent = AuctionEvent.From(message); auctionEvent.Type.ShouldEqual(AuctionEventType.Unknown); }
public ActionResult DeleteConfirmed(Guid id) { AuctionEvent auctionEvent = db.AuctionEvents.Find(id); db.AuctionEvents.Remove(auctionEvent); db.SaveChanges(); return(RedirectToAction("Index")); }
public void Does_not_parse_events_with_incorrect_format() { string message = "Incorrectly formatted message"; AuctionEvent auctionEvent = AuctionEvent.From(message); auctionEvent.Type.ShouldEqual(AuctionEventType.Unknown); }
public void Bidding_sniper_loses_when_auction_closes() { AuctionSniper sniper = CreateBiddingSniper(); AuctionCommand command = sniper.Process(AuctionEvent.Close()); command.ShouldEqual(AuctionCommand.None()); sniper.StateShouldBe(SniperState.Lost, 1, 3); }
public void Sniper_fails_when_auction_sends_unknown_event() { AuctionSniper sniper = CreateLosingSniper(); AuctionCommand command = sniper.Process(AuctionEvent.From("Some corrupted message")); command.ShouldEqual(AuctionCommand.None()); sniper.StateShouldBe(SniperState.Failed, 0, 0); }
public void Sniper_is_losing_when_it_cannot_beat_last_bid() { var sniper = new AuctionSniper("bidder", 20); AuctionCommand command = sniper.Process(AuctionEvent.Price(15, 10, "other bidder")); command.ShouldEqual(AuctionCommand.None()); sniper.StateShouldBe(SniperState.Losing, 15, 0); }
public void Losing_sniper_loses_when_auction_closes() { AuctionSniper sniper = CreateLosingSniper(); AuctionCommand command = sniper.Process(AuctionEvent.Close()); command.ShouldEqual(AuctionCommand.None()); sniper.Snapshot.State.ShouldEqual(SniperState.Lost); }
public void Bidding_sniper_is_winning_when_price_event_with_the_same_bidder_arrives() { AuctionSniper sniper = CreateBiddingSniper("bidder"); AuctionCommand command = sniper.Process(AuctionEvent.Price(3, 2, "bidder")); command.ShouldEqual(AuctionCommand.None()); sniper.StateShouldBe(SniperState.Winning, 3, 3); }
public void Sniper_bids_when_price_event_with_a_different_bidder_arrives() { var sniper = new AuctionSniper("", 200); AuctionCommand command = sniper.Process(AuctionEvent.Price(1, 2, "some bidder")); command.ShouldEqual(AuctionCommand.Bid(3)); sniper.StateShouldBe(SniperState.Bidding, 1, 3); }
public void Joining_sniper_loses_when_auction_closes() { var sniper = new AuctionSniper("", 200); AuctionCommand command = sniper.Process(AuctionEvent.Close()); command.ShouldEqual(AuctionCommand.None()); sniper.StateShouldBe(SniperState.Lost, 0, 0); }
public void Failed_sniper_does_not_react_on_further_messages() { AuctionSniper sniper = CreateFailedSniper(); AuctionCommand command = sniper.Process(AuctionEvent.Price(10, 5, "some bidder")); command.ShouldEqual(AuctionCommand.None()); sniper.StateShouldBe(SniperState.Failed, 0, 0); }
public void Parses_close_events() { string message = "SOLVersion: 1.1; Event: CLOSE;"; AuctionEvent auctionEvent = AuctionEvent.From(message); string serializedEvent = auctionEvent.ToString(); auctionEvent.Type.ShouldEqual(AuctionEventType.Close); serializedEvent.ShouldEqual(message); }
public void Price_method_returns_a_price_event() { AuctionEvent auctionEvent = AuctionEvent.Price(1, 2, "bidder"); auctionEvent.Type.ShouldEqual(AuctionEventType.Price); auctionEvent.CurrentPrice.ShouldEqual(1); auctionEvent.Increment.ShouldEqual(2); auctionEvent.Bidder.ShouldEqual("bidder"); auctionEvent.ToString().ShouldEqual("SOLVersion: 1.1; Event: PRICE; CurrentPrice: 1; Increment: 2; Bidder: bidder;"); }
public static AuctionEvent From(string messageBody) { var @event = new AuctionEvent(); foreach (var field in FieldsIn(messageBody)) { @event.AddField(field); } return(@event); }
public ActionResult Edit([Bind(Include = "Id,EventName,EventDate,OrganizationId")] AuctionEvent auctionEvent) { if (ModelState.IsValid) { db.Entry(auctionEvent).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OrganizationId = new SelectList(db.Organizations, "Id", "Name", auctionEvent.OrganizationId); return(View(auctionEvent)); }
public IActionResult DeleteAuction(int id) { if (ActiveUser == null) { return(RedirectToAction("Index", "Home")); } AuctionEvent toDelete = _dbContext.auctions.Where(a => a.auction_id == id).Include(a => a.bid).SingleOrDefault(); _dbContext.auctions.Remove(toDelete); _dbContext.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "Id,EventName,EventDate,OrganizationId")] AuctionEvent auctionEvent) { if (ModelState.IsValid) { auctionEvent.Id = Guid.NewGuid(); db.AuctionEvents.Add(auctionEvent); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OrganizationId = new SelectList(db.Organizations, "Id", "Name", auctionEvent.OrganizationId); return(View(auctionEvent)); }
public void Parses_price_events() { string message = "SOLVersion: 1.1; Event: PRICE; CurrentPrice: 12; Increment: 34; Bidder: sniper-56;"; AuctionEvent auctionEvent = AuctionEvent.From(message); string serializedEvent = auctionEvent.ToString(); auctionEvent.Type.ShouldEqual(AuctionEventType.Price); auctionEvent.CurrentPrice.ShouldEqual(12); auctionEvent.Increment.ShouldEqual(34); auctionEvent.Bidder.ShouldEqual("sniper-56"); serializedEvent.ShouldEqual(message); }
public void ProcessMessage(Chat chat, Message message) { var @event = AuctionEvent.From(message.Body); var type = @event.Type; if (type == "CLOSE") { _listener.AuctionClosed(); } else if (type == "PRICE") { _listener.CurrentPrice(@event.CurrentPrice, @event.Increment, @event.IsFrom(_sniperId)); } }
// GET: AuctionEvents/Details/5 public ActionResult Details(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AuctionEvent auctionEvent = db.AuctionEvents.Find(id); if (auctionEvent == null) { return(HttpNotFound()); } return(View(auctionEvent)); }
private void ChatMessageRecieved(string message) { AuctionEvent ev = AuctionEvent.From(message); AuctionCommand command = _auctionSniper.Process(ev); if (command != AuctionCommand.None()) { _chat.SendMessage(command.ToString()); } Notify(nameof(LastPrice)); Notify(nameof(LastBid)); Notify(nameof(State)); }
// GET: AuctionEvents/Edit/5 public ActionResult Edit(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AuctionEvent auctionEvent = db.AuctionEvents.Find(id); if (auctionEvent == null) { return(HttpNotFound()); } ViewBag.OrganizationId = new SelectList(db.Organizations, "Id", "Name", auctionEvent.OrganizationId); return(View(auctionEvent)); }
public void RunActionFrom(Message inMessage) { var ev = new AuctionEvent(inMessage.Body .Split(new char[] { ';' }) .Select(elem => elem.Split(new char[] { ':' }).Select(s => s.Trim()).ToList()) .Where(elem => elem.Count > 1) .ToDictionary(elem => elem[0], elem => elem[1]) ); Action <AuctionEvent> action; if (mActions.TryGetValue(ev.EventType, out action)) { action(ev); } }
public IActionResult ProcessBid(float amt, int itemid, int userid) { if (ActiveUser == null) { return(RedirectToAction("Index", "Home")); } User user = ActiveUser; AuctionEvent auctionInfo = _dbContext.auctions .Where(a => a.auction_id == HttpContext.Session .GetInt32("ItemId")).SingleOrDefault(); if (amt == 0) { TempData["Error"] = "Please specify the amount."; return(RedirectToAction("ShowAuction")); } else if (amt <= auctionInfo.highest_bid) { TempData["Error"] = "Your bid MUST be greater than the highest bid."; return(RedirectToAction("ShowAuction")); } else if (amt > user.wallet_balance) { TempData["Error"] = "You don't have enough balance for the bid."; return(RedirectToAction("ShowAuction")); } else { auctionInfo.highest_bid = amt; Bid newBid = new Bid { bidder = user, auctions = auctionInfo, }; if (_dbContext.bids.Where(b => b.auction_id == auctionInfo.auction_id) == null) { Bid theBid = _dbContext.Add(newBid).Entity; } else { Bid theBid = _dbContext.Update(newBid).Entity; } user.wallet_balance -= amt; _dbContext.SaveChanges(); return(RedirectToAction("Index")); } }
public IActionResult ShowAuction(int itemid, int userid) { if (ActiveUser == null) { return(RedirectToAction("Index", "Home")); } AuctionEvent auctionInfo = _dbContext.auctions .Where(a => a.auction_id == itemid) .Include(a => a.user) .Include(a => a.bid).SingleOrDefault(); Bid bidInfo = _dbContext.bids .Where(b => b.auction_id == auctionInfo.auction_id) .Include(b => b.bidder).LastOrDefault(); if (bidInfo != null) { ViewBag.bidInfo = bidInfo.bidder.first_name; } HttpContext.Session.SetInt32("ItemId", auctionInfo.auction_id); return(View(auctionInfo)); }
public void RunActionFrom(Message inMessage) { var ev = new AuctionEvent(inMessage.Body .Split(new char[] {';'}) .Select(elem => elem.Split(new char[] {':'}).Select(s => s.Trim()).ToList()) .Where(elem => elem.Count > 1) .ToDictionary(elem => elem[0], elem => elem[1]) ); Action<AuctionEvent> action; if (mActions.TryGetValue(ev.EventType, out action)) { action(ev); } }