internal override void Write(SplitGoal goal) { try { using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); var cmd = new SqlCommand(CommandText, conn); foreach (var parameter in Parameters) { if (CommandText.Contains(parameter.Key)) { cmd.Parameters.AddWithValue(parameter.Key, ParameterRetriever.RetireveValue(goal, parameter.Value)); } } cmd.ExecuteNonQuery(); conn.Close(); } } catch (Exception ex) { if (TargetService.Config.Targets.ThrowExceptions) { throw new Exception("Exception occurred in AbTestMaster when writing to database. Error Message: " + ex.Message); } } }
internal static void WriteToTargets(SplitGoal split) { foreach (var target in Targets.Where(t => t.DataType == TargetDataType.Goals)) { target.Write(split); } }
internal static void RemoveFromSession(SplitGoal splitGoal) { var viewList = SessionSplitViews; var newList = viewList.Where(splitView => splitView.Goal != splitGoal.Goal).ToList(); SessionSplitViews = newList; }
/// <summary> /// Calculate split goals based upon configured values /// </summary> /// <returns>SplitGoals or null if no goals</returns> public static SplitGoals GetSplitGoals() { Splits splits = ZAMsettings.Settings.Splits; if (!splits.ShowSplits || !splits.CalculateGoal) { return(null); } double numSplits = splits.GoalDistance / splits.SplitDistance; if (numSplits < 1) { return(null); } if (splits.GoalTime.TotalSeconds < 1) { return(null); } double goalSpeed = Math.Round((splits.GoalDistance / splits.GoalTime.TotalSeconds) * 3600, 1); double goalDistance = Math.Round(splits.GoalDistance, 1); SplitGoals splitGoals = new SplitGoals(goalSpeed, goalDistance, splits.SplitsInKm, splits.GoalTime); TimeSpan splitTime = new TimeSpan(0, 0, (int)Math.Round(splits.GoalTime.TotalSeconds / numSplits, 0)); int curDistance = 0; TimeSpan curTime = new TimeSpan(); for (int i = 0; i < (int)numSplits; i++) { int totalDistance = curDistance + splits.SplitDistance; TimeSpan totalTime = curTime.Add(splitTime); SplitGoal item = new SplitGoal(splits.SplitDistance, splitTime, totalDistance, totalTime, splits.SplitsInKm); splitGoals.Goals.Add(item); curDistance = totalDistance; curTime = totalTime; } if (numSplits != (int)numSplits) { double lastSplitDistance = Math.Round(splits.GoalDistance - curDistance, 1); TimeSpan lastSplitTime = splits.GoalTime.Subtract(curTime); SplitGoal item = new SplitGoal(lastSplitDistance, lastSplitTime, goalDistance, splits.GoalTime, splits.SplitsInKm); splitGoals.Goals.Add(item); } return(splitGoals); }
private SplitGoal HandleGoalCall(ControllerContext controllerContext, string actionName) { List <SplitGoal> splitGoals = SplitServices.SplitGoals; string controllerName = controllerContext.RouteData.Values[Constants.CONTROLLER].ToString(); string areaName = GetAreaName(controllerContext); SplitGoal splitGoal = splitGoals.SingleOrDefault(s => String.Equals(s.Action, actionName, StringComparison.InvariantCultureIgnoreCase) && String.Equals(s.Controller, controllerName, StringComparison.InvariantCultureIgnoreCase) && String.Equals(s.Area, areaName, StringComparison.InvariantCultureIgnoreCase)); return(splitGoal); }
internal void WriteToFile(SplitGoal goal) { var keyValuePairs = new Dictionary <string, string> { { Constants.DATE_TIME, DateTime.UtcNow.ToString() }, { Constants.SPLIT_GOAL, goal.Goal }, { Constants.SPLIT_VIEWS_SEQUENCE_TRAIL, HttpHelpers.GetViewTrail(goal.Goal) } }; CheckForFile(Constants.SPLIT_GOALS_FILE_PATH, new List <string> { Constants.DATE_TIME, Constants.SPLIT_GOAL, Constants.SPLIT_VIEWS_SEQUENCE_TRAIL }); WriteToFile(keyValuePairs, Constants.SPLIT_GOALS_FILE_PATH); }
public override bool InvokeAction(ControllerContext controllerContext, string actionName) { SplitGoal splitGoal = HandleGoalCall(controllerContext, actionName); List <SplitView> splitViews = SplitServices.SplitViews; SplitView splitView = GetCurrentSplit(controllerContext, actionName, splitViews); SplitView selectedSplit = null; if (splitView != null && controllerContext.RouteData.Values[Constants.ACTION].ToString() == actionName) { List <SplitView> all = splitViews.FindAll(s => s.SplitGroup == splitView.SplitGroup); selectedSplit = ChooseSplit(all, splitView.SplitGroup); controllerContext.RouteData.Values[Constants.ACTION] = selectedSplit.Action; controllerContext.RouteData.Values[Constants.CONTROLLER] = selectedSplit.Controller; AddRemoveArea(controllerContext, selectedSplit); AddRemoveNamespace(controllerContext, splitView, selectedSplit); ReinstantiateController(controllerContext, splitView, selectedSplit); actionName = selectedSplit.Action; } bool success = base.InvokeAction(controllerContext, actionName); if (!success || !HttpHelpers.IsIn200Family(controllerContext.HttpContext.Response.StatusDescription)) { return(success); } if (selectedSplit != null) { TargetService.WriteToTargets(selectedSplit); HttpHelpers.SaveToCookie(selectedSplit); HttpHelpers.SaveToSession(selectedSplit); } else if (splitGoal != null) { TargetService.WriteToTargets(splitGoal); HttpHelpers.RemoveFromSession(splitGoal); } return(true); }
internal override void Write(SplitGoal goal) { try { var logservice = new LogService(); var values = Parameters.ToDictionary(parameter => parameter.Key, parameter => ParameterRetriever.RetireveValue(goal, parameter.Value).ToString()); logservice.WriteToFile(values, Path); } catch (Exception ex) { if (TargetService.Config.Targets.ThrowExceptions) { throw new Exception("Exception occurred in AbTestMaster when writing to file. Error Message: " + ex.Message); } } }
internal static object RetireveValue(SplitGoal goal, string parameterName) { object value; switch (parameterName) { case Constants.SPLIT_VIEWS_SEQUENCE_TRAIL_VARIABLE: value = HttpHelpers.GetViewTrail(goal.Goal); break; case Constants.SPLIT_GOAL_VARIABLE: value = goal.Goal; break; default: value = RetireveValue(parameterName); break; } return(value); }
internal abstract void Write(SplitGoal goal);
/// <summary> /// Handle player state changes. /// Event distance is given in meters. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RiderStateEventHandler(object sender, RiderStateEventArgs e) { if (!m_started || !m_splits.ShowSplits) { return; } SplitGoal goal = null; if (m_splitGoals != null) { if (m_splitCount >= m_splitGoals.Goals.Count) { return; } // get the in-progress goal goal = m_splitGoals.Goals[m_splitCount]; } DateTime now = DateTime.Now; TimeSpan runningTime = (now - m_startTime); TimeSpan splitTime = (now - m_splitStartTime); if (m_eventCount++ == 0) { // Capture the current distance traveled value to use as an offset to each successive distance value. m_distanceSeedValue = e.Distance; } // Calculate total distance travelled int totalMeters = e.Distance - m_distanceSeedValue; double kmsTravelled = totalMeters / 1000.0; double milesTravelled = kmsTravelled / 1.609; double totalDistance = Math.Round(m_splits.SplitsInKm ? kmsTravelled : milesTravelled, 1); //double averageKph = Math.Round((kmsTravelled / runningTime.TotalSeconds) * 3600, 1); //double averageMph = Math.Round((milesTravelled / runningTime.TotalSeconds) * 3600, 1); // Calculate how deep into the split distance the rider is. int splitMeters = totalMeters - (m_splits.SplitDistanceAsMeters * m_splitCount); // How much of the split is completed (expressed as percentage) //double splitCompletedPct = Math.Round(splitMeters / (double)m_splits.SplitDistanceAsMeters, 4); double splitCompletedPct = splitMeters / (double)m_splits.SplitDistanceAsMeters; // Compute distance, leave unrounded double splitKmTravelled = splitMeters / 1000.0; double splitMiTravelled = splitKmTravelled / 1.609; double splitDistance = m_splits.SplitsInKm ? splitKmTravelled : splitMiTravelled; double splitSpeed = Math.Round((splitDistance / splitTime.TotalSeconds) * 3600, 1); // Now round the distance splitDistance = Math.Round(splitDistance, 1); //double splitAverageKph = Math.Round((splitKmTravelled / splitTime.TotalSeconds) * 3600, 1); //double splitAverageMph = Math.Round((splitMiTravelled / splitTime.TotalSeconds) * 3600, 1); if (goal != null) { if (splitKmTravelled >= goal.SplitDistanceKm) { // Calculate the deltaTime, positive number is good, negative bad. //TimeSpan deltaTime = goal.TotalTime.Subtract(runningTime); // Calculate the deltaTime, positive number is bad, negative good. TimeSpan deltaTime = new TimeSpan(0, 0, (int)Math.Round(runningTime.Subtract(goal.TotalTime).TotalSeconds, 0)); // This completes the split. TotalDistance travelled and Delta is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, totalDistance, runningTime, m_splits.SplitsInKm, deltaTime); OnSplitGoalCompletedEvent(args); // Reset time and begin next split m_splitStartTime = now; m_splitCount++; m_lastSplitMeters = 0; } else { if (splitMeters - m_lastSplitMeters >= 1) { // Goal time of split start TimeSpan splitStartTime = goal.TotalTime.Subtract(goal.SplitTime); // Goal time to get to this point in the split TimeSpan splitWaypointTime = splitStartTime.Add(goal.SplitTime.Multiply(splitCompletedPct)); // Calculate the deltaTime, positive number is good, negative bad. //TimeSpan deltaTime = splitWaypointTime.Subtract(runningTime); // Calculate the deltaTime, positive number is bad, negative good. TimeSpan deltaTime = new TimeSpan(0, 0, (int)Math.Round(runningTime.Subtract(splitWaypointTime).TotalSeconds, 0)); // This is an update to the split in-progress. SplitDistance travelled is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, splitDistance, runningTime, m_splits.SplitsInKm, deltaTime); OnSplitUpdatedEvent(args); m_lastSplitMeters = splitMeters; Logger.LogInformation($"%Complete: {splitCompletedPct} Start: {splitStartTime.ToString("m'm 's's'")} Waypoint: {splitWaypointTime.ToString("m'm 's's'")} Delta: {deltaTime.ToString("m'm 's's'")}"); } } } else { if (splitKmTravelled >= m_splits.SplitDistanceAsKm) { // This completes the split. TotalDistance traveled is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, totalDistance, runningTime, m_splits.SplitsInKm); OnSplitCompletedEvent(args); // Reset time and begin next split m_splitStartTime = now; m_splitCount++; m_lastSplitMeters = 0; } else { if (splitMeters - m_lastSplitMeters >= 1) { // This is an update to the split in-progress. SplitDistance traveled is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, splitDistance, runningTime, m_splits.SplitsInKm); OnSplitUpdatedEvent(args); m_lastSplitMeters = splitMeters; } } } }
public SplitGoalAttribute(string goalName) { SplitGoal = new SplitGoal { Goal = goalName }; }
/// <summary> /// Handle player state changes. /// Event distance is given in meters. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RiderStateEventHandler(object sender, RiderStateEventArgs e) { if (!m_started || !m_splits.ShowSplits) { return; } SplitGoal goal = null; if (m_splitGoals != null) { if (m_splitCount >= m_splitGoals.Goals.Count) { return; } // get the in-progress goal goal = m_splitGoals.Goals[m_splitCount]; } DateTime now = DateTime.Now; TimeSpan runningTime = (now - m_startTime); TimeSpan splitTime = (now - m_splitStartTime); if (m_eventCount++ == 0) { // Capture the current distance traveled value to use as an offset to each successive distance value. m_distanceSeedValue = e.Distance; } // Calculate total distance travelled int totalMeters = e.Distance - m_distanceSeedValue; double kmsTravelled = totalMeters / 1000.0; double milesTravelled = kmsTravelled / 1.609; double totalDistance = Math.Round(m_splits.SplitsInKm ? kmsTravelled : milesTravelled, 1); //double averageKph = Math.Round((kmsTravelled / runningTime.TotalSeconds) * 3600, 1); //double averageMph = Math.Round((milesTravelled / runningTime.TotalSeconds) * 3600, 1); // Calculate how deep into the split distance the rider is. int splitMeters = totalMeters - (m_splits.SplitDistanceAsMeters * m_splitCount); double splitKmTravelled = Math.Round(splitMeters / 1000.0, 1); double splitMiTravelled = Math.Round(splitKmTravelled / 1.609, 1); double splitDistance = m_splits.SplitsInKm ? splitKmTravelled : splitMiTravelled; double splitSpeed = Math.Round((splitDistance / splitTime.TotalSeconds) * 3600, 1); //double splitAverageKph = Math.Round((splitKmTravelled / splitTime.TotalSeconds) * 3600, 1); //double splitAverageMph = Math.Round((splitMiTravelled / splitTime.TotalSeconds) * 3600, 1); if (goal != null) { if (splitKmTravelled >= goal.SplitDistanceKm) { // Calculate the deltaTime, positive number is good, negative bad. TimeSpan deltaTime = goal.TotalTime.Subtract(runningTime); // This completes the split. TotalDistance travelled and Delta is included. SplitGoalCompletedEventArgs args = new SplitGoalCompletedEventArgs(m_splitCount + 1, splitTime, splitSpeed, totalDistance, runningTime, m_splits.SplitsInKm, deltaTime); OnSplitGoalCompletedEvent(args); // Reset time and begin next split m_splitStartTime = now; m_splitCount++; m_lastSplitMeters = 0; } else { if (splitMeters - m_lastSplitMeters >= 100) // only raise update event every 100 meters or so { // This is an update to the split in-progress. SplitDistance travelled is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, splitDistance, runningTime, m_splits.SplitsInKm); OnSplitUpdatedEvent(args); m_lastSplitMeters = splitMeters; } } } else { if (splitKmTravelled >= m_splits.SplitDistanceAsKm) { // This completes the split. TotalDistance traveled is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, totalDistance, runningTime, m_splits.SplitsInKm); OnSplitCompletedEvent(args); // Reset time and begin next split m_splitStartTime = now; m_splitCount++; m_lastSplitMeters = 0; } else { if (splitMeters - m_lastSplitMeters >= 100) // only raise update event every 100 meters or so { // This is an update to the split in-progress. SplitDistance traveled is included. SplitEventArgs args = new SplitEventArgs(m_splitCount + 1, splitTime, splitSpeed, splitDistance, runningTime, m_splits.SplitsInKm); OnSplitUpdatedEvent(args); m_lastSplitMeters = splitMeters; } } } }