Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. use rand::Rng;
  2. use std::borrow::Cow;
  3. use uuid::Uuid;
  4.  
  5. const ROUNDS: usize = 9;
  6. const GAMES_PER_ROUND: usize = 5;
  7.  
  8. /*
  9. 1 тур 1 - (10) 2 - 9 3 - 8 4 - 7 5 - 6
  10. 2 тур (10) - 6 7 - 5 8 - 4 9 - 3 1 - 2
  11. 3 тур 2 - (10) 3 - 1 4 - 9 5 - 8 6 - 7
  12. 4 тур (10) - 7 8 - 6 9 - 5 1 - 4 2 - 3
  13. 5 тур 3 - (10) 4 - 2 5 - 1 6 - 9 7 - 8
  14. 6 тур (10) - 8 9 - 7 1 - 6 2 - 5 3 - 4
  15. 7 тур 4 - (10) 5 - 3 6 - 2 7 - 1 8 - 9
  16. 8 тур (10) - 9 1 - 8 2 - 7 3 - 6 4 - 5
  17. 9 тур 5 - (10) 6 - 4 7 - 3 8 - 2 9 - 1
  18. */
  19. const BRACKETS: [[(usize, usize); GAMES_PER_ROUND]; ROUNDS] = [
  20. [(0, 9), (1, 8), (2, 7), (3, 6), (4, 5)],
  21. [(9, 5), (6, 4), (7, 3), (8, 2), (0, 1)],
  22. [(1, 9), (2, 0), (3, 8), (4, 7), (5, 6)],
  23. [(9, 6), (7, 5), (8, 4), (0, 3), (1, 2)],
  24. [(2, 9), (3, 1), (4, 0), (5, 8), (6, 7)],
  25. [(9, 7), (8, 6), (0, 5), (1, 4), (2, 3)],
  26. [(3, 9), (4, 2), (5, 1), (6, 0), (7, 8)],
  27. [(9, 8), (0, 7), (1, 6), (2, 5), (3, 4)],
  28. [(4, 9), (5, 3), (6, 2), (7, 1), (8, 0)],
  29. ];
  30.  
  31. #[derive(Debug, Clone)]
  32. struct Team<'a> {
  33. id: Uuid,
  34. name: Cow<'a, str>,
  35. game_list : Vec<Game<'a>>,
  36. }
  37.  
  38. impl<'a> Team<'a> {
  39. fn new<S>(name: S) -> Self
  40. where
  41. S: Into<Cow<'a, str>>,
  42. {
  43. let id = Uuid::new_v4();
  44. Team {
  45. id,
  46. name : name.into(),
  47. game_list : Vec::new(),
  48. }
  49. }
  50.  
  51. fn add_game(&mut self, game : &Game<'a>) {
  52. self.game_list.push(game.to_owned());
  53. }
  54. }
  55.  
  56. #[derive(Clone, Debug)]
  57. struct Teams<'a> {
  58. teams: Vec<Team<'a>>,
  59. }
  60.  
  61. impl<'a> Teams<'a> {
  62. fn new(teams: &[Team<'a>]) -> Self {
  63. Teams {
  64. teams: teams.to_vec(),
  65. }
  66. }
  67.  
  68. fn print(self) {
  69. println!("{:#?}", self.teams);
  70. }
  71. }
  72.  
  73. #[derive(Debug, Clone)]
  74. struct Game<'a> {
  75. id: Uuid,
  76. team_home_id: &'a Uuid,
  77. team_away_id: &'a Uuid,
  78. team_home_score: u8,
  79. team_away_score: u8,
  80. }
  81.  
  82. impl<'a> Game<'a> {
  83. fn new(team_home_id: &'a Uuid, team_away_id: &'a Uuid) -> Self {
  84. let id = Uuid::new_v4();
  85. let (team_home_score, team_away_score) = generate_score();
  86. Game {
  87. id,
  88. team_home_id,
  89. team_away_id,
  90. team_home_score,
  91. team_away_score,
  92. }
  93. }
  94. }
  95.  
  96. fn generate_score() -> (u8, u8) {
  97. let mut rng = rand::thread_rng();
  98. let team_home_score = rng.gen_range(0u8, 10);
  99. let team_away_score = rng.gen_range(0u8, 10);
  100. (team_home_score, team_away_score)
  101. }
  102.  
  103.  
  104. fn round<'a>(teams : &'a mut Teams<'a>, round_id : usize) {
  105. let round_value = BRACKETS[round_id];
  106. for (t1, t2) in round_value.to_vec() {
  107. let team1 = &teams.teams[t1].id;
  108. let team2 = &teams.teams[t2].id;
  109. let game = Game::new(team1, team2);
  110. teams.teams[t1].add_game(&game);
  111. teams.teams[t2].add_game(&game);
  112. }
  113. }
  114.  
  115. fn main() {
  116. let team1 = Team::new("team1");
  117. let team2 = Team::new("team2");
  118. let team3 = Team::new("team3");
  119. let team4 = Team::new("team4");
  120. let team5 = Team::new("team5");
  121. let team6 = Team::new("team6");
  122. let team7 = Team::new("team7");
  123. let team8 = Team::new("team8");
  124. let team9 = Team::new("team9");
  125. let team10 = Team::new("team10");
  126. let mut teams = Teams::new(&[
  127. team1, team2, team3, team4, team5, team6, team7, team8, team9, team10,
  128. ]);
  129.  
  130. round(&mut teams, 3);
  131.  
  132. teams.print();
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement