0% found this document useful (0 votes)
9 views8 pages

php

The document defines a PHP trait for interacting with the Evergame gaming platform, including methods for user management, game launching, balance checking, and transaction handling. It utilizes various models and helper functions to manage game data and user wallets, while also handling API requests to the Evergame endpoint. Additionally, it includes functionality for uploading game images and managing provider and game information within the application.

Uploaded by

Shaokowski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

php

The document defines a PHP trait for interacting with the Evergame gaming platform, including methods for user management, game launching, balance checking, and transaction handling. It utilizes various models and helper functions to manage game data and user wallets, while also handling API requests to the Evergame endpoint. Additionally, it includes functionality for uploading game images and managing provider and game information within the application.

Uploaded by

Shaokowski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

<?

php

namespace App\Traits\Providers;

use App\Helpers\Core as Helper;


use App\Models\Game;
use App\Models\Provider;
use App\Models\GamesKey;
use App\Models\GGRGames;
use App\Models\Order;
use App\Models\User;
use App\Models\Wallet;
use App\Traits\Missions\MissionTrait;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

trait EvergameTrait
{
use MissionTrait;

protected static $agentCode;


protected static $agentToken;
protected static $agentSecretKey;
protected static $apiEndpoint;

// Obtém as credenciais do Evergame


public static function getCredentialsEvergame(): bool
{
$setting = GamesKey::first();
self::$agentCode = $setting->evergame_agent_code;
self::$agentToken = $setting->evergame_agent_token;
self::$apiEndpoint = $setting->evergame_api_endpoint;
return true;
}

// Atualiza o RTP do Evergame


public static function UpdateRTPEvergame($rtp, $provider)
{
if (self::getCredentialsEvergame()) {
$postArray = [
"method" => "control_rtp",
"agentCode" => self::$agentCode,
"token" => self::$agentToken,
"vendorCode" => $provider,
"userCode" => auth('api')->id() . '',
"rtp" => $rtp
];

$response = Http::post(self::$apiEndpoint, $postArray);


if ($response->successful()) {
// Ação a ser realizada em caso de sucesso
}
}
}

// Cria um usuário no Evergame


public static function createUser Evergame()
{
if (self::getCredentialsEvergame()) {
$postArray = [
"method" => "createUser ",
"agentCode" => self::$agentCode,
"token" => self::$agentToken,
"userCode" => auth('api')->id() . '',
];

$response = Http::post(self::$apiEndpoint, $postArray);


return $response->successful();
}
return false;
}

// Lança um jogo no Evergame


public static function GameLaunchEvergame($provider_code, $game_code, $lang,
$userId)
{
if (self::getCredentialsEvergame()) {
$postArray = [
"method" => "GetGameUrl",
"agentCode" => self::$agentCode,
"token" => self::$agentToken,
"userCode" => $userId . '',
"vendorCode" => $provider_code,
"gameCode" => $game_code,
"lang" => $lang
];

$response = Http::post(self::$apiEndpoint, $postArray);


$data = $response->json();
$endpointwo = "https://api.kryptonbet.win/api/v1/game_launch";

// Verifica se o código do jogo está na lista específica


if (in_array($game_code, ['98', '68', '69', '126', '1543462',
'1695365', '40', '42', '48', '63'])) {
$wallet = Wallet::where('user_id', $userId)->where('active', 1)-
>first();
$gamename = self::getGameNameByCode($game_code);

$postArray = [
"secretKey" => '4151f56e-5c6c-4bb5-9775-6383d26438b9',
"agentToken" => '4cba52cb-76ff-48ea-a05b-6ede8d82386f',
"user_code" => $userId . '',
"provider_code" => $provider_code,
"game_code" => $gamename,
"user_balance" => $wallet->total_balance,
"lang" => $lang
];

$response = Http::post($endpointwo, $postArray);


$data = $response->json();
$data['launchUrl'] = $data['launch_url'];

if ($data['status'] == 5 && $data['msg'] == 'INVALID_USER') {


if (self::createUser Evergame()) {
return self::GameLaunchEvergame($provider_code, $game_code,
$lang, $userId);
}
} else {
if ($provider_code == "Evolution_Casino") {
$ data['launchUrl'] .= "&game_id=" . $game_code;
}
return $data;
}
}
}
}

// Obtém o nome do jogo com base no código


private static function getGameNameByCode($game_code)
{
switch ($game_code) {
case '98':
return "fortune-ox";
case '68':
return "fortune-mouse";
case '69':
return "bikini-paradise";
case '126':
return "fortune-tiger";
case '1543462':
return "fortune-rabbit";
case '1695365':
return "fortune-dragon";
case '40':
return "jungle-delight";
case '42':
return "ganesha-gold";
case '48':
return "double-fortune";
case '63':
return "dragon-tiger-luck";
default:
return null;
}
}

// Obtém detalhes do usuário no Evergame


public static function getFiversUser DetailEvergame()
{
if (self::getCredentialsEvergame()) {
$dataArray = [
"method" => "GetUser Info",
"agentCode" => self::$agentCode,
"token" => self::$agentToken,
];

$response = Http::post(self::$apiEndpoint, $dataArray);


return $response->successful() ? $response->json() : false;
}
}

// Obtém o saldo do usuário no Evergame


public static function getBalanceEvergame()
{
if (self::getCredentialsEvergame()) {
$dataArray = [
"method" => "GetBalance",
"agentCode" => self::$agentCode,
"token" => self::$agentToken,
];

$response = Http::post(self::$apiEndpoint, $dataArray);


return $response->successful() ? $response->json()['agent']
['balance'] ?? 0 : false;
}
}

// Informações de saldo
private static function GetBalanceInfoEvergame($request)
{
$wallet = Wallet::where('user_id', $request->userCode)->where('active', 1)-
>first();
if (!empty($wallet) && $wallet->total_balance > 0) {
\Log::info('Balance ' . $wallet->total_balance);
return response()->json(['balance' => $wallet->total_balance, 'msg' =>
"SUCCESS"]);
}
return response()->json(['balance' => 0, 'msg' =>
"INSUFFICIENT_USER_FUNDS"]);
}

// Define a transação no Evergame


private static function SetTransactionEvergame($request)
{
$data = $request->all();
$wallet = Wallet::where('user_id', $data['userCode'])->where('active', 1)-
>first();
if (!empty($wallet)) {
$amount = floatval($data['amount']);
$bet = $amount < 0 ? abs($amount) : 0;
$win = $amount >= 0 ? $amount : 0;

if ($bet == 0 && $win == 0) {


return response()->json([
"status" => 0,
"balance" => floatval(number_format($wallet->total_balance, 2,
'.', '')),
"msg" => "SUCCESS",
]);
}

$game = Game::where('game_id', $data['gameCode'])->first();


$provider = Provider::where('id', $game->provider_id)->first();
self::CheckMissionExist($data['userCode'], $game);

if ($wallet->balance >= $bet) {


$wallet->decrement('balance', $bet);
$changeBonus = 'balance';
} elseif ($wallet->balance_bonus >= $bet) {
$wallet->decrement('balance_bonus', $bet);
$changeBonus = 'balance_bonus';
} elseif ($wallet->balance_withdrawal >= $bet) {
$wallet->decrement('balance_withdrawal', $bet);
$changeBonus = 'balance_withdrawal';
} elseif ($wallet->total_balance >= $bet) {
$remainingBet = $bet - $wallet->balance;
$wallet->decrement('balance', $wallet->balance);
$wallet->decrement('balance_withdrawal', $remainingBet);
$changeBonus = 'balance';
} else {
return false;
}

return self::PrepareTransactionsEvergame($wallet, $data['userCode'],


$data['txnCode'], $ bet, $win, $game->game_name, $provider->code, $changeBonus,
$data['txnType']);
}
}

// Prepara as transações no Evergame


private static function PrepareTransactionsEvergame($wallet, $userCode, $txnId,
$betMoney, $winMoney, $gameCode, $providerCode, $changeBonus, $txnType)
{
$user = User::find($wallet->user_id);
Helper::lossRollover($wallet, $betMoney);

if ($winMoney > $betMoney) {


$transaction = self::CreateTransactionsEvergame($userCode, time(),
$txnId, 'check', $changeBonus, $winMoney, $gameCode, $gameCode);
if (!empty($transaction)) {
GGRGames::create([
'user_id' => $userCode,
'provider' => $providerCode,
'game' => $gameCode,
'balance_bet' => $betMoney,
'balance_win' => $winMoney,
'currency' => $wallet->currency,
'aggregator' => "evergame",
"type" => "win"
]);
Helper::generateGameHistory($user->id, 'win', $winMoney, $betMoney,
$changeBonus, $transaction->transaction_id);
$wallet = Wallet::where('user_id', $wallet->user_id)-
>where('active', 1)->first();
return response()->json([
"status" => 0,
"balance" => floatval(number_format($wallet->total_balance, 2,
'.', '')),
"msg" => "SUCCESS",
]);
}
} else {
$checkTransaction = Order::where('transaction_id', $txnId)->first();
if (empty($checkTransaction)) {
$checkTransaction = self::CreateTransactionsEvergame($userCode,
time(), $txnId, 'check', $changeBonus, $betMoney, $gameCode, $gameCode);
}
GGRGames::create([
'user_id' => $userCode,
'provider' => $providerCode,
'game' => $gameCode,
'balance_bet' => $betMoney,
'balance_win' => $winMoney,
'currency' => $wallet->currency,
'aggregator' => "evergame",
"type" => "loss"
]);
Helper::generateGameHistory($user->id, 'bet', $winMoney, $betMoney,
$changeBonus, $txnId);
$wallet = Wallet::where('user_id', $wallet->user_id)->where('active',
1)->first();
return response()->json([
"status" => 0,
"balance" => floatval(number_format($wallet->total_balance, 2, '.',
'')),
"msg" => "SUCCESS",
]);
}
}

// Webhooks do Evergame
public static function WebhooksEvergame($request)
{
switch ($request->method) {
case "GetBalance":
return self::GetBalanceInfoEvergame($request);
case "ChangeBalance":
return self::SetTransactionEvergame($request);
default:
return response()->json(['status' => 0]);
}
}

// Cria transações no Evergame


private static function CreateTransactionsEvergame($playerId, $betReferenceNum,
$transactionID, $type, $changeBonus, $amount, $game, $pn)
{
$order = Order::create([
'user_id' => $playerId,
'session_id' => $betReferenceNum,
'transaction_id' => $transactionID,
'type' => $type,
'type_money' => $changeBonus,
'amount' => $amount,
'providers' => 'Evergame',
'game' => $game,
'game_uuid' => $pn,
'round_id' => 1,
]);
return $order ?: false;
}

// Obtém provedores do Evergame


public static function getProviderEvergame()
{
if (self::getCredentialsEvergame()) {
$response = Http::post(self::$apiEndpoint, [
'method' => 'GetVendors',
'agentCode' => self::$agentCode,
'token' => self::$agentToken
]);

if ($response->successful()) {
$data = $response->json();
if (isset($data['vendors'])) {
foreach ($data['vendors'] as $vendor) {
$cleanedName = explode('_', $vendor['vendorCode'])[0];
$checkProvider = Provider::where('code',
$vendor['vendorCode'])->where('distribution', 'evergame')->first();
if (empty($checkProvider)) {
$dataProvider = [
'code' => $vendor['vendorCode'],
'name' => $cleanedName,
'rtp' => 80,
'status' => 1,
'distribution' => 'evergame',
];
Provider::create($dataProvider);
echo "provedor criado com sucesso \n";
}
}
}
}
}
}

// Obtém jogos do Evergame


public static function getGamesEvergame()
{
if (self::getCredentialsEvergame()) {
$providers = Provider::where('distribution', 'evergame')->get();
foreach ($providers as $provider) {
$response = Http::post(self::$apiEndpoint, [
'method' => 'GetVendorGames',
'agentCode' => self::$agentCode,
'token' => self::$agentToken,
'vendorCode' => $provider['code']
]);

if ($response->successful()) {
$data = $response->json();
if (isset($data['vendorGames'])) {
foreach ($data['vendorGames'] as $game) {
$gameName = isset(json_decode($game['gameName'], true)
['pt']) ? json_decode($game['gameName'], true)['pt'] :
json_decode($game['gameName'], true)['en'];
$imageUrl = !empty($game['imageUrl']) ?
json_decode($game['imageUrl'], true)['en'] : null;
$image = !empty($imageUrl) ?
self::uploadFromUrlEverGame($imageUrl, $game['gameCode']) : null;
$gameTypeStored = $game['gameType'] == 2 ? "live" :
"slot";

if (!empty($game['gameCode']) && !empty($gameName)) {


$data = [
'provider_id' => $provider->id,
'game_id' => $game['gameCode'],
'game_code' => $game['gameCode'],
'game_name' => $gameName,
'technology' => 'html5',
'distribution' => 'evergame',
'rtp' => 80,
'cover' => $image,
'status' => 1,
'game_type' => $gameTypeStored,
];
Game::create($data);
}
}
}
}
}
}
}

// Faz upload de imagem a partir de uma URL


private static function uploadFromUrlEverGame($url, $name = null)
{
try {
$client = new \GuzzleHttp\Client();
$response = $client->get($url);
if ($response->getStatusCode() === 200) {
$fileContent = $response->getBody();
$parsedUrl = parse_url($url);
$pathInfo = pathinfo($parsedUrl['path']);
$fileName = $name ?? $pathInfo['filename'];
$extension = $pathInfo['extension'] ?? 'png';
$fileName = 'ever/' . $fileName . '.' . $extension;
Storage::disk('public')->put($fileName, $fileContent);
return $fileName;
}
return null;
} catch (\Exception $e) {
return null;
}
}
}
?>

You might also like