Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Networking/Voting.cs
2 using System.Collections.Generic;
3 
4 namespace Barotrauma
5 {
6  partial class Voting
7  {
8  public enum VoteState { None = 0, Started = 1, Running = 2, Passed = 3, Failed = 4 };
9 
10  private static IReadOnlyDictionary<T, int> GetVoteCounts<T>(VoteType voteType, IEnumerable<Client> voters)
11  {
12  Dictionary<T, int> voteList = new Dictionary<T, int>();
13 
14  foreach (Client voter in voters)
15  {
16  T vote = voter.GetVote<T>(voteType);
17  if (vote == null) continue;
18 
19  if (!voteList.ContainsKey(vote))
20  {
21  voteList.Add(vote, 1);
22  }
23  else
24  {
25  voteList[vote]++;
26  }
27  }
28  return voteList;
29  }
30 
31  public static T HighestVoted<T>(VoteType voteType, IEnumerable<Client> voters)
32  {
33  return HighestVoted<T>(voteType, voters, out _);
34  }
35 
36  public static T HighestVoted<T>(VoteType voteType, IEnumerable<Client> voters, out int voteCount)
37  {
38  voteCount = 0;
39  if (voteType == VoteType.Sub && !GameMain.NetworkMember.ServerSettings.AllowSubVoting) { return default; }
40  if (voteType == VoteType.Mode && !GameMain.NetworkMember.ServerSettings.AllowModeVoting) { return default; }
41 
42  IReadOnlyDictionary<T, int> voteList = GetVoteCounts<T>(voteType, voters);
43 
44  T selected = default;
45  int highestVotes = 0;
46  foreach (KeyValuePair<T, int> votable in voteList)
47  {
48  if (voteType == VoteType.Sub
49  && votable.Key is SubmarineInfo subInfo
50  && GameMain.NetworkMember.ServerSettings.HiddenSubs.Contains(subInfo.Name))
51  {
52  //This sub is hidden so it can't be voted for, skip
53  continue;
54  }
55  if (selected == null || votable.Value > highestVotes)
56  {
57  highestVotes = votable.Value;
58  selected = votable.Key;
59  }
60  }
61  voteCount = highestVotes;
62  return selected;
63  }
64  }
65 }
static NetworkMember NetworkMember
Definition: GameMain.cs:190
static T HighestVoted< T >(VoteType voteType, IEnumerable< Client > voters)