public override void DoWindowContents(Rect inRect) { Rect exitVote = new Rect(inRect.width - 25f, 0f, 20f, 20f); if (Widgets.ButtonTextSubtle(exitVote, "X")) { VoteHandler.ForceEnd(); } GameFont old = Text.Font; Text.Font = ToolkitSettings.LargeVotingWindow ? GameFont.Medium : GameFont.Small; float lineheight = ToolkitSettings.LargeVotingWindow ? 50 : 30; string titleLabel = "<b>" + title + "</b>"; float titleHeight = Text.CalcHeight(titleLabel, inRect.width); Widgets.Label(inRect, titleLabel); inRect.y += titleHeight + 10; for (int i = 0; i < optionsKeys.Count; i++) { string msg = "[" + (i + 1) + "] "; msg += (vote.VoteKeyLabel(i)) + $": {vote.voteCounts[i]}"; Widgets.Label(inRect, msg); inRect.y += lineheight; } int secondsElapsed = TimeHelper.SecondsElapsed(VoteHandler.voteStartedAt); Rect bar = new Rect(inRect.x, inRect.y, 225, 20); Widgets.FillableBar(bar, ((float)ToolkitSettings.VoteTime * 60f - (float)secondsElapsed) / ((float)ToolkitSettings.VoteTime * 60f)); Text.Font = old; }
public void TestMethodAlter() { VoteHandler vh = new VoteHandler(); bool b = vh.CreateAlternative(10, 0); Assert.AreEqual(true, b); }
static void Main(string[] args) { XmlConfigurator.Configure(); try { ThreadPool.SetMinThreads(32, 1024); var statePersister = new StatePersister(); AuthController authController = new AuthController(StatePersister.LoadUsers(), statePersister); ElectroController electroController = new ElectroController(StatePersister.LoadElections(), StatePersister.LoadKeys(), authController, statePersister); var staticHandler = new StaticHandler(GetPrefix(null), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "web")); staticHandler.Start(); var registerHandler = new RegisterHandler(authController, GetPrefix("register")); registerHandler.Start(); var loginHandler = new LoginHandler(authController, GetPrefix("login")); loginHandler.Start(); var logoutHandler = new LogoutHandler(authController, GetPrefix("logout")); logoutHandler.Start(); var startElectionHandler = new StartElectionHandler(electroController, authController, GetPrefix("startElection")); startElectionHandler.Start(); var listElectionsHandler = new ListElectionsHandler(electroController, authController, GetPrefix("listElections")); listElectionsHandler.Start(); var findElectionHandler = new FindElectionHandler(electroController, authController, GetPrefix("findElection")); findElectionHandler.Start(); var nominateHandler = new NominateHandler(electroController, authController, GetPrefix("nominate")); nominateHandler.Start(); var voteHandler = new VoteHandler(electroController, authController, GetPrefix("vote")); voteHandler.Start(); while (true) { Thread.Sleep(electionsSnapshotTimeoutMs); try { log.Info("Saving elections snapshot"); StatePersister.SaveAllElections(electroController.DumpElections().Reverse()); log.Info("Elections snapshot saved"); } catch (Exception e) { log.Error("Failed to save elections snapshot. Sleeping and retrying", e); } } } catch (Exception e) { log.Fatal(e); } }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { if (VoteHandler.voteActive) { yield break; } float mtbNow = this.Props.mtbDays; IEnumerable <IncidentDef> options; List <IncidentDef> pickedoptions = new List <IncidentDef>(); if (this.Props.mtbDaysFactorByDaysPassedCurve != null) { mtbNow *= this.Props.mtbDaysFactorByDaysPassedCurve.Evaluate(GenDate.DaysPassedFloat); } if (Rand.MTBEventOccurs(mtbNow, 60000f, 1000f)) { IncidentDef selectedDef; options = base.UsableIncidentsInCategory(this.Props.category, target); Helper.Log("Trying to create events"); if (options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out selectedDef)) { if (options.Count() > ToolkitSettings.VoteOptions) { options = options.Where(k => k != selectedDef); pickedoptions.Add(selectedDef); for (int x = 0; x < ToolkitSettings.VoteOptions - 1 && x < options.Count(); x++) { options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out IncidentDef picked); if (picked != null) { options = options.Where(k => k != picked); pickedoptions.Add(picked); } } Dictionary <int, IncidentDef> incidents = new Dictionary <int, IncidentDef>(); for (int i = 0; i < pickedoptions.Count(); i++) { incidents.Add(i, pickedoptions.ToList()[i]); } VoteHandler.QueueVote(new VoteIncidentDef(incidents, this, this.GenerateParms(selectedDef.category, target))); Helper.Log("Events created"); yield break; } else if (options.Count() == 1) { yield return(new FiringIncident(selectedDef, this, this.GenerateParms(selectedDef.category, target))); } yield return(new FiringIncident(selectedDef, this, this.GenerateParms(selectedDef.category, target))); } yield break; } }
public async Task EndVoteCommand(CommandContext command) { await TallyVotes(); voter.StopVote(); string result = voter.AnnounceWinner(); voter = new VoteHandler(); await command.RespondAsync(result); }
public CmdList() { Cmd[] addCmds = new Cmd[] { new AutoCmd(this), new AutoSpecCmd(), new ClearCmd(), new CountdownCmd(), new DateCmd(), new DelCmd(), new DelsCmd(), new FilterCmd(), new HelpCmd(), new InfoCmd(), new KickCmd(), new LevelCmd(), new ListCmd(), new LoadCmd(), new LogCmd(), //new NameCmd(), // not supported new PlayCmd(), new PlaylistCmd(), new PlayersCmd(), new PluginCmd(), //new RestartCmd(), new RipCmd(), new SaveCmd(), new SayCmd(), new ScoresCmd(), new ServerCmd(), new SettingsCmd(), new ShuffleCmd(), new SpecCmd(), new StuckCmd(), new TimelimitCmd(), new UnstuckCmd(), new UpdateCmd(), new WelcomeCmd(), new WinCmd(), }; foreach (Cmd addCmd in addCmds) { cmds.Add(addCmd); } VoteHandler voteHandler = new VoteHandler(this); cmds.Add(voteHandler.voteCommand); cmds.Add(voteHandler.voteControlCommand); }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { voteTracker = Current.Game.GetComponent <StoryTellerVoteTracker>(); if (!VoteHandler.voteActive && Rand.MTBEventOccurs(ToolkitSettings.ToryTalkerMTBDays, 60000f, 1000f)) { List <VotingIncidentEntry> entries = VotingIncidentsByWeight(); List <VotingIncidentEntry> winners = new List <VotingIncidentEntry>(); for (int i = 0; i < ToolkitSettings.VoteOptions && i < 12; i++) { winners.Add( entries.Where(s => !winners.Contains(s) ).RandomElementByWeight( (VotingIncidentEntry vi) => vi.weight) ); int index = Math.Max(winners.Count - 1, 0); winners[index].incident.helper = VotingIncidentMaker.makeVotingHelper(winners[index].incident); winners[index].incident.helper.target = target; if (!winners[index].incident.helper.IsPossible()) { entries.RemoveAt(i); i--; winners.RemoveAt(index); } } Dictionary <int, VotingIncident> incidents = new Dictionary <int, VotingIncident>(); for (int i = 0; i < winners.Count; i++) { incidents.Add(i, winners[i].incident); } StorytellerPack pack = DefDatabase <StorytellerPack> .GetNamed("ToryTalker"); VoteHandler.QueueVote(new Vote_ToryTalker(incidents, pack, "Which event should happen next?")); } yield break; }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { if (VoteHandler.voteActive || !Rand.MTBEventOccurs(ToolkitSettings.UristBotMTBDays, 60000f, 1000f)) { yield break; } IncidentWorker worker = new IncidentWorker_RaidEnemy(); IncidentParms parms = StorytellerUtility.DefaultParmsNow(IncidentCategoryDefOf.ThreatSmall, target); worker.def = IncidentDefOf.RaidEnemy; parms.points = StorytellerUtility.DefaultThreatPointsNow(target); parms.raidStrategy = RaidStrategyDefOf.ImmediateAttack; parms.raidArrivalMode = PawnsArrivalModeDefOf.EdgeWalkIn; if (!worker.CanFireNow(parms)) { yield break; } int choice = Rand.Range(0, 0); switch (choice) { case 0: parms.faction = Find.FactionManager.RandomEnemyFaction(false, false, false); Dictionary <int, RaidStrategyDef> allStrategies = new Dictionary <int, RaidStrategyDef>(); List <RaidStrategyDef> raidStrategyDefs = new List <RaidStrategyDef>(DefDatabase <RaidStrategyDef> .AllDefs); raidStrategyDefs.Shuffle(); foreach (RaidStrategyDef strat in raidStrategyDefs) { allStrategies.Add(allStrategies.Count, strat); } StorytellerPack named = DefDatabase <StorytellerPack> .GetNamed("UristBot", true); VoteHandler.QueueVote(new Vote_RaidStrategy(allStrategies, named, worker, this, parms, "How should the next raiders attack?")); break; } }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { List <IncidentCategoryDef> triedCategories = new List <IncidentCategoryDef>(); List <IncidentDef> incidentDefs; if (!VoteHandler.voteActive && Rand.MTBEventOccurs(ToolkitSettings.HodlBotMTBDays, 60000f, 1000f)) { while (true) { incidentDefs = new List <IncidentDef>(); IncidentCategoryDef category = ChooseRandomCategory(target, triedCategories); IncidentParms parms = this.GenerateParms(category, target); IEnumerable <IncidentDef> options = from d in base.UsableIncidentsInCategory(category, target) where d.Worker.CanFireNow(parms) && (!d.NeedsParmsPoints || parms.points >= d.minThreatPoints) select d; for (int i = 0; options.Count() > 0 && incidentDefs.Count < ToolkitSettings.VoteOptions && i < 10; i++) { if (!options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out IncidentDef incDef)) { triedCategories.Add(category); break; } else { incidentDefs.Add(incDef); options = options.Where(s => s != incDef); } } if (incidentDefs.Count >= 2) { Dictionary <int, IncidentDef> incidents = new Dictionary <int, IncidentDef>(); for (int i = 0; i < incidentDefs.Count(); i++) { incidents.Add(i, incidentDefs.ToList()[i]); } VoteHandler.QueueVote(new VoteIncidentDef(incidents, this, parms)); yield break; } } } }
private void GenerateVotesFromDefs(List <IncidentDef> defs, IncidentParms parms) { if (defs != null && defs.Count() > 2) { defs.Shuffle(); Dictionary <int, IncidentDef> incidents = new Dictionary <int, IncidentDef>(); for (int i = 0; i < defs.Count; i++) { incidents.Add(i, defs[i]); } VoteHandler.QueueVote(new Vote_Mercurius(incidents, this, parms, "Which Event should happen next?")); } else { Log.Error("Only generated " + defs.Count + " incidents"); } }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { if (!VoteHandler.voteActive && Rand.MTBEventOccurs(ToolkitSettings.ToryTalkerMTBDays, 60000f, 1000f)) { List <VotingIncidentEntry> entries = VotingIncidentsByWeight(); List <VotingIncidentEntry> winners = new List <VotingIncidentEntry>(); for (int i = 0; i < ToolkitSettings.VoteOptions && i < 12; i++) { winners.Add( entries.Where(s => !winners.Contains(s) ).RandomElementByWeight( (VotingIncidentEntry vi) => vi.weight) ); int index = Math.Max(winners.Count - 1, 0); winners[index].incident.helper = VotingIncidentMaker.makeVotingHelper(winners[index].incident); winners[index].incident.helper.target = target; if (!winners[index].incident.helper.IsPossible()) { entries.RemoveAt(i); i--; winners.RemoveAt(index); } } Dictionary <int, VotingIncident> incidents = new Dictionary <int, VotingIncident>(); for (int i = 0; i < winners.Count; i++) { incidents.Add(i, winners[i].incident); } VoteHandler.QueueVote(new Vote_VotingIncident(incidents)); } yield break; }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { if (VoteHandler.voteActive) { yield break; } if (VoteHelper.TimeForEventVote()) { MakeRandomVoteEvent(target); yield break; } if (thread != null) { thread = new Thread(new ThreadStart(ThreadProc)); thread.Start(); } else { if (singleIncident != null) { thread.Join(); thread = null; yield return(singleIncident); } if (makeIncidentOptions && incidentOptions != null) { thread.Join(); thread = null; VoteHandler.QueueVote(incidentOptions); } Thread.Sleep(0); yield break; } yield break; }
/// <inheritdoc cref="GameComponent.GameComponentUpdate"/> public override void GameComponentUpdate() { VoteHandler.CheckForQueuedVotes(); bool isNull = Viewers.jsonallviewers.NullOrEmpty(); switch (isNull) { case true: Viewers.RefreshViewers(); break; case false when ToolkitSettings.EarningCoins: ProcessCoinReward(); break; } if (TkSettings.AsapPurchases) { ProcessNextEvent(); } }
private FiringIncident GenerateIncident(IIncidentTarget target) { Helper.Log("Trying OnOffCycle Incident"); List <IncidentDef> pickedoptions = new List <IncidentDef>(); IncidentParms parms = this.GenerateParms(this.Props.IncidentCategory, target); IncidentDef def2; if ((float)GenDate.DaysPassed < this.Props.forceRaidEnemyBeforeDaysPassed) { if (!IncidentDefOf.RaidEnemy.Worker.CanFireNow(parms, false)) { return(null); } def2 = IncidentDefOf.RaidEnemy; } else if (this.Props.incident != null) { if (!this.Props.incident.Worker.CanFireNow(parms, false)) { return(null); } def2 = this.Props.incident; } else { options = from def in base.UsableIncidentsInCategory(this.Props.IncidentCategory, parms) where parms.points >= def.minThreatPoints select def; Helper.Log($"Trying OFC Category: ${this.Props.IncidentCategory}"); if (options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out def2)) { if (options.Count() > 1) { options = options.Where(k => k != def2); pickedoptions.Add(def2); for (int x = 0; x < ToolkitSettings.VoteOptions - 1 && x < options.Count(); x++) { options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out IncidentDef picked); if (picked != null) { options = options.Where(k => k != picked); pickedoptions.Add(picked); } } Dictionary <int, IncidentDef> incidents = new Dictionary <int, IncidentDef>(); for (int i = 0; i < pickedoptions.Count(); i++) { incidents.Add(i, pickedoptions.ToList()[i]); } VoteHandler.QueueVote(new VoteIncidentDef(incidents, this, parms)); Helper.Log("Events created"); return(null); } else if (options.Count() == 1) { Helper.Log("Firing one incident OFC"); return(new FiringIncident(def2, this, parms)); } } return(null); } return(new FiringIncident(def2, this, parms)); }
public void MakeRandomVoteEvent(IIncidentTarget target) { Helper.Log("Forcing vote event"); if (!VoteHandler.voteActive) { if (ToolkitSettings.TimedStorytelling) { bool targetIsRaidBeacon = target.IncidentTargetTags().Contains(IncidentTargetTagDefOf.Map_RaidBeacon); List <IncidentCategoryDef> triedCategories = new List <IncidentCategoryDef>(); IncidentDef incDef; List <IncidentDef> pickedoptions = new List <IncidentDef>(); IEnumerable <IncidentDef> options; IncidentCategoryDef category = this.ChooseRandomCategory(target, triedCategories); Helper.Log($"Trying Category{category}"); parms = this.GenerateParms(category, target); options = from d in base.UsableIncidentsInCategory(category, target) where !d.NeedsParmsPoints || parms.points >= d.minThreatPoints select d; if (options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out incDef)) { } triedCategories.Add(category); if (triedCategories.Count >= this.Props.categoryWeights.Count) { } Helper.Log($"Events Possible: {options.Count()}"); if (options.Count() > 1) { options = options.Where(k => k != incDef); pickedoptions.Add(incDef); for (int x = 0; x < ToolkitSettings.VoteOptions - 1 && x < options.Count(); x++) { options.TryRandomElementByWeight(new Func <IncidentDef, float>(base.IncidentChanceFinal), out IncidentDef picked); if (picked != null) { options = options.Where(k => k != picked); pickedoptions.Add(picked); } } Dictionary <int, IncidentDef> incidents = new Dictionary <int, IncidentDef>(); for (int i = 0; i < pickedoptions.Count(); i++) { incidents.Add(i, pickedoptions.ToList()[i]); } if (incidents.Count > 1) { VoteHandler.QueueVote(new VoteIncidentDef(incidents, this, parms)); Helper.Log("Events created"); } } } } }
public override void Tick() { try { if (_game == null || _mod == null) { return; } _mod.Tick(); var minutes = (int)(_game.Info.RealPlayTimeInteracting / 60f); double getTime = (double)Time.time / 60f; int time = Convert.ToInt32(Math.Truncate(getTime)); if (IncidentHelpers.Count > 0) { for (int i = 0; i < IncidentHelpers.Count; i++) { var incidentHelper = IncidentHelpers.Dequeue(); if (!(incidentHelper is VotingHelper)) { Purchase_Handler.QueuePlayerMessage(incidentHelper.Viewer, incidentHelper.message); } incidentHelper.TryExecute(); } Helper.playerMessages = new List <string>(); } if (IncidentHelperVariables.Count > 0) { for (int i = 0; i < IncidentHelperVariables.Count; i++) { var incidentHelper = IncidentHelperVariables.Dequeue(); Purchase_Handler.QueuePlayerMessage(incidentHelper.Viewer, incidentHelper.message, incidentHelper.storeIncident.variables); incidentHelper.TryExecute(); if (Purchase_Handler.viewerNamesDoingVariableCommands.Contains(incidentHelper.Viewer.username)) { Purchase_Handler.viewerNamesDoingVariableCommands.Remove(incidentHelper.Viewer.username); } } Helper.playerMessages = new List <string>(); } if (Incidents.Count > 0) { var incident = Incidents.Dequeue(); IncidentParms incidentParms = new IncidentParms(); incidentParms.target = Helper.AnyPlayerMap; incident.TryExecute(incidentParms); } if (FiringIncidents.Count > 0) { Helper.Log("Firing " + FiringIncidents.First().def.defName); var incident = FiringIncidents.Dequeue(); incident.def.Worker.TryExecute(incident.parms); } VoteHandler.CheckForQueuedVotes(); if (_lastCoinReward < 0) { _lastCoinReward = time; } else if (ToolkitSettings.EarningCoins && ((time - _lastCoinReward) >= ToolkitSettings.CoinInterval) && Viewers.jsonallviewers != null) { _lastCoinReward = time; Viewers.AwardViewersCoins(); } if (_lastMinute < 0) { _lastMinute = time; } else if (_lastMinute < time) { _lastMinute = time; Toolkit.JobManager.CheckAllJobs(); Viewers.RefreshViewers(); } } catch (Exception ex) { Helper.Log("Exception: " + ex.Message + ex.StackTrace); } }
public override IEnumerable <FiringIncident> MakeIntervalIncidents(IIncidentTarget target) { if (VoteHandler.voteActive || !Rand.MTBEventOccurs(ToolkitSettings.HodlBotMTBDays, 60000f, 1000f)) { if (!forced) { yield break; } } if (Rand.Range(0, 1) == 1) { voteType = Vote_VotingIncident.VoteLabelType.Type; } List <VotingIncidentEntry> source = VotingIncidentsByWeight(); List <VotingIncidentEntry> winners = new List <VotingIncidentEntry>(); string str = null; switch (voteType) { case Vote_VotingIncident.VoteLabelType.Category: { EventCategory category = RandomCategory(); source = (from s in source where s.incident.eventCategory == category select s).ToList(); str = "Which " + category.ToString() + " event should happen?"; Helper.Log("rand cat picked " + category); break; } case Vote_VotingIncident.VoteLabelType.Type: { EventType randType = RandomType(); source = (from s in source where s.incident.eventType == randType select s).ToList(); str = "Which " + randType + " event should happen?"; Helper.Log("rand type picked " + randType); break; } } for (int i = 0; i < ToolkitSettings.VoteOptions; i++) { if (GenCollection.TryRandomElementByWeight <VotingIncidentEntry> (from s in source where !winners.Contains(s) select s, (Func <VotingIncidentEntry, float>)((VotingIncidentEntry vi) => vi.weight), out VotingIncidentEntry votingIncidentEntry)) { votingIncidentEntry.incident.Helper.target = target; if (votingIncidentEntry.incident.Helper.IsPossible()) { winners.Add(votingIncidentEntry); } } } if (winners.Count < 3) { Helper.Log("Less than 3 possible votes were found"); yield break; } Dictionary <int, VotingIncident> dictionary = new Dictionary <int, VotingIncident>(); for (int i = 0; i < winners.Count; i++) { dictionary.Add(i, winners[i].incident); } StorytellerPack named = DefDatabase <StorytellerPack> .GetNamed("HodlBot", true); VoteHandler.QueueVote(new Vote_HodlBot(dictionary, named, voteType, str)); }