Skip to content

Instantly share code, notes, and snippets.

@demonio
Created July 23, 2017 22:37
Show Gist options
  • Save demonio/cb47a06defd6be56d27665951342bca6 to your computer and use it in GitHub Desktop.
Save demonio/cb47a06defd6be56d27665951342bca6 to your computer and use it in GitHub Desktop.
Sockets para KumbiaPHP
<!DOCTYPE html>
<html id="important">
<head>
<link href="/css/material-icons.css" rel="stylesheet">
<link href="/css/materialize.min.css" rel="stylesheet">
<link href="/css/hacks.css" rel="stylesheet">
<link href="/css/helpers.css" rel="stylesheet">
<link href="/css/style.css" rel="stylesheet">
<?=Css::inc()?>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?=empty($title)?ucfirst("$controller_name — $action_name"):$title?></title>
</head>
<body class="row">
<div id="sidenav-overlay"></div>
<?php View::content(); ?>
<div class="modal pa15"></div>
<script src="/javascript/jquery-3.2.1.min.js"></script>
<script src="/javascript/prism.js"></script>
<script src="/javascript/materialize.min.js"></script>
<script src="/javascript/function.js"></script>
<script src="/javascript/ajax.js"></script>
<script src="/javascript/script.js"></script>
<?=Js::inc()?>
<?php View::partial('toast'); ?>
<?php if ( ! empty($socket) ) View::partial( 'socket', false, ['socket'=>$socket]); ?>
</body>
</html>
<ul class="collection" id="pizarra">
<?php View::partial('tiza', false, ['cartas'=>$cartas, 'usuarios'=>$usuarios]); ?>
</ul>
<?php
/**
*/
class PizarraController extends AppController
{
#
public function index()
{
if ( ! empty($_POST['aventuras_id']) )
exit( Redirect::to('/pj/pizarra/faith/' . $_POST['aventuras_id']) );
}
#
public function faith($aventuras_id, $sse=0)
{
$this->socket = "/pj/pizarra/faith/$aventuras_id/1";
if ($sse) :
$this->from = "/pj/pizarra/tiza/$aventuras_id";
$this->to = '#pizarra';
return View::select('', 'sse');
endif;
$this->cartas = (new Pizarra)->obtenerFilas($aventuras_id);
$this->usuarios = (new Usuarios)->todos();
}
#
public function tiza($aventuras_id)
{
$this->cartas = (new Pizarra)->obtenerFilas($aventuras_id);
$this->usuarios = (new Usuarios)->todos();
}
}
<script>
if(typeof(EventSource) !== "undefined")
{
var source = new EventSource('<?=$socket?>');
source.onmessage = function(e)
{
var data = e.data.split('@');
var to = data[1];
console.log(data[1]);
var from = data[0];
console.log(data[0]);
$(to).load(from);
};
}
else
{
<?php $s = _('LO SIENTO PERO TU NAVEGADOR NO SOPORTA TODAS LAS FUNCIONES, TE RECOMENDAMOS QUE USES CHROME'); ?>
alert('<?=$s?>');
}
</script>
<?php
header("Content-Type: text/event-stream\n\n");
header('Cache-Control: no-cache');
echo "data: $from@$to" . "\n\n";
ob_flush();
flush();
<?php
$verbos = ['jugar_top'=>'jugó del TOP', 'robar_carta'=>'robó del mazo', 'usar_carta'=>'jugó de su mano'];
$colores = ['jugar_top'=>'orange', 'robar_carta'=>'red', 'usar_carta'=>'blue'];
foreach ($cartas as $o) :
$usuario = $usuarios[$o->usuarios_id]; ?>
<li class="collection-item">
<img class="left mr15" src="/img/cartas/faith/<?=$o->imagen?>" width="128px">
<h3 class="<?=$colores[$o->accion]?>-text darken-1"><?=explode('@', $usuario->email)[0];?> <?=$verbos[$o->accion]?>:</h3>
<h4>
<?php if ($o->accion == 'robar carta') : ?>
Una carta.
<?php else : ?>
En el entorno de
<?php
$card = explode('.png', $o->imagen)[0];
if ( strstr($card, 'e') ) echo str_replace('e', 'espacio un ', $card);
if ( strstr($card, 'n') ) echo str_replace('n', 'naturaleza un ', $card);
if ( strstr($card, 's') ) echo str_replace('s', 'sistema operativo un ', $card);
if ( strstr($card, 'u') ) echo str_replace('u', 'urbano un ', $card);
?>.
<?php endif; ?>
</h4>
<div class="clearfix"></div>
</li>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment