示例#1
0
void		Manager::handlePaquet(const PaquetReady_SharedPtr &&paquet, const Addr &addr)
{
  DEBUG_MSG(*paquet);

  size_t	size = 0;
  PaquetLaunch	p;

  uint8_t	id = paquet->getID();
  auto &&party = _parties.findIn([&addr](auto &p) { return (p->isPlayer(addr)); });

  if (party) {

    party->setReady(id, paquet->getStatus());
    auto &players = party->getPlayers();

    broadcast_except(players, id, *paquet);
    size = players.count_if([] (auto &p) { return (p->getReady()); });

    if (players.size() == size) {
      party->setRunning(true);
      broadcast(players, p);
    }
  }
  else {
#ifdef DEBUG
    if (!party) {
      std::cerr << "Ready: Can't find party" << std::endl;
    }
#endif // !DEBUG
  }
}
示例#2
0
void		Manager::handlePaquet(const PaquetPlayerCoord_SharedPtr &&paquet)
{
  uint8_t	id = paquet->getPlayerID();

  DEBUG_MSG(*paquet);

  auto &&party = _parties.findIn([id] (auto &p) { return (p->isPlayer(id)); });
  if (party) {

    party->setCoordPlayer(paquet->getPlayerID(), paquet->getX(), paquet->getY());

    auto &&players = party->getPlayers();
    auto &&player = players.findIn([id] (auto &p) { return (p->getID() == id); });

    if (player) {
      PaquetPlayerCoord paquet(player);
      broadcast_except(players, id, paquet);
    }
  }
#ifdef DEBUG
  else {
    if (!party) {
      std::cerr << "Can't find party" << std::endl;
    }
  }
#endif // !DEBUG
}
示例#3
0
void connection_send_raw(uint8_t channel, const char *data, size_t size) {
    ENetPacket *packet = enet_packet_create(data, size, 0);
    if (connection_is_host()) {
        broadcast_except(NULL, packet, channel);
    } else if (server) {
        enet_peer_send(server, channel, packet);
    }
}
示例#4
0
void connection_update() {
    if (!host) return;

    ENetEvent ev;
    while (enet_host_service(host, &ev, 0)) {
        switch (ev.type) {
        case ENET_EVENT_TYPE_CONNECT: {
            if (connection_is_host()) {
                uint32_t new_sender_id = list_push(peers, ev.peer) + 1;

                connection_send_to(
                    USER_ID_ASSIGN_PACKET,
                    CHANNEL_COUNT - 1,
                    (const char *)&new_sender_id,
                    sizeof(new_sender_id),
                    ev.peer
                );
            }
            break;
        }
        case ENET_EVENT_TYPE_DISCONNECT:
            if (connection_is_host()) {
                size_t i;
                if (list_index_of(peers, ev.peer, &i)) {
                    list_remove(peers, i);
                }
            }
            break;
        case ENET_EVENT_TYPE_RECEIVE:
            if (connection_is_host()) {
                broadcast_except(ev.peer, ev.packet, ev.channelID);
            }

            if (ev.channelID > 0) {
                uint32_t sender_id, id;
                char *data = NULL;
                size_t data_len;
                unpack_format((char *)ev.packet->data, "uua", &sender_id, &id, &data, &data_len);

                ALLEGRO_EVENT event;
                event.type = CONNECTION_RECEIVE_EVENT_ID;
                event.user.data1 = id;
                event.user.data2 = (intptr_t)data;
                event.user.data3 = sender_id;
                al_emit_user_event(&event_source, &event, NULL);
            } else {
                //channel 0 is exclusive for entity updates
                entity_sync((const char *)ev.packet->data);
            }
            break;
        case ENET_EVENT_TYPE_NONE:
            break;
        }
    }
}
示例#5
0
void connection_send_to(uint32_t id, uint8_t channel, const char *data, size_t size, ENetPeer* target) {
    ENetPacket *packet = enet_packet_create(NULL, sizeof(user_id) +sizeof(id) + sizeof(uint32_t) + size, 0);
    pack_format((char *)packet->data, "uua", user_id, id, data, size);

    if (target) {
        enet_peer_send(target, channel, packet);
        return;
    }

    if (connection_is_host()) {
        broadcast_except(NULL, packet, channel);
    } else if (server) {
        enet_peer_send(server, channel, packet);
    }
}