public static bool EvaluateSettlers(ColonyState state)
        {
            var update = false;

            if (state.ColonyRef.OwnerIsOnline())
            {
                if (state.NextGenTime == 0)
                {
                    state.NextGenTime = Time.SecondsSinceStartDouble + Random.Next(8, 16) * IN_GAME_HOUR_IN_SECONDS;
                }

                if (Time.SecondsSinceStartDouble > state.NextGenTime && state.ColonyRef.FollowerCount >= MAX_BUYABLE)
                {
                    var chance =
                        state.ColonyRef.TemporaryData.GetAsOrDefault(GameLoader.NAMESPACE + ".SettlerChance", 0f) +
                        state.Difficulty.AdditionalChance;

                    chance += SettlerEvaluation.SpawnChance(state);

                    var rand = Random.NextFloat();

                    if (chance > rand)
                    {
                        var addCount = Math.Floor(state.MaxPerSpawn * chance);

                        // if we lost alot of colonists add extra to help build back up.
                        if (state.ColonyRef.FollowerCount < state.HighestColonistCount)
                        {
                            var diff = state.HighestColonistCount - state.ColonyRef.FollowerCount;
                            addCount += Math.Floor(diff * .25);
                        }

                        try
                        {
                            var skillChance = state.ColonyRef.TemporaryData.GetAsOrDefault(GameLoader.NAMESPACE + ".SkilledLaborer", 0f);
                            var numbSkilled = 0;
                            rand = Random.NextFloat();

                            try
                            {
                                if (skillChance > rand)
                                {
                                    numbSkilled = Pipliz.Random.Next(1,
                                                                     2 + Pipliz.Math.RoundToInt(state.ColonyRef.TemporaryData.GetAsOrDefault(GameLoader.NAMESPACE + ".NumberSkilledLaborer", 0f)));
                                }
                            }
                            catch (Exception ex)
                            {
                                PandaLogger.Log("NumberSkilledLaborer");
                                PandaLogger.LogError(ex);
                            }


                            if (addCount > 0)
                            {
                                if (addCount > 30)
                                {
                                    addCount = 30;
                                }

                                if (!state.NotifySettlers)
                                {
                                    AddNewSettlers(addCount, numbSkilled, state);
                                }
                                else
                                {
                                    foreach (var p in state.ColonyRef.Owners)
                                    {
                                        if (p.IsConnected())
                                        {
                                            NetworkMenu menu = new NetworkMenu();
                                            menu.LocalStorage.SetAs("header", addCount + _localizationHelper.LocalizeOrDefault("NewSettlers", p));
                                            menu.Width  = 600;
                                            menu.Height = 300;

                                            menu.Items.Add(new ButtonCallback(GameLoader.NAMESPACE + ".NewSettlers.Accept." + addCount + "." + numbSkilled,
                                                                              new LabelData(_localizationHelper.GetLocalizationKey("Accept"),
                                                                                            UnityEngine.Color.black,
                                                                                            UnityEngine.TextAnchor.MiddleCenter)));

                                            menu.Items.Add(new ButtonCallback(GameLoader.NAMESPACE + ".NewSettlers.Decline",
                                                                              new LabelData(_localizationHelper.GetLocalizationKey("Decline"),
                                                                                            UnityEngine.Color.black,
                                                                                            UnityEngine.TextAnchor.MiddleCenter)));

                                            NetworkMenuManager.SendServerPopup(p, menu);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            PandaLogger.Log("SkilledLaborer");
                            PandaLogger.LogError(ex);
                        }

                        if (state.ColonyRef.FollowerCount > state.HighestColonistCount)
                        {
                            state.HighestColonistCount = state.ColonyRef.FollowerCount;
                        }
                    }


                    state.NextGenTime = Time.SecondsSinceStartDouble + Random.Next(8, 16) * IN_GAME_HOUR_IN_SECONDS;

                    state.ColonyRef.SendCommonData();
                }
            }

            return(update);
        }
Example #2
0
        //Ref menu is added for change LocalStorage -> avoid client error
        public static bool LoadItem(JSONNode item, ref NetworkMenu menu, Players.Player player, out List <IItem> menuItem)
        {
            string itemType = item.GetAs <string>("type").Trim().ToLower();
            bool   found    = false;

            menuItem = null;

            switch (itemType)
            {
            case "label":
            {
                menuItem = new List <IItem>()
                {
                    new Label(GetLabelData(item)).ApplyPosition(item)
                };
                found = true;
            }
            break;

            case "space":
            {
                item.TryGetAsOrDefault <int>("height", out int height, 10);
                menuItem = new List <IItem>()
                {
                    new EmptySpace(height).ApplyPosition(item)
                };
                found = true;
            }
            break;

            case "line":
            {
                UnityEngine.Color color = UnityEngine.Color.black;

                if (item.HasChild("color"))
                {
                    color = GetColor(item.GetAs <string>("color"));
                }

                item.TryGetAsOrDefault <int>("height", out int height, 4);
                item.TryGetAsOrDefault <int>("width", out int width, -1);

                menuItem = new List <IItem>()
                {
                    new Line(color, height, width).ApplyPosition(item)
                };
                found = true;
            }
            break;

            case "icon":
            {
                item.TryGetAsOrDefault <string>("name", out string icon, "missingerror");
                menuItem = new List <IItem>()
                {
                    new ItemIcon(icon).ApplyPosition(item)
                };
                found = true;
            }
            break;

            case "jobrecipies":
            {
                if (item.TryGetAs("job", out string job))
                {
                    List <Recipes.Recipe> recipes = new List <Recipes.Recipe>();
                    menuItem = new List <IItem>();

                    if (ServerManager.RecipeStorage.RecipesPerLimitType.TryGetValue(job, out var recipesDefault))
                    {
                        recipes.AddRange(recipesDefault);
                    }

                    foreach (var recipe in recipes.OrderBy(r => r.Name))
                    {
                        menuItem.Add(RecipeLines(menu, player, recipe, item));
                    }

                    found = true;
                }
            }
            break;

            case "item":
            {
                if (!item.HasChild("name"))
                {
                    PandaLogger.Log("Item: Not name defined");
                    return(found);
                }

                item.TryGetAs <string>("name", out string name);

                if (!ItemTypes.IndexLookup.TryGetIndex(name, out ushort index))
                {
                    PandaLogger.Log("Item: Not item found with name: " + name);
                    return(found);
                }

                if (!Localization.TryGetSentence(player.LastKnownLocale, _localizationHelper.GetLocalizationKey("ItemDetails." + name), out var extendedDetail))
                {
                    extendedDetail = "";
                }
                else
                {
                    extendedDetail = Environment.NewLine + extendedDetail;
                }

                if (Localization.TryGetType(player.LastKnownLocale, index, out string localeName) && Localization.TryGetTypeUse(player.LastKnownLocale, index, out var description))
                {
                    menuItem = new List <IItem>()
                    {
                        new HorizontalSplit(new ItemIcon(index), new Label(new LabelData(localeName + Environment.NewLine + description + extendedDetail, UnityEngine.Color.black)), 30, .3f).ApplyPosition(item)
                    };
                    found = true;
                }
            }
            break;

            case "itemrecipe":
            {
                if (!item.HasChild("name"))
                {
                    PandaLogger.Log("ItemRecipe: Not name defined");
                    return(found);
                }

                item.TryGetAs <string>("name", out string name);

                if (!ItemTypes.IndexLookup.TryGetIndex(name, out ushort index))
                {
                    PandaLogger.Log("ItemRecipe: Not item found with name: " + name);
                    return(found);
                }

                if (!ItemRecipe.TryGetValue(index, out var recipe))
                {
                    PandaLogger.Log("ItemRecipe: Not recipe found for: " + name);
                    return(found);
                }

                menuItem = new List <IItem>();

                if (Localization.TryGetType(player.LastKnownLocale, index, out string localeName))
                {
                    menuItem.Add(new Label(localeName + ":").ApplyPosition(item));
                }
                else
                {
                    menuItem.Add(new Label(name + ":").ApplyPosition(item));
                }

                menuItem.Add(RecipeLines(menu, player, recipe, item));
                found = true;
            }
            break;

            case "dropdown":
            {
                string id;

                if (item.HasChild("id"))
                {
                    id = item.GetAs <string>("id");
                }
                else
                {
                    id = "dropdown";
                    PandaLogger.Log("Dropdown without ID defined, default: dropdown");
                }

                List <string> options = new List <string>();

                if (item.HasChild("options"))
                {
                    JSONNode optionsj = item.GetAs <JSONNode>("options");

                    foreach (var option in optionsj.LoopArray())
                    {
                        options.Add(option.GetAs <string>());
                    }
                }
                else
                {
                    options.Add("No options available");
                    PandaLogger.Log(string.Format("dropdown {0} without options", id));
                }

                item.TryGetAsOrDefault <int>("height", out int height, 30);
                item.TryGetAsOrDefault <int>("marginHorizontal", out int marginHorizontal, 4);
                item.TryGetAsOrDefault <int>("marginVertical", out int marginVertical, 2);

                // if label dropdown else dropdownNOLABEL
                if (item.TryGetChild("label", out JSONNode labelj))
                {
                    LabelData label = GetLabelData(labelj);
                    menuItem = new List <IItem>()
                    {
                        new DropDown(label.text, id, options).ApplyPosition(item)
                    };
                }
                else
                {
                    menuItem = new List <IItem>()
                    {
                        new DropDownNoLabel(id, options, height).ApplyPosition(item)
                    };
                }

                menu.LocalStorage.SetAs(id, 0);
                found = true;
            }
            break;

            case "toggle":
            {
                string id;

                if (item.HasChild("id"))
                {
                    id = item.GetAs <string>("id");
                }
                else
                {
                    id = "toggle";
                    PandaLogger.Log("Toggle without ID defined, default: toggle");
                }

                item.TryGetAsOrDefault <int>("height", out int height, 25);
                item.TryGetAsOrDefault <int>("toggleSize", out int toggleSize, 20);

                // if label toggle else togglenolabel
                if (item.TryGetChild("label", out JSONNode labelj))
                {
                    LabelData label = GetLabelData(labelj);
                    menuItem = new List <IItem>()
                    {
                        new Toggle(label, id, height, toggleSize).ApplyPosition(item)
                    };
                }
                else
                {
                    menuItem = new List <IItem>()
                    {
                        new ToggleNoLabel(id, toggleSize).ApplyPosition(item)
                    };
                }

                found = true;
                menu.LocalStorage.SetAs(id, false);
            }
            break;

            case "button":
            {
                string id;

                if (item.HasChild("id"))
                {
                    id = item.GetAs <string>("id");
                }
                else
                {
                    id = "button";
                    PandaLogger.Log("Button without ID defined, default: button");
                }

                item.TryGetAsOrDefault <int>("width", out int width, -1);
                item.TryGetAsOrDefault <int>("height", out int height, 25);

                if (item.TryGetChild("label", out JSONNode labelj))
                {
                    LabelData label = GetLabelData(labelj);
                    menuItem = new List <IItem>()
                    {
                        new ButtonCallback(id, label, width, height).ApplyPosition(item)
                    };
                }
                else
                {
                    PandaLogger.Log(string.Format("Button {0} without label", id));
                    menuItem = new List <IItem>()
                    {
                        new ButtonCallback(id, new LabelData("Key label not defined"), width, height).ApplyPosition(item)
                    };
                }

                found = true;
            }
            break;

            case "link":
            {
                string url;

                if (item.HasChild("url"))
                {
                    url = GameLoader.NAMESPACE + ".link_" + item.GetAs <string>("url");
                }
                else
                {
                    PandaLogger.Log("Link without URL defined");
                    return(found);
                }

                item.TryGetAsOrDefault <int>("width", out int width, -1);
                item.TryGetAsOrDefault <int>("height", out int height, 25);

                if (item.TryGetChild("label", out JSONNode labelj))
                {
                    LabelData label = GetLabelData(labelj);
                    menuItem = new List <IItem>()
                    {
                        new ButtonCallback(url, label, width, height).ApplyPosition(item)
                    };
                }
                else
                {
                    PandaLogger.Log(string.Format("Link {0} without label", url));
                    menuItem = new List <IItem>()
                    {
                        new ButtonCallback(url, new LabelData("Key label not defined"), width, height).ApplyPosition(item)
                    };
                }

                found = true;
            }
            break;

            case "table":
            {
                item.TryGetAsOrDefault <int>("row_height", out int height, 30);

                if (item.TryGetAs("rows", out JSONNode rows) && rows.ChildCount != 0)
                {
                    menuItem = new List <IItem>();
                    found    = true;

                    foreach (JSONNode row in rows.LoopArray())
                    {
                        List <ValueTuple <IItem, int> > items = new List <ValueTuple <IItem, int> >();
                        var width = menu.Width / row.ChildCount;

                        foreach (JSONNode col in row.LoopArray())
                        {
                            if (LoadItem(col, ref menu, player, out var newMenuItems))
                            {
                                foreach (var newItem in newMenuItems)
                                {
                                    if (col.TryGetAs("col_width", out int setWidth))
                                    {
                                        items.Add(ValueTuple.Create(newItem, setWidth));
                                    }
                                    else
                                    {
                                        items.Add(ValueTuple.Create(newItem, width));
                                    }
                                }
                            }
                        }

                        menuItem.Add(new HorizontalRow(items, height).ApplyPosition(item));
                    }
                }
            }
            break;


            default:
            {
                PandaLogger.Log(string.Format("It doesn't exist an item of type: {0}", itemType));
            }
            break;
            }

            return(found);
        }