Commit fae56949 authored by Jack Stupple's avatar Jack Stupple

refactor how images work + recommended endpoint

parent bd79959e
......@@ -6,22 +6,40 @@ use App\Resource;
class ResourceController extends Controller
{
public function index() {
$player = get_player();
protected function buildRecommendedQuery($player)
{
if (!$player) {
return 0;
}
$subquery = \DB::table('player_history')
->select('resource_resource_category.resource_id')
->leftJoin('answers', 'answers.id', 'player_history.answer_id')
->leftJoin('answer_resource_category', 'answer_resource_category.answer_id', 'player_history.answer_id')
->leftJoin('resource_categories', 'answer_resource_category.resource_category_id', 'resource_categories.id')
->leftJoin('resource_resource_category', 'resource_categories.id', 'resource_resource_category.resource_category_id')
->whereRaw('answers.weight < 1')
->groupBy('resource_resource_category.resource_id');
$recommended = \DB::table('resources')
->whereRaw('resources.id IN (' . $subquery->toSql() . ')')
->where('resources.language_id', $player->language_id)
->limit(5)
->where('resources.language_id', $player->language_id);
return $recommended;
}
public function getRecommended()
{
$recommended_query = $this->buildRecommendedQuery(get_player());
return [
'count' => $recommended_query ? $recommended_query->count() : null,
'recommended' => $recommended_query ? $recommended_query->get() : []
];
}
public function index() {
$player = get_player();
$recommended = $this->buildRecommendedQuery($player)
->get();
$resources = \App\Resource::with('categories')
......
......@@ -320,7 +320,7 @@ class Initial extends Migration
$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(APP_ROOT . '/resources/assets/icons/modifier-categories/' . $category['icon']));
$category['icon'] = '/modifiers/' . $category['icon'];
}
}
......@@ -351,7 +351,7 @@ class Initial extends Migration
$question_categories = static::$question_categories;
foreach ($question_categories as &$category) {
$category['icon'] = 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(dirname(__DIR__)) . '/resources/assets/icons/question-categories/' . $category['icon']));
$category['icon'] = '/question-categories/' . $category['icon'];
}
DB::table('question_categories')->insert($question_categories);
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCharacterPaperwork extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->down();
Schema::create('paperwork', function(Blueprint $blueprint) {
$blueprint->increments('id');
$blueprint->integer('character_id');
$blueprint->integer('language_id');
$blueprint->longText('uri');
$blueprint->timestamps();
});
$characters = ['emily', 'jamal', 'jake', 'ann'];
$languages = \App\Language::whereRaw('enabled <= NOW()')->get();
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();
$paperwork = json_decode(file_get_contents($json_path), 1);
foreach ($languages as $language) {
if (!isset($paperwork[$language->slug])) {
continue;
}
foreach ($paperwork[$language->slug] as $_page) {
$page = $character->paperwork()->create();
$page->language_id = $language->id;
$page->uri = 'data:image/png;base64,' . base64_encode(file_get_contents(APP_ROOT . '/resources/assets/characters/' . $_character . '/paperwork/' . $_page));
$page->save();
}
}
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('paperwork');
}
}
......@@ -56,7 +56,7 @@ class CharacterMigration extends Migration
foreach ($profile_pictures as $_profile_picture) {
$profile_picture = new \App\CharacterPicture();
$profile_picture->character_id = $this->character->id;
$profile_picture->uri = 'data:image/png;base64,' . base64_encode(file_get_contents(APP_ROOT . '/resources/assets/characters/' . strtolower($this->character->name) . '/profile-pictures/' . $_profile_picture['uri']));
$profile_picture->uri = '/characters/' . strtolower($this->character->name) . '/profile-pictures/' . $_profile_picture['uri'];
$profile_picture->season_roundup_position = $_profile_picture['season_roundup_position'];
$profile_picture->save();
}
......
[
{
"category": "salary",
"amount": 1500
},
{
"category": "rent",
"amount": 0
},
{
"category": "transport",
"amount": 150
},
{
"category": "groceries",
"amount": 150
},
{
"category": "bills",
"amount": 260
},
{
"category": "shopping",
"amount": 120
},
{
"category": "entertainment",
"amount": 150
},
{
"category": "credit cards \/ loans",
"amount": 50
},
{
"category": "investments",
"amount": 80
},
{
"category": "other_out",
"amount": 0
},
{
"category": "other_in",
"amount": 40
},
{
"category": "savings",
"amount": 80
}
]
{
"en": [
"ann-paperwork-1.png",
"ann-paperwork-2.png",
"ann-paperwork-3.png"
],
"fr": []
}
[
{
"uri": "ann.png",
"season_roundup_position": 0
},
{
"uri": "ann-summer.png",
"season_roundup_position": 1
},
{
"uri": "ann-winter.png",
"season_roundup_position": 2
}
]
This diff is collapsed.
{
"en": [
{
"roundup_text": "Ann qualifies for a government scheme to get subsidies on her electricity and heating costs, so her utility bills have gone down.",
"positive_negative": 1,
"modifiers": [
{
"category": "bills",
"amount": -20
}
]
},
{
"roundup_text": "Ann's dog Billy has fallen ill and will require surgery.",
"positive_negative": -1,
"modifiers": [
{
"category": "other_out",
"amount": 80
}
]
},
{
"roundup_text": "Ann's local bus has stopped running at the weekends so she will need to find alternative transport.",
"positive_negative": -1,
"modifiers": [
{
"category": "transport",
"amount": 25
}
]
},
{
"roundup_text": "Ann wins money in a radio competition.",
"positive_negative": 1,
"modifiers": []
},
{
"roundup_text": "Ann decides to adopt a cat from her friend who can longer look after it.",
"positive_negative": -1,
"modifiers": [
{
"category": "bills",
"amount": 15
}
]
},
{
"roundup_text": "Ann joins her friends in taking up a fitness class.",
"positive_negative": -1,
"modifiers": [
{
"category": "entertainment",
"amount": 25
}
]
},
{
"roundup_text": "Ann helps her friend out at her allotment and get free fruit and vegetables",
"positive_negative": 1,
"modifiers": [
{
"category": "groceries",
"amount": -15
}
]
},
{
"roundup_text": "Ann's son treats her to a weekend spa break for her birthday.",
"positive_negative": 1,
"modifiers": []
},
{
"roundup_text": "Ann subscribes to a film streaming service.",
"positive_negative": -1,
"modifiers": [
{
"category": "entertainment",
"amount": 9.99
}
]
}
],
"fr": []
}
{
"en": [
{
"body": "So much paperwork to sort out. So bored and ready for the weekend.",
"season_roundup_position": 0,
"likes": 3,
"comments": 1
},
{
"body": "The house is feeling pretty eerie these days. Feel I should have a garden party to bring it back to life. Anyone up for a BBQ this Saturday?",
"season_roundup_position": 1,
"likes": 13,
"comments": 40
},
{
"body": "Drinking port by the fire and sharing cheese crackers with Billy by my feet. Life is good! 🐶❤",
"season_roundup_position": 2,
"likes": 56,
"comments": 4
}
],
"fr": []
}
[
{
"category": "salary",
"amount": 2200
},
{
"category": "rent",
"amount": 1250
},
{
"category": "transport",
"amount": 260
},
{
"category": "groceries",
"amount": 90
},
{
"category": "bills",
"amount": 125
},
{
"category": "shopping",
"amount": 95
},
{
"category": "entertainment",
"amount": 165
},
{
"category": "credit cards \/ loans",
"amount": 210
},
{
"category": "investments",
"amount": 0
},
{
"category": "other_out",
"amount": 0
},
{
"category": "other_in",
"amount": 0
},
{
"category": "savings",
"amount": 0
}
]
{
"en": [
"jake-paperwork-1.png",
"jake-paperwork-2.png",
"jake-paperwork-3.png"
],
"fr": []
}
[
{
"uri": "jake.png",
"season_roundup_position": 0
},
{
"uri": "jake-summer.png",
"season_roundup_position": 1
},
{
"uri": "jake-winter.png",
"season_roundup_position": 2
}
]
This diff is collapsed.
{
"en": [
{
"roundup_text": "Jake has finished paying off his student loan and his incomings have now increased.",
"positive_negative": 1,
"modifiers": [
{
"category": "credit cards \/ loans",
"amount": -30
}
]
},
{
"roundup_text": "Jake has worked extra hard on a work project and recieved a bonus which he saves.",
"positive_negative": 1,
"modifiers": [
{
"category": "other_in",
"amount": 80
},
{
"category": "savings",
"amount": 80
}
]
},
{
"roundup_text": "Jake has decided to vary his design skils and starts a course at the local college.",
"positive_negative": -1,
"modifiers": [
{
"category": "other_out",
"amount": 20
}
]
},
{
"roundup_text": "He decides to purchase a subscription to a kids TV channel",
"positive_negative": -1,
"modifiers": [
{
"category": "bills",
"amount": 8
}
]
},
{
"roundup_text": "Jake saves money by switching to a cheaper supermarket.",
"positive_negative": 1,
"modifiers": [
{
"category": "groceries",
"amount": -50
}
]
},
{
"roundup_text": "Jake's girlfriend returns to work part time, meaning she can contribute more towards bills.",
"positive_negative": 1,
"modifiers": [
{
"category": "bills",
"amount": -50
}
]
},
{
"roundup_text": "The service charge bills on the apartment have increased.",
"positive_negative": -1,
"modifiers": [
{
"category": "bills",
"amount": 20
}
]
},
{
"roundup_text": "Jake's sofa has broken and he needs a new one.",
"positive_negative": -1,
"modifiers": [
{
"category": "credit cards \/ loans",
"amount": 24
}
]
},
{
"roundup_text": "Jake takes lunches to work to save money.",
"positive_negative": 1,
"modifiers": [
{
"category": "groceries",
"amount": 20
},
{
"category": "shopping",
"amount": 50
}
]
}
],
"fr": []
}
{
"en": [
{
"body": "The client literally just asked to make the logo bigger! I need a drink! 😭🥃",
"season_roundup_position": 0,
"likes": 56,
"comments": 12
},
{
"body": "Wow! So hot out! Who fancies lunch outside?",
"season_roundup_position": 1,
"likes": 30,
"comments": 10
},
{
"body": "Getting excited about Christmas already, changes when you have a little one!",
"season_roundup_position": 2,
"likes": 27,
"comments": 6
}
],
"fr": []
}
......@@ -55,6 +55,7 @@ $router->group(
function () use ($router) {
$router->get('/', 'ResourceController@index');
$router->get('/get-roundup', 'ResourceController@getRoundup');
$router->get('/get-recommended', 'ResourceController@getRecommended');
}
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment