Commit b3d1482f authored by Jack Stupple's avatar Jack Stupple

standard character migration

parent 9db1ed4c
<?php
include_once 'CharacterMigration.php';
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ImportEmilyQuestions extends Migration
class ImportEmilyQuestions extends CharacterMigration
{
protected $created_at;
protected $character;
protected $language;
static $cache = [];
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 $profile_pictures = '/resources/data/emily/profile-pictures.json';
/**
* Run the migrations.
......@@ -23,190 +25,18 @@ class ImportEmilyQuestions extends Migration
public function up()
{
$this->created_at = date('Y-m-d H:i:s');
$language = \App\Language::where('slug', 'en')->first();
$character = new \App\Character();
$character->name = 'Emily';
$character->language_id = $language->id;
$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'];
$income_outgoing = 'outgoing';
if ($_category == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_category, 'other_') !== false) {
if ($_category == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_category = 'other';
}
$category = $this->getModifierCategory($_category, $income_outgoing);
$base_modifier = new \App\Modifier();
$base_modifier->model = 'character';
$base_modifier->model_id = $character->id;
$base_modifier->amount = $amount;
$base_modifier->modifier_category_id = $category ? $category->id : null;
$base_modifier->save();
}
/**
*
* [
* 'question' => 'Emilys musical instrument or accessories eg amp broke',
* 'question_category' => 'shopping',
* 'answers' => [
* [
* 'title' => 'buy second hand amp using entertainment allowance',
* 'body' => 'Use enterainment allowance to buy a basic second hand amp',
* 'weight' => 1,
* 'modifiers' => [
* [
* 'category' => 'bills',
* 'amount' => -8
* ]
* ],
* 'resource_categories' => []
* ],
* [
* 'title' => 'Use credit card to buy brand new apm',
* 'body' => 'Buy a brand new Amp on her credit card',
* 'weight' => 0.25,
* 'modifiers' => [],
* 'resource_categories' => []
* ],
* [
* 'title' => 'Take out payday loan to buy brand new amp',
* 'body' => 'Buy a brand new Amp by taking out a payday loan',
* 'weight' => -1,
* 'modifiers' => [],
* 'resource_categories' => []
* ],
* ]
* ]
*/
foreach (json_decode(file_get_contents(APP_ROOT . static::$questions), 1) as $_question) {
$question_category = \App\QuestionCategory::where('name', 'LIKE', $_question['question_category'])->first();
$question = new \App\Question;
$question->title = $_question['question'];
$question->question_category_id = $question_category ? $question_category->id : 0;
$question->language_id = $language->id;
$question->character_id = $character->id;
$question->save();
foreach ($_question['answers'] as $_answer) {
$option = new \App\Answer();
$option->question_id = $question->id;
$option->title = $_answer['title'];
$option->body = $_answer['body'];
$option->weight = $_answer['weight'];
$option->save();
foreach ($_answer['modifiers'] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'answer';
$modifier->model_id = $option->id;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_modifier['category'] = 'other';
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->amount = $_modifier['amount'];
$modifier->save();
}
foreach ($_answer['resource_categories'] as $_category) {
$category = \App\ResourceCategory::where('name', 'LIKE', $_category)->first();
if ($category) {
$option->resourceCategories()->syncWithoutDetaching($category);
}
}
}
}
/**
* [
* 'roundup_text' => 'Due to recent electricity hikes, Emilys utilities bills have increased',
* 'positive_negative' => -1,
* 'modifiers' => []
* ],
*/
foreach (json_decode(file_get_contents(APP_ROOT . static::$season_roundups), 1) as $_roundup) {
$roundup = new \App\SeasonRoundup;
$roundup->roundup_text = $_roundup['roundup_text'];
$roundup->positive_negative = $_roundup['positive_negative'];
$roundup->character_id = $character->id;
$roundup->save();
foreach ($_roundup['modifiers'] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'season_roundup';
$modifier->model_id = $roundup->id;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_modifier['category'] = 'other';
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->amount = $_modifier['amount'];
$modifier->save();
}
}
}
protected function getModifierCategory($_category, $income_outgoing = 'outgoing')
{
if (isset(static::$cache[$_category . '-' . $income_outgoing])) {
$category = static::$cache[$_category . '-' . $income_outgoing];
} else {
$category = \App\ModifierCategory::where('name', 'LIKE', $_category)->where('income_outgoing_savings', $income_outgoing)->first();
if ($category) {
static::$cache[$_category . '-' . $income_outgoing] = $category;
}
}
return $category;
$this->language = \App\Language::where('slug', 'en')->first();
$this->character = new \App\Character();
$this->character->name = 'Emily';
$this->character->language_id = $this->language->id;
$this->character->enabled = $this->created_at;
$this->character->save();
$this->profilePictures(APP_ROOT . static::$profile_pictures);
$this->baseModifiers(APP_ROOT . static::$base_modifiers);
$this->questions(APP_ROOT . static::$questions);
$this->seasonRoundups(APP_ROOT . static::$season_roundups);
}
/**
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CharacterMigration extends Migration
{
protected function profilePictures($json_location)
{
if (!file_exists($json_location)) return false;
foreach (json_decode(file_get_contents($json_location)) 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->season_roundup_position = $_profile_picture->season_roundup_position;
$profile_picture->save();
}
}
protected function baseModifiers($json_location)
{
if (!file_exists($json_location)) return false;
foreach (json_decode(file_get_contents($json_location), 1) as $_base_modifier) {
$_category = $_base_modifier['category'];
$amount = $_base_modifier['amount'];
$income_outgoing = 'outgoing';
if ($_category == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_category, 'other_') !== false) {
if ($_category == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_category = 'other';
}
$category = $this->getModifierCategory($_category, $income_outgoing);
$base_modifier = new \App\Modifier();
$base_modifier->model = 'character';
$base_modifier->model_id = $this->character->id;
$base_modifier->amount = $amount;
$base_modifier->modifier_category_id = $category ? $category->id : null;
$base_modifier->save();
}
}
protected function questions($json_location)
{
if (!file_exists($json_location)) return false;
foreach (json_decode(file_get_contents($json_location), 1) as $_question) {
$question_category = \App\QuestionCategory::where('name', 'LIKE', $_question['question_category'])->first();
$question = new \App\Question;
$question->title = $_question['question'];
$question->question_category_id = $question_category ? $question_category->id : 0;
$question->language_id = $this->language->id;
$question->character_id = $this->character->id;
$question->save();
foreach ($_question['answers'] as $_answer) {
$option = new \App\Answer();
$option->question_id = $question->id;
$option->title = $_answer['title'];
$option->body = $_answer['body'];
$option->weight = $_answer['weight'];
$option->save();
foreach ($_answer['modifiers'] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'answer';
$modifier->model_id = $option->id;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_modifier['category'] = 'other';
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->amount = $_modifier['amount'];
$modifier->save();
}
foreach ($_answer['resource_categories'] as $_category) {
$category = \App\ResourceCategory::where('name', 'LIKE', $_category)->first();
if ($category) {
$option->resourceCategories()->syncWithoutDetaching($category);
}
}
}
}
}
protected function seasonRoundups($json_location)
{
if (!file_exists($json_location)) return false;
foreach (json_decode(file_get_contents($json_location), 1) as $_roundup) {
$roundup = new \App\SeasonRoundup;
$roundup->roundup_text = $_roundup['roundup_text'];
$roundup->positive_negative = $_roundup['positive_negative'];
$roundup->character_id = $this->character->id;
$roundup->save();
foreach ($_roundup['modifiers'] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'season_roundup';
$modifier->model_id = $roundup->id;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_modifier['category'] = 'other';
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->amount = $_modifier['amount'];
$modifier->save();
}
}
}
protected function getModifierCategory($_category, $income_outgoing = 'outgoing')
{
if (isset(static::$cache[$_category . '-' . $income_outgoing])) {
$category = static::$cache[$_category . '-' . $income_outgoing];
} else {
$category = \App\ModifierCategory::where('name', 'LIKE', $_category)->where('income_outgoing_savings', $income_outgoing)->first();
if ($category) {
static::$cache[$_category . '-' . $income_outgoing] = $category;
}
}
return $category;
}
}
......@@ -222,6 +222,7 @@
{
"title": "Buy a better laptop",
"body": "Jamal could buy a top specification laptop with all the latest features and pay for it on finance over 12 months.",
"weight": -1,
"modifiers": [
{
"category": "credit cards\/loans",
......@@ -330,7 +331,7 @@
{
"category": "other_out",
"amount": 50
},
}
],
"resource_categories": ["crowdfunding"]
},
......
......@@ -10,7 +10,7 @@
]
},
{
"roundup_text": "Jamal has been overpaying tax so he receives a lump sum for the overpayment. His salary is recalculated too so he doesn't overpay going forward. ",
"roundup_text": "Jamal has been overpaying tax so he receives a lump sum for the overpayment. His salary is recalculated too so he doesn't overpay going forward.",
"positive_negative": 1,
"modifiers": [
{
......@@ -20,7 +20,7 @@
]
},
{
"roundup_text": "The family decides to get a pet, so Jamal will need to take out some insurance and buy pet food. ",
"roundup_text": "The family decides to get a pet, so Jamal will need to take out some insurance and buy pet food.",
"positive_negative": -1,
"modifiers": [
{
......@@ -44,7 +44,7 @@
]
},
{
"roundup_text": "Jamal's wife gets a small increase in her wages meaning she can contribute more to household bills. ",
"roundup_text": "Jamal's wife gets a small increase in her wages meaning she can contribute more to household bills.",
"positive_negative": 1,
"modifiers": [
{
......@@ -64,7 +64,7 @@
]
},
{
"roundup_text": "Jamal gets a supermarket membership card that helps him save money on his groceries. ",
"roundup_text": "Jamal gets a supermarket membership card that helps him save money on his groceries.",
"positive_negative": 1,
"modifiers": [
{
......@@ -74,7 +74,7 @@
]
},
{
"roundup_text": "The government has announced their new budget, petrol prices have gone up considerably as a result. ",
"roundup_text": "The government has announced their new budget, petrol prices have gone up considerably as a result.",
"positive_negative": -1,
"modifiers": [
{
......
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