|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Illuminate\Support\Facades\Hash; |
|
8
|
|
|
use grubersjoe\BingPhoto; |
|
9
|
|
|
use Cache; |
|
10
|
|
|
use Storage; |
|
11
|
|
|
|
|
12
|
|
|
class AccountModel extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
public function generatePassword($length=8) |
|
15
|
|
|
{ |
|
16
|
|
|
$chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789'; |
|
17
|
|
|
|
|
18
|
|
|
$password=''; |
|
19
|
|
|
for ($i=0; $i<$length; $i++) { |
|
20
|
|
|
$password.=$chars[mt_rand(0, strlen($chars)-1)]; |
|
21
|
|
|
} |
|
22
|
|
|
return $password; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function feed($uid=null) |
|
26
|
|
|
{ |
|
27
|
|
|
$ret=[]; |
|
28
|
|
|
$solution=DB::table("problem_solution")->join("problem","problem.pid","=","problem_solution.pid")->where(["uid"=>$uid,"audit"=>1])->select("problem.pid as pid","pcode","title","problem_solution.created_at as created_at")->get()->all(); |
|
29
|
|
|
foreach($solution as &$s){ |
|
30
|
|
|
$s["type"]="event"; |
|
31
|
|
|
$s["color"]="wemd-orange"; |
|
32
|
|
|
$s["icon"]="comment-check-outline"; |
|
33
|
|
|
$ret[]=$s; |
|
34
|
|
|
} |
|
35
|
|
|
return $ret; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function generateContestAccount($cid, $ccode, $num) |
|
39
|
|
|
{ |
|
40
|
|
|
$ret=[]; |
|
41
|
|
|
$starting=DB::table("users")->where(["contest_account"=>$cid])->count(); |
|
42
|
|
|
$contestModel=new ContestModel(); |
|
43
|
|
|
for ($i=1; $i<=$num; $i++) { |
|
44
|
|
|
$pass=$this->generatePassword(); |
|
45
|
|
|
$name=strtoupper($ccode).str_pad($starting+$i, 3, "0", STR_PAD_LEFT); |
|
46
|
|
|
$uid=$this->add([ |
|
47
|
|
|
'name' => $name, |
|
48
|
|
|
'email' => "[email protected]", |
|
49
|
|
|
'email_verified_at' => date("Y-m-d H:i:s"), |
|
50
|
|
|
'password' => $pass, |
|
51
|
|
|
'avatar' => "/static/img/avatar/default.png", |
|
52
|
|
|
'contest_account' => $cid |
|
53
|
|
|
]); |
|
54
|
|
|
$contestModel->grantAccess($uid, $cid, 1); |
|
55
|
|
|
$ret[]=[ |
|
56
|
|
|
"uid"=>$uid, |
|
57
|
|
|
"name"=>$name, |
|
58
|
|
|
"email"=>"[email protected]", |
|
59
|
|
|
"password"=>$pass |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
return $ret; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function add($data) |
|
66
|
|
|
{ |
|
67
|
|
|
return DB::table("users")->insertGetId([ |
|
68
|
|
|
'name' => $data['name'], |
|
69
|
|
|
'email' => $data['email'], |
|
70
|
|
|
'email_verified_at' => $data["email_verified_at"], |
|
71
|
|
|
'password' => Hash::make($data['password']), |
|
72
|
|
|
'avatar' => $data["avatar"], |
|
73
|
|
|
'contest_account' => $data["contest_account"], |
|
74
|
|
|
'remember_token'=>null, |
|
75
|
|
|
'created_at'=>date("Y-m-d H:i:s"), |
|
76
|
|
|
'updated_at'=>date("Y-m-d H:i:s") |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function detail($uid) |
|
81
|
|
|
{ |
|
82
|
|
|
$ret=DB::table("users")->where(["id"=>$uid])->first(); |
|
83
|
|
|
$ret["submissionCount"]=DB::table("submission")->where([ |
|
84
|
|
|
"uid"=>$uid, |
|
85
|
|
|
])->whereNotIn('verdict', [ |
|
86
|
|
|
'System Error', |
|
87
|
|
|
'Submission Error' |
|
88
|
|
|
])->count(); |
|
89
|
|
|
$ret["solved"]=DB::table("submission")->where([ |
|
90
|
|
|
"uid"=>$uid, |
|
91
|
|
|
"verdict"=>"Accepted" |
|
92
|
|
|
])->join("problem", "problem.pid", "=", "submission.pid")->select('pcode')->distinct()->get()->all(); |
|
93
|
|
|
$ret["solvedCount"]=count($ret["solved"]); |
|
94
|
|
|
// Casual |
|
95
|
|
|
$ret["rank"]=Cache::tags(['rank',$ret["id"]])->get("rank", "N/A"); |
|
96
|
|
|
$ret["rankTitle"]=Cache::tags(['rank',$ret["id"]])->get("title"); |
|
97
|
|
|
$ret["rankTitleColor"]=RankModel::getColor($ret["rankTitle"]); |
|
98
|
|
|
// Professional |
|
99
|
|
|
$ret["professionalTitle"]=RankModel::getProfessionalTitle($ret["professional_rate"]); |
|
100
|
|
|
$ret["professionalTitleColor"]=RankModel::getProfessionalColor($ret["professionalTitle"]); |
|
101
|
|
|
// Administration Group |
|
102
|
|
|
$ret["admin"]=$uid==1?1:0; |
|
103
|
|
|
if (Cache::tags(['bing', 'pic'])->get(date("Y-m-d"))==null) { |
|
104
|
|
|
$bing=new BingPhoto([ |
|
105
|
|
|
'locale' => 'zh-CN', |
|
106
|
|
|
]); |
|
107
|
|
|
Storage::disk('NOJPublic')->put("static/img/bing/".date("Y-m-d").".jpg", file_get_contents($bing->getImage()['url']), 86400); |
|
108
|
|
|
Cache::tags(['bing', 'pic'])->put(date("Y-m-d"), "/static/img/bing/".date("Y-m-d").".jpg"); |
|
109
|
|
|
} |
|
110
|
|
|
$ret["image"]=Cache::tags(['bing', 'pic'])->get(date("Y-m-d")); |
|
111
|
|
|
return $ret; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getExtraInfo($uid,$secret_level = 0){ |
|
115
|
|
|
$ret = DB::table('users_extra')->where('uid',$uid)->get()->all(); |
|
116
|
|
|
$key_meaning = [ |
|
117
|
|
|
0 => 'gender', |
|
118
|
|
|
1 => 'contact', |
|
119
|
|
|
2 => 'school', |
|
120
|
|
|
3 => 'country', |
|
121
|
|
|
4 => 'location', |
|
122
|
|
|
//TODO... |
|
123
|
|
|
]; |
|
124
|
|
|
$result = []; |
|
125
|
|
|
if(!empty($ret)){ |
|
126
|
|
|
foreach ($ret as $value) { |
|
127
|
|
|
if(empty($value['secret_level']) || $value['secret_level'] <= $secret_level){ |
|
128
|
|
|
$key_name = $key_meaning[$value['key']] ?? 'unknown'; |
|
129
|
|
|
$result[$key_name] = $value['value']; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
return $result; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function setExtraInfo($uid,$key_name,$value = null,$secret_level = -1){ |
|
137
|
|
|
$key_value = [ |
|
138
|
|
|
'gender' => 0, |
|
139
|
|
|
'contact' => 1, |
|
140
|
|
|
'school' => 2, |
|
141
|
|
|
'country' => 3, |
|
142
|
|
|
'location' => 4, |
|
143
|
|
|
//TODO... |
|
144
|
|
|
]; |
|
145
|
|
|
$key = $key_value[$key_name]; |
|
146
|
|
|
$ret = DB::table('users_extra')->where('uid',$uid)->where('key',$key)->first(); |
|
147
|
|
|
if(!empty($ret)){ |
|
148
|
|
|
unset($ret['id']); |
|
149
|
|
|
if(!is_null($value)){ |
|
150
|
|
|
$ret['value'] = $value; |
|
151
|
|
|
} |
|
152
|
|
|
if($secret_level != -1){ |
|
153
|
|
|
$ret['secret_level'] = $secret_level; |
|
154
|
|
|
} |
|
155
|
|
|
DB::table('users_extra')->where('uid',$uid)->where('key',$key)->update($ret); |
|
156
|
|
|
}else{ |
|
157
|
|
|
return DB::table('users_extra')->insertGetId( |
|
158
|
|
|
[ |
|
159
|
|
|
'uid' => $uid, |
|
160
|
|
|
'key' => $key, |
|
161
|
|
|
'value' => $value, |
|
162
|
|
|
'secret_level' => $secret_level == -1 ? 0 : $secret_level, |
|
163
|
|
|
] |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function unsetExtraInfoIfExist($uid,$key_name){ |
|
169
|
|
|
$key_value = [ |
|
170
|
|
|
'gender' => 0, |
|
171
|
|
|
'contact' => 1, |
|
172
|
|
|
'school' => 2, |
|
173
|
|
|
'country' => 3, |
|
174
|
|
|
'location' => 4, |
|
175
|
|
|
//TODO... |
|
176
|
|
|
]; |
|
177
|
|
|
$key = $key_value[$key_name]; |
|
178
|
|
|
$ret = DB::table('users_extra')->where('uid',$uid)->where('key',$key)->delete(); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|