static public void ProcessWorld(IRealtimeCallback _callback) { //if another Process is taking care of it, return if (IsProcessingMatches()) { return; } var _context = GetDatabaseContext(); //skipping time == jumping to the first upcoming event(s) and processes those // DateTime next_date = GetEarliestEvent(_context); GetWorldState().AsyncProcessesCount++; _callback.OnWorldStateChanged(GetWorldState()); //set world date GetWorldState().CurrentDateTime = next_date; _context.SaveChanges(); ProcessGameTick(_context, _callback, true); GetWorldState().AsyncProcessesCount--; _context.SaveChanges(); _callback.OnWorldStateChanged(GetWorldState()); }
static private void ProcessGameTick(SoccerWorldDatabaseContext _context, IRealtimeCallback _callback, bool first_run = false) { //check if new CompetitionEvents are found, and activate them ExecuteCompetitionEvents(_context, _callback, GetWorldState().CurrentDateTime); //check if new matches found on current time, to add to matches-pool UpdateMatchPool(_context, _callback, GetWorldState().CurrentDateTime); //if matches-pool is empty if (_MATCHES.Count == 0) { // end Process return; } //else process all matches ProcessMatchPool(); GetWorldState().CurrentDateTime = GetWorldState().CurrentDateTime.AddMinutes(1); GetDatabaseContext().SaveChanges(); if (_MATCHES.Count > 0) { _callback.OnMatchesChanged(_MATCHES); } _callback.OnWorldStateChanged(GetWorldState()); Thread.Sleep(2000); ProcessGameTick(_context, _callback); }
static private void UpdateMatchPool(SoccerWorldDatabaseContext _context, IRealtimeCallback _callback, DateTime date) { //select ended matches and send callback _callback.OnMatchesEnd(_MATCHES.Where(o => o.HasEnded)); //check if matches ended, to remove from matches-pool _MATCHES = _MATCHES.Where(o => o.HasEnded == false).ToList(); //check if matches started, to add them to matches-pool List <Match> new_matches = _context.Matches.Where(o => o.Date == date) .Include(o => o.HomeClub) .Include(o => o.AwayClub) .Include(o => o.CompetitionEvent) .ThenInclude(e => e.Competition) .ThenInclude(c => c.Country) .ToList(); _MATCHES.AddRange(new_matches); }
static private void ProcessWorld(SoccerWorldDatabaseContext _context, IRealtimeCallback _callback, DateTime target_date) { //if another Process is taking care of it, return if (IsProcessingMatches()) { return; } //as long we did not reach target date while (GetWorldState().CurrentDateTime < target_date) { DateTime eventdate = GetEarliestEvent(_context); //update world date with the first next date (target or event) GetWorldState().CurrentDateTime = (eventdate < target_date ? eventdate : target_date); GetWorldState().AsyncProcessesCount++; _context.SaveChanges(); _callback.OnWorldStateChanged(GetWorldState()); ProcessGameTick(_context, _callback, true); GetWorldState().AsyncProcessesCount--; _context.SaveChanges(); } }
static private bool ExecuteCompetitionEvents(SoccerWorldDatabaseContext _context, IRealtimeCallback _callback, DateTime date) { //get all events for current date ICollection <CompetitionEvent> events = _context.CompetitionEvents.Where(o => o.Date == date) .Include(o => o.NextCompetitionEvent) .Include(o => o.Competition) .ThenInclude(o => o.Clubs) .ThenInclude(o => o.Country) .OrderByDescending(o => o.Id) .ToList(); if (events.Count == 0) { return(false); } //process each event sequential foreach (CompetitionEvent comp_event in events) { comp_event.Execute(); } _context.SaveChanges(); foreach (CompetitionEvent comp_event in events) { _callback.OnCompetitionEventProcessed(comp_event); } return(true); }