Skip to main content

lichess_api/api/
swiss_tournaments.rs

1//! Swiss-system tournaments.
2//!
3//! Unlike arena tournaments, Swiss tournaments are always organized by a team
4//! ([`LichessApi::create_swiss_tournament`] takes a `team_id`), run over a
5//! fixed number of rounds, and pair players based on score each round rather
6//! than continuously. Most write operations (creating, updating, joining,
7//! scheduling the next round, terminating, withdrawing) require the
8//! `tournament:write` scope; reading tournament info, results, games, and the
9//! TRF export are public.
10
11use futures::stream::StreamExt;
12
13use crate::client::LichessApi;
14use crate::error::Result;
15use crate::model::SwissTournament;
16use crate::model::games::GameJson;
17use crate::model::swiss_tournaments::*;
18
19impl LichessApi<reqwest::Client> {
20    pub async fn create_swiss_tournament(
21        &self,
22        team_id: &str,
23        form: create::CreateSwissTournamentForm,
24    ) -> Result<SwissTournament> {
25        self.get_single_model(create::PostRequest::new(team_id, form))
26            .await
27    }
28
29    pub async fn get_swiss_tournament(
30        &self,
31        request: impl Into<show::GetRequest>,
32    ) -> Result<SwissTournament> {
33        self.get_single_model(request.into()).await
34    }
35
36    pub async fn update_swiss_tournament(
37        &self,
38        id: &str,
39        form: update::UpdateSwissTournamentForm,
40    ) -> Result<SwissTournament> {
41        self.get_single_model(update::PostRequest::new(id, form))
42            .await
43    }
44
45    pub async fn export_swiss_tournament_games(
46        &self,
47        id: &str,
48        query: games::GetQuery,
49    ) -> Result<impl StreamExt<Item = Result<GameJson>>> {
50        self.get_streamed_models(games::GetRequest::new(id, query))
51            .await
52    }
53
54    pub async fn join_swiss_tournament(
55        &self,
56        id: &str,
57        form: join::JoinSwissTournamentForm,
58    ) -> Result<bool> {
59        self.get_ok(join::PostRequest::new(id, form)).await
60    }
61
62    pub async fn get_swiss_tournament_results(
63        &self,
64        id: &str,
65        query: results::GetQuery,
66    ) -> Result<impl StreamExt<Item = Result<SwissResult>>> {
67        self.get_streamed_models(results::GetRequest::new(id, query))
68            .await
69    }
70
71    pub async fn schedule_next_swiss_round(
72        &self,
73        id: &str,
74        form: schedule_next_round::ScheduleNextRoundForm,
75    ) -> Result<()> {
76        self.get_empty(schedule_next_round::PostRequest::new(id, form))
77            .await
78    }
79
80    pub async fn terminate_swiss_tournament(
81        &self,
82        request: impl Into<terminate::PostRequest>,
83    ) -> Result<bool> {
84        self.get_ok(request.into()).await
85    }
86
87    pub async fn withdraw_from_swiss_tournament(
88        &self,
89        request: impl Into<withdraw::PostRequest>,
90    ) -> Result<bool> {
91        self.get_ok(request.into()).await
92    }
93
94    pub async fn get_swiss_tournament_trf(
95        &self,
96        request: impl Into<trf::GetRequest>,
97    ) -> Result<String> {
98        self.get_text(request.into()).await
99    }
100}