2 using System.Globalization;
4 using Microsoft.Xna.Framework;
13 private GUITextBlock votingTextBlock, votedTextBlock, voteCounter;
15 private GUIButton yesVoteButton, noVoteButton;
16 private Action onVoteEnd;
18 private int yesVotes, noVotes, maxVotes;
19 private Func<int> getYesVotes, getNoVotes, getMaxVotes;
20 private bool votePassed;
23 private float votingTime = 100f;
25 private VoteType currentVoteType;
26 private static Color SubmarineColor => GUIStyle.Orange;
27 private Point createdForResolution;
34 if (starter ==
null || info ==
null) {
return null; }
38 votingTime = votingTime,
43 subVoting.onVoteEnd = () => subVoting.SendSubmarineVoteEndMessage(info, type);
44 subVoting.SetSubmarineVotingText(starter, info, transferItems, type);
45 subVoting.Initialize(starter, type);
51 if (starter ==
null) {
return null; }
52 if (from ==
null && to ==
null) {
return null; }
56 votingTime = votingTime,
61 transferVoting.onVoteEnd = () => transferVoting.SendMoneyTransferVoteEndMessage(from, to, amount);
62 transferVoting.SetMoneyTransferVotingText(starter, from, to, amount);
63 transferVoting.Initialize(starter, VoteType.TransferMoney);
64 return transferVoting;
68 private void Initialize(
Client starter, VoteType type)
70 currentVoteType = type;
76 private void CreateVotingGUI()
78 createdForResolution =
new Point(GameMain.GraphicsWidth, GameMain.GraphicsHeight);
81 frame =
new GUIFrame(HUDLayoutSettings.ToRectTransform(HUDLayoutSettings.VotingArea, GameMain.Client.InGameHUD.RectTransform), style:
"");
83 int padding = HUDLayoutSettings.Padding * 2;
84 int spacing = HUDLayoutSettings.Padding;
85 int yOffset = padding;
86 int paddedWidth = frame.
Rect.Width - padding * 2;
88 votingTextBlock =
new GUITextBlock(
new RectTransform(
new Point(paddedWidth, 0), frame.
RectTransform), votingOnText, wrap:
true);
93 yOffset += votingTextBlock.
Rect.Height + spacing;
95 voteCounter =
new GUITextBlock(
new RectTransform(
new Point(paddedWidth, 0), frame.
RectTransform),
"(0/0)", GUIStyle.Green, textAlignment: Alignment.Center);
100 yOffset += voteCounter.
Rect.Height + spacing;
102 votingTimer =
new GUIProgressBar(
new RectTransform(
new Point(paddedWidth, Math.Max(spacing, 8)), frame.
RectTransform) { AbsoluteOffset = new Point(padding, yOffset) }, HUDLayoutSettings.Padding);
104 yOffset += votingTimer.
Rect.Height + spacing;
106 int buttonWidth = (int)(paddedWidth * 0.3f);
107 yesVoteButton =
new GUIButton(
new RectTransform(
new Point(buttonWidth, 0), frame.
RectTransform) { AbsoluteOffset = new Point((int)(frame.Rect.Width / 2f - buttonWidth - spacing), yOffset) }, TextManager.Get(
"yes"))
109 OnClicked = (applyButton, obj) =>
111 SetGUIToVotedState(2);
112 GameMain.Client.Vote(currentVoteType, 2);
117 noVoteButton =
new GUIButton(
new RectTransform(
new Point(buttonWidth, 0), frame.
RectTransform) { AbsoluteOffset = new Point(yesVoteButton.RectTransform.AbsoluteOffset.X + yesVoteButton.Rect.Width + padding, yOffset) }, TextManager.Get(
"no"))
119 OnClicked = (applyButton, obj) =>
121 SetGUIToVotedState(1);
122 GameMain.Client.Vote(currentVoteType, 1);
127 votedTextBlock =
new GUITextBlock(
new RectTransform(
new Point(paddedWidth, yesVoteButton.
Rect.Height), frame.
RectTransform),
string.Empty, textAlignment: Alignment.Center);
130 votedTextBlock.
Visible =
false;
132 yOffset += yesVoteButton.
Rect.Height;
137 private void SetGUIToVotedState(
int vote)
140 votedTextBlock.
Text = TextManager.Get(vote == 2 ?
"yesvoted" :
"novoted");
148 yesVotes = getYesVotes();
149 noVotes = getNoVotes();
150 maxVotes = getMaxVotes();
151 voteCounter.
Text = $
"({yesVotes + noVotes}/{maxVotes})";
153 votingTimer.
BarSize = timer / votingTime;
156 public void EndVote(
bool passed,
int yesVoteFinal,
int noVoteFinal)
160 yesVotes = yesVoteFinal;
161 noVotes = noVoteFinal;
165 #region Submarine Voting
167 private void SetSubmarineVotingText(
Client starter,
SubmarineInfo info,
bool transferItems, VoteType type)
170 string name = starter.
Name;
172 Color nameColor = prefab !=
null ? prefab.
UIColor : Color.White;
173 string characterRichString = $
"‖color:{nameColor.R},{nameColor.G},{nameColor.B}‖{name}‖color:end‖";
174 string submarineRichString = $
"‖color:{SubmarineColor.R},{SubmarineColor.G},{SubmarineColor.B}‖{info.DisplayName}‖color:end‖";
175 string tag =
string.Empty;
179 case VoteType.PurchaseAndSwitchSub:
180 tag = transferItems ?
"submarinepurchaseandswitchwithitemsvote" :
"submarinepurchaseandswitchvote";
181 text = TextManager.GetWithVariables(tag,
182 (
"[playername]", characterRichString),
183 (
"[submarinename]", submarineRichString),
184 (
"[amount]", price.ToString()),
185 (
"[currencyname]", TextManager.Get(
"credit").ToLower()));
187 case VoteType.PurchaseSub:
188 text = TextManager.GetWithVariables(
"submarinepurchasevote",
189 (
"[playername]", characterRichString),
190 (
"[submarinename]", submarineRichString),
191 (
"[amount]", price.ToString()),
192 (
"[currencyname]", TextManager.Get(
"credit").ToLower()));
194 case VoteType.SwitchSub:
195 tag = transferItems ?
"submarineswitchwithitemsnofeevote" :
"submarineswitchnofeevote";
196 text = TextManager.GetWithVariables(tag,
197 (
"[playername]", characterRichString),
198 (
"[submarinename]", submarineRichString));
201 votingOnText = RichString.
Rich(text);
204 private void SendSubmarineVoteEndMessage(SubmarineInfo info, VoteType type)
206 GameMain.NetworkMember.AddChatMessage(GetSubmarineVoteResultMessage(info, type, yesVotes, noVotes, votePassed).Value,
ChatMessageType.Server);
209 private LocalizedString GetSubmarineVoteResultMessage(SubmarineInfo info, VoteType type,
int yesVoteCount,
int noVoteCount,
bool votePassed)
211 int price = info.GetPrice();
212 LocalizedString result =
string.Empty;
216 case VoteType.PurchaseAndSwitchSub:
217 result = TextManager.GetWithVariables(votePassed ?
"submarinepurchaseandswitchvotepassed" :
"submarinepurchaseandswitchvotefailed",
218 (
"[submarinename]", info.DisplayName),
219 (
"[amount]",
string.Format(CultureInfo.InvariantCulture,
"{0:N0}", price)),
220 (
"[currencyname]", TextManager.Get(
"credit").ToLower()),
221 (
"[yesvotecount]", yesVoteCount.ToString()),
222 (
"[novotecount]" , noVoteCount.ToString()));
224 case VoteType.PurchaseSub:
225 result = TextManager.GetWithVariables(votePassed ?
"submarinepurchasevotepassed" :
"submarinepurchasevotefailed",
226 (
"[submarinename]", info.DisplayName),
227 (
"[amount]",
string.Format(CultureInfo.InvariantCulture,
"{0:N0}", price)),
228 (
"[currencyname]", TextManager.Get(
"credit").ToLower()),
229 (
"[yesvotecount]", yesVoteCount.ToString()),
230 (
"[novotecount]", noVoteCount.ToString()));
232 case VoteType.SwitchSub:
233 result = TextManager.GetWithVariables(votePassed ?
"submarineswitchnofeevotepassed" :
"submarineswitchnofeevotefailed",
234 (
"[submarinename]", info.DisplayName),
235 (
"[yesvotecount]", yesVoteCount.ToString()),
236 (
"[novotecount]", noVoteCount.ToString()));
246 private void SetMoneyTransferVotingText(
Client starter,
Client from,
Client to,
int amount)
248 string name = starter.
Name;
250 Color nameColor = prefab !=
null ? prefab.
UIColor : Color.White;
251 string characterRichString = $
"‖color:{nameColor.R},{nameColor.G},{nameColor.B}‖{name}‖color:end‖";
253 LocalizedString text =
string.Empty;
254 if (from ==
null && to !=
null)
256 text = TextManager.GetWithVariables(
"crewwallet.requestbanktoselfvote",
257 (
"[requester]", characterRichString),
258 (
"[amount]",
string.Format(CultureInfo.InvariantCulture,
"{0:N0}", amount)));
260 else if (from !=
null && to ==
null)
262 text = TextManager.GetWithVariables(
"crewwallet.requestselftobankvote",
263 (
"[requester]", characterRichString),
264 (
"[amount]",
string.Format(CultureInfo.InvariantCulture,
"{0:N0}", amount)));
269 LocalizedString bankName = TextManager.Get(
"crewwallet.bank");
270 text = TextManager.GetWithVariables(
"crewwallet.requesttransfervote",
271 (
"[requester]", characterRichString),
272 (
"[player1]", from?.Character ==
null ? bankName : from.
Character.
Name),
273 (
"[player2]", to?.Character ==
null ? bankName : to.
Character.
Name),
274 (
"[amount]",
string.Format(CultureInfo.InvariantCulture,
"{0:N0}", amount)));
277 votingOnText = RichString.
Rich(text);
279 private void SendMoneyTransferVoteEndMessage(
Client from,
Client to,
int amount)
287 if (from ==
null && to !=
null)
289 result = TextManager.GetWithVariables(votePassed ?
"crewwallet.banktoplayer.votepassed" :
"crewwallet.banktoplayer.votefailed",
290 (
"[playername]", to.
Name),
291 (
"[amount]",
string.Format(CultureInfo.InvariantCulture,
"{0:N0}", transferAmount)),
292 (
"[yesvotecount]", yesVoteCount.ToString()),
293 (
"[novotecount]", noVoteCount.ToString()));
virtual void RemoveChild(GUIComponent child)
RectTransform RectTransform
static int GraphicsHeight
static NetworkMember NetworkMember
readonly byte SessionId
An ID for this client for the current session. THIS IS NOT A PERSISTENT VALUE. DO NOT STORE THIS LONG...
static RichString Rich(LocalizedString str, Func< string, string >? postProcess=null)
int GetPrice(Location location=null, ImmutableHashSet< Character > characterList=null)
void Update(float deltaTime)
void EndVote(bool passed, int yesVoteFinal, int noVoteFinal)
static VotingInterface CreateMoneyTransferVotingInterface(Client starter, Client from, Client to, int amount, float votingTime)
static LocalizedString GetMoneyTransferVoteResultMessage(Client from, Client to, int transferAmount, int yesVoteCount, int noVoteCount, bool votePassed)
static VotingInterface CreateSubmarineVotingInterface(Client starter, SubmarineInfo info, VoteType type, bool transferItems, float votingTime)