52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Code.Example.Race.Track;
|
|
using Code.Infrastructure.EventBus;
|
|
|
|
namespace Code.Example.Race
|
|
{
|
|
public class RaceScores : IRacerPassedCheckpointHandler, IDisposable
|
|
{
|
|
private Dictionary<Racer.AI.Racer, int> _scores = new();
|
|
private EventBus _eventBus;
|
|
|
|
public RaceScores(EventBus eventBus)
|
|
{
|
|
_eventBus = eventBus;
|
|
_eventBus.Subscribe(this);
|
|
}
|
|
public void SubscribeRacer(Racer.AI.Racer racer)
|
|
{
|
|
_scores.Add(racer, 0);
|
|
}
|
|
|
|
public void ChangeScore(Racer.AI.Racer racer, int additionalScore)
|
|
{
|
|
_scores[racer] += additionalScore;
|
|
_eventBus.RaiseEvent<IScoreChangedHandle>(x=> x.ScoreChanged(racer,additionalScore, GetScores()));
|
|
}
|
|
|
|
public void UnsubscribeRacer(Racer.AI.Racer racer)
|
|
{
|
|
_scores.Remove(racer);
|
|
}
|
|
|
|
public List<KeyValuePair<Racer.AI.Racer, int>> GetScores()
|
|
{
|
|
return _scores.OrderByDescending(x => x.Value).ToList();
|
|
}
|
|
|
|
public void OnRacerPassedCheckpoint(Racer.AI.Racer racer) => ChangeScore(racer,1);
|
|
|
|
public void Dispose()
|
|
{
|
|
_eventBus.Unsubscribe(this);
|
|
}
|
|
}
|
|
|
|
public interface IScoreChangedHandle : IGlobalSubscriber
|
|
{
|
|
void ScoreChanged(Racer.AI.Racer racer, int additionalScore, List<KeyValuePair<Racer.AI.Racer, int>> scores);
|
|
}
|
|
} |