public Beneficiary PickAWinner() { double rngValue = random.NextDouble() * totalPercentContribution; Beneficiary winner = beneficiaryList[0]; for (int i = 0; i < beneficiaryList.Count; i++) { if (beneficiaryList[i].isValidAndActive == false) { continue; } if (rngValue <= beneficiaryList[i].percentTime) { winner = beneficiaryList[i]; break; } else { rngValue -= beneficiaryList[i].percentTime; } } return(winner); }
public void AddBeneficiary( Beneficiary beneficiaryToAdd, bool shouldSkipSave = false) { Debug.Assert(beneficiaryToAdd != null); // De-dupe for (int i = 0; i < beneficiaryList.Count; i++) { Beneficiary beneficiary = beneficiaryList[i]; if (beneficiary.wallet == beneficiaryToAdd.wallet) { beneficiaryToAdd.percentTime = Math.Max(beneficiaryToAdd.percentTime, beneficiary.percentTime); RemoveBeneficiary(beneficiary); break; } } beneficiaryList.Add(beneficiaryToAdd); beneficiaryList.Sort(beneficiarySorter); if (shouldSkipSave == false) { Save(); } totalPercentContribution = CalcTotalPercent(); onBeneficiaryListChanged?.Invoke(); }
public NetworkMiningStatsViewModel( MainViewModel mainViewModel, Beneficiary beneficiary) : base(mainViewModel) { Debug.Assert(beneficiary != null); this._beneficiary = beneficiary; }
public void RemoveBeneficiary( Beneficiary beneficiary) { Debug.Assert(beneficiary != null); if (beneficiaryList.Remove(beneficiary)) { Save(); onBeneficiaryListChanged?.Invoke(); } }
public void Save() { bool foundDevWallet = false; for (int i = 0; i < beneficiaryList.Count; i++) { Beneficiary beneficiary = beneficiaryList[i]; if (beneficiary.wallet == devWallet) { beneficiary.percentTime = Math.Max(devMinPercent, beneficiary.percentTime); foundDevWallet = true; break; } } if (foundDevWallet == false) { AddBeneficiary(new Beneficiary(devName, devWallet, devMinPercent, false), true); } bool foundUsersWallet = false; for (int i = 0; i < beneficiaryList.Count; i++) { if (beneficiaryList[i].isUsersWallet) { if (foundUsersWallet) { // Only accept one wallet as the users beneficiaryList[i].isUsersWallet = false; } foundUsersWallet = true; } } if (totalPercentContribution > 0 && Math.Abs(1 - totalPercentContribution) > .01) { for (int i = 0; i < beneficiaryList.Count; i++) { // Setting percentTime triggers a save and crash... hence _percentTime instead. Hacky. beneficiaryList[i]._percentTime = beneficiaryList[i].percentTime / totalPercentContribution; } } totalPercentContribution = CalcTotalPercent(); Debug.Assert(beneficiaryList.Count > 0); // TODO this failed -- nice to have. Debug.Assert(Math.Abs(totalPercentContribution - 1) < .01); string beneficiaryJson = JsonConvert.SerializeObject(beneficiaryList); Debug.Assert(string.IsNullOrWhiteSpace(beneficiaryJson) == false); File.WriteAllText(beneficiaryFilename, beneficiaryJson); }