Commit 90c72064 authored by Jack Stupple's avatar Jack Stupple

Allow character profile pictures to change and social statuses too! :D

parent 08762c60
......@@ -27,4 +27,20 @@ class Character extends Model
{
return $this->hasMany(\App\Paperwork::class);
}
public function status()
{
$player = get_player($_SERVER['HTTP_SESSION_ID'], false);
return $this->hasOne(\App\CharacterSocial::class)
->where('season_roundup_position', '=', $player ? $player->season_roundup_position : 0);
}
public function profile_picture()
{
$player = get_player($_SERVER['HTTP_SESSION_ID'], false);
return $this->hasOne(\App\CharacterPicture::class)
->where('season_roundup_position', '=', $player ? $player->season_roundup_position : 0);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Question;
class CharacterPicture extends Model
{
protected $guarded = [];
protected $casts = [];
public function character()
{
return $this->belongsTo(\App\Character::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Question;
class CharacterSocial extends Model
{
protected $table = 'character_social';
protected $guarded = [];
protected $casts = [];
public function character()
{
return $this->belongsTo(\App\Character::class);
}
}
......@@ -10,14 +10,24 @@ class CharacterController extends Controller
public function index(Request $request)
{
$player = get_player($request);
return Character::where('language_id', $player ? $player->language_id : \App\Language::where('slug', 'en')->first()->id)->get();
return Character::where('language_id', $player ? $player->language_id : \App\Language::where('slug', 'en')->first()->id)->with([
'status',
'profile_picture'
])->get();
}
public function show(Request $request, $character_id)
{
$player = get_player($request);
return Character::where('id', $character_id)->with(['baseModifiers', 'baseModifiers.modifierCategory', 'questions', 'questions.options', 'questions.options.modifiers'])
->firstOrFail();
return Character::where('id', $character_id)->with([
'baseModifiers',
'baseModifiers.modifierCategory',
'questions',
'questions.options',
'questions.options.modifiers',
'status',
'profile_picture'
])->firstOrFail();
}
public function destroy($character_id)
......@@ -60,4 +70,4 @@ class CharacterController extends Controller
return $character;
}
}
\ No newline at end of file
}
......@@ -18,7 +18,7 @@ class Player extends Model
'score' => 'float'
];
protected $appends = ['progress', 'questions', 'score', 'score_string'];
protected $appends = ['progress', 'questions', 'score', 'score_string', 'season_roundup_position'];
public function language()
{
......@@ -95,4 +95,36 @@ class Player extends Model
return 'good';
}
}
public function getSeasonRoundupPositionAttribute()
{
/**
* SELECT ph.`season_roundup_position`
* FROM `player_history` ph
* LEFT JOIN `players` p ON ph.`player_id` = p.`id`
* WHERE `player_id` = 11
* AND ph.`season_roundup_id` IS NOT NULL
* AND ph.`playthrough` = p.`playthrough`
* GROUP BY ph.`season_roundup_position`
* ORDER BY ph.`season_roundup_position` DESC
*/
$roundup_position = DB::table('player_history')
->select('season_roundup_position')
->where('player_id', $this->id)
->where('season_roundup_id', '!=', null)
->where('playthrough', '=', $this->playthrough)
->groupBy('season_roundup_position')
->orderBy('season_roundup_position', 'DESC')
->limit(1)
->first();
// emulate a response with 0 - we have not had a roundup yet
if (!$roundup_position) {
$roundup_position = (object) [
'season_roundup_position' => 0
];
}
return $roundup_position->season_roundup_position;
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\Request;
class Summary extends Model
{
protected $guarded = [];
protected $table = 'summaries';
protected $casts = [
'language_id' => 'int'
];
public function characters()
{
return $this->belongsToMany(\App\Character::class);
}
public function answers()
{
return $this->hasMany(\App\SummaryAnswer::class);
}
}
\ No newline at end of file
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class SummaryAnswer extends Model
{
protected $guarded = [];
protected $casts = [
'language_id' => 'int'
];
protected $appends = ['selected'];
protected $_selected = false;
public function characters()
{
return $this->belongsToMany(\App\Character::class);
}
public function getSelectedAttribute()
{
return $this->_selected;
}
public function setSelectedAttribute($opt)
{
$this->_selected = $opt;
return $opt;
}
}
\ No newline at end of file
......@@ -5,22 +5,31 @@ define('APP_ROOT', dirname(__DIR__));
static $_PLAYER;
// @todo: write phpunit test for this method
function get_player(\Illuminate\Http\Request $request)
function get_player($session_id, $joins = true)
{
// seems dodgy but a simple singleton? to avoid the history being retrieved fresh everytime in 1 request
if (!empty($_PLAYER)) {
return $_PLAYER;
}
if ($request->has('modern-english')) {
$session_id = 'modern-english';
} else if ($request->header('Session-Id')) {
$session_id = $request->header('Session-Id');
} else {
return null;
if (is_a($session_id, \Illuminate\Http\Request::class)) {
$session_id = $session_id->header('Session-Id');
}
$player = \App\Player::where('session_id', $session_id)->with(['character', 'language', 'character.baseModifiers', 'character.baseModifiers.modifierCategory'])->firstOrFail();
$player = \App\Player::where('session_id', $session_id);
if ($joins) {
$player->with([
'character',
'language',
'character.baseModifiers',
'character.baseModifiers.modifierCategory',
'character.status',
'character.profile_picture'
]);
}
$player = $player->firstOrFail();
$_PLAYER = $player;
return $player;
......
......@@ -409,7 +409,6 @@ class Initial extends Migration
$blueprint->increments('id');
$blueprint->integer('language_id');
$blueprint->string('name');
$blueprint->longtext('profile_picture')->nullable();
$blueprint->datetime('enabled')->nullable();
$blueprint->timestamps();
......@@ -417,61 +416,50 @@ class Initial extends Migration
}
);
Schema::create(
'modifier_categories',
function (Blueprint $blueprint) {
$blueprint->increments('id');
$blueprint->string('name');
$blueprint->string('income_outgoing_savings');
$blueprint->longText('icon')->nullable(); // expects base64 or a CDN related link
$blueprint->timestamps();
}
);
$modifier_categories = static::$modifier_categories;
foreach ($modifier_categories as &$category) {
if (isset($category['icon'])) {
$category['icon'] = 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(dirname(__DIR__)) . '/resources/assets/icons/modifier-categories/' . $category['icon']));
}
}
DB::table('modifier_categories')->insert($modifier_categories);
Schema::create(
'character_social',
function (Blueprint $blueprint) {
$blueprint->increments('id');
$blueprint->integer('character_id');
$blueprint->string('language_id');
$blueprint->string('bio');
$blueprint->integer('season_roundup_position');
$blueprint->string('body');
$blueprint->integer('likes');
$blueprint->integer('comments');
$blueprint->timestamps();
// $blueprint->index(['character_id', 'language_id']);
}
);
Schema::create(
'summaries',
'character_pictures',
function (Blueprint $blueprint) {
$blueprint->increments('id');
$blueprint->integer('language_id');
$blueprint->integer('character_id');
$blueprint->string('title');
$blueprint->integer('season_roundup_position');
$blueprint->longText('uri');
$blueprint->timestamps();
}
);
Schema::create(
'summary_answers',
'modifier_categories',
function (Blueprint $blueprint) {
$blueprint->increments('id');
$blueprint->integer('summary_id');
$blueprint->string('title');
$blueprint->string('good_neutral_bad');
$blueprint->string('name');
$blueprint->string('income_outgoing_savings');
$blueprint->longText('icon')->nullable(); // expects base64 or a CDN related link
$blueprint->timestamps();
}
);
$modifier_categories = static::$modifier_categories;
foreach ($modifier_categories as &$category) {
if (isset($category['icon'])) {
$category['icon'] = 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(dirname(__DIR__)) . '/resources/assets/icons/modifier-categories/' . $category['icon']));
}
}
DB::table('modifier_categories')->insert($modifier_categories);
Schema::create(
'questions',
function (Blueprint $blueprint) {
......@@ -581,14 +569,13 @@ class Initial extends Migration
Schema::dropIfExists('modifier_categories');
Schema::dropIfExists('characters');
Schema::dropIfExists('character_social');
Schema::dropIfExists('character_pictures');
Schema::dropIfExists('questions');
Schema::dropIfExists('answers');
Schema::dropIfExists('modifiers');
Schema::dropIfExists('resources');
Schema::dropIfExists('season_roundups');
Schema::dropIfExists('player_history');
Schema::dropIfExists('summaries');
Schema::dropIfExists('summary_answers');
Schema::dropIfExists('resource_categories');
Schema::dropIfExists('answer_resource_category');
Schema::dropIfExists('resource_resource_category');
......
......@@ -10,11 +10,10 @@ class ImportEmilyQuestions extends Migration
static $cache = [];
static $profile_picture = '/resources/assets/characters/emily.png';
static $base_modifiers = '/resources/data/emily/base-modifiers.json';
static $questions = '/resources/data/emily/questions.json';
static $season_roundups = '/resources/data/emily/season-roundups.json';
static $summary_answers = '/resources/data/emily/summary-answers.json';
// static $summary_answers = '/resources/data/emily/summary-answers.json';
/**
* Run the migrations.
......@@ -29,10 +28,17 @@ class ImportEmilyQuestions extends Migration
$character = new \App\Character();
$character->name = 'Emily';
$character->language_id = $language->id;
$character->profile_picture = 'data:image/png;base64,' . base64_encode(file_get_contents(APP_ROOT . static::$profile_picture));
$character->enabled = date('Y-m-d H:i:s');
$character->save();
foreach (json_decode(file_get_contents(APP_ROOT . '/resources/data/emily/profile-pictures.json')) as $_profile_picture) {
$profile_picture = new \App\CharacterPicture();
$profile_picture->character_id = $character->id;
$profile_picture->uri = 'data:image/png;base64,' . base64_encode(file_get_contents(APP_ROOT . '/resources/assets/characters/' . strtolower($character->name) . '/profile-pictures/' . $_profile_picture->uri));
$profile_picture->season_roundup_position = $_profile_picture->season_roundup_position;
$profile_picture->save();
}
foreach (json_decode(file_get_contents(APP_ROOT . static::$base_modifiers), 1) as $_base_modifier) {
$_category = $_base_modifier['category'];
$amount = $_base_modifier['amount'];
......@@ -187,25 +193,6 @@ class ImportEmilyQuestions extends Migration
$modifier->save();
}
}
$summary = new App\Summary;
$summary->language_id = $language->id;
$summary->character_id = $character->id;
$summary->title = 'Will she/won\'t she';
$summary->save();
/**
* [
* 'title' => 'Emily didnt make the most sound choices this year and has not saved nearly enough, so Emily and her friends decided to stay close to their home city and camp for a few days',
* ]
*/
foreach (json_decode(file_get_contents(APP_ROOT . static::$summary_answers), 1) as $_answer) {
$answer = new \App\SummaryAnswer;
$answer->summary_id = $summary->id;
$answer->title = $_answer['title'];
$answer->good_neutral_bad = $_answer['good_neutral_bad'];
$answer->save();
}
}
protected function getModifierCategory($_category, $income_outgoing = 'outgoing')
......
......@@ -263,11 +263,10 @@ class ImportJakeQuestions extends Migration
$character = new \App\Character();
$character->name = 'Jake';
$character->language_id = $language->id;
$character->profile_picture = 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(dirname(__DIR__)) . '/resources/assets/characters/jake.png'));
$character->save();
return;
//
// foreach (static::$base_modifiers as $_base_modifier) {
// list ($_category, $amount) = $_base_modifier;
......
......@@ -263,8 +263,6 @@ class ImportJamalsQuestions extends Migration
$character = new \App\Character();
$character->name = 'Jamal';
$character->language_id = $language->id;
$character->profile_picture = 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(dirname(__DIR__)) . '/resources/assets/characters/jamal.png'));
// $character->enabled = date('Y-m-d H:i:s');
$character->save();
return;
......
......@@ -263,12 +263,11 @@ class ImportAnnQuestions extends Migration
$character = new \App\Character();
$character->name = 'Ann';
$character->language_id = $language->id;
$character->profile_picture = 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(dirname(__DIR__)) . '/resources/assets/characters/ann.png'));
$character->save();
return;
//
// foreach (static::$base_modifiers as $_base_modifier) {
// list ($_category, $amount) = $_base_modifier;
......
......@@ -22,15 +22,19 @@ class AddCharacterPaperwork extends Migration
$blueprint->timestamps();
});
$json = json_decode(file_get_contents(APP_ROOT . '/database/seeds/data/paperwork.json'));
foreach ($json as $_character => $paperwork) {
$characters = ['emily', 'jamal', 'jake', 'ann'];
foreach ($characters as $_character) {
$json_path = APP_ROOT . '/resources/data/' . $_character . '/paperwork.json';
if (!file_exists($json_path)) continue;
$character = \App\Character::where('name', 'LIKE', $_character)->first();
if ($character) {
foreach ($paperwork as $_page) {
$page = $character->paperwork()->create();
$page->uri = 'data:image/png;base64,' . base64_encode(file_get_contents(APP_ROOT . $_page));
$page->save();
}
$json = json_decode(file_get_contents($json_path));
foreach ($json as $_page) {
$page = $character->paperwork()->create();
$page->uri = 'data:image/png;base64,' . base64_encode(file_get_contents(APP_ROOT . '/resources/assets/characters/' . $_character . '/paperwork/' . $_page));
$page->save();
}
}
}
......
......@@ -13,32 +13,23 @@ class AddSocialText extends Migration
*/
public function up()
{
Schema::table('characters', function(Blueprint $blueprint) {
$blueprint->string('social')->after('name');
});
foreach (['emily', 'jake', 'jamal', 'ann'] as $_character) {
$character = \App\Character::where('name', 'LIKE', $_character)->first();
$json_path = APP_ROOT . '/resources/data/' . $_character . '/social-statuses.json';
$emily = \App\Character::where('name', 'Emily')->first();
if ($emily) {
$emily->social = 'Hoping to go on a big adventure holiday next year with my BFF! 🗺 Need to save some 💰💰 tho 🤞';
$emily->save();
}
$jake = \App\Character::where('name', 'Jake')->first();
if ($jake) {
$jake->social = 'The client literally just asked to make the logo bigger! I need a drink! 😭🥃';
$jake->save();
}
$jamal = \App\Character::where('name', 'Jamal')->first();
if ($jamal) {
$jamal->social = 'Feeling exited about the future... planning a new venture, watch this space!';
$jamal->save();
}
if (!$character || !file_exists($json_path)) {
continue;
}
$ann = \App\Character::where('name', 'Ann')->first();
if ($ann) {
$ann->social = 'So much paperwork to sort out. So bored and ready for the weekend.';
$ann->save();
foreach (json_decode(file_get_contents($json_path)) as $_status) {
$status = new \App\CharacterSocial();
$status->character_id = $character->id;
$status->body = $_status->body;
$status->likes = $_status->likes;
$status->comments = $_status->comments;
$status->season_roundup_position = $_status->season_roundup_position;
$status->save();
}
}
}
......@@ -49,8 +40,5 @@ class AddSocialText extends Migration
*/
public function down()
{
Schema::table('characters', function(Blueprint $blueprint) {
$blueprint->dropColumn('social');
});
}
}
[
"emily-paperwork-1.png",
"emily-paperwork-2.png",
"emily-paperwork-3.png"
]
[
{
"uri": "emily.png",
"season_roundup_position": 0
},
{
"uri": "emily-summer.png",
"season_roundup_position": 1
},
{
"uri": "emily-winter.png",
"season_roundup_position": 2
}
]
[
{
"body": "Hoping to go on a big adventure holiday next year with my BFF! 🗺 Need to save some 💰💰 tho 🤞",
"season_roundup_position": 0,
"likes": 9,
"comments": 31
},
{
"body": "Buzzing from our gig last night. Big turn out which always helps us enjoy performing 🎸🤘",
"season_roundup_position": 1,
"likes": 16,
"comments": 4
},
{
"body": "Feeling sick 🤮 So not up for going out tonight. Anyone want my ticket to the show?",
"season_roundup_position": 2,
"likes": 3,
"comments": 12
}
]
[
{
"category": "salary",
"amount": 1131
},
{
"category": "rent",
"amount": 450
},
{
"category": "transport",
"amount": 55
},
{
"category": "groceries",
"amount": 66.55
},
{
"category": "bills",
"amount": 60
},
{
"category": "shopping",
"amount": 119.50
},
{
"category": "entertainment",
"amount": 160
},
{
"category": "credit cards \/ loans",
"amount": 50
},
{