2017-05-26 18:49:27 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using SharedLibrary;
|
|
|
|
|
using SharedLibrary.Network;
|
2017-05-27 00:22:50 -04:00
|
|
|
|
using SharedLibrary.Interfaces;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-06-06 23:45:21 -04:00
|
|
|
|
#if DEBUG
|
2017-05-26 18:49:27 -04:00
|
|
|
|
namespace Votemap_Plugin
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Allow clients to vote for the next map at the end of a round
|
|
|
|
|
/// Map choices are defined in the server
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class VoteMap : Command
|
|
|
|
|
{
|
|
|
|
|
public VoteMap() : base("vote", "vote for the next map. syntax !v <mapname>", "v", Player.Permission.User, 1, false) { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Properties of Event E
|
|
|
|
|
/// Owner: The server the event came from
|
|
|
|
|
/// Origin: The player generating the event
|
|
|
|
|
/// Target: Optional target the player specified
|
|
|
|
|
/// Data: Chat message which triggered the command event
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="E">This is the `say` event that comes from the server</param>
|
|
|
|
|
public override async Task ExecuteAsync(Event E)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
var voting = Vote.GetServerVotes(E.Owner.GetPort());
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
|
|
|
|
// we only want to allow a vote during a vote session
|
|
|
|
|
if (voting.voteInSession)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
if (voting.ClientHasVoted(E.Origin.NetworkID))
|
2017-05-26 18:49:27 -04:00
|
|
|
|
await E.Origin.Tell("You have already voted. Use ^5!vc ^7to ^5cancel ^7your vote");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string mapSearch = E.Data.ToLower().Trim();
|
|
|
|
|
// probably not the most optimized way to match the map.. but nothing is time critical here
|
2017-06-19 13:58:01 -04:00
|
|
|
|
Map votedMap = E.Owner.Maps.Find(m => (m.Alias.ToLower().Contains(mapSearch) || m.Name.Contains(mapSearch)));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
if (votedMap == null)
|
|
|
|
|
await E.Origin.Tell("^1" + E.Data + " is not a recognized map");
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
voting.CastClientVote(E.Origin.NetworkID, votedMap);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
await E.Origin.Tell("You voted for ^5" + votedMap.Alias);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
await E.Origin.Tell("There is no vote in session");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class VoteCancel : Command
|
|
|
|
|
{
|
|
|
|
|
public VoteCancel() : base("votecancel", "cancel your vote for the next map. syntax !vc", "vc", Player.Permission.User, 0, false) { }
|
|
|
|
|
|
|
|
|
|
public override async Task ExecuteAsync(Event E)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
var voting = Vote.GetServerVotes(E.Owner.GetPort());
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
|
|
|
|
if (voting.voteInSession)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
if (voting.ClientHasVoted(E.Origin.NetworkID))
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
voting.CancelClientVote(E.Origin.NetworkID);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
await E.Origin.Tell("Vote cancelled");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await E.Origin.Tell("You have no vote to cancel");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
await E.Origin.Tell("There is no vote in session");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Vote : IPlugin
|
|
|
|
|
{
|
|
|
|
|
public class VoteData
|
|
|
|
|
{
|
|
|
|
|
public string guid;
|
|
|
|
|
public Map map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MapResult
|
|
|
|
|
{
|
|
|
|
|
public Map map;
|
|
|
|
|
public int voteNum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ServerVoting
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public int ServerId
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
get; private set;
|
|
|
|
|
}
|
|
|
|
|
public bool voteInSession;
|
|
|
|
|
public bool matchEnded;
|
|
|
|
|
public bool votePassed;
|
|
|
|
|
public bool waitForLoad;
|
|
|
|
|
public DateTime voteTimeStart;
|
|
|
|
|
public DateTime loadStartTime;
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public List<VoteData> VoteList
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
get; private set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ServerVoting(int id)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
ServerId = id;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
voteInSession = false;
|
|
|
|
|
votePassed = false;
|
|
|
|
|
matchEnded = false;
|
|
|
|
|
waitForLoad = true;
|
2017-06-13 18:33:47 -04:00
|
|
|
|
VoteList = new List<VoteData>();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public int GetTotalVotes()
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
return VoteList.Count;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public bool ClientHasVoted(string guid)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
return VoteList.Exists(x => (x.guid == guid));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public void CastClientVote(string guid, Map map)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
var vote = new VoteData()
|
|
|
|
|
{
|
|
|
|
|
guid = guid,
|
|
|
|
|
map = map
|
|
|
|
|
};
|
|
|
|
|
VoteList.Add(vote);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public void CancelClientVote(string guid)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
VoteList.RemoveAll(x => (x.guid == guid));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public MapResult GetTopVotedMap()
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
List<MapResult> results = new List<MapResult>();
|
2017-06-13 18:33:47 -04:00
|
|
|
|
MapResult result = new MapResult()
|
|
|
|
|
{
|
|
|
|
|
map = new Map("Remain", "Remain"),
|
|
|
|
|
voteNum = 0
|
|
|
|
|
};
|
|
|
|
|
foreach (var vote in VoteList)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
if (!results.Exists(x => (x.map.Name == vote.map.Name)))
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
MapResult newResult = new MapResult()
|
|
|
|
|
{
|
|
|
|
|
map = vote.map,
|
|
|
|
|
voteNum = 1
|
|
|
|
|
};
|
2017-05-26 18:49:27 -04:00
|
|
|
|
results.Add(newResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var map = results.Find(x => x.map.Name == vote.map.Name);
|
|
|
|
|
map.voteNum += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var map in results)
|
|
|
|
|
if (map.voteNum > result.voteNum)
|
|
|
|
|
result = map;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<ServerVoting> serverVotingList;
|
|
|
|
|
public static int minVotes = 3;
|
|
|
|
|
|
|
|
|
|
public string Author
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "RaidMax";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Version
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return 1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "Votemap Plugin";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public async Task OnLoadAsync()
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
serverVotingList = new List<ServerVoting>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public async Task OnUnloadAsync()
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
serverVotingList.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The server monitor thread calls this about every 1 second
|
|
|
|
|
/// This is not high-precision, but will run 1 time per second
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="S"></param>
|
2017-05-27 18:08:04 -04:00
|
|
|
|
public async Task OnTickAsync(Server S)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-12 20:24:12 -04:00
|
|
|
|
return;
|
2017-06-13 18:33:47 -04:00
|
|
|
|
var serverVotes = GetServerVotes(S.GetPort());
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
|
|
|
|
if (serverVotes != null)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if ((DateTime.Now - serverVotes.loadStartTime).TotalSeconds < 30 /* || S.getPlayers().Count < 3*/)
|
|
|
|
|
return;
|
|
|
|
|
else
|
|
|
|
|
serverVotes.waitForLoad = false;
|
|
|
|
|
|
|
|
|
|
// dvar that is set & updated by the game script...
|
|
|
|
|
serverVotes.matchEnded = (await S.GetDvarAsync<int>("scr_gameended")).Value == 1;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Console.WriteLine("===========================");
|
|
|
|
|
Console.WriteLine("Match ended->" + serverVotes.matchEnded);
|
|
|
|
|
Console.WriteLine("Vote in session->" + serverVotes.voteInSession);
|
|
|
|
|
Console.WriteLine("Vote passed->" + serverVotes.votePassed);*/
|
|
|
|
|
|
|
|
|
|
if (!serverVotes.voteInSession && serverVotes.matchEnded && serverVotes.voteTimeStart == DateTime.MinValue)
|
|
|
|
|
{
|
|
|
|
|
await S.Broadcast("Voting has started for the ^5next map");
|
|
|
|
|
await S.Broadcast("Type ^5!v <map> ^7to vote for the nextmap!");
|
|
|
|
|
serverVotes.voteInSession = true;
|
|
|
|
|
serverVotes.voteTimeStart = DateTime.Now;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!serverVotes.voteInSession && serverVotes.votePassed && (DateTime.Now - serverVotes.voteTimeStart).TotalSeconds > 30)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
await S.ExecuteCommandAsync("map " + serverVotes.GetTopVotedMap().map.Name);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
serverVotes.votePassed = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (serverVotes.voteInSession)
|
|
|
|
|
{
|
|
|
|
|
if ((DateTime.Now - serverVotes.voteTimeStart).TotalSeconds > 25)
|
|
|
|
|
{
|
|
|
|
|
serverVotes.voteInSession = false;
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
MapResult m = serverVotes.GetTopVotedMap();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
await S.Broadcast("Voting has ended!");
|
|
|
|
|
|
2017-06-07 20:59:59 -04:00
|
|
|
|
if (m.voteNum < minVotes && S.GetPlayersAsList().Count > 4)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
await S.Broadcast("Vote map failed. At least ^5" + minVotes + " ^7people must choose the same map");
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
await S.Broadcast(String.Format("Next map is ^5{0} ^7- [^2{1}/{2}^7] votes", m.map.Alias, m.voteNum, serverVotes.GetTotalVotes()));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
serverVotes.votePassed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 18:08:04 -04:00
|
|
|
|
public async Task OnEventAsync(Event E, Server S)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-12 20:24:12 -04:00
|
|
|
|
return;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
if (E.Type == Event.GType.Start)
|
|
|
|
|
{
|
2017-06-12 13:50:00 -04:00
|
|
|
|
serverVotingList.Add(new ServerVoting(S.GetPort()));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Type == Event.GType.Stop)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
serverVotingList.RemoveAll(x => x.ServerId == S.GetPort());
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Type == Event.GType.MapEnd || E.Type == Event.GType.MapChange)
|
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
var serverVotes = GetServerVotes(S.GetPort());
|
|
|
|
|
serverVotes.VoteList.Clear();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
serverVotes.voteTimeStart = DateTime.MinValue;
|
|
|
|
|
serverVotes.loadStartTime = DateTime.Now;
|
|
|
|
|
serverVotes.waitForLoad = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
public static ServerVoting GetServerVotes(int serverID)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-06-13 18:33:47 -04:00
|
|
|
|
return serverVotingList.Find(x => (x.ServerId == serverID));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-06 23:45:21 -04:00
|
|
|
|
#endif
|