예제 #1
0
int main()
{
	dp_result_t err;
	time_t now, t_last, t_start = time(NULL);
	wmq_t *wmq;
	wmq_record_t record;
	char buf[1024];
	char *pbuf;
	unsigned short len;
	time_t data;
	long offset_old = 0;
	time_t t_old = t_start;

	record.buf = buf;
	record.buflen = 1024;

	wmq = wmq_create("wmq", 0);
	assert(wmq);

	now = time(0);
	err = wmq_seek(wmq, now, 0);  /* seek to beginning of today */
	assert(!err);

	while (1) {
		char *pbuf = buf;
		char sessid[8];  /* inet only: inetadr(4) port(2) karma(2) */
		dp_species_t sessType;
		dp_uid_t uid;
		dpid_t id;
		scorerep_t *rep;
		scorerep_buf_t repbuf;
		scorerep_player_t *player;
		int score;
		int won;

		err = wmq_get(wmq, &record);
		assert(err == dp_RES_OK || err == dp_RES_EMPTY);

		if (err != dp_RES_OK)
			continue;
		DPRINT(("wmq2mysql: read %d bytes, time:%08x: %s\n", record.datalen, wmq->t, hexstring(record.buf, record.datalen)));
		if (record.datalen < 12) {
			DPRINT(("wmq2mysql: len %d is not big enough for a header!\n", record.datalen));
			exit(1);
		}
		memcpy(sessid, pbuf, 8);
		pbuf += 8;
		sessType = dpMAKESHORT(pbuf[0], pbuf[1]);
		pbuf += 2;
		repbuf.len = dpMAKESHORT(pbuf[0], pbuf[1]);
		pbuf += 2;
		assert(repbuf.len > 0 && repbuf.len < scorerep_MAX_BUFLEN);
		memcpy(repbuf.buf, pbuf, repbuf.len);

		rep = scorerep_create();
		if (rep == NULL) {
			DPRINT(("wmq2mysql: can't create scorerep\n"));
			exit(1);
		}
		err = scorerep_fromBuf(rep, &repbuf);
		if (err != dp_RES_OK) {
			DPRINT(("wmq2mysql: scorerep_fromBuf returns err:%d\n", err));
			exit(1);
		}
		if (!(rep->flags & scorerep_FLAGS_SELFEXIT)) {
			DPRINT(("wmq2mysql: ignoring non-SELFEXIT score\n"));
			continue;
		}
		uid = rep->uid;
		id = rep->id;
		player = (scorerep_player_t *)assoctab_subscript(rep->players, id);
		if (!player) {
			DPRINT(("wmq2mysql: no entry for self (id:%d) in SELFEXIT!\n", id));
			exit(1);
		}
		assert(uid == player->uid);
		DPRINT(("wmq2mysql: read sessType:%d uid:%d (id:%d)\n", sessType, uid, id));
		if (player->bloblen < 3) {
			DPRINT(("wmq2mysql: report has bloblen:%d, no room for standard score + win byte\n", player->bloblen));
			continue;
		}
		score = dpMAKESHORT(player->blob[0], player->blob[1]);
		won = (int)player->blob[2];
		DPRINT(("wmq2mysql: read score:%d won:%d\n", score, won));

	}
	wmq_destroy(wmq);
	return 0;
}
예제 #2
0
int scorerep_test(void)
{
	dp_result_t err;
	dp_uid_t myUID;
	dpid_t myId;
	dp_uid_t hisUID;
	dpid_t hisId;
	scorerep_t *rep;
	scorerep_buf_t repbuf;
	scorerep_player_t *player;
	unsigned short bloblen;
	char blob[scorerep_MAX_BLOBLEN];

	/* create a rep */
	rep = scorerep_create();
	assert(rep);

	/* set my id and uid */
	myId = 1;
	myUID = (long) myId + 10000;
	err = scorerep_setSelf(rep, myId, myUID);
	assert(!err);
	assert(rep->id == myId);
	assert(rep->uid == myUID);
	
	/* fill my score report with a few blobs */
	for (hisId = myId; hisId < myId + 10; hisId++) {
		hisUID = (long) hisId + 10000;
		blob[0] = dpGETSHORT_FIRSTBYTE(hisId);
		blob[1] = dpGETSHORT_SECONDBYTE(hisId);
		blob[2] = dpGETLONG_FIRSTBYTE(hisUID);
		blob[3] = dpGETLONG_SECONDBYTE(hisUID);
		blob[4] = dpGETLONG_THIRDBYTE(hisUID);
		blob[5] = dpGETLONG_FOURTHBYTE(hisUID);
		bloblen = 6;
		err = scorerep_set(rep, hisId, hisUID, 0, blob, bloblen);
		assert(!err);
	}
	
	/* convert it to a buffer with everyone's scores */
	err = scorerep_toBuf(rep, scorerep_FLAGS_SELFEXIT, myId, &repbuf);
	assert(!err);
	
	/* destroy the rep */
	scorerep_destroy(rep);
	rep = NULL;

	/* create a new rep */
	rep = scorerep_create();
	assert(rep);

	/* set my id and uid */
	myId = 1;
	myUID = (long) myId + 10000;
	err = scorerep_setSelf(rep, myId, myUID);
	assert(!err);
	assert(rep->id == myId);
	assert(rep->uid == myUID);
	
	/* read the buffer into my report table */
	err = scorerep_fromBuf(rep, &repbuf);
	assert(!err);
	assert(rep->players);
	assert(rep->flags == scorerep_FLAGS_SELFEXIT);
	
	/* check that it contains what I put there and only that */
	assert(rep->players->n_used = 10);
	for (hisId = myId; hisId < myId + 10; hisId++) {
		player = (scorerep_player_t *)assoctab_subscript(rep->players, hisId);
		assert(player);
		hisUID = (long) hisId + 10000;
		assert(player->uid = hisUID);
		assert(player->bloblen == 6);
		assert(hisId == dpMAKESHORT(player->blob[0], player->blob[1]));
		assert(hisUID == dpMAKELONG(player->blob[2], player->blob[3], player->blob[4], player->blob[5]));
	}
	
	/* convert it to a buffer with only my and his scores */
	hisId = myId + 1;
	hisUID = (long) hisId + 10000;
	memset(&repbuf, 0, sizeof(repbuf));
	err = scorerep_toBuf(rep, 0, hisId, &repbuf);
	assert(!err);
	
	/* destroy the rep */
	scorerep_destroy(rep);
	rep = NULL;

	/* create a new rep */
	rep = scorerep_create();
	assert(rep);

	/* set my id and uid */
	myId = 1;
	myUID = (long) myId + 10000;
	err = scorerep_setSelf(rep, myId, myUID);
	assert(!err);
	assert(rep->id == myId);
	assert(rep->uid == myUID);

	/* read the buffer into my report table */
	err = scorerep_fromBuf(rep, &repbuf);
	assert(!err);
	assert(rep->players);
	assert(rep->flags == 0);

	/* check that it contains what I put there and only that */
	assert(rep->players->n_used = 2);
	player = (scorerep_player_t *)assoctab_subscript(rep->players, myId);
	assert(player);
	assert(player->uid = myUID);
	assert(player->bloblen == 6);
	assert(myId == dpMAKESHORT(player->blob[0], player->blob[1]));
	assert(myUID == dpMAKELONG(player->blob[2], player->blob[3], player->blob[4], player->blob[5]));

	hisId = myId + 1;
	hisUID = (long) hisId + 10000;
	player = (scorerep_player_t *)assoctab_subscript(rep->players, hisId);
	assert(player);
	assert(player->uid = hisUID);
	assert(player->bloblen == 6);
	assert(hisId == dpMAKESHORT(player->blob[0], player->blob[1]));
	assert(hisUID == dpMAKELONG(player->blob[2], player->blob[3], player->blob[4], player->blob[5]));

	/* destroy the rep */
	scorerep_destroy(rep);
	rep = NULL;
	
	return 0;
}