1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Helpers; |
9
|
|
|
|
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
use App\Models\Game; |
12
|
|
|
use App\Models\Developer; |
13
|
|
|
use App\Models\GamesFile; |
14
|
|
|
use App\Models\UserOnline; |
15
|
|
|
use App\Models\BoardThread; |
16
|
|
|
use App\Models\BoardThreadsTracker; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class DatabaseHelper |
20
|
|
|
* @package App\Helpers |
21
|
|
|
*/ |
22
|
|
|
class DatabaseHelper |
23
|
|
|
{ |
24
|
|
|
/** Sets Vots and Comments |
25
|
|
|
* @param $gameid |
26
|
|
|
*/ |
27
|
|
|
public static function setVotesAndComments($gameid) |
28
|
|
|
{ |
29
|
|
|
$game = Game::whereId($gameid)->first(); |
30
|
|
|
|
31
|
|
|
Game::whereId($gameid) |
32
|
|
|
->update([ |
33
|
|
|
'voteup' => $game->votes['up'], |
34
|
|
|
'votedown' => $game->votes['down'], |
35
|
|
|
'avg' => $game->votes['avg'], |
36
|
|
|
'comments' => $game->comments()->count(), |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** Set Release Informations on Game Change |
41
|
|
|
* @param $gameid |
42
|
|
|
*/ |
43
|
|
|
public static function setReleaseInfos($gameid) |
44
|
|
|
{ |
45
|
|
|
$game = Game::find($gameid); |
46
|
|
|
|
47
|
|
|
$reltype = 99; |
48
|
|
|
$reldate = Carbon::create(); |
49
|
|
|
|
50
|
|
|
//prüfen ob Techdemo existiert |
51
|
|
|
$gamefiles = GamesFile::whereGameId($gameid) |
52
|
|
|
->where('release_type', '=', 1) |
53
|
|
|
->orderBy('release_year', 'ASC') |
54
|
|
|
->orderBy('release_month', 'ASC') |
55
|
|
|
->orderBy('release_day', 'ASC') |
56
|
|
|
->first(); |
57
|
|
|
|
58
|
|
View Code Duplication |
if ($gamefiles) { |
|
|
|
|
59
|
|
|
$reltype = $gamefiles->release_type; |
60
|
|
|
$reldate = Carbon::parse($gamefiles->release_year.'-'.$gamefiles->release_month.'-'.$gamefiles->release_day); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
//Prüfen ob Demo vorhanden |
64
|
|
|
$gamefiles = GamesFile::whereGameId($gameid) |
65
|
|
|
->where('release_type', '=', 2) |
66
|
|
|
->orderBy('release_year', 'ASC') |
67
|
|
|
->orderBy('release_month', 'ASC') |
68
|
|
|
->orderBy('release_day', 'ASC') |
69
|
|
|
->first(); |
70
|
|
|
|
71
|
|
View Code Duplication |
if ($gamefiles) { |
|
|
|
|
72
|
|
|
$reltype = $gamefiles->release_type; |
73
|
|
|
$reldate = Carbon::parse($gamefiles->release_year.'-'.$gamefiles->release_month.'-'.$gamefiles->release_day); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//Prüfen ob Vollversion vorhanden. |
77
|
|
|
$gamefiles = GamesFile::whereGameId($gameid) |
78
|
|
|
->where('release_type', '=', 3) |
79
|
|
|
->orderBy('release_year', 'ASC') |
80
|
|
|
->orderBy('release_month', 'ASC') |
81
|
|
|
->orderBy('release_day', 'ASC') |
82
|
|
|
->first(); |
83
|
|
|
|
84
|
|
View Code Duplication |
if ($gamefiles) { |
|
|
|
|
85
|
|
|
$reltype = $gamefiles->release_type; |
86
|
|
|
$reldate = Carbon::parse($gamefiles->release_year.'-'.$gamefiles->release_month.'-'.$gamefiles->release_day); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($game->release_type == $reltype) { |
90
|
|
|
if (Carbon::parse($game->release_date) > $reldate) { |
91
|
|
|
Game::whereId($gameid) |
92
|
|
|
->update([ |
93
|
|
|
'release_date' => $reldate, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
Game::whereId($gameid) |
98
|
|
|
->update([ |
99
|
|
|
'release_date' => $reldate, |
100
|
|
|
'release_type' => $reltype, |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null|object|static |
107
|
|
|
*/ |
108
|
|
|
public static function getOnlineUserCount() |
109
|
|
|
{ |
110
|
|
|
$users = \DB::table('user_online') |
111
|
|
|
->selectRaw('COUNT(id) as online') |
112
|
|
|
->where('created_at', '>=', Carbon::now()->addMinutes(-10)) |
113
|
|
|
->first(); |
114
|
|
|
|
115
|
|
|
return $users; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $where |
120
|
|
|
*/ |
121
|
|
|
public static function setOnline($where) |
122
|
|
|
{ |
123
|
|
|
if (\Auth::check()) { |
124
|
|
|
UserOnline::updateOrInsert([ |
125
|
|
|
'user_id' => \Auth::id(), |
126
|
|
|
], [ |
127
|
|
|
'last_place' => $where, |
128
|
|
|
'created_at' => Carbon::now(), |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $thread_id |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public static function isThreadUnread($thread_id) |
139
|
|
|
{ |
140
|
|
|
if (\Auth::check()) { |
141
|
|
|
$track = BoardThreadsTracker::where('thread_id', '=', $thread_id) |
142
|
|
|
->where('user_id', '=', \Auth::id())->first(); |
143
|
|
|
|
144
|
|
|
$thread = BoardThread::whereId($thread_id)->first(); |
145
|
|
|
|
146
|
|
|
if ($track) { |
147
|
|
|
if (Carbon::parse($track->last_read)->timestamp < Carbon::parse($thread->last_created_at)->timestamp) { |
148
|
|
|
return true; |
149
|
|
|
} else { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
} else { |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
} else { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $thread_id |
162
|
|
|
*/ |
163
|
|
|
public static function setThreadViewDate($thread_id) |
164
|
|
|
{ |
165
|
|
|
if (\Auth::check()) { |
166
|
|
|
$track = BoardThreadsTracker::updateOrInsert([ |
|
|
|
|
167
|
|
|
'user_id' => \Auth::id(), |
168
|
|
|
'thread_id' => $thread_id, |
169
|
|
|
], [ |
170
|
|
|
'last_read' => Carbon::now(), |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $gameid |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public static function getReleaseDateFromGameId($gameid) |
180
|
|
|
{ |
181
|
|
|
$game = Game::whereId($gameid)->first(); |
182
|
|
|
|
183
|
|
|
if (is_null($game)) { |
184
|
|
|
return ''; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (Carbon::parse($game->release_date)->year == -1 || is_null($game->release_date)) { |
188
|
|
|
$releasedate = GamesFile::whereGameId($gameid) |
189
|
|
|
->selectRaw('CONCAT(release_year, "-", LPAD(release_month, 2, "0"), "-", release_day) as reldate') |
190
|
|
|
->orderBy('release_type', 'desc') |
191
|
|
|
->orderBy('reldate', 'asc') |
192
|
|
|
->first(); |
193
|
|
|
|
194
|
|
|
if ($releasedate) { |
195
|
|
|
return Carbon::parse($releasedate->reldate)->toDateString(); |
196
|
|
|
} else { |
197
|
|
|
return ''; |
198
|
|
|
} |
199
|
|
|
} else { |
200
|
|
|
return Carbon::parse($game->release_date)->toDateString(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $gameid |
206
|
|
|
* @param bool $urlstyle |
207
|
|
|
* @return bool|string |
208
|
|
|
*/ |
209
|
|
|
public static function getDevelopersUrlList($gameid, $urlstyle = true) |
210
|
|
|
{ |
211
|
|
|
$developers = \DB::table('games_developer') |
212
|
|
|
->leftJoin('developer', 'developer.id', '=', 'games_developer.developer_id') |
213
|
|
|
->where('games_developer.game_id', '=', $gameid) |
214
|
|
|
->get(); |
215
|
|
|
|
216
|
|
|
$res = ''; |
217
|
|
|
foreach ($developers as $dev) { |
218
|
|
|
if ($urlstyle == true) { |
|
|
|
|
219
|
|
|
$res = $res.'<a href="'.url('developer', $dev->id).'">'.$dev->name.'</a> :: '; |
220
|
|
|
} else { |
221
|
|
|
$res .= $dev->name.', '; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$res = substr($res, 0, -4); |
226
|
|
|
|
227
|
|
|
return $res; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $id |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public static function getResourcePathArray($id) |
235
|
|
|
{ |
236
|
|
|
$resource = \DB::table('resources') |
237
|
|
|
->where('id', '=', $id) |
238
|
|
|
->first(); |
239
|
|
|
|
240
|
|
|
$res = [ |
241
|
|
|
'type' => $resource->type, |
242
|
|
|
'cat' => $resource->cat, |
243
|
|
|
'id' => $id, |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
return $res; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param $reason |
251
|
|
|
* @return mixed |
252
|
|
|
*/ |
253
|
|
|
public static function getObyxPoints($reason) |
254
|
|
|
{ |
255
|
|
|
$obyx = \DB::table('obyx') |
256
|
|
|
->where('reason', '=', $reason) |
257
|
|
|
->first(); |
258
|
|
|
|
259
|
|
|
return $obyx->value; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return mixed |
264
|
|
|
*/ |
265
|
|
|
public static function getGameViewsMax() |
266
|
|
|
{ |
267
|
|
|
$v = \DB::table('games') |
268
|
|
|
->selectRaw('MAX(views) as maxviews') |
269
|
|
|
->first(); |
270
|
|
|
|
271
|
|
|
return $v->maxviews; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param string $type |
276
|
|
|
*/ |
277
|
|
|
public static function getCommentsMax($type) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
$v = \DB::table('comments') |
280
|
|
|
->selectRaw('count(id) as maxviews') |
281
|
|
|
->where('content_type', '=', \DB::raw("'".$type."'")) |
282
|
|
|
->first(); |
283
|
|
|
|
284
|
|
|
return $v->maxviews; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param $short |
289
|
|
|
* @return int|mixed |
290
|
|
|
*/ |
291
|
|
|
public static function langId_from_short($short) |
292
|
|
|
{ |
293
|
|
|
$lang = \DB::table('languages') |
294
|
|
|
->select('id') |
295
|
|
|
->where('short', '=', $short) |
296
|
|
|
->first(); |
297
|
|
|
|
298
|
|
|
if ($lang) { |
299
|
|
|
return $lang->id; |
300
|
|
|
} else { |
301
|
|
|
return 0; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param $developername |
307
|
|
|
* @return int|mixed |
308
|
|
|
*/ |
309
|
|
|
public static function developerId_from_developerName($developername) |
310
|
|
|
{ |
311
|
|
|
$dev = \DB::table('developer') |
312
|
|
|
->select('id') |
313
|
|
|
->where('name', '=', $developername) |
314
|
|
|
->first(); |
315
|
|
|
|
316
|
|
|
if ($dev) { |
317
|
|
|
return $dev->id; |
318
|
|
|
} else { |
319
|
|
|
return 0; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param $developername |
325
|
|
|
* @return int |
326
|
|
|
*/ |
327
|
|
|
public static function developer_add_and_get_developerId($developername) |
328
|
|
|
{ |
329
|
|
|
$d = new Developer(); |
|
|
|
|
330
|
|
|
$d->name = $developername; |
|
|
|
|
331
|
|
|
$d->user_id = \Auth::id(); |
332
|
|
|
$d->save(); |
333
|
|
|
|
334
|
|
|
return $d->id; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.