| | 1 | | using MRA.DTO.Models; |
| | 2 | |
|
| | 3 | | namespace MRA.Services.Models.Drawings; |
| | 4 | |
|
| | 5 | | public static class DrawingVoteManager |
| | 6 | | { |
| | 7 | | public static void UpdateDrawingScore(this DrawingModel drawing, int newScore) |
| | 8 | | { |
| 4 | 9 | | newScore = newScore.AdjustUserScore(); |
| 4 | 10 | | if (drawing.VotesPopular > 0) |
| | 11 | | { |
| 2 | 12 | | drawing.ScorePopular = CalculateUserScore(newScore, drawing.ScorePopular, drawing.VotesPopular); |
| 2 | 13 | | drawing.VotesPopular = drawing.VotesPopular + 1; |
| | 14 | | } |
| | 15 | | else |
| | 16 | | { |
| 2 | 17 | | drawing.ScorePopular = newScore; |
| 2 | 18 | | drawing.VotesPopular = 1; |
| | 19 | | } |
| 2 | 20 | | } |
| | 21 | |
|
| | 22 | | public static double CalculateUserScore(int newScore, double scorePopular, int currentVotes) |
| | 23 | | { |
| 3 | 24 | | return ((scorePopular * currentVotes) + newScore.AdjustUserScore()) / (currentVotes + 1); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | private static int AdjustUserScore(this int score) |
| | 28 | | { |
| 8 | 29 | | if (score > 100) score = 100; |
| 8 | 30 | | if (score < 0) score = 0; |
| 7 | 31 | | return score; |
| | 32 | | } |
| | 33 | | } |