-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option polls #2431
Merged
Merged
Option polls #2431
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b40d8b6
Polls now come with multiple options instead of just Yes and No. Adde…
DeinFreund ed017f8
Send BattlePoll to users when they join a battle and when a vote ends
DeinFreund 1bb0bb2
correct comments on status when there is no poll
DeinFreund 9d66d64
show up to 50% supported maps if the autohost is configured to allow …
DeinFreund 4977d04
Merge branch 'master' into optionPolls
DeinFreund 51b1254
amend
DeinFreund 07b53d8
add property DefaultPoll to BattlePoll message
DeinFreund 90d6e40
Make default polls backwards compatible
DeinFreund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,65 +10,88 @@ public class CommandPoll | |
{ | ||
private int winCount; | ||
private ServerBattle battle; | ||
private Dictionary<string, bool> userVotes = new Dictionary<string, bool>(); | ||
private Dictionary<string, int> userVotes = new Dictionary<string, int>(); //stores votes, zero indexed | ||
private Func<string, string> EligiblitySelector; //return null if player is allowed to vote, otherwise reason | ||
private bool AbsoluteMajorityVote; //if set to yes, at least N/2 players need to vote for an option to be selected. Otherwise the option with the majority of votes wins | ||
private bool DefaultPoll; //if set to yes, there must be only two options being yes and no. | ||
|
||
public bool Ended { get; private set; } = false; | ||
public string question { get; private set; } | ||
public string Topic { get; private set; } | ||
public List<PollOption> Options; | ||
public Say Creator { get; private set; } | ||
private BattleCommand command; | ||
private bool AbsoluteMajorityVote; | ||
public PollOutcome Outcome { get; private set; } | ||
|
||
public event EventHandler<PollOutcome> PollEnded; | ||
|
||
public CommandPoll(ServerBattle battle, bool absoluteMajorityVote = true) | ||
public CommandPoll(ServerBattle battle, bool absoluteMajorityVote = true, bool defaultPoll = false) | ||
{ | ||
this.battle = battle; | ||
this.AbsoluteMajorityVote = absoluteMajorityVote; | ||
DefaultPoll = defaultPoll; | ||
} | ||
|
||
public async Task<bool> Setup(BattleCommand cmd, Say e, string args) | ||
public async Task Setup(Func<string, string> eligibilitySelector, List<PollOption> options, Say creator, string Topic) | ||
{ | ||
command = cmd.Create(); | ||
EligiblitySelector = eligibilitySelector; | ||
Options = options; | ||
Creator = creator; | ||
|
||
|
||
Creator = e; | ||
question = command.Arm(battle, e, args); | ||
if (question == null) return false; | ||
string ignored; | ||
winCount = battle.Users.Values.Count(x => command.GetRunPermissions(battle, x.Name, out ignored) >= BattleCommand.RunPermission.Vote && !cmd.IsSpectator(battle, x.Name, x)) / 2 + 1; | ||
winCount = battle.Users.Values.Count(x => EligiblitySelector(x.Name) == null) / 2 + 1; | ||
if (winCount <= 0) winCount = 1; | ||
|
||
if (!await Vote(e, true)) | ||
{ | ||
if (e == null) await battle.SayBattle($"Poll: {question} [!y=0/{winCount}, !n=0/{winCount}]"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, this can't be removed because in-game needs it. |
||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
await battle.server.Broadcast(battle.Users.Keys, GetBattlePoll()); | ||
} | ||
|
||
return true; | ||
public BattlePoll GetBattlePoll() | ||
{ | ||
return new BattlePoll() | ||
{ | ||
Options = Options.Select((o, i) => new BattlePoll.PollOption() | ||
{ | ||
Id = i + 1, | ||
Name = o.Name, | ||
Votes = userVotes.Count(x => x.Value == i) | ||
}).ToList(), | ||
Topic = Topic, | ||
VotesToWin = winCount, | ||
DefaultPoll = DefaultPoll | ||
}; | ||
} | ||
|
||
private async Task<bool> CheckEnd(bool timeout) | ||
{ | ||
var yes = userVotes.Count(x => x.Value == true); | ||
var no = userVotes.Count(x => x.Value == false); | ||
|
||
if (yes >= winCount || timeout && !AbsoluteMajorityVote && yes > no) | ||
List<int> votes = Options.Select((o, i) => userVotes.Count(x => x.Value == i)).ToList(); | ||
var winnerId = votes.IndexOf(votes.Max()); | ||
|
||
if (votes[winnerId] >= winCount || timeout && !AbsoluteMajorityVote) | ||
{ | ||
Ended = true; | ||
await battle.SayBattle($"Poll: {question} [END:SUCCESS]"); | ||
if (command.Access == BattleCommand.AccessType.NotIngame && battle.spring.IsRunning) return true; | ||
if (command.Access == BattleCommand.AccessType.Ingame && !battle.spring.IsRunning) return true; | ||
await command.ExecuteArmed(battle, Creator); | ||
Outcome = new PollOutcome() { Success = true }; | ||
if (DefaultPoll) | ||
{ | ||
await battle.SayBattle($"Poll: {Topic} [END:SUCCESS]"); | ||
} | ||
else | ||
{ | ||
await battle.SayBattle($"Option Poll: {Topic} [END: Selected {Options[winnerId].Name}]"); | ||
} | ||
await Options[winnerId].Action(); | ||
Outcome = new PollOutcome() { ChosenOption = Options[winnerId] }; | ||
return true; | ||
} | ||
else if (no >= winCount || timeout) | ||
else if (timeout) | ||
{ | ||
Ended = true; | ||
await battle.SayBattle($"Poll: {question} [END:FAILED]"); | ||
Outcome = new PollOutcome() { Success = false }; | ||
if (DefaultPoll) | ||
{ | ||
await battle.SayBattle($"Poll: {Topic} [END:FAILED]"); | ||
} | ||
else | ||
{ | ||
await battle.SayBattle($"Option Poll: {Topic} [END: No option achieved absolute majority]"); | ||
} | ||
Outcome = new PollOutcome() { ChosenOption = null }; | ||
return true; | ||
} | ||
return false; | ||
|
@@ -85,34 +108,38 @@ public async Task End() | |
} | ||
|
||
|
||
public async Task<bool> Vote(Say e, bool vote) | ||
//Vote for an option, one-indexed | ||
public async Task<bool> Vote(Say e, int vote) | ||
{ | ||
if (e == null) return false; | ||
string reason; | ||
if (command.GetRunPermissions(battle, e.User, out reason) >= BattleCommand.RunPermission.Vote && !Ended) | ||
string ineligibilityReason = EligiblitySelector(e.User); | ||
if (ineligibilityReason == null && !Ended) | ||
{ | ||
if (command.IsSpectator(battle, e.User, null)) return false; | ||
|
||
userVotes[e.User] = vote; | ||
userVotes[e.User] = vote - 1; | ||
|
||
var yes = userVotes.Count(x => x.Value == true); | ||
var no = userVotes.Count(x => x.Value == false); | ||
await battle.SayBattle(string.Format("Poll: {0} [!y={1}/{3}, !n={2}/{3}]", question, yes, no, winCount)); | ||
if (DefaultPoll) await battle.SayBattle(string.Format("Poll: {0} [!y={1}/{3}, !n={2}/{3}]", Topic, userVotes.Count(x => x.Value == 0), userVotes.Count(x => x.Value == 1), winCount)); | ||
await battle.server.Broadcast(battle.Users.Keys, GetBattlePoll()); | ||
|
||
if (await CheckEnd(false)) return true; | ||
} | ||
else | ||
{ | ||
await battle.Respond(e, reason); | ||
await battle.Respond(e, ineligibilityReason); | ||
return false; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
public class PollOption | ||
{ | ||
public string Name; | ||
public Func<Task> Action; | ||
} | ||
|
||
public class PollOutcome : EventArgs | ||
{ | ||
public bool Success { get; set; } | ||
public PollOption ChosenOption { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be simpler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A server could be set to only allow Matchmaker maps. Imagine a 1v1 training server for example. In this case he should not show any featured maps.