Created
July 20, 2016 13:14
-
-
Save furkankadioglu/a307fe51dc5493d55de69169f1a9209e to your computer and use it in GitHub Desktop.
gameapistore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function store(Request $request) | |
{ | |
$postCategory = $request->get('postCategory'); | |
$gameToken = $request->header('gameToken'); | |
$csrf = $request->header('X-CSRF-TOKEN'); | |
$validator = Validator::make($request->all(), | |
[ | |
'gamePoint' => 'required', | |
'gameId' => 'required|numeric', | |
'userId' => 'required|numeric' | |
]); | |
if($validator->fails()) | |
{ | |
return response()->json(["Error" => $validator->errors()]); | |
} | |
if(isset($gameToken)) | |
{ | |
$senderIp = $request->ip(); | |
$game = GameObject::where('gameToken', $gameToken)->first(); | |
if($game) | |
{ | |
/*if($senderIp == $game->gameIp) | |
{*/ | |
/* | |
Dikkat: ObjectController'da her game componenti gösterilmeden bir csrf kodu gameplayertokene eklenmelidir. | |
*/ | |
$t = GamePlayerToken::where('token', $csrf)->first(); | |
if($t) | |
{ | |
$programRelId = $request->get('programRelId'); | |
$gamePoint = $request->get('gamePoint'); | |
$gamePoint = base64_decode($gamePoint); | |
$gamePoint = substr($gamePoint, 8); //userpuan delete | |
$userId = $request->get('userId'); | |
if(!is_int($gamePoint)) | |
{ | |
return response()->json(["Status" => "Error", "Reason" => "Cannot decode."]); | |
} | |
/*$datetime1 = new DateTime("now"); | |
$datetime2 = new DateTime($t->created_at); | |
$interval = $datetime1->diff($datetime2); | |
$h = $interval->format('%h'); | |
$i = $interval->format('%i'); | |
if($h < 1) | |
{*/ | |
$result = GameResult::where('userId', $userId)->where('gameId', $game->id)->first(); | |
if($result) | |
{ | |
$result->point = $gamePoint; | |
$result->gamePlayerToken = $t->token; | |
$result->save(); | |
return response()->json(["Status" => "Done"]); | |
} | |
else | |
{ | |
$result = new GameResult; | |
$result->gameId = $game->id; | |
$result->userId = $userId; | |
$result->programRelId = $programRelId; | |
$result->gamePlayerToken = $t->token; | |
$result->point = $gamePoint; | |
$result->save(); | |
return response()->json(["Status" => "Done"]); | |
} | |
//} | |
//return response()->json(["Error" => "Timeout"]); | |
} | |
else | |
{ | |
return response()->json(["Error" => "Wrong PLAYER(!) Token"]); | |
} | |
//} | |
/*else | |
{*/ | |
return response()->json(["Error" => "Wrong IP Adress."]); | |
/*}*/ | |
//} | |
//else | |
//{ | |
// return response()->json(["Error" => "404"]); | |
} | |
} | |
return $request; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment