/// <summary> /// Halts the current session /// </summary> /// <param name="finalSessionScore">the score of the current session</param> public void SessionEnd(double finalSessionScore) { try { //Is there an existing session open? if (CurrentSessionID == -1) { throw new Exception("There is no session open."); } // merge staging to BestSolutions cache foreach (var stageBsol in MemoryManager.BestSolutionStaging) { // update sessions score stageBsol.SessionScore = finalSessionScore; Dictionary <long, BestSolution> bsDict; if (MemoryManager.BestSolutions.TryGetValue(stageBsol.RneuronId, out bsDict)) { BestSolution bSol; if (bsDict.TryGetValue(stageBsol.SolutionId, out bSol)) { if ((stageBsol.SessionScore > bSol.SessionScore) || (stageBsol.SessionScore == bSol.SessionScore && stageBsol.CycleScore > bSol.CycleScore) || (stageBsol.SessionScore == bSol.SessionScore && stageBsol.CycleScore == bSol.CycleScore && stageBsol.CycleOrder > bSol.CycleOrder)) { bsDict[stageBsol.SolutionId] = stageBsol; } } else { bsDict.Add(stageBsol.SolutionId, stageBsol); } } else { var newDict = new Dictionary <long, BestSolution>(); newDict.Add(stageBsol.SolutionId, stageBsol); MemoryManager.BestSolutions.TryAdd(stageBsol.RneuronId, newDict); } } MemoryManager.BestSolutionStaging.Clear(); // TODO need to change to actual update for the session var session = MemoryManager.Sessions[CurrentSessionID]; session.DateTimeStop = DateTime.Now; session.SessionScore = finalSessionScore; session.Hidden = false; //update session to db if (PersistData) { MemoryManager.AddSessionToUpdateToQueue(session); } //Throw the endsession event if (SessionComplete != null) { SessionComplete(CurrentSessionID, this); } LastSessionID = CurrentSessionID; CurrentSessionID = -1; } catch (Exception e) { throw; } }