Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Content.Client/Voting/UI/VotePopup.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public VotePopup(VoteManager.ActiveVote vote)

Modulate = Color.White.WithAlpha(0.75f);
_voteButtons = new Button[vote.Entries.Length];
var group = new ButtonGroup();
var group = _vote.Multivariate ? null : new ButtonGroup(); // Erida-edit

for (var i = 0; i < _voteButtons.Length; i++)
{
Expand Down Expand Up @@ -70,9 +70,17 @@ public void UpdateData()
{
_voteButtons[i].Text = Loc.GetString("ui-vote-button-no-votes", ("text", entry.Text));
}

if (_vote.OurVote == i)
_voteButtons[i].Pressed = true;
// Erida-start
if (_vote.OurVotes != null)
if (_vote.OurVotes.Contains(i))
{
_voteButtons[i].Pressed = true;
}
else
{
_voteButtons[i].Pressed = false;
}
// Erida-end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно сократить до
if (_vote.OurVotes != null)
_voteButtons[i].Pressed = _vote.OurVotes.Contains(i);

}
}

Expand Down
25 changes: 19 additions & 6 deletions Content.Client/Voting/VoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ private void ReceiveVoteData(MsgVoteData message)
{
Entries = message.Options
.Select(c => new VoteEntry(c.name))
.ToArray()
.ToArray(),
Multivariate = message.Multivariate // Erida-edit
};

existingVote = vote;
_votes.Add(voteId, vote);
}
Expand All @@ -176,8 +176,9 @@ private void ReceiveVoteData(MsgVoteData message)
}

// Update vote data from incoming.
if (message.IsYourVoteDirty)
existingVote.OurVote = message.YourVote;
if (message.IsYourVoteDirty
&& message.YourVotes != null) // Erida-edit
existingVote.OurVotes = message.YourVotes.Select(b => (int)b).ToList(); // Erida-edit
// On the server, most of these params can't change.
// It can't hurt to just re-set this stuff since I'm lazy and the server is sending it anyways, so...
existingVote.Initiator = message.VoteInitiator;
Expand All @@ -186,6 +187,7 @@ private void ReceiveVoteData(MsgVoteData message)
existingVote.EndTime = _gameTiming.RealServerToLocal(message.EndTime);
existingVote.DisplayVotes = message.DisplayVotes;
existingVote.TargetEntity = message.TargetEntity;
existingVote.Multivariate = message.Multivariate; // Erida-edit

// Logger.Debug($"{existingVote.StartTime}, {existingVote.EndTime}, {_gameTiming.RealTime}");

Expand Down Expand Up @@ -232,7 +234,17 @@ public void SendCastVote(int voteId, int option)
var data = _votes[voteId];
// Update immediately to avoid any funny reconciliation bugs.
// See also code in server side to avoid bulldozing this.
data.OurVote = option;
// Erida-start
if (data.Multivariate
&& data.OurVotes != null)
{
data.OurVotes.Add(option);
}
else
{
data.OurVotes = new List<int> { option };
}
// Erida-end
_console.LocalShell.RemoteExecuteCommand($"vote {voteId} {option}");
}

Expand All @@ -245,10 +257,11 @@ public sealed class ActiveVote
public TimeSpan EndTime;
public string Title = "";
public string Initiator = "";
public int? OurVote;
public List<int>? OurVotes = new(); // Erida-edit
public int Id;
public bool DisplayVotes;
public int? TargetEntity; // NetEntity
public bool Multivariate; // Erida-edit
public ActiveVote(int voteId)
{
Id = voteId;
Expand Down
2 changes: 1 addition & 1 deletion Content.Server/Voting/IVoteHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface IVoteHandle
/// <summary>
/// Dictionary of votes cast by players, matching the option's id.
/// </summary>
IReadOnlyDictionary<ICommonSession, int> CastVotes { get; }
IReadOnlyDictionary<ICommonSession, List<int>> CastVotes { get; } // Erida-edit

/// <summary>
/// Current count of votes per option type.
Expand Down
10 changes: 6 additions & 4 deletions Content.Server/Voting/Managers/VoteManager.DefaultVotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ private void CreatePresetVote(ICommonSession? initiator)
Title = Loc.GetString("ui-vote-gamemode-title"),
Duration = alone
? TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.VoteTimerAlone))
: TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.VoteTimerPreset))
: TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.VoteTimerPreset)),
Multivariate = true // Erida-edit
};

if (alone)
Expand Down Expand Up @@ -272,7 +273,8 @@ private void CreateMapVote(ICommonSession? initiator)
Title = Loc.GetString("ui-vote-map-title"),
Duration = alone
? TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.VoteTimerAlone))
: TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.VoteTimerMap))
: TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.VoteTimerMap)),
Multivariate = true // Erida-edit
};

if (alone)
Expand Down Expand Up @@ -488,11 +490,11 @@ private async void CreateVotekickVote(ICommonSession? initiator, string[]? args)
List<ICommonSession> noVoters = new();
foreach (var (voter, castVote) in vote.CastVotes)
{
if (castVote == 0)
if (castVote[0] == 0) // Erida-edit
{
yesVoters.Add(voter);
}
if (castVote == 1)
if (castVote[1] == 1) // Erida-edit
{
noVoters.Add(voter);
}
Expand Down
71 changes: 55 additions & 16 deletions Content.Server/Voting/Managers/VoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,53 @@ private void CastVote(VoteReg v, ICommonSession player, int? option)
{
if (!IsValidOption(v, option))
throw new ArgumentOutOfRangeException(nameof(option), "Invalid vote option ID");

if (v.CastVotes.TryGetValue(player, out var existingOption))
// Erida-start
if (v.Multivariate)
{
v.Entries[existingOption].Votes -= 1;
}
if (!v.CastVotes.ContainsKey(player))
{
v.CastVotes[player] = new List<int>();
}

if (option != null)
{
v.Entries[option.Value].Votes += 1;
v.CastVotes[player] = option.Value;
var playerVotes = v.CastVotes[player];

if (option != null)
{
if (playerVotes.Contains(option.Value))
{
playerVotes.Remove(option.Value);
v.Entries[option.Value].Votes -= 1;
}
else
{
v.Entries[option.Value].Votes += 1;
playerVotes.Add(option.Value);
}

if (playerVotes.Count == 0)
{
v.CastVotes.Remove(player); // ???
}
}
}
else
{
v.CastVotes.Remove(player);
}
if (v.CastVotes.TryGetValue(player, out var existingOption))
{
v.Entries[existingOption[0]].Votes -= 1;
}

if (option != null)
{
v.Entries[option.Value].Votes += 1;
v.CastVotes[player] = new List<int> { option.Value };
}
else
{
v.CastVotes.Remove(player);
}
}
// Erida-end
v.VotesDirty.Add(player);
v.Dirty = true;
}
Expand Down Expand Up @@ -211,7 +242,7 @@ public IVoteHandle CreateVote(VoteOptions options)
var start = _timing.RealTime;
var end = start + options.Duration;
var reg = new VoteReg(id, entries, options.Title, options.InitiatorText,
options.InitiatorPlayer, start, end, options.VoterEligibility, options.DisplayVotes, options.TargetEntity);
options.InitiatorPlayer, start, end, options.VoterEligibility, options.DisplayVotes, options.TargetEntity, options.Multivariate); // Erida-edit

var handle = new VoteHandle(this, reg);

Expand Down Expand Up @@ -260,6 +291,7 @@ private void SendSingleUpdate(VoteReg v, ICommonSession player)
msg.VoteInitiator = v.InitiatorText;
msg.StartTime = v.StartTime;
msg.EndTime = v.EndTime;
msg.Multivariate = v.Multivariate; // Erida-edit

if (v.TargetEntity != null)
{
Expand All @@ -276,7 +308,7 @@ private void SendSingleUpdate(VoteReg v, ICommonSession player)
msg.IsYourVoteDirty = dirty;
if (dirty)
{
msg.YourVote = (byte) cast;
msg.YourVotes = cast.Select(c => (byte)c).ToArray(); // Erida-edit
}
}

