public virtual SendResource AcceptOffer(int offerId, int quantity, ISession session) { Offer offer = null; try { offer = session.Load<Offer>(offerId); } catch { throw new Exception("Offer không tồn tại"); } if (offer.AtVillage == this.Village) return null; int resourceNeeded = offer.ForQuantity * quantity; if (resourceNeeded > this.Village[offer.ForType]) throw new Exception("Không đủ tài nguyên"); int merchantNeeded = (int)Math.Ceiling((double)resourceNeeded / 1000); if (merchantNeeded > this.Village.VillageBuildingData.Merchant) throw new Exception("Không đủ merchant"); if (offer.OfferNumber < quantity) quantity = offer.OfferNumber; offer.OfferNumber -= quantity; SendResource sendToSource = new SendResource(); sendToSource.FromVillage = this.Village; sendToSource.ToVillage = offer.AtVillage; sendToSource.StartingTime = DateTime.Now; sendToSource.LandingTime = Map.LandingTime(TroopType.Merchant, this.Village, offer.AtVillage, sendToSource.StartingTime, Research.SpeedValuesDictionary[this.Village[ResearchType.Speed]]); sendToSource.Merchant = merchantNeeded; switch (offer.ForType) { case ResourcesType.Clay: sendToSource.Clay = resourceNeeded; break; case ResourcesType.Wood: sendToSource.Wood = resourceNeeded; break; case ResourcesType.Iron: sendToSource.Iron = resourceNeeded; break; default: break; } SendResource sendFromSource = new SendResource(); sendFromSource.FromVillage = offer.AtVillage; sendFromSource.ToVillage = this.Village; sendFromSource.StartingTime = DateTime.Now; sendFromSource.LandingTime = Map.LandingTime(TroopType.Merchant, this.Village, offer.AtVillage, sendFromSource.StartingTime); sendFromSource.Merchant = (int)Math.Ceiling((double)offer.OfferQuantity / 1000) * quantity; switch (offer.OfferType) { case ResourcesType.Clay: sendFromSource.Clay = offer.OfferQuantity * quantity; break; case ResourcesType.Wood: sendFromSource.Wood = offer.OfferQuantity * quantity; break; case ResourcesType.Iron: sendFromSource.Iron = offer.OfferQuantity * quantity; break; default: break; } this.Village[offer.ForType] -= resourceNeeded; this.Village.VillageBuildingData.Merchant -= sendToSource.Merchant; offer.AtVillage[offer.OfferType] -= resourceNeeded; offer.AtVillage.VillageBuildingData.Merchant -= sendFromSource.Merchant; OfferAcceptedReport report = new OfferAcceptedReport(); report.BoughtQuantity = offer.ForQuantity * quantity; report.BoughtType = offer.ForType; report.SoldQuantity = offer.OfferQuantity * quantity; report.SoldType = offer.OfferType; report.FromVillage = offer.AtVillage; report.FromPlayer = offer.AtVillage.Player; report.ToVillage = this.Village; report.ToPlayer = this.Village.Player; report.Owner = offer.AtVillage.Player; report.Time = DateTime.Now; report.Unread = true; report.Title = string.Format("{0} mua tài nguyên ở {1} ({2}|{3}", this.Village.Player.Username, offer.AtVillage.Name, offer.AtVillage.X.ToString("000"), offer.AtVillage.Y.ToString("000")); offer.AtVillage.MovingCommandsFromMe.Add(sendFromSource); this.Village.MovingCommandsToMe.Add(sendFromSource); offer.AtVillage.MovingCommandsToMe.Add(sendToSource); this.Village.MovingCommandsFromMe.Add(sendToSource); session.Save(sendToSource); session.Save(sendFromSource); if (offer.OfferNumber == 0) { offer.AtVillage.Offers.Remove(offer); session.Delete(offer); } else session.Update(offer); session.Update(offer.AtVillage); session.Update(this.Village); session.Save(report); return sendToSource; }
public virtual SendResource CreateSendResource(ISession session, int x, int y, int clay, int wood, int iron) { if (x == this.Village.X && y == this.Village.Y) throw new Exception("Nhập toạ độ"); if ((clay + wood + iron) == 0) throw new Exception("Nhập một loại tài nguyên"); if ((clay > this.Village.VillageResourceData.Clay) || (wood > this.Village.VillageResourceData.Wood) || (iron > this.Village.VillageResourceData.Iron)) throw new Exception("Không đủ tài nguyên"); Village toVillage = Village.GetVillageByCoordinate(x, y, session); if (toVillage == null) throw new Exception("Toạ độ không tồn tại"); if (this.Village.VillageBuildingData.Merchant < SendResource.CalculateMerchant(wood, clay, iron)) throw new Exception("Không đủ thương nhân"); SendResource sendResource = new SendResource(); sendResource.Clay = clay; sendResource.Iron = iron; sendResource.Wood = wood; sendResource.Merchant = (int)Math.Ceiling((double)(clay + iron + wood)/1000); sendResource.StartingTime = DateTime.Now; sendResource.LandingTime = Map.LandingTime(TroopType.Merchant, this.Village, toVillage, sendResource.StartingTime); sendResource.FromVillage = this.Village; sendResource.ToVillage = toVillage; return sendResource; }