Example #1
0
        public string SetStyle(string style, MensaBotEntities mensaBotEntities, string channelId, string conversationId, string serviceURL)
        {
            FoodDisplayStyle foodDisplayStyle;

            if (FoodDisplayStyle.TryParse(style.ToUpper(), out foodDisplayStyle))
            {
                DatabaseUtilities.CreateChatEntry(mensaBotEntities, channelId, conversationId, serviceURL);
                if (DatabaseUtilities.AddEntry(DatabaseUtilities.StyleTag, foodDisplayStyle.ToString().ToLower(), mensaBotEntities, channelId, conversationId))
                {
                    return(Lang.style_update + ": " + foodDisplayStyle.ToString().ToLower());
                }
                else
                {
                    return(Lang.style_fail_to_add_database);
                }
            }
            else
            {
                return(Lang.style_not_found + ": " + style);
            }
        }
Example #2
0
        public string [] CreateMensaReply(string paramFirst, string paramSecond, MensaBotEntities mensaBotEntities, string channelId, string conversationId, bool forceDate)
        {
            bool isShortRequest = false;

            //check if paramFirst is date, if true -> change order
            if (!string.IsNullOrEmpty(paramFirst) && string.IsNullOrEmpty(paramSecond))
            {
                var isDate = (MessageInterpreter.Get.FindDate(paramFirst) != DateIndex.none);
                if (isDate)
                {
                    paramSecond = paramFirst;
                    paramFirst  = null;
                }
            }

            if (string.IsNullOrEmpty(paramFirst))
            {
                paramFirst = DatabaseUtilities.GetValueBytKey(mensaBotEntities, DatabaseUtilities.DefaultMensaTag, channelId, conversationId);

                if (string.IsNullOrEmpty(paramFirst))
                {
                    return new string[] { Lang.please_add + " " + MessageInterpreter.MarkBold(Lang.canteen_name) + "!" }
                }
                ;
                else
                {
                    isShortRequest = true;
                }
            }

            CanteenName canteenName = MessageInterpreter.Get.FindCanteen(paramFirst);

            if (canteenName == CanteenName.none)
            {
                return(new string[] { Lang.canteen_not_found + ": " + MessageInterpreter.MarkBold(paramFirst) });
            }

            try
            {
                if (_canteens == null)
                {
                    CreateCanteenInfo();
                }

                _canteens[(int)canteenName].LoadElements(3);
            }
            catch (Exception e)
            {
                return(new string[] { Lang.fail_to_load_information + ": " + MessageInterpreter.MarkBold(paramFirst) });
            }

            DateIndex dateIndex = DateIndex.TODAY;

            if (!string.IsNullOrEmpty(paramSecond))
            {
                dateIndex = MessageInterpreter.Get.FindDate(paramSecond);
                if (dateIndex == DateIndex.none)
                {
                    return(new string[] { Lang.date_not_found + " " + MessageInterpreter.MarkBold(paramSecond) });
                }
            }


            //Find correct date element.
            DateTime now = DateTime.Now.ToUniversalTime().AddDays((int)dateIndex).AddHours(1);

            DayElement[] dayElements = null;

            try
            {
                if (isShortRequest && !forceDate)
                {
                    int maxDays = 3;
                    for (int i = 0; i < maxDays; i++)
                    {
                        if (i == 0 && now.Hour > 16)//TODO read close time via website
                        {
                            continue;
                        }

                        dayElements = _canteens[(int)canteenName].DayElements.FindAll(t => t.Date.Day == now.AddDays(i).Day).ToArray();
                        if (dayElements != null && dayElements.Length != 0)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    dayElements = _canteens[(int)canteenName].DayElements.FindAll(t => t.Date.Day == now.Day).ToArray();
                }
            }
            catch (Exception e)
            {
                dayElements = null;
            }

            if (dayElements == null || dayElements.Length == 0)
            {
                return(new string[] { MessageInterpreter.MarkBold(Lang.could_not_find_date + " " + now.ToString("dd.MM.yyyy") + Lang.broken_heart) });
            }

            IEnumerable <FoodTags> filter = null;

            try
            {
                filter =
                    CommandBucket.Get.GetValue(mensaBotEntities, DatabaseUtilities.IgnoreTags, channelId, conversationId)
                    .Split(MessageInterpreter.ParamDivider)
                    .Select(t => Enum.Parse(typeof(FoodTags), t.ToUpper()))
                    .Cast <FoodTags>();
            }
            catch (Exception e)
            {
                filter = null;
            }

            string[] menuItems = new string[dayElements.Length];

            //List all elements for dayElement
            for (int i = 0; i < dayElements.Length; i++)
            {
                menuItems[i] = Lang.menu_for + ":" + MessageInterpreter.LineBreak + MessageInterpreter.MarkBold(_canteens[(int)canteenName].GetDescription(i)) + " "
                               + Lang.menu_for_at + " " + MessageInterpreter.MarkItalic(dayElements[i].Date.ToString("dd.MM.yyyy"))
                               + MessageInterpreter.DrawLine;

                foreach (var foodElement in dayElements[i].FoodElements)
                {
                    bool hideElement = false;
                    var  tagResult   = "";
                    if (foodElement.Tags != null)
                    {
                        foreach (var tag in foodElement.Tags)
                        {
                            if (tag != null)
                            {
                                if (filter != null && filter.Contains(tag))
                                {
                                    hideElement = true;
                                    break;
                                }
                                tagResult += FoodElement.FoodTagsToEmoji(tag) + ",";
                            }
                            else
                            {
                                tagResult += "❎";
                            }
                        }
                    }
                    if (tagResult.Length > 1)
                    {
                        tagResult = tagResult.Remove(tagResult.Length - 1, 1).ToLower() + ";";
                    }

                    string warning = null;

                    if (string.IsNullOrEmpty(foodElement.EnglishName.Trim()) && CultureInfo.CurrentCulture.ToString() != (new CultureInfo("de")).ToString())
                    {
                        warning = MessageInterpreter.MarkItalic(Lang.no_english_available);
                    }

                    FoodDisplayStyle foodDisplayStyle = FoodDisplayStyle.none;
                    string           styleString      = GetValue(mensaBotEntities, DatabaseUtilities.StyleTag, channelId, conversationId);
                    if (!string.IsNullOrEmpty(styleString))
                    {
                        if (!FoodDisplayStyle.TryParse(styleString.ToUpper(), out foodDisplayStyle))
                        {
                            foodDisplayStyle = FoodDisplayStyle.none;
                        }
                    }

                    var tagsDisplay = "";
                    switch (foodDisplayStyle)
                    {
                    case FoodDisplayStyle.none:
                    case FoodDisplayStyle.MAXIMUM:
                        tagsDisplay = MessageInterpreter.LineBreak + tagResult + (!string.IsNullOrEmpty(foodElement.Cost) ? foodElement.Cost + "€" : "") + MessageInterpreter.DrawLine;
                        break;

                    case FoodDisplayStyle.MINIMUM_NOLINES:
                        tagsDisplay = MessageInterpreter.LineBreak;
                        break;

                    case FoodDisplayStyle.MINIMUM:
                        tagsDisplay = MessageInterpreter.DrawLine;
                        break;

                    case FoodDisplayStyle.INLINE:
                        tagsDisplay = " " + ((tagResult.Length > 1) ? tagResult.Remove(tagResult.Length - 1, 1) : tagResult) + MessageInterpreter.DrawLine;
                        break;
                    }

                    if (!hideElement)
                    {
                        if (CultureInfo.CurrentCulture.ToString() == (new CultureInfo("de")).ToString() || warning != null)
                        {
                            menuItems[i] += MessageInterpreter.MarkBold(foodElement.GermanName) + warning + " " + tagsDisplay;
                        }
                        else
                        {
                            menuItems[i] += MessageInterpreter.MarkBold(MessageInterpreter.FirstCharToUpper(foodElement.EnglishName)) + " " + tagsDisplay;
                        }
                    }
                }
            }

            return(menuItems);
        }