Skip to content

Commit ad48d54

Browse files
authored
Merge pull request #4 from thomasfn/staging
release 0.1.1
2 parents 05ca98d + 6eeaedb commit ad48d54

File tree

4 files changed

+108
-10
lines changed

4 files changed

+108
-10
lines changed

EcoCompaniesMod/CompaniesPlugin.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ public static void DynamicSetGameValue(object parent, PropertyInfo prop, object
140140
// This shouldn't really be possible as EmployerLegalPersonAlias is marked as NonSelectable but do it anyway to be safe
141141
newValue = GetGameValueType<EmployerLegalPerson>();
142142
}
143+
else if (gvt.Type == typeof(CompanyCeo) && prop.PropertyType == typeof(GameValue<IAlias>))
144+
{
145+
// The property wants an IAlias and the client sent an CompanyCeo, so remap it to CompanyCeoAlias
146+
newValue = GetGameValueType<CompanyCeoAlias>();
147+
}
148+
else if (gvt.Type == typeof(CompanyCeoAlias) && prop.PropertyType == typeof(GameValue<User>))
149+
{
150+
// The property wants a User and the client sent an CompanyCeoAlias, so remap it to CompanyCeo
151+
// This shouldn't really be possible as CompanyCeoAlias is marked as NonSelectable but do it anyway to be safe
152+
newValue = GetGameValueType<CompanyCeo>();
153+
}
143154
}
144155
GameValueManager.DynamicSetGameValue(parent, prop, newValue);
145156
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
3+
namespace Eco.Mods.Companies
4+
{
5+
using Core.Controller;
6+
using Core.Utils;
7+
using Core.Utils.PropertyScanning;
8+
9+
using Shared.Localization;
10+
using Shared.Networking;
11+
using Shared.Utils;
12+
13+
using Gameplay.Civics.GameValues;
14+
using Gameplay.Economy;
15+
using Gameplay.Systems.TextLinks;
16+
using Gameplay.Players;
17+
using Gameplay.Aliases;
18+
19+
[Eco, LocCategory("Companies"), LocDescription("The CEO of the company via a legal person.")]
20+
public class CompanyCeo : GameValue<User>
21+
{
22+
[Eco, Advanced, LocDescription("The legal person of the company to fetch the CEO of.")] public GameValue<User> LegalPerson { get; set; }
23+
24+
private Eval<User> FailNullSafe<T>(Eval<T> eval, string paramName) =>
25+
eval != null ? Eval.Make($"Invalid {Localizer.DoStr(paramName)} specified on {GetType().GetLocDisplayName()}: {eval.Message}", null as User)
26+
: Eval.Make($"{Localizer.DoStr(paramName)} not set on {GetType().GetLocDisplayName()}.", null as User);
27+
28+
public override Eval<User> Value(IContextObject action)
29+
{
30+
var legalPerson = this.LegalPerson?.Value(action); if (legalPerson?.Val == null) return this.FailNullSafe(legalPerson, nameof(this.LegalPerson));
31+
var company = Company.GetFromLegalPerson(legalPerson.Val) ?? Company.GetEmployer(legalPerson.Val);
32+
33+
return Eval.Make<User>($"{company?.Ceo.UILinkNullSafe()} ({company?.UILink()}'s CEO)", company?.Ceo);
34+
}
35+
public override LocString Description() => Localizer.Do($"the company of {this.LegalPerson.DescribeNullSafe()}'s CEO");
36+
}
37+
38+
[Eco, LocCategory("Companies"), LocDescription("The CEO of the company via a legal person.")]
39+
[NonSelectable]
40+
public class CompanyCeoAlias : GameValue<IAlias>
41+
{
42+
[Eco, Advanced, LocDescription("The legal person of the company to fetch the CEO of.")] public GameValue<User> LegalPerson { get; set; }
43+
44+
private Eval<IAlias> FailNullSafe<T>(Eval<T> eval, string paramName) =>
45+
eval != null ? Eval.Make($"Invalid {Localizer.DoStr(paramName)} specified on {GetType().GetLocDisplayName()}: {eval.Message}", null as IAlias)
46+
: Eval.Make($"{Localizer.DoStr(paramName)} not set on {GetType().GetLocDisplayName()}.", null as IAlias);
47+
48+
public override Eval<IAlias> Value(IContextObject action)
49+
{
50+
var legalPerson = this.LegalPerson?.Value(action); if (legalPerson?.Val == null) return this.FailNullSafe(legalPerson, nameof(this.LegalPerson));
51+
var company = Company.GetFromLegalPerson(legalPerson.Val) ?? Company.GetEmployer(legalPerson.Val);
52+
53+
return Eval.Make<IAlias>($"{company?.Ceo.UILinkNullSafe()} ({company?.UILink()}'s CEO)", company?.Ceo);
54+
}
55+
public override LocString Description() => Localizer.Do($"the company of {this.LegalPerson.DescribeNullSafe()}'s CEO");
56+
}
57+
}

EcoCompaniesMod/GameValues/SkillCount.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class SkillCount : GameValue<float>
2222

2323
[Eco, Advanced, LocDescription("Whether to only consider unique skills.")] public GameValue<bool> UniqueSkills { get; set; } = new No();
2424

