1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
|
8
|
|
|
class ScanController extends Controller |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param $param |
12
|
|
|
* |
13
|
|
|
* @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
14
|
|
|
*/ |
15
|
|
|
public function showScan($param) |
16
|
|
|
{ |
17
|
|
|
$rawQR = DB::select('SELECT * FROM game_codes WHERE game_code = ?', [$param]); |
18
|
|
|
$checkExists = $this->checkCodeExists($param, Auth::user()->name_gen); |
|
|
|
|
19
|
|
|
$maxPoints = CodeCount::getTotalPoints(); |
20
|
|
|
|
21
|
|
|
if (count($rawQR) > 0) { |
22
|
|
|
if (count($checkExists) < 1) { |
23
|
|
|
DB::table('users_codes')->insert(['fk_users' => Auth::user()->id, 'fk_game_codes' => $rawQR[0]->id]); |
|
|
|
|
24
|
|
|
$checkExists = DB::select('SELECT * FROM users RIGHT JOIN users_codes ON users.id = users_codes.fk_users RIGHT JOIN game_codes ON users_codes.fk_game_codes = game_codes.id WHERE name_gen = ? AND game_code = ?;', [Auth::user()->name_gen, $param]); |
25
|
|
|
$checkExists = json_decode(json_encode($checkExists)); |
26
|
|
|
$view = view('user.scan', ['checkExists' => $checkExists, 'maxPoints' => $maxPoints, 'first' => 1]); |
27
|
|
|
} else { |
28
|
|
|
$checkExists = DB::select('SELECT * FROM users RIGHT JOIN users_codes ON users.id = users_codes.fk_users RIGHT JOIN game_codes ON users_codes.fk_game_codes = game_codes.id WHERE name_gen = ? AND game_code = ?;', [Auth::user()->name_gen, $param]); |
29
|
|
|
$checkExists = json_decode(json_encode($checkExists)); |
30
|
|
|
$view = view('user.scan', ['checkExists' => $checkExists, 'maxPoints' => $maxPoints])->withErrors('Der Code wurde bereits gezählt.'); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->calcTotalPoints(Auth::user()->name_gen); |
34
|
|
|
|
35
|
|
|
return $view; |
36
|
|
|
} else { |
37
|
|
|
abort(404); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $code |
43
|
|
|
* @param $name_gen |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
private function checkCodeExists($code, $name_gen) |
48
|
|
|
{ |
49
|
|
|
return DB::select('SELECT * FROM users RIGHT JOIN users_codes ON users.id = users_codes.fk_users RIGHT JOIN game_codes ON users_codes.fk_game_codes = game_codes.id WHERE name_gen = ? AND game_code = ?;', [$name_gen, $code]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $name_gen |
54
|
|
|
*/ |
55
|
|
|
private function calcTotalPoints($name_gen) |
56
|
|
|
{ |
57
|
|
|
$allPoints = $this->getAllPointsPerUser($name_gen); |
58
|
|
|
|
59
|
|
|
$totalPoints = 0; |
60
|
|
|
foreach ($allPoints as $point) { |
61
|
|
|
$totalPoints += $point['points']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($totalPoints > 0) { |
65
|
|
|
DB::update('UPDATE users SET total_points = ? WHERE name_gen = ?;', [$totalPoints, $name_gen]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (count($allPoints) == 1) { |
69
|
|
|
DB::table('users')->where('id', Auth::User()->id)->limit(1)->update(['start' => Carbon::now()->toDateTimeString()]); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$codeCount = $this->getCodeCount(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (count($allPoints) == $codeCount) { |
75
|
|
|
DB::table('users')->where('id', Auth::User()->id)->update(['end' => Carbon::now()->toDateTimeString()]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $name_gen |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
private function getAllPointsPerUser($name_gen) |
85
|
|
|
{ |
86
|
|
|
$pointsPerUser = DB::select('SELECT points,name_gen FROM users LEFT JOIN users_codes ON users.id = users_codes.fk_users LEFT JOIN game_codes ON users_codes.fk_game_codes = game_codes.id WHERE users.name_gen = ?;', [$name_gen]); |
87
|
|
|
|
88
|
|
|
return $pointsPerUserArray = json_decode(json_encode($pointsPerUser), true); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|