Skip to content

Instantly share code, notes, and snippets.

@chandrewz
Created March 9, 2018 03:28
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 chandrewz/304be21545daa9f4929fc28a28ef5f98 to your computer and use it in GitHub Desktop.
Save chandrewz/304be21545daa9f4929fc28a28ef5f98 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\SteamUser;
use App\Http\Requests\CreateSteamUserRequest;
class GemController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$id = $this->formatSteamIds($request->steam_id);
$user = SteamUser::firstOrNew(['steam_id' => $id]);
if ($user->exists) {
return $user;
}
$user->steam_id = $id;
$user->json_body = $request->body;
$user->save();
return $user;
}
/**
* Sorted array of steam IDs based on a string of comma seperated Steam IDs
* @param string $steam_id one or many steam IDs
* @return array
*/
private function formatSteamIds($steam_id) {
$steam_ids = explode(',', $steam_id);
foreach ($steam_ids as $i => $sid) {
$steam_ids[$i] = strpos($sid, '_') ? substr($sid, 0, strpos($sid, '_')) : $sid;
}
asort($steam_ids);
$formatted_ids = implode(',', $steam_ids);
return $formatted_ids;
}
/**
* Array of steam_id to hero_index
* @param string $steam_id
* @return array
*/
private function steamdIdHeroIndex($steam_id) {
$steam_ids = explode(',', $steam_id);
$map = [];
foreach ($steam_ids as $i => $sid) {
$explode = explode('_', $sid);
$map[$explode[0]] = isset($explode[1]) ? $explode[1] : null;
}
return $map;
}
/**
* Display the specified resource.
*
* @param int $steam_id
* @return \Illuminate\Http\Response
*/
public function show($steam_id)
{
$id = $this->formatSteamIds($steam_id);
$user = SteamUser::where('steam_id', $id)->first();
if ($user) {
return $user->json_body;
}
return response()->json([
'message' => 'Record not found for ' . $id,
], 404);
}
/**
* Display the specified resource.
*
* @param int $steam_id
* @return \Illuminate\Http\Response
*/
public function find($steam_id)
{
$id = $this->formatSteamIds($steam_id);
$ids = explode(',', $id);
$users = SteamUser::whereIn('steam_id', $ids)->get();
if (!$users) {
return response()->json([
'message' => 'Record not found for ' . $id,
], 404);
}
$json = [
'err' => 0,
'msg' => 'success',
'data' => []
];
$indexMap = $this->steamdIdHeroIndex($steam_id);
foreach ($users as $user) {
$obj = json_decode($user->json_body, true);
if (isset($indexMap[$user->steam_id]) && !empty($indexMap[$user->steam_id])) {
// set hero_index based in game
$obj['data'][$user->steam_id]['hero_index'] = $indexMap[$user->steam_id];
}
// append JSON of all players together
$json['data'][$user->steam_id] = $obj['data'][$user->steam_id];
}
return collect($json);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment