Skip to content

Instantly share code, notes, and snippets.

@JABirchall
Created April 9, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JABirchall/49cc15f5fcdd2746313dd7c7011750aa to your computer and use it in GitHub Desktop.
Save JABirchall/49cc15f5fcdd2746313dd7c7011750aa to your computer and use it in GitHub Desktop.
<?php
namespace Fastoon\Http\Controllers;
use Fastoon\Entries;
use Fastoon\Keys;
use Illuminate\Http\Request;
use Validator;
use File;
use Illuminate\Support\Facades\Cache;
use Fastoon\Http\Requests;
use Fastoon\Giveaways;
use Carbon\Carbon;
use Auth;
use Fastoon\Http\Controllers\Controller;
class GiveawayController extends Controller
{
private $apiPath = "http://store.steampowered.com/api/appdetails/?appids=%d&cc=us&l=english";
public function giveawayList()
{
$listGiveaways = Giveaways::isActive()->orderBy('endDateTime', 'asc')->paginate(10);
return view('layouts.giveawaylist')->with('listGiveaways', $listGiveaways);
}
public function view(Giveaways $id)
{
if ($id->deleted === 1 || $id->active === 0)
return abort(404);
$id->endsIn = Carbon::parse($id->endDateTime)->diffForHumans();
$id->started = Carbon::parse($id->startDateTime)->diffForHumans();
$id->winner = (Auth::Check()) ? $id->keys()->where('winnerId', Auth::User()->id)
->where('claimed', '!=', 3)
->first() : NULL;
return view('layouts.giveaway')->with('giveaway', $id);
}
public function enter(Giveaways $id)
{
if ($id->deleted === 1 || $id->active === 0)
return abort(404);
if ($id->active != 1)
return redirect(route('Giveaway::view', $id))->with('error', 'This giveaway has already ended!');
if ($id->creatorId === Auth::User()->id)
return redirect(route('Giveaway::view', $id))->with('error', 'You can not enter your own giveaways!');
if ($id->entries()->where('userId', Auth::User()->id)->count() >= 1)
return redirect(route('Giveaway::view', $id))->with('error', 'You have already entered this giveaway.');
if (Auth::User()->gold < $id->cost)
return redirect(route('Giveaway::view', $id))->with('error', 'You do not have enough gold to enter this giveaway');
$entry = new \Fastoon\Entries;
$entry->giveawayId = $id->id;
$entry->userId = Auth::User()->id;
$entry->save();
$user = Auth::User();
$user->gold -= $id->cost;
$user->save();
return redirect(route('Giveaway::view', $id))->with('success', 'You have entered this giveaway.');
}
public function claim(Keys $id, $claim = 1)
{
if ($id->giveaway->deleted === 1 || $id->giveaway->active === 0)
return abort('404');
if ($id->winnerId != Auth::User()->id)
return abort('404');
if (!$id->giveaway->endDateTime->isPast() || !$id->giveaway->startDateTime->isPast())
return abort('404');
if ($id->claimed != 0)
return redirect(route('Giveaway::view', $id->giveawayId))->with('error', 'You have already received this key.');
$id->claimed = $claim;
$id->save();
switch ($claim) {
case 1:
$user = $id->giveaway->creator;
$user->gold += ($id->giveaway->cost * 2);
$user->maxGiveaways += mt_rand(1, 2);
$user->save();
\Fastoon\Log::create([
'userId' => Auth::User()->id,
'type' => 1,
'message' => 'userId ' . Auth::User()->id . ' claimed key ' . $id->id . " successfully",
]);
return redirect(route('Giveaway::view', $id->giveawayId))->with('success', 'You have successfully received this key.');
break;
case 2:
$user = $id->giveaway->creator;
$user->gold -= $id->giveaway->cost;
$user->maxGiveaways -= 1;
$user->save();
\Fastoon\Log::create([
'userId' => Auth::User()->id,
'type' => 1,
'message' => 'userId ' . Auth::User()->id . ' claimed key ' . $id->id . " invalid",
]);
return redirect(route('Giveaway::view', $id->giveawayId))->with('error', 'You have claimed this key invalid!');
break;
case 3:
$ourWinner = $id->giveaway->entries()->where('userId', Auth::User()->id)->first();
$ourWinner->removed = 1;
$ourWinner->save();
$winner = new Keys;
$winner->giveawayId = $id->giveawayId;
$winner->key = $id->key;
$winner->winnerId = $id->giveaway->entries()->where('removed', 0)->get()
->toArray()[
mt_rand(0, $id->giveaway->entries
->where('removed', 0)
->count() -1)
]['userId']; // this is fucking disgusting
$winner->save();
return redirect(route('Giveaway::view', $id->giveawayId))->with('success', 'You have have given this key back.');
break;
}
}
public function create()
{
if (Auth::User()->maxGiveaways < 1)
return abort(404);
return view('layouts.creategiveaway');
}
public function doCreate(Request $r)
{
if (Auth::User()->maxGiveaways < 1)
return abort(404);
$validator = Validator::make($r->all(), [
'startDateTime' => 'required',
'endDateTime' => 'required',
'appid' => 'required|numeric',
'type' => 'required|digits_between:1,2',
'key' => 'required',
]);
if ($validator->fails())
return redirect('/giveaway/create')->withErrors($validator)->withInput();
list($startDate, $startTime) = explode('-', $r->input('startDateTime'));
list($endDate, $endTime) = explode('-', $r->input('endDateTime'));
$startDateTime = Carbon::parse(implode('', [
$startDate,
$startTime,
]));
$endDateTime = Carbon::parse(implode('', [
$endDate,
$endTime,
]));
if ($startDateTime->isPast())
$startDateTime = Carbon::now();
if ($endDateTime->isPast())
return "Giveaways cant end before they have started ";
if ($endDateTime->getTimestamp() <= $startDateTime->getTimestamp())
return "Giveaways can not end before 1 hour after they have started";
if (!Cache::has('game' . $r->input('appid'))) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => sprintf($this->apiPath, $r->input('appid')),
CURLOPT_RETURNTRANSFER => TRUE,
]);
$gameInfo = curl_exec($ch);
curl_close($ch);
Cache::put('game' . $r->input('appid'), $gameInfo, Carbon::now()->addWeek());
}
else
$gameInfo = Cache::get('game' . $r->input('appid'));
$gameInfo = json_decode($gameInfo);
if ($gameInfo->{$r->input('appid')}->success === FALSE)
return "There was an unexpected error, please contact an admin or support and give them this error:</br>SARF{$r->input('appid')}";
$giveaway = new \Fastoon\Giveaways;
$giveaway->startDateTime = $startDateTime->format('Y-m-d H:i:s');
$giveaway->endDateTime = $endDateTime->format('Y-m-d H:i:s');
$giveaway->type = $r->input('type');
$giveaway->storeId = $r->input('appid');
$giveaway->description = $r->input('description') ?? 'No description';
$giveaway->creatorId = Auth::User()->id;
$giveaway->active = 0;
$giveaway->game = $gameInfo->{$r->input('appid')}->data->name;
$giveaway->header = $gameInfo->{$r->input('appid')}->data->header_image;
$giveaway->cost = ceil($gameInfo->{$r->input('appid')}->data->price_overview->final / 100) +
(5 - (ceil($gameInfo->{$r->input('appid')}->data->price_overview->final / 100) % 5)) %
5; // Passed
$giveaway->save();
$key = new \Fastoon\Keys; // Add support for Multiple key giveaways.
$key->giveawayId = $giveaway->id;
$key->key = $r->input('key');
$key->save();
\Fastoon\Log::create([
'userId' => Auth::User()->id,
'type' => 1,
'message' => 'created giveaway ' . $giveaway->id,
]);
return view('layouts.confirmgiveaway')->with(compact('giveaway'));
}
public function confirm(Giveaways $id)
{
if ($id->creatorId != Auth::User()->id || $id->deleted != 0)
return abort(404);
if ($id->active != 0)
return redirect()->route('Giveaway::view', $id->id)
->with('success', 'This giveaway has already been activated.');
$id->active = 1;
$id->save();
Auth::User()->decrement('maxGiveaways');
\Fastoon\Log::create([
'userId' => Auth::User()->id,
'type' => 1,
'message' => 'confirmed giveaway ' . $id->id,
]);
return redirect()->route('Giveaway::view', $id->id)
->with('success', 'Your giveaway has been activated and is now public.');
}
public function entries()
{
$entries = Auth::User()->entries;
return view('layouts.entrylist')->with(compact('entries'));
}
public function created()
{
$created = Auth::User()->giveaways;
return view('layouts.createdlist')->with('giveaways', $created);
}
public function viewCreated(\Fastoon\User $id)
{
$created = $id->giveaways;
return view('layouts.createdlist')->with('giveaways', $created);
}
public function viewEntries(Giveaways $id)
{
if ($id->endDateTime >= Carbon::now()->format("Y-m-d H:i:s"))
return redirect(route('Giveaway::view', $id))->with('error', 'Entries can only be viewed once the giveaway has finished and the winner claimed their prize.');
return view('layouts.entrylist')->with('entries', $id->entries);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment