Пример #1
0
/**
 * Method handling the response to an invitation
 * @param JSON::Dict : content of the invitation
 * @param int : id of the user who sent the invitation 
 */
void GameManager::sendInvitationResponseToPlayer(const JSON::Dict &response, int peer_id){
	string username;
	if (ISSTR(response.get("username"))) 
		username = STR(response.get("username")).value();
	string answer;
	if (ISSTR(response.get("answer")))
		answer = STR(response.get("answer")).value();
	map<int, User*>::iterator findSender = _users.find(peer_id);
	for (map<int, User*>::iterator it=_users.begin(); it!=_users.end(); it++){
		if (it->second->getUsername() == username){
			JSON::Dict toSend;
			toSend.set("type", MSG::FRIENDLY_GAME_INVITATION_RESPONSE);
			JSON::Dict data;
			data.set("username", findSender->second->getUsername());
			data.set("answer", answer);
			toSend.set("data", data);
			Message status(it->first, toSend.clone());
			_outbox.push(status);
			if (answer == MSG::FRIENDLY_GAME_INVITATION_ACCEPT){
				startMatch(peer_id, it->first, false);
			}
		}
	}
}
Пример #2
0
/* os_strings: List the strings of a binary and
 * check if the regex given is there.
 */
int os_string(char *file, char *regex)
{
    int ch, cnt;
    
    unsigned char *C;
    unsigned char *bfr;
 
    char line[OS_SIZE_1024 +1];
    char *buf;
    
    EXEC *head;

    os_strings oss;
   
    /* Return didn't match */
    if(!file || !regex)
    {
        return(0);
    }
    
    
    /* Allocating for the buffer */ 
    bfr = calloc(STR_MINLEN + 2, sizeof(char *));
    if (!bfr)
    {
        merror(MEM_ERROR, ARGV0);
        return(0);
    }

    /* Opening the file */
    oss.fp = fopen(file, "r");
    if(!oss.fp)
    {
        free(bfr);
        return(0);
    }

    /* cleaning the line */
    memset(line, '\0', OS_SIZE_1024 +1);
    
    /* starting .. (from old strings.c) */
    oss.foff = 0;
    oss.head_len = 0;
    
    oss.read_len = -1;
    head = (EXEC *)oss.hbfr;

    
    if ((oss.head_len = read(fileno(oss.fp), head, sizeof(EXEC))) == -1)
    {
        oss.head_len = 0;
        oss.read_len = -1;
    }
    else if (oss.head_len == sizeof(EXEC) && !N_BADMAG(*head)) 
    {
        oss.foff = N_TXTOFF(*head);
        if (fseek(stdin, oss.foff, SEEK_SET) == -1)
        {
            oss.read_len = -1;
        }
        else
        {
            #ifdef AIX
            oss.read_len = head->tsize + head->dsize;
            #else
            oss.read_len = head->a_text + head->a_data;
            #endif
        }

        oss.head_len = 0;
    }
    else
    {
        oss.hcnt = 0;
    }

    /* Read the file and perform the regex comparison */
    for (cnt = 0; (ch = os_getch(&oss)) != EOF;) 
    {
        if (ISSTR(ch)) 
        {
            if (!cnt)
                C = bfr;
            *C++ = ch;
            if (++cnt < STR_MINLEN)
                continue;
            
            strncpy(line, (char *)bfr, STR_MINLEN +1);
            buf = line;
            buf+=strlen(line);
            

            while ((ch = os_getch(&oss)) != EOF && ISSTR(ch))
            {
                if(cnt < OS_SIZE_1024)
                {
                    *buf = (char)ch;
                    buf++;
                }
                else
                {
                    *buf = '\0';
                    break;
                }
		cnt++;
            }

            *buf = '\0';

            if(OS_PRegex(line, regex))
            {
                if(oss.fp)
                    fclose(oss.fp);
                free(bfr);
                return(1);
            }
        }

        cnt = 0;
    }

    if(oss.fp)
        fclose(oss.fp);
    free(bfr);
    return(0);
}
void ChampionshipController::treatMessage(std::string const & type, JSON::Value const * data)
{
	if (type == net::MSG::JOIN_CHAMPIONSHIP)
	{
		if(ISSTR(data)){
			std::string response = STR(data).value();
			std::string message;
			bool ok=false;

			if(response == net::MSG::ALREADY_IN_CHAMPIONSHIP){
				message =  "Cannot take part in more than one championship at the same time.";
				ok = false;
			}
			else if(response == net::MSG::CHAMPIONSHIP_FULL){
				message = "The championship you tried to join is now full.";
				ok = false;
			}
			else if(response == net::MSG::CHAMPIONSHIP_NOT_FOUND){
				message = "Championship not found.";
				ok = false;
			}
			else if(response == net::MSG::ADDED_TO_CHAMPIONSHIP){
				message = "Added to championship.";
				ok = true;
			}
			this->onJoinChampionship(ok, message);
		}
	}
	else if (type == net::MSG::JOINABLE_CHAMPIONSHIPS_LIST)
	{
		JSON::List & champs = LIST(data);
		_champs.clear();
		for(size_t i = 0; i<champs.len();++i){
			_champs.push_back(Championship(DICT(champs[i])));
		}
		this->onChampionshipsLoad();
	}
	else if(type == net::MSG::LEAVE_CHAMPIONSHIP)
	{
		if(ISSTR(data)){
			std::string response = STR(data).value();
			std::string message;

			bool ok = false;

			if(response == net::MSG::CHAMPIONSHIP_STARTED){
				message = "Your championship started; cannot leave it.";
				ok = false;
			}
			else if(response == net::MSG::NOT_IN_CHAMPIONSHIP){
				message = "You are currently not taking part in any championship.";
				ok = false;
			}
			else if(response == net::MSG::REMOVED_FROM_CHAMPIONSHIP){
				message = "Removed from championship.";
				user().joinedChamp = Championship();
				ok = true;
			}
			this->onLeaveChampionship(ok,message);
		}
	}
	else if(type == net::MSG::JOINED_CHAMPIONSHIP)
	{
		user().joinedChamp = Championship();
		if(ISSTR(data)){
			if(STR(data).value() == net::MSG::CHAMPIONSHIP_NOT_FOUND){}
		}
		else if(ISDICT(data)){
			user().joinedChamp = DICT(data);
		}
		onJoinedChampionship();
	}
}