Expand Down Expand Up @@ -387,7 +419,12 @@ private void EndVote(VoteReg v)
{
if (!CheckVoterEligibility(playerVote.Key, v.VoterEligibility))
{
v.Entries[playerVote.Value].Votes -= 1;
// Erida-start
foreach (var value in playerVote.Value)
{
v.Entries[value].Votes -= 1;
}
// Erida-end
v.CastVotes.Remove(playerVote.Key);
}
}
Expand Down Expand Up @@ -496,7 +533,7 @@ private void WirePresetVoteInitiator(VoteOptions options, ICommonSession? player
private sealed class VoteReg
{
public readonly int Id;
public readonly Dictionary<ICommonSession, int> CastVotes = new();
public readonly Dictionary<ICommonSession, List<int>> CastVotes = new(); // Erida-edit
public readonly VoteEntry[] Entries;
public readonly string Title;
public readonly string InitiatorText;
Expand All @@ -506,6 +543,7 @@ private sealed class VoteReg
public readonly VoterEligibility VoterEligibility;
public readonly bool DisplayVotes;
public readonly NetEntity? TargetEntity;
public readonly bool Multivariate; // Erida-edit

public bool Cancelled;
public bool Finished;
Expand All @@ -516,7 +554,7 @@ private sealed class VoteReg
public ICommonSession? Initiator { get; }

public VoteReg(int id, VoteEntry[] entries, string title, string initiatorText,
ICommonSession? initiator, TimeSpan start, TimeSpan end, VoterEligibility voterEligibility, bool displayVotes, NetEntity? targetEntity)
ICommonSession? initiator, TimeSpan start, TimeSpan end, VoterEligibility voterEligibility, bool displayVotes, NetEntity? targetEntity, bool multivariate) // Erida-edit
{
Id = id;
Entries = entries;
Expand All @@ -528,6 +566,7 @@ public VoteReg(int id, VoteEntry[] entries, string title, string initiatorText,
VoterEligibility = voterEligibility;
DisplayVotes = displayVotes;
TargetEntity = targetEntity;
Multivariate = multivariate; // Erida-edit
}
}

Expand Down Expand Up @@ -567,7 +606,7 @@ private sealed class VoteHandle : IVoteHandle
public string InitiatorText => _reg.InitiatorText;
public bool Finished => _reg.Finished;
public bool Cancelled => _reg.Cancelled;
public IReadOnlyDictionary<ICommonSession, int> CastVotes => _reg.CastVotes;
public IReadOnlyDictionary<ICommonSession, List<int>> CastVotes => _reg.CastVotes; // Erida-edit

public IReadOnlyDictionary<object, int> VotesPerOption { get; }

Expand Down
4 changes: 3 additions & 1 deletion Content.Server/Voting/VoteOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public sealed class VoteOptions
public bool DisplayVotes = true;

/// <summary>
/// Whether the vote should have an entity attached to it, to be used for things like letting ghosts follow it.
/// Whether the vote should have an entity attached to it, to be used for things like letting ghosts follow it.
/// </summary>
public NetEntity? TargetEntity = null;

Expand All @@ -75,5 +75,7 @@ public void SetInitiatorOrServer(ICommonSession? player)
InitiatorText = Loc.GetString("vote-options-server-initiator-text");
}
}

public bool Multivariate = false; // Erida-edit
}
}
33 changes: 28 additions & 5 deletions Content.Shared/Voting/MsgVoteData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public sealed class MsgVoteData : NetMessage
public TimeSpan EndTime; // Server RealTime.
public (ushort votes, string name)[] Options = default!;
public bool IsYourVoteDirty;
public byte? YourVote;
public byte[]? YourVotes; // Erida-edit
public bool DisplayVotes;
public int TargetEntity;
public bool Multivariate; // Erida-edit

public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer)
{
Expand All @@ -35,6 +36,7 @@ public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer
EndTime = TimeSpan.FromTicks(buffer.ReadInt64());
DisplayVotes = buffer.ReadBoolean();
TargetEntity = buffer.ReadVariableInt32();
Multivariate = buffer.ReadBoolean(); // Erida-edit

Options = new (ushort votes, string name)[buffer.ReadByte()];
for (var i = 0; i < Options.Length; i++)
Expand All @@ -45,8 +47,22 @@ public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer
IsYourVoteDirty = buffer.ReadBoolean();
if (IsYourVoteDirty)
{
YourVote = buffer.ReadBoolean() ? buffer.ReadByte() : null;
// Erida-start
if (buffer.ReadBoolean())
{
var voteCount = buffer.ReadByte();
YourVotes = new byte[voteCount];
for (var i = 0; i < voteCount; i++)
{
YourVotes[i] = buffer.ReadByte();
}
}
else
{
YourVotes = null;
}
}
// Erida-end
}

public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer)
Expand All @@ -64,6 +80,7 @@ public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer
buffer.Write(EndTime.Ticks);
buffer.Write(DisplayVotes);
buffer.WriteVariableInt32(TargetEntity);
buffer.Write(Multivariate); // Erida-edit

buffer.Write((byte) Options.Length);
foreach (var (votes, name) in Options)
Expand All @@ -75,12 +92,18 @@ public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer
buffer.Write(IsYourVoteDirty);
if (IsYourVoteDirty)
{
buffer.Write(YourVote.HasValue);
if (YourVote.HasValue)
// Erida-start
buffer.Write(YourVotes != null);
if (YourVotes != null)
{
buffer.Write(YourVote.Value);
buffer.Write((byte) YourVotes.Length);
foreach (var vote in YourVotes)
{
buffer.Write(vote);
}
}
}
// Erida-end
}

public override NetDeliveryMethod DeliveryMethod => NetDeliveryMethod.ReliableOrdered;
Expand Down
4 changes: 2 additions & 2 deletions Resources/Prototypes/_Erida/game_presets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- type: gamePreset
id: LightDynamic
name: light-dynamic-title
showInVote: false
showInVote: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет смысла смешивать все голосования воедино, включая и всё сразу

description: light-dynamic-description
rules:
- MeteorSwarmScheduler
Expand All @@ -23,7 +23,7 @@
- type: gamePreset
id: HardDynamic
name: hard-dynamic-title
showInVote: false
showInVote: true
description: hard-dynamic-description
rules:
- RampingStationEventScheduler
Expand Down
Loading
Loading