-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathweb_api.rs
266 lines (250 loc) · 8.92 KB
/
web_api.rs
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
use axum::http::StatusCode;
use engine::composite::CompositeEvaluator;
use engine::dice::Dice;
use engine::evaluator::Evaluator;
use engine::position::Position;
use logic::bg_move::{BgMove, MoveDetail};
use logic::cube::CubeInfo;
use serde::{Deserialize, Serialize};
use utoipa::{IntoParams, ToSchema};
pub struct WebApi<T: Evaluator> {
evaluator: T,
}
impl WebApi<CompositeEvaluator> {
pub fn try_default() -> Option<Self> {
match CompositeEvaluator::try_default() {
Ok(evaluator) => Some(Self { evaluator }),
Err(_) => None,
}
}
}
impl<T: Evaluator> WebApi<T> {
pub fn new(evaluator: T) -> Self {
Self { evaluator }
}
pub fn get_eval(&self, pip_params: PipParams) -> Result<EvalResponse, (StatusCode, String)> {
let position = Position::try_from(pip_params);
match position {
Err(error) => Err((StatusCode::BAD_REQUEST, error.to_string())),
Ok(position) => {
let evaluation = self.evaluator.eval(&position);
let cube = CubeInfo::from(&evaluation);
let probabilities = ProbabilitiesView::from(evaluation);
Ok(EvalResponse {
cube,
probabilities,
})
}
}
}
pub fn get_move(
&self,
pip_params: PipParams,
dice_params: DiceParams,
) -> Result<MoveResponse, &'static str> {
let position = Position::try_from(pip_params)?;
let dice = Dice::try_from((dice_params.die1, dice_params.die2))?;
let pos_and_probs = self
.evaluator
.positions_and_probabilities_by_equity(&position, &dice);
let moves: Vec<MoveInfo> = pos_and_probs
.into_iter()
.map(|(new_pos, probabilities)| {
let bg_move = BgMove::new(&position, &new_pos, &dice);
let play = bg_move.into_details();
let probabilities = probabilities.into(); // convert model into view model
MoveInfo {
play,
probabilities,
}
})
.collect();
Ok(MoveResponse { moves })
}
}
#[derive(Serialize, ToSchema)]
/// The whole body of the HTTP response.
/// Contains the probabilities for this position and cube decisions.
pub struct EvalResponse {
cube: CubeInfo,
probabilities: ProbabilitiesView,
}
#[derive(Serialize, ToSchema)]
/// The whole body of the HTTP response. Contains the list of all legal moves.
pub struct MoveResponse {
/// The array is ordered by match equity. First move is the best one.
/// If moving checkers is not possible, the array contains exactly one move
/// and the `play` array is empty.
#[schema(minimum = 0)]
moves: Vec<MoveInfo>,
}
#[derive(Serialize, ToSchema)]
/// This represents one complete move.
pub struct MoveInfo {
/// Contains 0 to 4 elements for moving a single checker.
/// If no move is possible because everything is blocked, the array is empty.
/// If the dice are different, the array contains up to 2 elements.
/// If the dice are identical (double roll), the array contains up to 4 elements.
#[schema(minimum = 0, maximum = 4)]
play: Vec<MoveDetail>,
probabilities: ProbabilitiesView,
}
// This is similar to evaluator::Probabilities. But while the former serves
// as a model for calculations, this is more like a view model for the web API.
// While in evaluator::Probabilities all 6 numbers add up to 1.0, this is different.
/// Chances for winning/losing normal/gammon/backgammon.
///
/// `win` includes the chances to win gammon or BG.
/// `winG` includes the chances to win BG and `loseG` includes the chance to lose BG.
/// This way we use the same format as earlier engines like GnuBG have done.
/// `lose` is not given, you can calculate it through `1 - win`.
#[derive(Serialize, ToSchema)]
#[allow(non_snake_case)]
#[schema(title = "Probabilities")]
pub struct ProbabilitiesView {
/// Probability to win normal, gammon or backgammon
#[schema(minimum = 0, maximum = 1)]
pub(crate) win: f32,
/// Probability to win gammon or backgammon
#[schema(minimum = 0, maximum = 1)]
pub(crate) winG: f32,
/// Probability to win backgammon
#[schema(minimum = 0, maximum = 1)]
pub(crate) winBg: f32,
/// Probability to lose gammon or backgammon
#[schema(minimum = 0, maximum = 1)]
pub(crate) loseG: f32,
/// Probability to lose backgammon
#[schema(minimum = 0, maximum = 1)]
pub(crate) loseBg: f32,
}
impl From<engine::probabilities::Probabilities> for ProbabilitiesView {
fn from(value: engine::probabilities::Probabilities) -> Self {
Self {
win: value.win_normal + value.win_gammon + value.win_bg,
winG: value.win_gammon + value.win_bg,
winBg: value.win_bg,
loseG: value.lose_gammon + value.lose_bg,
loseBg: value.lose_bg,
}
}
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct DiceParams {
#[param(minimum = 1, maximum = 6, example = 3)]
die1: usize,
#[param(minimum = 1, maximum = 6, example = 1)]
die2: usize,
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct PipParams {
/// Bar for the opponent `o`.
#[param(minimum = -15, maximum = 0)]
p0: Option<i8>,
#[param(minimum = -15, maximum = 15, example = -2)]
p1: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p2: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p3: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p4: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p5: Option<i8>,
#[param(minimum = -15, maximum = 15, example = 5)]
p6: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p7: Option<i8>,
#[param(minimum = -15, maximum = 15, example = 3)]
p8: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p9: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p10: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p11: Option<i8>,
#[param(minimum = -15, maximum = 15, example = -5)]
p12: Option<i8>,
#[param(minimum = -15, maximum = 15, example = 5)]
p13: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p14: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p15: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p16: Option<i8>,
#[param(minimum = -15, maximum = 15, example = -3)]
p17: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p18: Option<i8>,
#[param(minimum = -15, maximum = 15, example = -5)]
p19: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p20: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p21: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p22: Option<i8>,
#[param(minimum = -15, maximum = 15)]
p23: Option<i8>,
#[param(minimum = -15, maximum = 15, example = 2)]
p24: Option<i8>,
/// Bar for your player `x`.
#[param(minimum = 0, maximum = 15)]
p25: Option<i8>,
}
impl TryFrom<PipParams> for Position {
type Error = &'static str;
fn try_from(params: PipParams) -> Result<Self, Self::Error> {
// let params = params.pips;
let pips: [i8; 26] = [
params.p0.unwrap_or_default(),
params.p1.unwrap_or_default(),
params.p2.unwrap_or_default(),
params.p3.unwrap_or_default(),
params.p4.unwrap_or_default(),
params.p5.unwrap_or_default(),
params.p6.unwrap_or_default(),
params.p7.unwrap_or_default(),
params.p8.unwrap_or_default(),
params.p9.unwrap_or_default(),
params.p10.unwrap_or_default(),
params.p11.unwrap_or_default(),
params.p12.unwrap_or_default(),
params.p13.unwrap_or_default(),
params.p14.unwrap_or_default(),
params.p15.unwrap_or_default(),
params.p16.unwrap_or_default(),
params.p17.unwrap_or_default(),
params.p18.unwrap_or_default(),
params.p19.unwrap_or_default(),
params.p20.unwrap_or_default(),
params.p21.unwrap_or_default(),
params.p22.unwrap_or_default(),
params.p23.unwrap_or_default(),
params.p24.unwrap_or_default(),
params.p25.unwrap_or_default(),
];
Position::try_from(pips)
}
}
#[cfg(test)]
mod probabilities_tests {
#[test]
fn from() {
let model_probs = engine::probabilities::Probabilities {
win_normal: 0.32,
win_gammon: 0.26,
win_bg: 0.12,
lose_normal: 0.15,
lose_gammon: 0.1,
lose_bg: 0.05,
};
let view_probs: crate::web_api::ProbabilitiesView = model_probs.into();
assert_eq!(view_probs.win, 0.7);
assert_eq!(view_probs.winG, 0.38);
assert_eq!(view_probs.winBg, 0.12);
assert_eq!(view_probs.loseG, 0.15);
assert_eq!(view_probs.loseBg, 0.05);
}
}