예제 #1
0
 public function register_user(WebSocketTransportInterface $user, $data)
 {
     $tournament = Tournament::get($data->tournament);
     if ($tournament == null) {
         $user->sendString('{"msg": "Tournament not exist"}');
         $this->say('Tournament ' . $data->tournament . ' not exist');
         return false;
     }
     // Register itself
     $user->tournament = $tournament;
     $player = $tournament->get_player($user->player_id);
     if ($player == null) {
         $this->observer->say('Player ' . $user->player_id . ' not found in draft');
     } else {
         $player->connect($this->type);
         $user->player = $player;
         if ($tournament->status == 3) {
             $booster = $tournament->get_booster($player->order);
             if ($booster == null) {
                 $this->observer->say("Booster not found t {$this->tournament->id}" . " p {$this->player->order} n {$this->tournament->round}");
             } else {
                 $user->sendString(json_encode($booster));
             }
         }
         $user->sendString(json_encode($tournament));
         $user->sendString(json_encode($player->get_deck()));
     }
 }
 public function recieved(WebSocketTransportInterface $user, $data)
 {
     switch ($data->type) {
         case 'save':
             $player = $user->tournament->get_player($data->player);
             if ($player != null) {
                 $name = $user->tournament->name . ' ' . $player->nick;
                 $deck = "// Deck file for Magic Workstation created with mogg.fr\n";
                 $deck .= "// NAME : {$user->tournament->name}\n";
                 $deck .= "// CREATOR : {$player->nick}\n";
                 $deck .= "// FORMAT : {$user->tournament->type}\n";
                 $deck .= $player->get_deck()->summarize();
                 $deck = json_encode($deck);
                 $user->sendString('{"type": "save", "name": "' . $name . '", "deck": ' . $deck . '}');
             }
             break;
         case 'drop':
             $player = $user->tournament->get_player($user->player_id);
             if ($player != null && $player->status < 5) {
                 $player->drop('Requested by player');
             }
             break;
         default:
             $this->observer->say('Unknown type : ' . $data->type);
             print_r($data);
     }
 }
예제 #3
0
 public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg)
 {
     $data = json_decode($msg->getData());
     if ($data == null) {
         $this->say('Unparsable JSON : ' . $msg->getData());
         return false;
     }
     if (!isset($data->type)) {
         $this->say('No type given');
         return false;
     }
     if ($this->debug) {
         $this->say(' <- ' . $data->type);
     }
     switch ($data->type) {
         case 'ping':
             $data->type = 'pong';
             $user->sendString(json_encode($data));
             break;
         case 'pong':
             $user->ping = true;
             break;
         case 'register':
             if (isset($data->player_id) && isset($data->nick)) {
                 $ban = $this->observer->bans->is(null, $data->player_id);
                 if ($ban) {
                     $this->say('Connexion attempt from banned user ' . $data->player_id . ' (' . $ban->reason . ')');
                     $user->sendString('{"type": "ban", "reason": "' . $ban->reason . '"}');
                 } else {
                     $user->player_id = $data->player_id;
                     $user->nick = $data->nick;
                     $this->register_user($user, $data);
                 }
             } else {
                 $this->say('Incomplete registration : ' . $data->player_id . ' / ' . $data->nick);
                 $user->close();
             }
             break;
         default:
             if (isset($user->player_id)) {
                 $this->recieve($user, $data);
             } else {
                 $this->say('Action ' . $data->type . ' from unregistered user');
                 $user->close();
             }
     }
 }
예제 #4
0
 public function recieved(WebSocketTransportInterface $user, $data)
 {
     if ($user->follow != null) {
         return false;
     }
     switch ($data->type) {
         case 'add':
             $user->player->add($data->cardname, $data->nb);
             $user->tournament->send('tournament', 'build');
             $this->broadcast_following($user->player);
             break;
         case 'remove':
             $user->player->remove($data->cardname);
             $user->tournament->send('tournament', 'build');
             $this->broadcast_following($user->player);
             break;
         case 'toggle':
             if ($user->player->toggle($data->cardname, $data->from)) {
                 $user->tournament->send('tournament', 'build');
                 $this->broadcast_following($user->player);
             } else {
                 $user->sendString('{"msg": "' . $data->cardname . ' not found in ' . $data->from . '"}');
             }
             break;
         case 'ready':
             if ($user->tournament->status == 4 && property_exists($data, 'ready')) {
                 if ($data->ready && $user->player->deck_cards < 40) {
                     $user->sendString('{"msg": "You only have ' . $user->player->deck_cards . ' cards in your deck"}');
                 } else {
                     $user->player->set_ready($data->ready);
                     $user->tournament->send('tournament', 'build');
                 }
             }
             break;
         default:
             $this->say('Unknown type : ' . $data->type);
             print_r($data);
     }
 }
예제 #5
0
 /**
  * @param $data
  */
 public function establishConnection($data)
 {
     $this->transport = WebSocketTransportFactory::fromSocketData($this, $data, $this->logger);
     $myself = $this;
     $this->transport->on("handshake", function (Handshake $request) use($myself) {
         $myself->emit("handshake", [$request]);
     });
     $this->transport->on("connect", function () use($myself) {
         $myself->emit("connect", [$myself]);
     });
     $this->transport->on("message", function ($message) use($myself) {
         $myself->emit("message", ["message" => $message]);
     });
     $this->transport->on("flashXmlRequest", function ($message) use($myself) {
         $myself->emit("flashXmlRequest");
     });
     if ($this->transport instanceof WebSocketTransportFlash) {
         return;
     }
     $request = Request::fromString($data);
     $this->transport->respondTo($request);
 }
예제 #6
0
 protected function addStream(WebSocketTransportInterface $user, $id, \React\Stream\Stream $stream)
 {
     $this->streams[$user->getId()][$id] = $stream;
 }
예제 #7
0
 public function recieve(WebSocketTransportInterface $user, $data)
 {
     switch ($data->type) {
         // Shout
         case 'shout':
             global $db;
             $nick = $db->escape($user->nick);
             $message = $db->escape($data->message);
             $db->query("INSERT\n\t\t\t\t\tINTO `shout` (`sender_id`, `sender_nick`, `message`)\n\t\t\t\t\tVALUES ('{$user->player_id}', '{$nick}', '{$message}')");
             $data->player_id = $user->player_id;
             $data->player_nick = $user->nick;
             $data->time = now();
             $this->shouts[] = $data;
             if (count($this->shouts) > $this->nbshouts) {
                 array_shift($this->shouts);
             }
             $this->broadcast(json_encode($data));
             break;
         case 'blur':
             $user->inactive = true;
             $this->broadcast(json_encode($this->list_users()));
             break;
         case 'focus':
             $user->inactive = false;
             $this->broadcast(json_encode($this->list_users()));
             break;
         case 'keypress':
             $user->typing = true;
             $this->broadcast(json_encode($this->list_users()));
             break;
         case 'keyup':
             $user->typing = false;
             $this->broadcast(json_encode($this->list_users()));
             break;
             // Duels
         // Duels
         case 'pendingduel':
             $data->creator_id = $user->player_id;
             $duel = new Game($data);
             $this->observer->pending_duels[] = $duel;
             $this->broadcast(json_encode($duel));
             break;
         case 'joineduel':
             $duel_index = $this->observer->pending_duel($data->id);
             if ($duel_index === false) {
                 $this->say('Pending duel ' . $data->id . ' not found');
                 break;
             }
             $splduels = array_splice($this->observer->pending_duels, $duel_index, 1);
             $duel = $splduels[0];
             if ($duel->creator_id == $user->player_id) {
                 $this->broadcast('{"type": "duelcancel", "id": "' . $data->id . '"}');
             } else {
                 $data->joiner_id = $user->player_id;
                 $duel = $duel->join($data);
                 $duel->type = 'joineduel';
                 // Send first one time to both players
                 $duel->redirect = true;
                 $json = json_encode($duel);
                 $this->send_first($json, $duel->creator_id);
                 $this->send_first($json, $duel->joiner_id);
                 // Then broadcast to inform other users game was joined
                 $duel->redirect = false;
                 $this->broadcast(json_encode($duel));
                 $this->observer->joined_duels[] = $duel;
             }
             break;
         case 'goldfish':
             $data->type = 'joineduel';
             $data->name = 'Goldfish';
             $data->creator_id = $user->player_id;
             $data->joiner_id = $user->player_id;
             $duel = new Game($data);
             $duel->join($data);
             $duel->redirect = true;
             $user->sendString(json_encode($duel));
             $duel->redirect = false;
             $this->observer->joined_duels[] = $duel;
             break;
             // Tournament
         // Tournament
         case 'pending_tournament':
             foreach ($this->observer->pending_tournaments as $tournament) {
                 if ($tournament->registered($user) !== false) {
                     $tournament->unregister($user);
                 }
             }
             $data->type = 'pending_tournament';
             $tournament = Tournament::create($data, $user);
             if (is_string($tournament)) {
                 $user->sendString('{"type": "msg", "msg": "' . $tournament . '"}');
             } else {
                 $tournament->type = $data->type;
                 $this->observer->pending_tournaments[] = $tournament;
                 $data->player_id = $user->player_id;
                 $tournament->register($data, $user);
             }
             break;
         case 'tournament_register':
             $data->player_id = $user->player_id;
             foreach ($this->observer->pending_tournaments as $tournament) {
                 if ($tournament->registered($user) !== false) {
                     $tournament->unregister($user);
                 } else {
                     // Client not registered
                     if ($tournament->id == $data->id) {
                         // Wanted tournament
                         $tournament->register($data, $user);
                     }
                 }
             }
             break;
         default:
             $this->say('Unknown type : ' . $data->type);
             print_r($data);
     }
 }
 public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg)
 {
     //do nothing
     $this->logger->notice("Client is talking to the wind. ({$msg->getData()}, {$user->getId()})");
 }
예제 #9
0
파일: chat.php 프로젝트: sammarks/phpws
 public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg)
 {
     //do nothing
     $this->logger->notice("User {$user->getId()} is not in a room but tried to say: {$msg->getData()}");
 }
예제 #10
0
 public function recieve(WebSocketTransportInterface $user, $data)
 {
     switch ($data->type) {
         case 'tournament_set':
             $t = Tournament::get($data->id);
             if ($t == null) {
                 $user->sendString('{"type": "msg", "msg": "No tournament ' . $data->id . '"}');
             } else {
                 foreach ($data as $field => $value) {
                     if ($field != 'type' && $field != 'id') {
                         if (!property_exists($t, $field)) {
                             $this->say('Updating non existing field ' . $field);
                             continue;
                         }
                         $this->say('Admin setting "' . $field . '" to "' . $value . '"');
                         if ($field == 'due_time') {
                             // Have to relaunch timer, using dedicated func
                             // Go on
                             $left = strtotime($value) - time();
                             if ($left > 0) {
                                 // Some time left in current tournament step
                                 $t->timer_goon($left);
                             } else {
                                 $user->sendString('{"type": "msg", "msg": "Can\'t set due to past time"}');
                             }
                         } else {
                             $t->{$field} = $value;
                             $t->commit($field);
                         }
                     }
                 }
                 $t->send();
             }
             break;
         case 'refresh_mtg_data':
             $this->observer->import_mtg();
             $this->refresh($user);
             break;
         case 'kick':
             if (property_exists($data, 'handler') && property_exists($data, 'id')) {
                 if (property_exists($this->observer, $data->handler)) {
                     $handler = $this->observer->{$data->handler};
                     $kicked = array();
                     foreach ($handler->getConnections() as $cnx) {
                         if (isset($cnx->player_id) && $cnx->player_id == $data->id) {
                             $cnx->close('Kicked');
                             $kicked[] = $cnx->nick;
                         }
                     }
                     if (count($kicked) == 0) {
                         $this->say('No players with id ' . $data->id . ' to kick');
                     } else {
                         $this->say(implode($kicked) . ' kicked');
                         $this->refresh($user);
                     }
                 } else {
                     $this->say('Trying to kick from unexisting handler ' . $data->handler);
                 }
             }
             break;
         case 'ban':
             if (property_exists($data, 'id') && property_exists($data, 'reason')) {
                 $this->observer->bans->add($data->reason, null, $data->id);
                 $this->refresh($user);
             }
             break;
         case 'unban':
             if (property_exists($data, 'id')) {
                 $this->observer->bans->del($data->id);
                 $this->refresh($user);
             }
             break;
         default:
             $this->say('Unknown type : ' . $data->type);
             print_r($data);
     }
 }
예제 #11
0
 /**
  * Clean up after users who leave.
  *
  * @param WebSocketTransportInterface $user
  */
 public function onDisconnect(WebSocketTransportInterface $user)
 {
     $this->logger->notice("Client skedaddled. ({$user->getId()})");
     $mess = 0;
     foreach ($this->subscriptions['queries'] as $curQuery => &$curClients) {
         foreach ($curClients as $key => $curClient) {
             if ($key === 'current') {
                 continue;
             }
             if ($user->getId() === $curClient['client']->getId()) {
                 unset($curClients[$key]);
                 if (RequirePHP::_('NymphPubSubConfig')['broadcast_counts']) {
                     // Notify clients of the subscription count.
                     $count = count($curClients) - 1;
                     foreach ($curClients as $key => $curCountClient) {
                         if ($key === 'current') {
                             continue;
                         }
                         if ($curCountClient['count']) {
                             $curCountClient['client']->sendString(json_encode(['query' => $curCountClient['query'], 'count' => $count]));
                         }
                     }
                 }
                 if (count($curClients) === 1) {
                     unset($this->subscriptions['queries'][$curQuery]);
                 }
                 $mess++;
             }
         }
     }
     unset($curClients);
     foreach ($this->subscriptions['uids'] as $curUID => &$curClients) {
         foreach ($curClients as $key => $curClient) {
             if ($user->getId() === $curClient['client']->getId()) {
                 unset($curClients[$key]);
                 if (RequirePHP::_('NymphPubSubConfig')['broadcast_counts']) {
                     // Notify clients of the subscription count.
                     $count = count($curClients);
                     foreach ($curClients as $curCountClient) {
                         if ($curCountClient['count']) {
                             $curCountClient['client']->sendString(json_encode(['uid' => $curUID, 'count' => $count]));
                         }
                     }
                 }
                 if (count($curClients) === 0) {
                     unset($this->subscriptions['uids'][$curUID]);
                 }
                 $mess++;
             }
         }
     }
     unset($curClients);
     if ($mess) {
         $this->logger->notice("Cleaned up client's mess. ({$mess}, {$user->getId()})");
     }
 }