private async Task UpdateAtr(IBucket bucket, string key) { if (string.IsNullOrWhiteSpace(AtrId)) { AtrId = AtrIdsHelper.GetAtrId(key); AtrBucket = bucket; State = AttemptState.Pending; var result = await AtrBucket.MutateIn <dynamic>(AtrId) .Upsert($"attempts.{AttemptId}.st", AttemptState.Pending.GetDescription(), SubdocPathFlags.CreatePath | SubdocPathFlags.Xattr, SubdocDocFlags.UpsertDocument) .Upsert($"attempts.{AttemptId}.tst", MutationCasMacro, SubdocPathFlags.Xattr | SubdocPathFlags.ExpandMacroValues) .Upsert($"attempts.{AttemptId}.exp", _config.Expiration.TotalMilliseconds, SubdocPathFlags.Xattr) .WithDurability(_config.PersistTo, _config.ReplicateTo) .WithTimeout(_config.KeyValueTimeout) .ExecuteAsync() .ConfigureAwait(false); if (!result.Success) { //TODO: Failed to create ATR } } }
public async Task Commit() { if (string.IsNullOrWhiteSpace(AtrId) || AtrBucket == null) { // no mutations State = AttemptState.Completed; return; } var result = await AtrBucket.MutateIn <dynamic>(AtrId) .Upsert($"attempts.{AttemptId}.st", AttemptState.Committed.GetDescription(), SubdocPathFlags.Xattr) .Upsert($"attempts.{AttemptId}.tsc", MutationCasMacro, SubdocPathFlags.Xattr | SubdocPathFlags.ExpandMacroValues) //.Upsert($"attempts.{AttemptId}.ins", CreateCommitData(_stagedInserts), SubdocPathFlags.Xattr) //.Upsert($"attempts.{AttemptId}.rep", CreateCommitData(_stagedReplaces), SubdocPathFlags.Xattr) //.Upsert($"attempts.{AttemptId}.rem", CreateCommitData(_stagedRemoves), SubdocPathFlags.Xattr) .ExecuteAsync() .ConfigureAwait(false); if (!result.Success) { //TODO: Failed to update ATR record } State = AttemptState.Committed; var tasks = new List <Task>(); //tasks.AddRange( // _stagedInserts.Concat(_stagedReplaces) // .Select(x => new KeyValuePair<string, TransactionDocument<dynamic>>(x.Key, x.Value as TransactionDocument<dynamic>)) // .Select<KeyValuePair<string, TransactionDocument<dynamic>>, Task>(entry => // { // return entry.Value.Bucket.MutateIn<dynamic>(entry.Key) // .Remove(TransactionInterfacePrefix, SubdocPathFlags.Xattr) // .Upsert(string.Empty, entry.Value.Content) // . // .ExecuteAsync(); // }) //); //tasks.AddRange( // _stagedRemoves // .Select(x => new KeyValuePair<string, TransactionDocument<dynamic>>(x.Key, x.Value as TransactionDocument<dynamic>)) // .Select<KeyValuePair<string, TransactionDocument<dynamic>>, Task>(entry => // { // return entry.Value.Bucket.RemoveAsync(entry.Key); // }) //); foreach (var entry in _stagedInserts) { var document = (entry.Value as TransactionDocument <dynamic>); var task = document.Bucket.MutateIn <dynamic>(entry.Key) .Remove(TransactionInterfacePrefix, SubdocPathFlags.Xattr) .Upsert(document.Content) .ExecuteAsync(); tasks.Add(task); } await Task.WhenAll(tasks).ConfigureAwait(false); }
/// <summary> /// Sets the state of the lift tracker at the current attempt. /// </summary> /// <param name="currentAttemptState">The current attempt state.</param> public void LogCurrentAttempt(AttemptState currentAttemptState) { // Set the current attempt state (this.LiftTrackerState as AttemptState[])[this.CurrentAttempt] = currentAttemptState; // Unless we are already at the last attempt ... if (this.CurrentAttempt <= this.maxAllowedAttempts) { // ... increment the current attempt index (e.g. will get up to 9, if your max is 9) this.CurrentAttempt++; } // Throw exception if over else if (this.CurrentAttempt > this.maxAllowedAttempts) { throw new ArgumentOutOfRangeException("Exceeded the max number of attempts."); } }
/// <summary> /// Update the state of the lift tracker with a given AttemptState enum. /// </summary> public void UpdateState(AttemptState attemptState) { if (attemptState == AttemptState.Success) { liftTrackerArr[gameData.CurrentAttempt].sprite = greenCheck; liftTrackerArr[gameData.CurrentAttempt].color = Color.white; } else if (attemptState == AttemptState.Fail) { liftTrackerArr[gameData.CurrentAttempt].sprite = redX; liftTrackerArr[gameData.CurrentAttempt].color = Color.white; } else if (attemptState == AttemptState.NotAttempted) { liftTrackerArr[gameData.CurrentAttempt].sprite = smallDot; liftTrackerArr[gameData.CurrentAttempt].color = Color.black; } else { throw new InvalidOperationException("This should never happen."); } }
/// <summary> /// Activate the lights based on the AttemptState. /// </summary> public IEnumerator SetLights(AttemptState attemptState) { if (attemptState == AttemptState.Success) //3 whites, maybe 1 red { for (int i = 0; i < lightArray.Length; i++) { lightArray[i].gameObject.SetActive(true); yield return(new WaitForSeconds(0.3f)); } int rand = Random.Range(0, 10); for (int i = 0; i < lightArray.Length; i++) { lightArray[i].sprite = lightSprite; if (rand < 3) { lightArray[rand].color = Color.red; } } } else if (attemptState == AttemptState.Fail) //3 reds { for (int i = 0; i < lightArray.Length; i++) { lightArray[i].gameObject.SetActive(true); yield return(new WaitForSeconds(0.3f)); } for (int i = 0; i < lightArray.Length; i++) { lightArray[i].sprite = lightSprite; lightArray[i].color = Color.red; } } else { throw new InvalidOperationException("This should never happen."); } }