1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! The Ingo algorithm, the predecessor of DWZ and one of the first rating algorithms invented in 1947.  
//! Sometimes still used in Xiangqi ("Chinese Chess").
//!
//! Unlike with the other rating systems, with Ingo a lower rating is more desirable,
//! and negative values are possible, though unlikely.  
//! A player with an Ingo rating of 0 has an equivalent Elo rating of 2840, and an Ingo rating of -1 equals 2848 Elo.
//!
//! # Quickstart
//!
//! This is the most basic example on how to use the Ingo Module.  
//! Please take a look at the functions below to see more advanced use cases.
//!
//! ```
//! use skillratings::{
//!     ingo::{ingo, IngoRating},
//!     Outcomes,
//! };
//!
//! // Initialise a new player rating.
//! // We need to set the actual age for the player,
//! // if you are unsure what to set here, choose something that is greater than 25.
//! let player_one = IngoRating::new(19);
//!
//! // Or you can initialise it with your own values of course.
//! // Imagine these numbers being pulled from a database.
//! let (some_rating, some_age) = (150.4, 23);
//! let player_two = IngoRating {
//!     rating: some_rating,
//!     age: some_age,
//! };
//!
//! // The outcome of the match is from the perspective of player one.
//! let outcome = Outcomes::WIN;
//!
//! // The ingo function will calculate the new ratings for both players and return them.
//! let (new_player_one, new_player_two) = ingo(&player_one, &player_two, &outcome);
//! ```
//!
//! # More Information
//!
//! - [Wikipedia Article (German, no english version available)](https://de.wikipedia.org/wiki/Ingo-Zahl)
//! - [Archive of Ingo Ratings (German)](https://www.schachbund.de/ingo-spiegel.html)
//! - [Ingo Rules (German, PDF Download)](https://www.schachbund.de/ingo-spiegel.html?file=files/dsb/historie/ingo-spiegel/Ingo-Regeln.pdf&cid=28120)

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{elo::EloRating, Outcomes};

#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// The Ingo rating of a player.
///
/// Note that unlike in the other systems, a lower score is better than a higher score.  
/// Negative values are possible.
///
/// The age is the actual age of the player, if unsure or unavailable set this to `>25`.  
/// Converting from an `EloRating` or using `IngoRating::default()` will set the age to 26.
///
/// The default rating is 230.0.
pub struct IngoRating {
    /// The rating value for a player, by default 230.0.
    /// Note that a lower rating is more desirable.
    pub rating: f64,
    /// The age of the player, if uncertain or unavailable set this to `>25`.
    pub age: usize,
}

impl IngoRating {
    #[must_use]
    /// Initialise a new `IngoRating` with a rating of 230.0 and the given age.  
    /// The age is the actual age of the player, if unsure or unavailable set this to `>25`.
    pub const fn new(age: usize) -> Self {
        Self { rating: 230.0, age }
    }
}

impl Default for IngoRating {
    fn default() -> Self {
        Self::new(26)
    }
}

impl From<(f64, usize)> for IngoRating {
    fn from((r, a): (f64, usize)) -> Self {
        Self { rating: r, age: a }
    }
}

// Just in case the age is unknown.
impl From<f64> for IngoRating {
    fn from(r: f64) -> Self {
        Self { rating: r, age: 26 }
    }
}

impl From<EloRating> for IngoRating {
    fn from(e: EloRating) -> Self {
        Self {
            rating: 355.0 - (e.rating / 8.0),
            ..Default::default()
        }
    }
}

