Blame view

app/Core/Predictor/Prediction.php 2.86 KB
e77200db5   nologostudio.ru   Initial commit
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
  <?php
  
  namespace FootyRoom\Core\Predictor;
  
  use FootyRoom\Core\CoreException;
  use FootyRoom\Core\EventGenerator;
  use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
  
  /**
   * @ODM\Document(collection="predictor.predictions")
   */
  class Prediction
  {
      use EventGenerator;
  
      /**
       * @ODM\Id
       *
       * @var string
       */
      protected $id;
  
      /**
       * @ODM\Field(type="int")
       *
       * @var int|string
       */
      protected $userId;
  
      /**
       * @ODM\Field(type="int")
       *
       * @var int
       */
      protected $matchId;
  
      /**
       * @ODM\Field(type="int")
       *
       * @var string
       */
      protected $homeScore;
  
      /**
       * @ODM\Field(type="int")
       *
       * @var string
       */
      protected $awayScore;
  
      /**
       * @ODM\Field(type="int")
       *
       * @var int
       */
      protected $stake;
  
      /**
       * @ODM\Field(type="bool")
       * 
       * @var boolean
       */
      protected $settled;
  
      public function __construct(int $userId, int $matchId, int $homeScore, int $awayScore, int $stake = 0)
      {
          $this->userId = $userId;
          $this->matchId = $matchId;
          $this->setScore($homeScore, $awayScore);
          $this->stake = 0;
          $this->settled = false;
  
          $this->bet($stake);
  
          $this->raise(new PredictionCreated($this));
      }
  
      public function getUserId(): int
      {
          return $this->userId;
      }
  
      public function getMatchId(): int
      {
          return $this->matchId;
      }
  
      public function getStake(): int
      {
          return $this->stake;
      }
  
      public function getHomeScore(): int
      {
          return $this->homeScore;
      }
  
      public function getAwayScore(): int
      {
          return $this->awayScore;
      }
  
      public function getSettled(): bool
      {
          return $this->settled;
      }
  
      public function bet(int $stake)
      {
          if ($stake < 0) {
              throw new CoreException('You can\'t make a negative bet.');
          }
  
          if ($this->stake > $stake) {
              throw new CoreException('You can\'t lower your bet.');
          }
  
          $raise = $stake - $this->stake;
  
          $this->stake = $stake;
  
          if ($raise > 0) {
              $this->raise(new PredictionStakeRaised($this, $raise));
          }
      }
  
      public function change(int $homeScore, int $awayScore)
      {
          if ($this->stake > 0) {
              if ($this->homeScore !== $homeScore || $this->awayScore !== $awayScore) {
                  throw new CoreException('You can\'t change your prediction once you\'ve placed a bet.');
              }
          }
  
          $this->setScore($homeScore, $awayScore);
      }
  
      protected function setScore(int $homeScore, int $awayScore)
      {
          if ($homeScore > 20 || $awayScore > 20) {
              throw new CoreException('Please check your prediction, it\'s outrageous!');
          }
  
          $this->homeScore = $homeScore;
          $this->awayScore = $awayScore;
      }
  }