2 using Microsoft.Xna.Framework;
3 using System.Collections.Generic;
12 private struct SubmarineVoteInfo
14 public SubmarineInfo SubmarineInfo {
get;
set; }
15 public bool TransferItems {
get;
set; }
17 public SubmarineVoteInfo(SubmarineInfo submarineInfo,
bool transferItems)
19 SubmarineInfo = submarineInfo;
20 TransferItems = transferItems;
24 private readonly Dictionary<VoteType, int>
25 voteCountYes =
new Dictionary<VoteType, int>(),
26 voteCountNo =
new Dictionary<VoteType, int>(),
27 voteCountMax =
new Dictionary<VoteType, int>();
31 voteCountYes.TryGetValue(voteType, out
int value);
36 voteCountNo.TryGetValue(voteType, out
int value);
41 voteCountMax.TryGetValue(voteType, out
int value);
46 voteCountYes[voteType] = value;
50 voteCountNo[voteType] = value;
54 voteCountMax[voteType] = value;
63 GUIListBox listBox = (voteType == VoteType.Sub) ?
71 if (clients ==
null) {
return; }
73 IReadOnlyDictionary<object, int> voteList = GetVoteCounts<object>(voteType, clients);
74 foreach (KeyValuePair<object, int> votable
in voteList)
76 SetVoteText(listBox, votable.Key, votable.Value);
79 case VoteType.StartRound:
80 if (clients ==
null) {
return; }
81 foreach (
Client client
in clients)
84 if (clientReady !=
null)
86 clientReady.
Visible = client.GetVote<
bool>(VoteType.StartRound);
93 private void SetVoteText(
GUIListBox listBox,
object userData,
int votes)
95 if (userData ==
null) {
return; }
98 if (comp.UserData != userData) {
continue; }
99 if (comp.FindChild(
"votes") is not GUITextBlock voteText)
101 voteText =
new GUITextBlock(
new RectTransform(
new Point(GUI.IntScale(30), comp.Rect.Height), comp.RectTransform,
Anchor.CenterRight),
102 "", textAlignment: Alignment.Center)
104 Padding = Vector4.Zero,
108 voteText.Text = votes == 0 ?
"" : votes.ToString();
114 foreach (
Client client
in connectedClients)
119 foreach (VoteType voteType
in Enum.GetValues(typeof(VoteType)))
143 if (sub.EqualityCheckVal <= 0)
153 case VoteType.EndRound:
154 if (data is not
bool endRound) {
return false; }
158 if (data is not
Client votedClient) {
return false; }
162 case VoteType.StartRound:
163 if (data is not
bool startRound) {
return false; }
166 case VoteType.PurchaseAndSwitchSub:
167 case VoteType.PurchaseSub:
168 case VoteType.SwitchSub:
186 case VoteType.TransferMoney:
187 if (data is not
int money) {
return false; }
191 case VoteType.Traitor:
208 for (
int i = 0; i < votableCount; i++)
212 List<SubmarineInfo> serversubs =
new List<SubmarineInfo>();
220 SubmarineInfo sub = serversubs.FirstOrDefault(s => s.Name == subName);
229 for (
int i = 0; i < votableCount; i++)
245 byte activeVoteStateByte = inc.
ReadByte();
248 try { activeVoteState = (
VoteState)activeVoteStateByte; }
249 catch (System.Exception e)
251 DebugConsole.ThrowError(
"Failed to cast vote type \"" + activeVoteStateByte +
"\"", e);
257 VoteType voteType = VoteType.Unknown;
258 try { voteType = (VoteType)voteTypeByte; }
259 catch (System.Exception e)
261 DebugConsole.ThrowError(
"Failed to cast vote type \"" + voteTypeByte +
"\"", e);
264 int readVote(
int value)
267 for (
int i = 0; i < clientCount; i++)
271 matchingClient?.SetVote(voteType, value);
277 int yesClientCount = readVote(value: 2);
278 int noClientCount = readVote(value: 1);
280 byte maxClientCount = inc.
ReadByte();
286 switch (activeVoteState)
294 if (myClient ==
null || !myClient.
InGame) {
return; }
298 case VoteType.PurchaseSub:
299 case VoteType.PurchaseAndSwitchSub:
300 case VoteType.SwitchSub:
306 DebugConsole.ThrowError(
"Failed to find a matching submarine, vote aborted");
311 case VoteType.TransferMoney:
328 SubmarineVoteInfo submarineVoteInfo =
default;
331 case VoteType.PurchaseSub:
332 case VoteType.PurchaseAndSwitchSub:
333 case VoteType.SwitchSub:
339 if (submarineInfo ==
null)
341 DebugConsole.ThrowError(
"Failed to find a matching submarine, vote aborted");
344 submarineVoteInfo =
new SubmarineVoteInfo(submarineInfo, transferItems);
350 if (passed && submarineVoteInfo.SubmarineInfo is { } subInfo)
354 case VoteType.PurchaseAndSwitchSub:
360 case VoteType.PurchaseSub:
363 case VoteType.SwitchSub:
375 byte readyClientCount = inc.
ReadByte();
376 for (
int i = 0; i < readyClientCount; i++)
380 matchingClient?.SetVote(VoteType.StartRound,
true);
GUIComponent FindChild(Func< GUIComponent, bool > predicate, bool recursive=false)
IEnumerable< GUIComponent > Children
override void RemoveChild(GUIComponent child)
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
static GameSession?? GameSession
static NetLobbyScreen NetLobbyScreen
static NetworkMember NetworkMember
static List< GameModePreset > List
List< SubmarineInfo > OwnedSubmarines
bool TryPurchaseSubmarine(SubmarineInfo newSubmarine, Client? client=null)
void SwitchSubmarine(SubmarineInfo newSubmarine, bool transferItems, Client? client=null)
Switch to another submarine. The sub is loaded when the next round starts.
VotingInterface VotingInterface
readonly List< SubmarineInfo > ServerSubmarines
void ShowMoneyTransferVoteInterface(Client starter, Client from, int amount, Client to, float timeOut)
void ShowSubmarineChangeVoteInterface(Client starter, SubmarineInfo info, VoteType type, bool transferItems, float timeOut)
static bool ContentRefreshRequired
void ClientRead(IReadMessage inc)
int GetVoteCountMax(VoteType voteType)
int GetVoteCountNo(VoteType voteType)
void SetVoteCountMax(VoteType voteType, int value)
void ResetVotes(IEnumerable< Client > connectedClients)
bool ClientWrite(IWriteMessage msg, VoteType voteType, object data)
Returns true if the given data is valid for the given vote type, returns false otherwise....
void SetVoteCountYes(VoteType voteType, int value)
void UpdateVoteTexts(IEnumerable< Client > clients, VoteType voteType)
int GetVoteCountYes(VoteType voteType)
void SetVoteCountNo(VoteType voteType, int value)
void EndVote(bool passed, int yesVoteFinal, int noVoteFinal)
void WriteString(string val)
void WriteBoolean(bool val)
void WriteIdentifier(Identifier val)
void WriteInt32(Int32 val)