Файл: app/Http/Controllers/ShopController.php
Строк: 144
<?php
namespace AppHttpControllers;
use AppModelsGameSettingModel;
use IlluminateHttpRequest;
use Auth;
use AppModelsUserModel;
use AppModelsCardModel;
use AppModelsCardUserModel;
use AppServicesServices;
use AppModelsMarketCardModel;
class ShopController extends Controller
{
public function index()
{
return view('game.shop.index');
}
public function cards()
{
$issetLevels = Services::getAvailableCardLevel();
$userCards = CardUserModel::where('user', Auth::user()->id)->get();
$userCardsArray = [];
foreach($userCards as $uC)
{
$userCardsArray[] = $uC->card;
}
$cards = CardModel::whereIn('level', $issetLevels)->whereNotIn('id', $userCardsArray)->whereNotIn('type', ['tutorial-common', 'tutorial-premium'])->paginate(5);
return view('game.shop.cards', ['cards' => $cards]);
}
public function card($id)
{
$action = GameSettingModel::where('key', 'action')->first();
$card = CardModel::findOrFail($id);
$issetLevels = Services::getAvailableCardLevel();
if(!in_array($card->level, $issetLevels)) return redirect('/shop')->with('error', 'Эта карта ещё закрыта!');
if(CardUserModel::where('user', Auth::user()->id)->where('card', $id)->count() > 0) return back()->with('error', 'Такая карта уже есть в колоде!');
if($action->value != '')
{
$act = json_decode($action->value);
if($act->action == 4)
{
return view('game.shop.card', ['card' => $card, "act" => $act]);
}
}
return view('game.shop.card', ['card' => $card]);
}
public function buy($id)
{
$action = GameSettingModel::where('key', 'action')->first();
$card = CardModel::findOrFail($id);
$issetLevels = Services::getAvailableCardLevel();
if(!in_array($card->level, $issetLevels)) return redirect('/shop')->with('error', 'Эта карта ещё закрыта!');
if(CardUserModel::where('user', Auth::user()->id)->where('card', $id)->count() > 0) return back()->with('error', 'Такая карта уже есть в колоде!');
if(Auth::user()->exp < $card->open_cost) return back()->with('error', 'Недостаточно монет!');
$cost = $card->open_cost;
if($action->value != '')
{
$act = json_decode($action->value);
if($act->action == 4) $cost -= round($cost/100*$act->percent);
}
CardUserModel::create([
'user' => Auth::user()->id,
'card' => $card->id,
'learn_time' => time()+($card->learn_time*60),
'status' => 'bought'
]);
Services::addMoney(-$cost, 0);
return redirect('/shop/learning')->with('ok', 'Началось обучение!');
}
public function learning()
{
$cards = CardUserModel::with('getCardInfo')->where('user', Auth::user()->id)->whereIn('status', ['bought', 'sold'])->paginate(10);
return view('game.shop.learning.index', ['cards' => $cards]);
}
public function learningInfo($id)
{
$card = CardUserModel::with('getCardInfo')->findOrFail($id);
if($card->user != Auth::user()->id) return back()->with('error', 'Ошибка!');
return view('game.shop.learning.info', ['card' => $card]);
}
public function redeem($id)
{
$card = CardUserModel::with('getCardInfo')->findOrFail($id);
if($card->user != Auth::user()->id) return back()->with('error', 'Ошибка!');
if($card->learn_time > time()) return back()->with('error', 'Карта ещё не изучена!');
if($card->status == 'redeemed') return back()->with('error', 'Карта уже выкуплена!');
$currency = $card->getCardInfo->type == 'premium' ? 'valmers' : 'exp';
if(Auth::user()->$currency < $card->getCardInfo->buyout_cost) return back()->with('error', 'Недостаточно '.($currency == 'valmers' ? 'вальмер' : 'монет').'!');
$card->status = 'redeemed';
$card->save();
$user = UserModel::find(Auth::user()->id);
$user->$currency -= $card->getCardInfo->buyout_cost;
$user->save();
return redirect('/shop/learning')->with('ok', 'Карта выкуплена!');
}
public function market()
{
$cards = MarketCardModel::with('getCardUserInfo.getCardInfo')->where('user', '<>', Auth::user()->id)->orderBy('cost', 'ASC')->paginate(10);
return view('game.shop.market.index', ["cards" => $cards]);
}
public function marketCardInfo($id)
{
$card = MarketCardModel::with('getCardUserInfo.getCardInfo', 'getUserInfo')->findOrFail($id);
if($card->user == Auth::user()->id) return redirect('/');
return view('game.shop.market.info', ["card" => $card]);
}
public function marketCardBuy($id)
{
$card = MarketCardModel::with('getCardUserInfo.getCardInfo')->findOrFail($id);
if($card->user == Auth::user()->id) return redirect('/');
if(Auth::user()->valmers < $card->cost) return back()->with('error', 'Недостаточно вальмер!');
if(CardUserModel::where('user', Auth::user()->id)->where('card', $card->getCardUserInfo->card)->count() > 0) return back()->with('error', 'Карта уже есть в наличии!');
UserModel::find($card->user)->increment('valmers', $card->cost);
$cardUser = CardUserModel::find($card->card);
$cardUser->user = Auth::user()->id;
$cardUser->learn_time = time()+($card->getCardUserInfo->getCardInfo->learn_time*60);
$cardUser->status = 'sold';
$cardUser->save();
Services::addMoney(0, -$card->cost);
MarketCardModel::destroy($id);
return redirect('/shop/learning')->with('ok', 'Карта куплена!');
}
}