var socketInfo = {}; socket = new WebSocket('ws://localhost:8000'); socket.onopen = function (e){ socketInfo.method = "connect"; socket.send(JSON.stringify(socketInfo)); console.log(' ws://localhost:8000'); } socket.onclose = function (e){ console.log(' !'); } socket.onmessage = function (e){ if (typeof e.data === "string"){ var request = JSON.parse(e.data); Actions[request.function](request.args); }; }
var Actions = { myId: function(id){ localStorage.setItem("myId", id); }, log: function(str){ console.log(str); }, ....... }
function websocket_onmessage($keyINsock, $str){ global $Users; $json = json_decode($str); $method = strval($json->{'method'}); $args = $json->{'args'}; if (!isset($args)) $args = $keyINsock; if (!empty($method)) $Users->$method($keyINsock, $args); }
public function frAccept($myid, $opId){ $this->opponents[$myid] = $opId; $this->opponents[$opId] = $myid; $args = array($myid, $opId); $arrOut = array('function' => 'frAccept', 'args' => $args); $arrOutJSON = json_encode($arrOut); websock_send($opId, $arrOutJSON); websock_send($myid, $arrOutJSON); }
frAccept: function(id){ fight_start(id); console.log('Fight starting'); } ....... function fight_start(ids){ $('body').load('fight.html'); curplayer = ids[0]; localStorage.setItem("opponent", ids[0]); if (ids[0] == localStorage.getItem("myId")) localStorage.setItem("opponent", ids[1]); }
<meta http-equiv="Cache-Control" content="no-cache" /> <div id="to_area"></div> <div id="wrapper"> <div id="opHand"></div> <div id="area"> <div id="timer"> <div class="bg"></div> </div> <div id="opUnits"> <div class="nexus card" data-health="30" id="" data-attack="0"> <div class="health"> <div class="inner">30</div> </div> </div> <div class="bg"></div> </div> <div id="myUnits"> <div class="nexus card" data-health="30" data-attack="0"> <div class="health"> <div class="inner">30</div> </div> </div> <div class="bg"></div> </div> <div id="next"> </div> </div> <div id="myhand"></div> </div>
function get_cards(num){ socketInfo.method = "get_cards"; socketInfo.args = num; socket.send(JSON.stringify(socketInfo)); }
public function get_cards($myid, $num){ global $Cards; for ($i=0; $i < $num; $i++){ $card = $Cards->getRandom(); $args = array( "player_id" => $myid, "card" => $card ); $arrOut = array('function' => 'player_get_card', 'args' => $args); $arrOutJSON = json_encode($arrOut); websock_send($myid, $arrOutJSON); websock_send($this->opponents[$myid], $arrOutJSON); } }
player_get_card: function(args){ if (!me(args.player_id)) { $('#opHand').append('<div class="card" id="'+args.card.id+'" />'); return; }else{ var card = card_construct(args.card); $('#myhand').append(card); return; } }
$('#myhand').on('click',".card",function(){ // if (!me(curplayer)) return; to_area($(this)); });
function to_area(card){ // card.appendTo('#myUnits'); socketInfo.method = 'opGetCard'; socketInfo.args = card.attr("id"); socket.send(JSON.stringify(socketInfo)); }
public function opGetCard($myid, $card_id){ global $Cards; $card = $Cards->getById($card_id); $arrOut = array('function' => 'opGetCard', 'args' => $card); $arrOutJSON = json_encode($arrOut); websock_send($this->opponents[$myid], $arrOutJSON); }
$('#area').on('click',"#myUnits .card",function(){ ////////////////// if(!me(curplayer) || $(this).hasClass('nexus'))return; if ($(this).hasClass('active')) { $(this).removeClass('active'); }else{ $('#area #myUnits .card.active').removeClass('active'); $(this).addClass('active'); } });
$('#area').on('click',"#opUnits .card",function(){ ////////////////// $agressor = $('#area #myUnits .card.active'); if ($agressor.length < 1) { return false; }; attack_request($agressor,$(this)); });
function attack_request(agressor, victim){ //////////// socketInfo.method = 'attack_request'; socketInfo.args = agressor.attr("id")+'-'+victim.attr("id"); socket.send(JSON.stringify(socketInfo)); }
public function attack_request($myid, $args){ $arrOut = array('function' => 'attack', 'args' => $args); $arrOutJSON = json_encode($arrOut); websock_send($myid, $arrOutJSON); websock_send($this->opponents[$myid], $arrOutJSON); }
attack: function (args){ id = args.split('-'); var alias = $('#'+id[0]).data('alias'); onBeforeAttack(alias,id); } .................. function onBeforeAttack(alias, id){ // if(Units[alias] == undefined){ var uFile = 'js/units/'+alias+'.unit.js'; var xmlhttp = getXmlHttp(); xmlhttp.open('GET', uFile, false); xmlhttp.send(null); if(xmlhttp.status == 200){ $('body').append('<script src="'+uFile+'"></scipt>'); }else{ Units[alias] = new defaultUnit(); } } Units[alias].attack(id[0], id[1]); }
function defaultUnit(){} defaultUnit.prototype.attack = function(agressor_id, victim_id){ var victim = $('#'+victim_id); var agressor = $('#'+agressor_id); victim.css('z-index',5); agressor.css('z-index',10); var $ypos = victim.offset().top - agressor.offset().top; var $xpos = victim.offset().left - agressor.offset().left; agressor.animate({top:$ypos, left:$xpos}, 100).delay(200).animate({top:0, left:0}, 100); if (isNaN(victim.data('attack'))) victim.data('attack',0); if (isNaN(agressor.data('attack'))) agressor.data('attack',0); victim.data('health',victim.data('health')-agressor.data('attack')); agressor.data('health',agressor.data('health')-victim.data('attack')); refreshCards(); }
refreshCards = function(){ //// $('#myUnits .card.active').removeClass('active'); var rest = setTimeout(function(){ $('#area .card').each(function(){ refresh_card($(this)); if ($(this).data('health') < 1) { death($(this)); }; }); clearTimeout(rest); },1000); } function refresh_card(card){ ////////// $('.health .inner',card).html(card.data('health')); $('.attack .inner',card).html(card.data('attack')); }
function death(unit){ ////////// if (unit.hasClass('nexus')) { var lose = localStorage.getItem("opponent"); if (!me(curplayer)) lose = localStorage.getItem("myId"); socketInfo.method = 'lose'; socketInfo.args = lose; socket.send(JSON.stringify(socketInfo)); }; unit.addClass('die'); var dt = setTimeout(function(){ unit.remove(); clearTimeout(dt); },2000); }
Source: https://habr.com/ru/post/252897/
All Articles