Esempio n. 1
0
void Player::update(Server& s)
{
	while(socket->bytesAvailable()) {
		QDataStream is(socket);
		if (packetSize<0) {
			if (socket->bytesAvailable()<4) break;
			is>>packetSize;
		}
		if (socket->bytesAvailable()<packetSize) break;
		packetSize=-1;
		quint8 type;
		is>>type;
		switch(type) {
			case MSG_STATE:
				readState(is);
				break;
			case MSG_SHOOT:
				readShoot(is, s);
				break;
			case MSG_INFO:
				readInfo(is);
				break;
		    case MSG_CHAT:
		        readChat(is);
		        break;
			default:
				qDebug()<<type;
				abort();
		}
	}

	updatePhysics(s);
}
Esempio n. 2
0
void Connection::update()
{
	while(bytesAvailable()) {
		QDataStream s(this);
		if (packetSize<0) {
			if (bytesAvailable()<4) break;
			s>>packetSize;
//			if (packetSize>1<<20) //qDebug()<<"big packet"<<packetSize;
		}
		if (bytesAvailable()<packetSize) break;
//		//qDebug()<<"packet size"<<packetSize;
		packetSize=-1;
		quint8 type;
		s>>type;
//		//qDebug()<<"available: "<<type;
		switch(type) {
			case MSG_INITIAL:
				readInitial(s);
				break;
			case MSG_STATE:
				readState(s);
				break;
			case MSG_SHOOT:
				readShoot(s);
				break;
			case MSG_HIT:
				readHit(s);
				break;
			case MSG_ENEMY:
			    readEnemy(s);
			    break;
			case MSG_ITEM:
			    readItem(s);
			    break;
			case MSG_GET:
				readGet(s);
				break;
			case MSG_DIE:
				for(int i=0; i<engine.bulletCounts.size(); ++i) engine.bulletCounts[i]=0;
				player->weapon = 1;
				break;
			case MSG_LIGHTNING:
				readLightning(s);
				break;
			case MSG_STATS:
				readStats(s);
				break;
			case MSG_CHAT:
			    readChat(s);
			    break;
			default:
				//qDebug()<<type;
				abort();
		}
		++packetCount;
	}
}
Esempio n. 3
0
MessagesDialogs TelegramCache::readDialogs() const
{
    MessagesDialogs result(MessagesDialogs::typeMessagesDialogs);

    const QString filePath = p->path + "/dialogs";
    const QList<QVariant> &list = readList(filePath);
    QList<Dialog> dialogs;

    QHash<QByteArray, Chat> chats;
    QHash<QByteArray, User> users;
    QHash<QByteArray, Message> messages;

    Q_FOREACH(const QVariant &var, list)
    {
        const Dialog &dialog = Dialog::fromMap( var.toMap() );

        const Peer &peer = dialog.peer();
        const QByteArray &key = TelegramTools::identifier(peer);
        switch(static_cast<int>(peer.classType()))
        {
        case Peer::typePeerChannel:
        case Peer::typePeerChat:
            if(!chats.contains(key))
                chats[key] = readChat(peer);
            break;
        case Peer::typePeerUser:
            if(!users.contains(key))
                users[key] = readUser(peer);
            break;
        }

        if(dialog.topMessage())
        {
            QByteArray topMsgKey = TelegramTools::identifier(dialog.peer(), dialog.topMessage());
            const QString &messageFolder = getMessageFolder(peer);
            const QString messageFile = messageFolder + "/" + QString::number(dialog.topMessage());
            messages[topMsgKey] = Message::fromMap( readMap(messageFile) );
        }

        dialogs << dialog;
    }

    result.setDialogs(dialogs);
    result.setChats(chats.values());
    result.setUsers(users.values());
    result.setMessages(messages.values());
    result.setCount(dialogs.count());

    return result;
}
Esempio n. 4
0
Chat TelegramCache::readChat(const InputPeer &peer) const
{
    return readChat(TelegramTools::inputPeerPeer(peer));
}
Esempio n. 5
0
MessagesMessages TelegramCache::readMessages(const Peer &peer, int offset, int limit) const
{
    MessagesMessages result(MessagesMessages::typeMessagesMessages);

    const QString folderPath = getMessageFolder(peer);
    QStringList files = QDir(folderPath).entryList(QDir::Files);
    qStableSort(files.begin(), files.end(), fileListDeSort);

    files = files.mid(offset, limit);

    QHash<QByteArray, Chat> chats;
    QHash<QByteArray, User> users;

    QList<Message> messages;
    Q_FOREACH(const QString &f, files)
    {
        const QString path = folderPath + "/" + f;
        const QMap<QString, QVariant> &map = readMap(path);
        if(map.isEmpty())
            continue;

        const Message &msg = Message::fromMap(map);
        const Peer &toPeer = msg.toId();
        const QByteArray &toKey = TelegramTools::identifier(toPeer);
        switch(static_cast<int>(toPeer.classType()))
        {
        case Peer::typePeerChannel:
        case Peer::typePeerChat:
            if(!chats.contains(toKey))
                chats[toKey] = readChat(toPeer);
            break;
        case Peer::typePeerUser:
            if(!users.contains(toKey))
                users[toKey] = readUser(toPeer);
            break;
        }

        if(msg.fromId())
        {
            Peer fromPeer(Peer::typePeerUser);
            fromPeer.setUserId(msg.fromId());
            const QByteArray &fromKey = TelegramTools::identifier(fromPeer);
            switch(static_cast<int>(fromPeer.classType()))
            {
            case Peer::typePeerChannel:
            case Peer::typePeerChat:
                if(!chats.contains(fromKey))
                    chats[fromKey] = readChat(fromPeer);
                break;
            case Peer::typePeerUser:
                if(!users.contains(fromKey))
                    users[fromKey] = readUser(fromPeer);
                break;
            }
        }

        messages << msg;
    }

    result.setMessages(messages);
    result.setChats(chats.values());
    result.setUsers(users.values());
    result.setCount(messages.count());

    return result;
}