public void generateWorkout(DateTime startDate, int userID, ref List <int> muscleGroupIDs, List <int> subMuscleGroups) { int workoutID = createWorkout(); UserWorkout userWorkout = generateUserWorkout(userID, workoutID, startDate); generateWorkoutExercises(userWorkout, ref muscleGroupIDs, subMuscleGroups); return; }
public UserWorkout generateUserWorkout(int userID, int workoutID, DateTime day) { try { TimeSpan start = TimeSpan.FromHours(hourMin); TimeSpan end = TimeSpan.FromHours(hourMax); int maxMinutes = (int)((end - start).TotalMinutes); string Day = convertDateToString(day); int minutes = rnd.Next(maxMinutes); TimeSpan workoutStartTime = start.Add(TimeSpan.FromMinutes(minutes)); String randomStartDate = Day + " " + workoutStartTime.ToString(); Console.WriteLine("Start Date: " + randomStartDate + "\n"); UserWorkout userWorkout = new UserWorkout(); userWorkout.workoutID = workoutID; userWorkout.userID = userID; userWorkout.startDate = Day; userWorkout.startTime = workoutStartTime; using (MySqlConnection con = new MySqlConnection(SqlDBConfig.connectionString)) { con.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = con; string query = "INSERT INTO USERWORKOUTS (user,workout,start_date) VALUES (" + userWorkout.userID.ToString() + "," + userWorkout.workoutID.ToString() + ",'" + randomStartDate + "');"; cmd.CommandText = query; cmd.ExecuteNonQuery(); } return(userWorkout); } catch (NullReferenceException) { Console.WriteLine("Null Reference Exception occured."); return(null); } }
public void generateWorkoutExercises(UserWorkout userWorkout, ref List <int> muscleGroupIDs, List <int> subMuscleGroups) { List <int> exerciseIDList = dbConfig.getExerciseIDs(); TimeSpan time = userWorkout.startTime; int order = 1; int exerciseNumber = exerciseNumberMin + rnd.Next(exerciseNumberMax - exerciseNumberMin + 1); int i = 0; while (i < exerciseNumber) { using (MySqlConnection con = new MySqlConnection(SqlDBConfig.connectionString)) { List <int> previousCardioExercises = new List <int>(); if (i < cardioExerciseNumber) { con.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = con; WorkoutExercise workoutExercise = new WorkoutExercise(); workoutExercise.workoutID = userWorkout.workoutID; workoutExercise.startDate = userWorkout.startDate + " " + time.ToString(); workoutExercise.startTime = time; workoutExercise.setNumber = 1; workoutExercise.setTime = TimeSpan.FromMinutes(rnd.Next(10, 15)); workoutExercise.setTimeInSeconds = Convert.ToInt32(workoutExercise.setTime.TotalSeconds); workoutExercise.endTime = time + calculateExerciseTime(workoutExercise.setNumber, workoutExercise.setTime); workoutExercise.endDate = userWorkout.startDate + " " + workoutExercise.endTime.ToString(); workoutExercise.exerciseOrder = order; workoutExercise.exerciseID = chooseCardioExercise(); bool found = false; if (previousCardioExercises.Count != 0) { for (int a = 0; a < previousCardioExercises.Count; a++) { if (workoutExercise.exerciseID == previousCardioExercises[a]) { found = true; } } } if (found) { continue; } else { previousCardioExercises.Add(workoutExercise.exerciseID); } i++; order++; time = workoutExercise.endTime + workoutExercise.restTime; string query = "INSERT INTO WORKOUTEXERCISES" + " (workout,exercise,exercise_order," + "set_number,set_time," + "start_date,end_date) VALUES (" + workoutExercise.workoutID + "," + workoutExercise.exerciseID + "," + workoutExercise.exerciseOrder + "," + workoutExercise.setNumber + "," + workoutExercise.setTimeInSeconds + ",'" + workoutExercise.startDate + "','" + workoutExercise.endDate + "');"; cmd.CommandText = query; cmd.ExecuteNonQuery(); } else { con.Open(); int times = minExerciseMuscleGroup + rnd.Next(maxExerciseMuscleGroup - minExerciseMuscleGroup + 1); int j = 0; if (muscleGroupIDs.Count == 0) { for (int k = 0; k < subMuscleGroups.Count; k++) { muscleGroupIDs.Add(subMuscleGroups[k]); } } int muscleGroup = muscleGroupIDs[rnd.Next(muscleGroupIDs.Count)]; muscleGroupIDs.Remove(muscleGroup); List <int> previousExercises = new List <int>(); while (j < times && i < exerciseNumber) { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = con; WorkoutExercise workoutExercise = new WorkoutExercise(); workoutExercise.workoutID = userWorkout.workoutID; workoutExercise.startDate = userWorkout.startDate + " " + time.ToString(); workoutExercise.startTime = time; workoutExercise.endTime = time + calculateExerciseTime(workoutExercise.setNumber, workoutExercise.setTime); workoutExercise.endDate = userWorkout.startDate + " " + workoutExercise.endTime.ToString(); workoutExercise.exerciseOrder = order; workoutExercise.exerciseID = chooseExercise(muscleGroup); bool found = false; if (previousExercises.Count != 0) { for (int a = 0; a < previousExercises.Count; a++) { if (workoutExercise.exerciseID == previousExercises[a]) { found = true; } } } if (found) { continue; } else { previousExercises.Add(workoutExercise.exerciseID); } i++; j++; order++; time = workoutExercise.endTime + workoutExercise.restTime; string query = "INSERT INTO WORKOUTEXERCISES" + " (workout,exercise,exercise_order," + "set_number,set_time," + "start_date,end_date) VALUES (" + workoutExercise.workoutID + "," + workoutExercise.exerciseID + "," + workoutExercise.exerciseOrder + "," + workoutExercise.setNumber + "," + workoutExercise.setTimeInSeconds + ",'" + workoutExercise.startDate + "','" + workoutExercise.endDate + "');"; cmd.CommandText = query; cmd.ExecuteNonQuery(); } } } } }