Passed
Push — master ( 0d9fa9...456d1a )
by John
04:48
created
app/Models/ContestModel.php 2 patches
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -151,22 +151,22 @@  discard block
 block discarded – undo
151 151
                 ->join('contest', 'group_member.gid', '=', 'contest.gid')
152 152
                 ->leftJoin('contest_participant', 'contest.cid', '=', 'contest_participant.cid')
153 153
                 ->where(
154
-                    function ($query) {
154
+                    function($query) {
155 155
                         $query->where('public', 1)
156 156
                               ->where('audit', 1);
157 157
                     }
158 158
                 )
159 159
                 ->orWhere(
160
-                    function ($query) use ($uid) {
160
+                    function($query) use ($uid) {
161 161
                         $query->where('group_member.uid', $uid)
162 162
                                 ->where('group_member.role', '>', 0)
163
-                                ->where(function ($query) use ($uid) {
163
+                                ->where(function($query) use ($uid) {
164 164
                                     $query->where('contest_participant.uid', $uid)
165 165
                                           ->orWhereNull('contest_participant.uid');
166 166
                                 })
167
-                              ->where(function ($query) {
167
+                              ->where(function($query) {
168 168
                                   $query->where('registration', 0)
169
-                                                ->orWhere(function ($query) {
169
+                                                ->orWhere(function($query) {
170 170
                                                     $query->where('registration', 1)
171 171
                                                           ->whereNotNull('contest_participant.uid');
172 172
                                                 });
@@ -506,7 +506,7 @@  discard block
 block discarded – undo
506 506
                     "problem_detail" => $prob_detail
507 507
                 ];
508 508
             }
509
-            usort($ret, function ($a, $b) {
509
+            usort($ret, function($a, $b) {
510 510
                 if ($a["score"]==$b["score"]) {
511 511
                     if ($a["penalty"]==$b["penalty"]) {
512 512
                         return 0;
@@ -553,7 +553,7 @@  discard block
 block discarded – undo
553 553
                     "problem_detail" => $prob_detail
554 554
                 ];
555 555
             }
556
-            usort($ret, function ($a, $b) {
556
+            usort($ret, function($a, $b) {
557 557
                 if ($a["score"]==$b["score"]) {
558 558
                     if ($a["solved"]==$b["solved"]) {
559 559
                         return 0;
@@ -646,7 +646,7 @@  discard block
 block discarded – undo
646 646
     {
647 647
         return DB::table("contest_clarification")->where([
648 648
             "cid"=>$cid
649
-        ])->where(function ($query) {
649
+        ])->where(function($query) {
650 650
             $query->where([
651 651
                 "public"=>1
652 652
             ])->orWhere([
@@ -794,7 +794,7 @@  discard block
 block discarded – undo
794 794
                 "users.id",
795 795
                 "=",
796 796
                 "submission.uid"
797
-            )->where(function ($query) use ($frozen_time) {
797
+            )->where(function($query) use ($frozen_time) {
798 798
                 $query->where(
799 799
                     "submission_date",
800 800
                     "<",
@@ -987,12 +987,12 @@  discard block
 block discarded – undo
987 987
     public function updateProfessionalRate($cid)
988 988
     {
989 989
         $basic=$this->basic($cid);
990
-        if($basic["rated"]&&!$basic["is_rated"]){
990
+        if ($basic["rated"] && !$basic["is_rated"]) {
991 991
             $ratingCalculator=new RatingCalculator($cid);
992
-            if($ratingCalculator->calculate()){
992
+            if ($ratingCalculator->calculate()) {
993 993
                 $ratingCalculator->storage();
994 994
                 return true;
995
-            }else{
995
+            } else {
996 996
                 return false;
997 997
             }
998 998
         } else {
@@ -1002,7 +1002,7 @@  discard block
 block discarded – undo
1002 1002
 
1003 1003
     public function arrangeContest($gid, $config, $problems)
1004 1004
     {
1005
-        DB::transaction(function () use ($gid, $config, $problems) {
1005
+        DB::transaction(function() use ($gid, $config, $problems) {
1006 1006
             $cid=DB::table($this->tableName)->insertGetId([
1007 1007
                 "gid"=>$gid,
1008 1008
                 "name"=>$config["name"],
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -992,7 +992,7 @@
 block discarded – undo
992 992
             if($ratingCalculator->calculate()){
993 993
                 $ratingCalculator->storage();
994 994
                 return true;
995
-            }else{
995
+            } else{
996 996
                 return false;
997 997
             }
998 998
         } else {
Please login to merge, or discard this patch.
app/Models/Rating/RatingCalculator.php 1 patch
Spacing   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -17,14 +17,14 @@  discard block
 block discarded – undo
17 17
     public $totParticipants=0;
18 18
     public $INITIAL_RATING=1500;
19 19
 
20
-    public function __construct($cid){
20
+    public function __construct($cid) {
21 21
         $this->cid=$cid;
22 22
 
23 23
         // get rank
24 24
         $this->getRecord();
25 25
     }
26 26
 
27
-    private function getRecord(){
27
+    private function getRecord() {
28 28
         $contestRankRaw=Cache::tags(['contest', 'rank'])->get($this->cid);
29 29
 
30 30
         if ($contestRankRaw==null) {
@@ -32,8 +32,8 @@  discard block
 block discarded – undo
32 32
             $contestRankRaw=$contestModel->contestRankCache($this->cid);
33 33
         }
34 34
 
35
-        $this->totParticipants = count($contestRankRaw);
36
-        foreach($contestRankRaw as $c){
35
+        $this->totParticipants=count($contestRankRaw);
36
+        foreach ($contestRankRaw as $c) {
37 37
             $this->contestants[]=[
38 38
                 "uid"=>$c["uid"],
39 39
                 "points"=>$c["score"],
@@ -42,84 +42,84 @@  discard block
 block discarded – undo
42 42
         }
43 43
     }
44 44
 
45
-    private function reassignRank(){
45
+    private function reassignRank() {
46 46
         $this->sort("points");
47
-        $idx = 0;
48
-        $points = $this->contestants[0]["points"];
49
-        $i = 1;
50
-        while($i < $this->totParticipants){
51
-            if($this->contestants[$i]["points"] < $points){
52
-                $j = $idx;
53
-                while($j < $i){
54
-                    $this->contestants[$j]["rank"] = $i;
55
-                    $j += 1;
47
+        $idx=0;
48
+        $points=$this->contestants[0]["points"];
49
+        $i=1;
50
+        while ($i<$this->totParticipants) {
51
+            if ($this->contestants[$i]["points"]<$points) {
52
+                $j=$idx;
53
+                while ($j<$i) {
54
+                    $this->contestants[$j]["rank"]=$i;
55
+                    $j+=1;
56 56
                 }
57
-                $idx = $i;
58
-                $points = $this->contestants[$i]["points"];
57
+                $idx=$i;
58
+                $points=$this->contestants[$i]["points"];
59 59
             }
60
-            $i += 1;
60
+            $i+=1;
61 61
         }
62
-        $j = $idx;
63
-        while($j < $this->totParticipants){
64
-            $this->contestants[$j]["rank"] = $this->totParticipants;
65
-            $j += 1;
62
+        $j=$idx;
63
+        while ($j<$this->totParticipants) {
64
+            $this->contestants[$j]["rank"]=$this->totParticipants;
65
+            $j+=1;
66 66
         }
67 67
     }
68 68
 
69
-    private function getEloWinProbability($Ra, $Rb){
70
-        return 1.0 / (1 + pow(10, ($Rb-$Ra)/400.0));
69
+    private function getEloWinProbability($Ra, $Rb) {
70
+        return 1.0 / (1+pow(10, ($Rb-$Ra) / 400.0));
71 71
     }
72 72
 
73
-    private function getSeed($rating){
74
-        $result = 1.0;
75
-        foreach($this->contestants as $other){
76
-            $result += $this->getEloWinProbability($other["rating"], $rating);
73
+    private function getSeed($rating) {
74
+        $result=1.0;
75
+        foreach ($this->contestants as $other) {
76
+            $result+=$this->getEloWinProbability($other["rating"], $rating);
77 77
         }
78 78
         return $result;
79 79
     }
80 80
 
81
-    private function getRatingToRank($rank){
81
+    private function getRatingToRank($rank) {
82 82
         $left=1;
83 83
         $right=8000;
84
-        while($right - $left > 1){
85
-            $mid = floor(($right + $left)/2);
86
-            if($this->getSeed($mid) < $rank){
87
-                $right = $mid;
88
-            }else{
89
-                $left = $mid;
84
+        while ($right-$left>1) {
85
+            $mid=floor(($right+$left) / 2);
86
+            if ($this->getSeed($mid)<$rank) {
87
+                $right=$mid;
88
+            } else {
89
+                $left=$mid;
90 90
             }
91 91
         }
92 92
         return $left;
93 93
     }
94 94
 
95
-    private function sort($key){
96
-        usort($this->contestants, function ($a, $b) use ($key) {
95
+    private function sort($key) {
96
+        usort($this->contestants, function($a, $b) use ($key) {
97 97
             return $b[$key] <=> $a[$key];
98 98
         });
99 99
     }
100 100
 
101
-    public function calculate(){
102
-        if(empty($this->contestants)){
101
+    public function calculate() {
102
+        if (empty($this->contestants)) {
103 103
             return;
104 104
         }
105 105
 
106 106
         // recalc rank
107 107
         $this->reassignRank();
108 108
 
109
-        foreach($this->contestants as &$member){
110
-            $member["seed"] = 1.0;
111
-            foreach($this->contestants as $other){
112
-                if($member["uid"] != $other["uid"]){
113
-                    $member["seed"] += $this->getEloWinProbability($other["rating"], $member["rating"]);
109
+        foreach ($this->contestants as &$member) {
110
+            $member["seed"]=1.0;
111
+            foreach ($this->contestants as $other) {
112
+                if ($member["uid"]!=$other["uid"]) {
113
+                    $member["seed"]+=$this->getEloWinProbability($other["rating"], $member["rating"]);
114 114
                 }
115 115
             }
116 116
         }
117 117
         unset($member);
118 118
 
119
-        foreach($this->contestants as &$contestant){
120
-            $midRank = sqrt($contestant["rank"] * $contestant["seed"]);
121
-            $contestant["needRating"] = $this->getRatingToRank($midRank);
122
-            $contestant["delta"] = floor(($contestant["needRating"] - $contestant["rating"])/2);
119
+        foreach ($this->contestants as &$contestant) {
120
+            $midRank=sqrt($contestant["rank"] * $contestant["seed"]);
121
+            $contestant["needRating"]=$this->getRatingToRank($midRank);
122
+            $contestant["delta"]=floor(($contestant["needRating"]-$contestant["rating"]) / 2);
123 123
         }
124 124
         unset($contestant);
125 125
 
@@ -127,39 +127,39 @@  discard block
 block discarded – undo
127 127
 
128 128
         // DO some adjuct
129 129
         // Total sum should not be more than ZERO.
130
-        $sum = 0;
130
+        $sum=0;
131 131
 
132
-        foreach($this->contestants as $contestant){
133
-            $sum += $contestant["delta"];
132
+        foreach ($this->contestants as $contestant) {
133
+            $sum+=$contestant["delta"];
134 134
         }
135
-        $inc = -floor($sum / $this->totParticipants) - 1;
136
-        foreach($this->contestants as &$contestant){
137
-            $contestant["delta"] += $inc;
135
+        $inc=-floor($sum / $this->totParticipants)-1;
136
+        foreach ($this->contestants as &$contestant) {
137
+            $contestant["delta"]+=$inc;
138 138
         }
139 139
         unset($contestant);
140 140
 
141 141
         // Sum of top-4*sqrt should be adjusted to ZERO.
142 142
 
143
-        $sum = 0;
144
-        $zeroSumCount = min(intval(4*round(sqrt($this->totParticipants))), $this->totParticipants);
143
+        $sum=0;
144
+        $zeroSumCount=min(intval(4 * round(sqrt($this->totParticipants))), $this->totParticipants);
145 145
 
146
-        for($i=0;$i<$zeroSumCount;$i++){
147
-            $sum += $this->contestants[$i]["delta"];
146
+        for ($i=0; $i<$zeroSumCount; $i++) {
147
+            $sum+=$this->contestants[$i]["delta"];
148 148
         }
149 149
 
150
-        $inc = min(max(-floor($sum / $zeroSumCount), -10), 0);
150
+        $inc=min(max(-floor($sum / $zeroSumCount), -10), 0);
151 151
 
152
-        for($i=0;$i<$zeroSumCount;$i++){
153
-            $this->contestants[$i]["delta"] += $inc;
152
+        for ($i=0; $i<$zeroSumCount; $i++) {
153
+            $this->contestants[$i]["delta"]+=$inc;
154 154
         }
155 155
 
156 156
         return $this->validateDeltas();
157 157
     }
158 158
 
159
-    public function storage(){
159
+    public function storage() {
160 160
         $contestants=$this->contestants;
161
-        DB::transaction(function () use ($contestants) {
162
-            foreach($contestants as $contestant){
161
+        DB::transaction(function() use ($contestants) {
162
+            foreach ($contestants as $contestant) {
163 163
                 $newRating=$contestant["rating"]+$contestant["delta"];
164 164
                 DB::table("users")->where([
165 165
                     "id"=>$contestant["uid"]
@@ -181,20 +181,20 @@  discard block
 block discarded – undo
181 181
         }, 5);
182 182
     }
183 183
 
184
-    private function validateDeltas(){
184
+    private function validateDeltas() {
185 185
         $this->sort("points");
186 186
 
187
-        for($i=0;$i<$this->totParticipants;$i++){
188
-            for($j=$i+1;$j<$this->totParticipants;$j++){
189
-                if($this->contestants[$i]["rating"] > $this->contestants[$j]["rating"]){
190
-                    if($this->contestants[$i]["rating"] + $this->contestants[$i]["delta"] < $this->contestants[$j]["rating"] + $this->contestants[$j]["delta"]){
187
+        for ($i=0; $i<$this->totParticipants; $i++) {
188
+            for ($j=$i+1; $j<$this->totParticipants; $j++) {
189
+                if ($this->contestants[$i]["rating"]>$this->contestants[$j]["rating"]) {
190
+                    if ($this->contestants[$i]["rating"]+$this->contestants[$i]["delta"]<$this->contestants[$j]["rating"]+$this->contestants[$j]["delta"]) {
191 191
                         Log::warning("First rating invariant failed: {$this->contestants[i]["uid"]} vs. {$this->contestants[j]["uid"]}.");
192 192
                         return false;
193 193
                     }
194 194
                 }
195 195
 
196
-                if($this->contestants[$i]["rating"] < $this->contestants[$j]["rating"]){
197
-                    if($this->contestants[$i]["delta"] < $this->contestants[$j]["delta"]){
196
+                if ($this->contestants[$i]["rating"]<$this->contestants[$j]["rating"]) {
197
+                    if ($this->contestants[$i]["delta"]<$this->contestants[$j]["delta"]) {
198 198
                         Log::warning("Second rating invariant failed: {$this->contestants[i]["uid"]} vs.  {$this->contestants[j]["uid"]}.");
199 199
                         return false;
200 200
                     }
Please login to merge, or discard this patch.
app/Http/Controllers/Ajax/ContestController.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@
 block discarded – undo
44 44
         $all_data=$request->all();
45 45
 
46 46
         $contestModel=new ContestModel();
47
-        return $contestModel->updateProfessionalRate($all_data["cid"])?ResponseModel::success(200):ResponseModel::err(1001);
47
+        return $contestModel->updateProfessionalRate($all_data["cid"]) ?ResponseModel::success(200) : ResponseModel::err(1001);
48 48
     }
49 49
 
50 50
     public function requestClarification(Request $request)
Please login to merge, or discard this patch.