53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/6/2
|
|
* Time: 10:52
|
|
*/
|
|
|
|
namespace common\services;
|
|
|
|
|
|
use common\models\Farm;
|
|
use common\models\Item;
|
|
use common\models\Land;
|
|
use yii\base\Object;
|
|
|
|
/**
|
|
* 农场
|
|
* Class FarmService
|
|
* @package common\services
|
|
*/
|
|
class FarmService extends Object
|
|
{
|
|
/*
|
|
* 初始化新农场数据
|
|
*/
|
|
public static function initNewFarmDataAndSave(Farm $farmDbData, $userID)
|
|
{
|
|
$farmDbData->user_id = $userID;
|
|
|
|
$farmDbData->bg_image_id = GameConfigServices::getParamByPath('init.1.bg_image_id', 104004);//背景物品ID
|
|
if (!$farmDbData->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR,$farmDbData->getErrors());
|
|
|
|
//向仓库中添加默认背景物品
|
|
$itemConfig = GameConfigServices::getParamByPath('item_list.' . $farmDbData->bg_image_id);
|
|
$item = new Item();
|
|
$item->user_id = $userID;
|
|
$item->item_id = $farmDbData->bg_image_id;
|
|
$item->name = $itemConfig['name'];
|
|
$item->type = $itemConfig['type2'];
|
|
if (!$item->save()) return false;
|
|
|
|
//新建土地
|
|
$land_num = GameConfigServices::getParamByPath('init.1.land_num', 1);//背景图片图形ID
|
|
$land_num=1;
|
|
for ($i = 0; $i < $land_num; $i++) {
|
|
$land = new Land();
|
|
if (!LandService::initNewFarmDataAndSave($land, $farmDbData->id, $i)) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |