├── README.md ├── src ├── Caffeinated │ └── Gamification │ │ ├── Models │ │ ├── Gamification.php │ │ └── Badge.php │ │ ├── Gamify.php │ │ ├── GamificationServiceProvider.php │ │ ├── ExampleUsage.php │ │ └── Traits │ │ └── GamificationTrait.php ├── migrations │ ├── 2015_02_07_093443_create_badges_table.php │ ├── 2015_02_07_093432_create_gamification_table.php │ └── 2015_02_07_093454_create_badge_user_table.php └── config │ └── gamification.php ├── composer.json └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # This package has been abandoned and is no longer maintained. 2 | 3 | Caffeinated Gamification 4 | ======================== 5 | Caffeinated Gamification aims to be a light-weight and easy to use gamification package to implement within your Laravel 5.0 application. 6 | 7 | Caffeinated Gamification will cover the basic PBL (points, badges, leaderboards) functionality commonly offered by gamification implementations. 8 | 9 | This is a very early WIP, and is not available yet for testing. 10 | -------------------------------------------------------------------------------- /src/Caffeinated/Gamification/Models/Gamification.php: -------------------------------------------------------------------------------- 1 | belongsTo(Config::get('auth.model'))->withTimestamps(); 31 | } 32 | } -------------------------------------------------------------------------------- /src/migrations/2015_02_07_093443_create_badges_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('name')->unique(); 18 | $table->string('slug')->unique(); 19 | $table->text('description')->nullable(); 20 | $table->integer('points')->default(1); 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::dropIfExists('badges'); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Caffeinated/Gamification/Gamify.php: -------------------------------------------------------------------------------- 1 | givePointsToBadge($points, $badge); 11 | return $this->givePointsToUser($points); 12 | } 13 | 14 | private function givePointsToUser($points) 15 | { 16 | Auth::user()->points += $points; 17 | return Auth::user()->save(); 18 | } 19 | 20 | private function givePointsToBadge($points, $badge) 21 | { 22 | if(Badge::byName($badge) && Auth::user()->hasBadge($badge)) { 23 | Auth::user()->badge->where('name', $badge)->points += $points; 24 | return Auth::user()->badge->save(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/Caffeinated/Gamification/Models/Badge.php: -------------------------------------------------------------------------------- 1 | where('name', $badge)->first(); 26 | } 27 | /** 28 | * Badges can belong to many users. 29 | * 30 | * @return Model 31 | */ 32 | public function users() 33 | { 34 | return $this->belongsToMany(Config::get('auth.model'))->withPivot('points')->withTimestamps(); 35 | } 36 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "caffeinated/gamification", 3 | "description": "Laravel 5 Gamification", 4 | "keywords": ["gamification", "gamify", "laravel", "caffeinated"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Shea Lewis", 9 | "email": "shea.lewis89@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "illuminate/database": "~5.0", 15 | "illuminate/support": "~5.0", 16 | "illuminate/filesystem": "~5.0", 17 | "illuminate/config": "~5.0" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Caffeinated\\Gamification\\": "src/Caffeinated/Gamification/" 22 | } 23 | }, 24 | "extra": { 25 | "branch-alias": { 26 | "dev-master": "1.0.x-dev" 27 | } 28 | }, 29 | "minimum-stability": "dev" 30 | } -------------------------------------------------------------------------------- /src/migrations/2015_02_07_093432_create_gamification_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->integer('user_id'); 18 | $table->integer('points')->default(0); 19 | $table->integer('level')->default(1); 20 | $table->timestamps(); 21 | 22 | $table->foreign('user_id')->references('id')->on(Config::get('auth.model'))->onDelete('cascade'); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('gamification'); 34 | } 35 | } -------------------------------------------------------------------------------- /src/Caffeinated/Gamification/GamificationServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 23 | __DIR__.'/../../config' => config_path() 24 | ], 'config'); 25 | 26 | $this->publishes([ 27 | __DIR__.'/../../migrations' => $this->app->databasePath().'/migrations' 28 | ], 'migrations'); 29 | } 30 | 31 | /** 32 | * Register the service provider 33 | * 34 | * @return void 35 | */ 36 | public function register() 37 | { 38 | $this->mergeConfigFrom( 39 | __DIR__.'/../../config/gamification.php', 'gamification' 40 | ); 41 | } 42 | } -------------------------------------------------------------------------------- /src/migrations/2015_02_07_093454_create_badge_user_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->integer('badge_id')->unisigned()->index(); 18 | $table->foreign('badge_id')->references('id')->on('badges')->onDelete('cascade'); 19 | $table->integer('user_id')->unsigned()->index(); 20 | $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 21 | $table->integer('points')->default(1); 22 | $table->timestamps(); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('badge_user'); 34 | } 35 | } -------------------------------------------------------------------------------- /src/Caffeinated/Gamification/ExampleUsage.php: -------------------------------------------------------------------------------- 1 | hasBadge('example'); 20 | 21 | // Checks if user is at least level 5 22 | Auth::user()->atLeastLevel(5); 23 | 24 | // Returns the users level 25 | Auth::user()->getLevel(); 26 | 27 | // Returns the users total experience points 28 | Auth::user()->getPoints(); 29 | 30 | // Returns the users badges 31 | Auth::user()->badges(); 32 | 33 | // Returns the users remaining experience points needed till leveling up 34 | Auth::user()->untilNextLevel(); 35 | 36 | // Returns the remaining experience points until user hits level 10 37 | Auth::user()->untilLevel(10); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Shea Lewis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/Caffeinated/Gamification/Traits/GamificationTrait.php: -------------------------------------------------------------------------------- 1 | hasOne('Caffeinated\Gamification\Models\Gamification')->withTimestamps(); 14 | } 15 | 16 | /** 17 | * Users can have many badges. 18 | * 19 | * @return Illuminate\Database\Eloquent\Model 20 | */ 21 | public function badges() 22 | { 23 | return $this->belongsToMany('Caffeinated\Gamification\Models\Badge')->withPivot('points')->withTimestamps(); 24 | } 25 | 26 | /** 27 | * Get all of the users badges. 28 | * 29 | * @return array|null 30 | */ 31 | public function getBadges() 32 | { 33 | if (! is_null($this->badges)) { 34 | return $this->badges->lists('slug'); 35 | } 36 | 37 | return null; 38 | } 39 | 40 | /** 41 | * Checks if the user has the given badge. 42 | * 43 | * @return bool 44 | */ 45 | public function hasBadge($slug) 46 | { 47 | $slug = strtolower($slug); 48 | 49 | foreach ($this->badges as $badge) { 50 | if ($badge->slug == $slug) return true; 51 | } 52 | 53 | return false; 54 | } 55 | } -------------------------------------------------------------------------------- /src/config/gamification.php: -------------------------------------------------------------------------------- 1 | true, 18 | 19 | /* 20 | |-------------------------------------------------------------------------- 21 | | Levels 22 | |-------------------------------------------------------------------------- 23 | | 24 | | Define the levels and required experience to obtain each level. The 25 | | initial set of levels below are based off the experience points required 26 | | to level up in the first Final Fantasy game. Feel free to add more! 27 | | 28 | */ 29 | 30 | 'levels' => [ 31 | 1 => 0, 32 | 2 => 28, 33 | 3 => 84, 34 | 4 => 196, 35 | 5 => 392, 36 | 6 => 700, 37 | 7 => 1148, 38 | 8 => 1764, 39 | 9 => 2576, 40 | 10 => 3612, 41 | 11 => 4900, 42 | 12 => 6468, 43 | 13 => 8344, 44 | 14 => 10556, 45 | 15 => 13132, 46 | 16 => 16100, 47 | 17 => 19488, 48 | 18 => 23324, 49 | 19 => 27636, 50 | 20 => 32452, 51 | 21 => 37800, 52 | 22 => 43708, 53 | 23 => 50204, 54 | 24 => 57317, 55 | 25 => 65072 56 | ], 57 | 58 | ]; --------------------------------------------------------------------------------