25+
[Eco, Advanced, LocDescription("Whether to return the highest employee skill count instead of the sum.")] public GameValue<bool> Highest { get; set; } = new No();
26+
2527
private Eval<float> FailNullSafeFloat<T>(Eval<T> eval, string paramName) =>
2628
eval != null ? Eval.Make($"Invalid {Localizer.DoStr(paramName)} specified on {GetType().GetLocDisplayName()}: {eval.Message}", float.MinValue)
2729
: Eval.Make($"{Localizer.DoStr(paramName)} not set on {GetType().GetLocDisplayName()}.", float.MinValue);
@@ -30,22 +32,42 @@ public override Eval<float> Value(IContextObject action)
3032
{
3133
var legalPerson = this.LegalPerson?.Value(action); if (legalPerson?.Val == null) return this.FailNullSafeFloat(legalPerson, nameof(this.LegalPerson));
3234
var uniqueSkills = this.UniqueSkills?.Value(action); if (uniqueSkills?.Val == null) return this.FailNullSafeFloat(uniqueSkills, nameof(this.UniqueSkills));
35+
var highest = this.Highest?.Value(action); if (highest?.Val == null) return this.FailNullSafeFloat(highest, nameof(this.Highest));
3336

3437
var company = Company.GetFromLegalPerson(legalPerson.Val);
3538
if (company == null) return this.FailNullSafeFloat(legalPerson, nameof(this.LegalPerson));
36-
var companySkills = company.AllEmployees
37-
.SelectMany(user => user.Skillset.Skills)
38-
.Where(skill => skill.Level > 0 && skill.RootSkillTree != skill.SkillTree)
39-
.Select(skill => skill.GetType());
40-
if (uniqueSkills.Val)
39+
40+
float companySkillCount;
41+
if (highest.Val)
42+
{
43+
companySkillCount = company.AllEmployees
44+
.Select(user => user.Skillset.Skills.Where(skill => skill.Level > 0 && skill.RootSkillTree != skill.SkillTree).Count())
45+
.OrderByDescending(count => count)
46+
.FirstOrDefault();
47+
// TODO: Figure out what it means when unique = true, e.g. in what case does unique highest != highest?
48+
}
49+
else
4150
{
42-
companySkills = companySkills.Distinct();
51+
var companySkills = company.AllEmployees
52+
.SelectMany(user => user.Skillset.Skills)
53+
.Where(skill => skill.Level > 0 && skill.RootSkillTree != skill.SkillTree)
54+
.Select(skill => skill.GetType());
55+
if (uniqueSkills.Val)
56+
{
57+
companySkills = companySkills.Distinct();
58+
}
59+
companySkillCount = companySkills.Count();
4360
}
44-
float companySkillCount = companySkills.Count();
4561

46-
return Eval.Make($"{Text.StyledNum(companySkillCount)} ({(uniqueSkills.Val ? "unique " : "")}skill count of {company.UILink()})", companySkillCount);
62+
return Eval.Make($"{Text.StyledNum(companySkillCount)} ({(highest.Val ? "highest " : "")}{(uniqueSkills.Val ? "unique " : "")}skill count of {company.UILink()})", companySkillCount);
4763
}
4864

49-
public override LocString Description() => Localizer.Do($"({(UniqueSkills is Yes ? "unique " : UniqueSkills is No ? "" : $"unique (when {UniqueSkills.DescribeNullSafe()}) ")}skill count of company of {LegalPerson.DescribeNullSafe()}");
65+
public override LocString Description() => Localizer.Do($"({DescribeHighest()}{DescribeUniqueSkills()}skill count of company of {LegalPerson.DescribeNullSafe()}");
66+
67+
private string DescribeUniqueSkills()
68+
=> UniqueSkills is Yes ? "unique " : UniqueSkills is No ? "" : $"unique (when {UniqueSkills.DescribeNullSafe()}) ";
69+
70+
private string DescribeHighest()
71+
=> Highest is Yes ? "highest " : Highest is No ? "" : $"highest (when {Highest.DescribeNullSafe()}) ";
5072
}
5173
}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ Retrieves the legal person user from a given employee user. This is helpful to d
4646
| - | - | - |
4747
| Citizen | User | The employee. |
4848

49+
#### Company CEO
50+
Retrieves the CEO user from a given company. The legal person for the company will be needed as context.
51+
52+
| Property Name | Type | Description |
53+
| - | - | - |
54+
| LegalPerson | User | The legal person of the company being evaluated. This could be retrieved from context via Account Legal Person or Employer Legal Person, or directly if using a citizen timer combined with a Is Company Legal Person condition. |
55+
4956
#### Employee Count
5057
Retrieves the number of employees of a company, including the CEO. The legal person for the company will be needed as context.
5158

@@ -54,12 +61,13 @@ Retrieves the number of employees of a company, including the CEO. The legal per
5461
| LegalPerson | User | The legal person of the company being evaluated. This could be retrieved from context via Account Legal Person or Employer Legal Person, or directly if using a citizen timer combined with a Is Company Legal Person condition. |
5562

5663
#### Skill Count
57-
Retrieves the number of specialisations of all employees of a company, including Self Improvement. This only counts skills into which a star has been invested. There is an option to choose unique skills only or not. The legal person for the company will be needed as context.
64+
Retrieves the number of specialisations of all employees of a company, including Self Improvement. This only counts skills into which a star has been invested. There is an option to choose unique skills only or not and an option to choose whether to pick the highest skill count or sum them. The legal person for the company will be needed as context.
5865

5966
| Property Name | Type | Description |
6067
| - | - | - |
6168
| LegalPerson | User | The legal person of the company being evaluated. This could be retrieved from context via Account Legal Person or Employer Legal Person, or directly if using a citizen timer combined with a Is Company Legal Person condition. |
6269
| UniqueSkills | Yes/No | Whether to consider unique skills only. For example, two employees both with Mining would count as 2 skills, but only 1 unique skill. |
70+
| Highest | Yes/No | Whether to select the highest number of skills held per employee rather than the sum. |
6371

6472
#### Is CEO Of Company
6573
Gets if the given citizen is the CEO of any company.

0 commit comments

Comments
 (0)