#[must_use]
/// Calculates the [`IngoRating`]s of two players based on their ratings, and the outcome of the game.
///
/// Takes in two players as [`IngoRating`]s, and an [`Outcome`](Outcomes).
///
/// Instead of the traditional way of calculating the Ingo for only one player only using a list of results,
/// we are calculating the Ingo rating for two players at once, like in the Elo calculation,
/// to make it easier to see instant results.
///
/// The outcome of the match is in the perspective of `player_one`.
/// This means [`Outcomes::WIN`] is a win for `player_one` and [`Outcomes::LOSS`] is a win for `player_two`.
///
/// A lower Ingo rating is more desirable, and while negative values are possible,
/// a player with an Ingo rating of 0 has an equivalent Elo rating of 2840.
///
/// # Examples
/// ```
/// use skillratings::{
///     ingo::{ingo, IngoRating},
///     Outcomes,
/// };
///
/// let player_one = IngoRating {
///     rating: 130.0,
///     age: 40,
/// };
/// let player_two = IngoRating {
///     rating: 160.0,
///     age: 40,
/// };
///
/// let (new_one, new_two) = ingo(&player_one, &player_two, &Outcomes::WIN);
///
/// assert!((new_one.rating.round() - 129.0).abs() < f64::EPSILON);
/// assert!((new_two.rating.round() - 161.0).abs() < f64::EPSILON);
/// ```
pub fn ingo(
    player_one: &IngoRating,
    player_two: &IngoRating,
    outcome: &Outcomes,
) -> (IngoRating, IngoRating) {
    let outcome1 = outcome.to_chess_points();
    let outcome2 = 1.0 - outcome1;

    let perf1 = performance(player_two.rating, outcome1);
    let perf2 = performance(player_one.rating, outcome2);

    let development1 = age_to_devcoefficent(player_one.age);
    let development2 = age_to_devcoefficent(player_two.age);

    let new_rating1 = perf1.mul_add(1.0, player_one.rating * development1) / (1.0 + development1);
    let new_rating2 = perf2.mul_add(1.0, player_two.rating * development2) / (1.0 + development2);

    (
        IngoRating {
            rating: new_rating1,
            age: player_one.age,
        },
        IngoRating {
            rating: new_rating2,
            age: player_two.age,
        },
    )
}

#[must_use]
/// The "traditional" way of calculating a [`IngoRating`] of a player in a rating period.
///
/// Takes in a player as an [`IngoRating`] and their results as a Slice of tuples containing the opponent as an [`IngoRating`],
/// and the outcome of the game as an [`Outcome`](Outcomes)
///
/// The outcome of the match is in the perspective of the player.
/// This means [`Outcomes::WIN`] is a win for the player and [`Outcomes::LOSS`] is a win for the opponent.
///
/// # Examples
/// ```
/// use skillratings::{
///     ingo::{ingo_rating_period, IngoRating},
///     Outcomes,
/// };
///
/// let player = IngoRating {
///     rating: 130.0,
///     age: 40,
/// };
///
/// let opponent1 = IngoRating {
///     rating: 160.0,
///     age: 40,
/// };
///
/// let opponent2 = IngoRating {
///     rating: 160.0,
///     age: 40,
/// };
///
/// let opponent3 = IngoRating {
///     rating: 55.0,
///     age: 40,
/// };
///
/// let opponent4 = IngoRating {
///     rating: 90.0,
///     age: 40,
/// };
///
/// let results = vec![
///     (opponent1, Outcomes::WIN),
///     (opponent2, Outcomes::DRAW),
///     (opponent3, Outcomes::WIN),
///     (opponent4, Outcomes::LOSS),
/// ];
///
/// let new_player = ingo_rating_period(&player, &results);
///
/// assert!((new_player.rating.round() - 126.0).abs() < f64::EPSILON);
/// ```
pub fn ingo_rating_period(player: &IngoRating, results: &[(IngoRating, Outcomes)]) -> IngoRating {
    // Ingo was meant to be used in tournaments, so we do not need to loop over the opponents here.
    let development = age_to_devcoefficent(player.age);

    let average_points =
        results.iter().map(|r| r.1.to_chess_points()).sum::<f64>() / results.len() as f64;

    let average_opponent_rating =
        results.iter().map(|r| r.0.rating).sum::<f64>() / results.len() as f64;

    let performance = performance(average_opponent_rating, average_points);

    let new_rating = performance.mul_add(results.len() as f64, player.rating * development)
        / (results.len() as f64 + development);

    IngoRating {
        rating: new_rating,
        age: player.age,
    }
}

#[must_use]
/// Calculates the expected outcome of two players based on Ingo.
///
/// Takes in two players as [`IngoRating`]s and returns the probability of victory for each player as an [`f64`] between 1.0 and 0.0.  
/// 1.0 means a certain victory for the player, 0.0 means certain loss.
/// Values near 0.5 mean a draw is likely to occur.
///
/// # Examples
/// ```
/// use skillratings::{
///     ingo::{expected_score, IngoRating},
///     Outcomes,
/// };
///
/// let player_one = IngoRating {
///     rating: 130.0,
///     age: 40,
/// };
/// let player_two = IngoRating {
///     rating: 160.0,
///     age: 40,
/// };
///
/// let (exp1, exp2) = expected_score(&player_one, &player_two);
///
/// assert!(((exp1 * 100.0).round() - 80.0).abs() < f64::EPSILON);
/// assert!(((exp2 * 100.0).round() - 20.0).abs() < f64::EPSILON);
///
/// assert!((exp1 + exp2 - 1.0).abs() < f64::EPSILON);
/// ```
pub fn expected_score(player_one: &IngoRating, player_two: &IngoRating) -> (f64, f64) {
    let exp_one = 0.5 + (player_two.rating - player_one.rating) / 100.0;
    let exp_two = 1.0 - exp_one;

    (exp_one, exp_two)
}

