Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml.Linq;
using System.Web.SessionState;
using System.IO;
/// <summary>
/// Summary description for FakeDataHelper
/// </summary>
public class FakeDataHelper {
public const string PhotosFolder = "~\\Content\\Photos";
const string CreatedHotelsTitlesSID = "CHT";
const int TitleMinLength = 2;
const int TitleMaxLength = 4;
const int MaxRoomPrice = 500;
const int MinRoomPrice = 10;
const int MinSearchResultsCount = 5;
public FakeDataHelper() {
Random = new Random();
Photos = new List<string>(System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath(PhotosFolder)));
}
protected Random Random { get; set; }
protected List<string> Photos { get; set; }
// Countries
string GetRandomCity(Country country) {
return country.Cities[Random.Next(0, country.Cities.Count)];
}
Country GetRandomCountry() {
return Data.Instance.Countries[Random.Next(0, Data.Instance.Countries.Count)];
}
public IEnumerable<Country> GetPopularCountries(List<Country> countries, int count) {
return (from country in countries
where country.IsPopular
orderby Random.Next()
select country)
.Take(count).ToList();
}
public int GetPopularity(bool isPopular) {
return isPopular ? Random.Next(1, 5) : 0;
}
public IEnumerable<Hotel> GetHotels(SearchQuery searchQuery, IEnumerable<XElement> titleGroups) {
int count = Random.Next(15, 50);
return GetHotels(searchQuery, titleGroups, count);
}
public IEnumerable<Hotel> GetHotels(SearchQuery searchQuery, IEnumerable<XElement> titleGroups, int count) {
List<Hotel> hotels = new List<Hotel>();
for(int i = 0; i < count; i++)
hotels.Add(GenerateHotel(searchQuery, titleGroups));
return hotels;
}
public void UpdateHotelsList(List<XElement> titleGroups) {
Data data = Data.Instance;
int matchedHotelsCount = data.Hotels.FindAll(h => data.CurrentQuery.IsMatch(h)).Count;
if(matchedHotelsCount < MinSearchResultsCount) {
data.Hotels.AddRange(
GetHotels(
data.CurrentQuery,
titleGroups,
Random.Next(MinSearchResultsCount, 15)
)
);
}
}
protected List<string> CreatedHotelsTitles {
get {
HttpSessionState session = HttpContext.Current.Session;
if(session[CreatedHotelsTitlesSID] == null)
session[CreatedHotelsTitlesSID] = new List<string>();
return (List<string>)session[CreatedHotelsTitlesSID];
}
}
// Hotel generator
Hotel GenerateHotel(SearchQuery query, IEnumerable<XElement> titleGroups) {
Data data = Data.Instance;
Country country;
string city;
if(string.IsNullOrEmpty(query.Country)) {
country = query.IsExclusive ? GetPopularCountries(data.Countries, 1).First() : GetRandomCountry();
city = GetRandomCity(country);
}
else {
country = data.Countries.Find(c => c.Name == query.Country);
city = string.IsNullOrEmpty(query.City) ? GetRandomCity(country) : query.City;
}
Hotel hotel = new Hotel(GetHotelTitle(titleGroups));
hotel.Country = country;
hotel.City = city;
hotel.Stars = query.Stars.Length > 0 ? query.Stars[Random.Next(0, query.Stars.Length)] : Random.Next(2, 6);
hotel.RoomServices.AddRange(GenerateServices(data.RoomServices, query.RoomServices));
hotel.HotelServices.AddRange(GenerateServices(data.HotelServices, query.HotelServices));
hotel.Rooms.AddRange(GenerateRooms(query, hotel));
hotel.ImageUrl = Path.GetFileName(Photos[Random.Next(0, Photos.Count)]);
hotel.Rating = (decimal)(Random.Next(3, 5) + Random.NextDouble());
hotel.VoteCounter = Random.Next(10, 25);
hotel.IsExclusive = query.IsExclusive;
hotel.DescriptionIndex = GenerateDescriptionIndex();
return hotel;
}
int[] GenerateDescriptionIndex() {
return new int[] {
Random.Next(0, Data.Instance.CommonDescriptions.Count),
Random.Next(0, Data.Instance.HotelDescriptions.Count),
Random.Next(0, Data.Instance.RoomDescriptions.Count)
};
}
IEnumerable<string> GenerateServices(List<string> services, string[] impServices) {
List<string> res = services.OrderBy(s => Random.Next()).Take(Random.Next(1, services.Count + 1)).ToList();
foreach(string impserv in impServices) {
if(!res.Contains(impserv))
res.Add(impserv);
}
return res;
}
IEnumerable<Room> GenerateRooms(SearchQuery query, Hotel hotel) {
int power = 4;
double koeff = (double)(MaxRoomPrice - MinRoomPrice) / Math.Pow(5, power);
int minPrice = (int)((double)MinRoomPrice + Math.Pow(hotel.Stars - 1, power) * koeff);
int maxPrice = (int)((double)MinRoomPrice + Math.Pow(hotel.Stars, power) * koeff);
List<Room> rooms = new List<Room>();
for(int i = 0; i < Data.Instance.RoomTypes.Count; i++) {
int price = minPrice + Random.Next(minPrice, maxPrice);
rooms.Add(new Room(Data.Instance.RoomTypes[i], 3, Data.Instance.RoomTypesMaxChildrenCount[i], price));
}
return rooms;
}
// Title generator
string GetHotelTitle(IEnumerable<XElement> titleGroups) {
string title = string.Empty;
for(var i = 0; i < 10; i++) {
title = GenerateTitle(titleGroups);
if(!CreatedHotelsTitles.Contains(title)) {
CreatedHotelsTitles.Add(title);
return title;
}
}
return title;
}
string GenerateTitle(IEnumerable<XElement> titleGroups) {
StringBuilder sb = new StringBuilder();
string res = string.Empty;
List<XElement> groups = new List<XElement>(titleGroups);
int length = Random.Next(TitleMinLength, TitleMaxLength + 1);
for(int i = 0; i < length; i++) {
XElement group = groups[Random.Next(0, groups.Count)];
if(group.Attribute("OnlyOneWord") != null)
groups.Remove(group);
if(group.Attribute("LastWord") != null)
sb.Append(GetRandomWord(group));
else
sb.Insert(0, GetRandomWord(group) + " ");
}
return sb.ToString().Trim(' ');
}
string GetRandomWord(XElement cGroup) {
return (from word in cGroup.Elements()
orderby Random.Next()
select word.Value).First();
}
}