This repository was archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGamification.php
139 lines (114 loc) · 3.36 KB
/
Gamification.php
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
<?php
/**
* This file is part of reflar/gamification.
*
* Copyright (c) ReFlar.
*
* http://reflar.io
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Reflar\Gamification;
use Flarum\Post\PostRepository;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Flarum\User\UserRepository;
class Gamification
{
/**
* Sets the maximum number of user exposed through the ranking api.
*/
const MAXIMUM_USER_EXPOSED = 25;
/**
* @var PostRepository
*/
protected $posts;
/**
* @var UserRepository
*/
protected $users;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param PostRepository $posts
* @param UserRepository $users
* @param SettingsRepositoryInterface $settings
*/
public function __construct(PostRepository $posts, UserRepository $users, SettingsRepositoryInterface $settings)
{
$this->posts = $posts;
$this->users = $users;
$this->settings = $settings;
}
/**
* The Reddit hotness algorithm from https://github.com/reddit/reddit.
*
* @param $discussion
*/
public function calculateHotness($discussion)
{
$date = strtotime($discussion->start_time);
$s = $discussion->votes;
$order = log10(max(abs($s), 1));
if ($s > 0) {
$sign = 1;
} elseif ($s < 0) {
$sign = -1;
} else {
$sign = 0;
}
$seconds = $date - 1134028003;
$discussion->hotness = round($sign * $order + $seconds / 45000, 10);
$discussion->save();
}
/**
* @return mixed
*/
public function orderByPoints($limit, $offset)
{
$blockedUsers = explode(', ', $this->settings->get('reflar.gamification.blockedUsers'));
if ($limit > self::MAXIMUM_USER_EXPOSED) {
$limit = self::MAXIMUM_USER_EXPOSED;
}
if ($offset >= self::MAXIMUM_USER_EXPOSED) {
return [];
}
if (($limit + $offset) > self::MAXIMUM_USER_EXPOSED) {
$limit = $limit + $offset - self::MAXIMUM_USER_EXPOSED;
}
$query = User::query()
->whereNotIn('username', $blockedUsers)
->orderBy('votes', 'desc')
->offset($offset)
->take($limit)
->get();
return $query;
}
/**
* @param $post_id
* @param $user_id
* @param User $actor
*/
public function convertLike($post_id, $user_id)
{
$user = $this->users->query()->where('id', $user_id)->first();
$post = $this->posts->query()->where('id', $post_id)->first();
if (null !== $post && null !== $post->user && null !== $user) {
$post->user->increment('votes');
if ($post->number = 1) {
$post->discussion->increment('votes');
}
$vote = vote::build($post, $user);
$vote->type = 'Up';
$vote->save();
$ranks = json_decode($this->settings->get('reflar.gamification.ranks'), true);
if (isset($ranks[$post->user->votes])) {
$post->user->rank = $ranks[$post->user->votes];
$post->user->save();
}
}
}
}