fn performance(average_rating: f64, score: f64) -> f64 {
    average_rating - (100.0 * score - 50.0)
}

/// Similar to the DWZ algorithm, we use the age of the player to get the development coefficient.
const fn age_to_devcoefficent(age: usize) -> f64 {
    match age {
        0..=20 => 10.0,
        21..=25 => 15.0,
        _ => 20.0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ingo() {
        let player_one = IngoRating {
            rating: 130.0,
            age: 40,
        };
        let player_two = IngoRating {
            rating: 160.0,
            age: 40,
        };

        let (p1, p2) = ingo(&player_one, &player_two, &Outcomes::WIN);

        assert!((p1.rating.round() - 129.0).abs() < f64::EPSILON);
        assert!((p2.rating.round() - 161.0).abs() < f64::EPSILON);

        let (p1, p2) = ingo(&player_one, &player_two, &Outcomes::LOSS);

        assert!((p1.rating.round() - 134.0).abs() < f64::EPSILON);
        assert!((p2.rating.round() - 156.0).abs() < f64::EPSILON);

        let young_player = IngoRating {
            rating: 230.0,
            age: 7,
        };

        let old_player = IngoRating {
            rating: 109.0,
            age: 78,
        };

        let (yp, op) = ingo(&young_player, &old_player, &Outcomes::DRAW);

        assert!((yp.rating.round() - 219.0).abs() < f64::EPSILON);
        assert!((op.rating.round() - 115.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_ingo_rating_period() {
        let player_one = IngoRating {
            rating: 130.0,
            age: 22,
        };
        let player_two = IngoRating {
            rating: 160.0,
            age: 40,
        };

        let results = vec![(player_two, Outcomes::WIN)];

        let p1 = ingo_rating_period(&player_one, &results);

        assert!((p1.rating.round() - 129.0).abs() < f64::EPSILON);

        let player_two = IngoRating {
            rating: 160.0,
            age: 40,
        };

        let player_three = IngoRating {
            rating: 160.0,
            age: 40,
        };

        let player_four = IngoRating {
            rating: 55.0,
            age: 22,
        };

        let player_five = IngoRating {
            rating: 90.0,
            age: 40,
        };

        let results = vec![
            (player_two, Outcomes::WIN),
            (player_three, Outcomes::DRAW),
            (player_four, Outcomes::WIN),
            (player_five, Outcomes::LOSS),
        ];

        let p1 = ingo_rating_period(&player_one, &results);

        assert!((p1.rating.round() - 124.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_expected_score() {
        let player_one = IngoRating {
            rating: 130.0,
            age: 40,
        };
        let player_two = IngoRating {
            rating: 160.0,
            age: 40,
        };

        let (exp1, exp2) = expected_score(&player_one, &player_two);

        assert!(((exp1 * 100.0).round() - 80.0).abs() < f64::EPSILON);
        assert!(((exp2 * 100.0).round() - 20.0).abs() < f64::EPSILON);

        assert!((exp1 + exp2 - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn ingo_elo_conv() {
        let elo_player = EloRating::new();

        let ingo_player = IngoRating::from(elo_player);

        assert!((ingo_player.rating - 230.0).abs() < f64::EPSILON);
        assert_eq!(ingo_player.age, 26);

        let ingo_player = IngoRating::default();

        let elo_player = EloRating::from(ingo_player);

        assert!((elo_player.rating - 1000.0).abs() < f64::EPSILON);
    }

    #[test]
    #[allow(clippy::clone_on_copy)]
    fn test_misc_stuff() {
        let player_one = IngoRating::new(14);

        assert_eq!(player_one, player_one.clone());

        assert!(!format!("{:?}", player_one).is_empty());

        assert_eq!(IngoRating::from((222.0, 26)), IngoRating::from(222.0));
    }
}