Skip to content

Instantly share code, notes, and snippets.

@kaioken
Last active August 29, 2015 13:59
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 kaioken/10739505 to your computer and use it in GitHub Desktop.
Save kaioken/10739505 to your computer and use it in GitHub Desktop.
Popular PHP
function get_sessionid(){
$result = NULL;
$sessionid = uniqid() . md5(mt_rand());
$result = set_action($sessionid, 'start');
return $result;
}
function get_premios_count(){
$result = NULL;
$result = mt_rand(1, 1000);
return $result;
}
function set_action($sessionid = NULL, $action = NULL) {
$result = NULL;
switch ($action) :
case 'start' :
$result = $sessionid;
break;
case 'goal' :
$result = 'ok';
break;
case 'endgame' :
$result = $sessionid;
break;
endswitch;
return $result;
}
function get_premio($sessionid){
$premios = array(
1 => array(
'descripcion'=>'pelota'
, 'cantidad' => 100
)
, 2 => array(
'descripcion'=>'gorra'
, 'cantidad' => 200
)
, 3 => array(
'descripcion'=>'camiseta'
, 'cantidad' => 500
)
);
$result = NULL;
$premio_id = mt_rand(0, 3);
$result = $premios[$premio_id]['descripcion'];
if (mt_rand(0,10)>5) :
$result = 'nohaymas';
endif;
return $result;
}
function set_premio($sessionid, $userdata){
$result = 'ok';
if (mt_rand(0,10)>5) :
$result = 'expired';
endif;
return $result;
}
function get_user_by_dni($user_dni = NULL) {
$result = false;
if ($user_dni=='654654654') $result = true;
return $result;
}
function get_last_ganadores(){
$result = array();
/*
$nombres = array('Roberto', 'Juan', 'María', 'Agustin', 'Felipe', 'Pedro' , 'Diego');
$apellidos = array('Perez', 'Alvarez', 'Martinez', 'Graddo', 'Thirja', 'Pomes' , 'Valderrama', 'Hulk', 'De La Vega');
for ($i=0;$i<3;$i++) :
$nyap = $nombres[mt_rand(0, count($nombres)-1)] .' '. $apellidos[mt_rand(0, count($apellidos)-1)];
$result[] = array('nombre' =>$nyap, 'puntos' => mt_rand(15, 60));
endfor;
//*/
return $result;
}
$result = NULL;
if (isset($_POST['action'])) :
switch ($_POST['action']) :
case 'timezone' :
$result['timezone'] = date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');
break;
case 'start' :
$result['sessionid'] = get_sessionid();
$result['premios'] = get_premios_count();
break;
case 'goal' :
if (isset($_POST['sessionid'])) :
$result['error'] = set_action($_POST['sessionid'], 'goal');
else :
$result['error'] = 'no';
endif;
break;
case 'endgame' :
$errors = array();
if (isset($_POST['sessionid'])) :
if (set_action($_POST['sessionid'], 'endgame')) :
$result['premio'] = get_premio($_POST['sessionid']);
endif;
else :
$errors[] = 'faltandatos';
endif;
$result['error'] = $errors;
break;
case 'userwins' :
$errors = array();
if (
isset($_POST['sessionid'])
&& isset($_POST['user_dni'])
&& isset($_POST['user_name'])
&& isset($_POST['user_email'])
) :
if (set_action($_POST['sessionid'], 'endgame')) :
if (!get_user_by_dni($_POST['user_dni'])) :
$userdata = array(
'dni' => $_POST['user_dni']
, 'name' => $_POST['user_name']
, 'email' => $_POST['user_email']
);
$result['error'] = set_premio($_POST['sessionid'], $userdata);
else :
$errors[] ='dniduplicado';
endif;
else :
$errors[] = 'fail';
endif;
else :
$errors[] = 'faltandatos';
endif;
$result['error'] = $errors;
break;
case 'listaganadores' :
$result['ganadores'] = get_last_ganadores();
break;
endswitch;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment