示例#1
0
Controller::Controller(
#ifdef ANDROID_NDK
					   AndroidAssetManager *mgr,
#endif
					   Controller::NetworkMode mode,
					   unsigned flags,
					   const char* aFile,
					   const char *address,
					   const char *port)
{
	// Platform-specific initalisation
#if defined(ANDROID_NDK)
	// Save asset manager
	assetManager = mgr;
#elif defined(_WIN32)
	// Start WinSock library for networking
	WSADATA wsa;
	WSAStartup(MAKEWORD(2, 0), &wsa);
#endif
	startTimeCount();

	networkInterface = NULL;
	
	touchRecognizer = new TouchesRecognizer(this);
	robotTouchHandler = NULL;
	
	upArrow = false;
	downArrow = false;
	leftArrow = false;
	rightArrow = false;
	aDown = false;
	dDown = false;
	sDown = false;
	wDown = false;
	xDown = false;
	cDown = false;
	wheelScrollDirection = 0.0f;
	
	if (aFile)
	{
		filename = new char[strlen(aFile)+1];
		strncpy(filename, aFile, strlen(aFile)+1);
		try
		{
			executionContext = new ExecutionContext(filename);
		}
		catch (std::runtime_error &e)
		{
			ShowErrorAndExit(L"Fehler beim \u00D6ffnen der Bytecode-Datei %s: \"%s\"", L"Error opening bytecode file %s: \"%s\"", filename, e.what());
		}
	}
	else
	{
		filename = NULL;
		executionContext = NULL;
	}
	// environment properties
	environment = new Environment(25, 25, 1.0f, 0.75f);
	simulation = new Simulation(environment);
	editor = new EnvironmentEditor(environment);
	editor->setMode(EnvironmentEditor::None);
	drawer = 0;
	soundController = 0;
	userinterface = 0;
	
	unsigned dataSize;
	char *data = (char *) getDataAndSizeForUserInterfaceKey("environment", dataSize);
	if (data)
		editor->loadFromSerialization((const NetworkPacket *) data);

	
	if (mode == SingleMode)
	{
		startSingle();
		serverBrowser = 0;
	}
	else if (mode == ClientMode)
	{
		if (!address) throw std::invalid_argument("No adress specified for client to connect to.");
		if (!port) port = "10412";
		// Get the address
		struct addrinfo *result;
		if (getaddrinfo(address, port, NULL, &result) != 0)
			throw std::runtime_error("Could not resolve host name.");
		
		for (struct addrinfo *res = result; res != 0; res = res->ai_next)
		{
			if (res->ai_family != AF_INET && res->ai_family != AF_INET6)
				continue; // Maybe other protocols could work, but that seems
			// like too much work.
			
			startAsClient(res->ai_addr, res->ai_family == AF_INET6, flags & ClientFlagUIOnServer);
			serverBrowser = 0;
			break;
		}
		if (!networkInterface) throw std::runtime_error("Not an IPv4 or IPv6 address.");
	}
	else if (mode == ServerMode)
	{
		unsigned short portForServer = networkPortNumber;
		if (port) portForServer = (uint16_t) strtoul("port", NULL, 10);
		startAsServer(portForServer, !(flags & ServerFlagNoBroadcast));
		serverBrowser = 0;
	}
	else // Includes LetUserChoose
		serverBrowser = new ServerBrowser(networkPortNumber);
}
示例#2
0
void CPlayerClientMe::needSelect(const QString &question,int selectionType,const QList<QVariant> values,
                                 const QList<QVariant> &defaultResult,int min,int max)
{
    CEvent *ev=new CEvent(game);
    ev->addFunc([](){});
    auto f=std::bind([&](const QString question, int selectionType, const QList<QVariant> values,
                     const QList<QVariant> &defaultResult,int min,int max){
        QByteArray ba=GAME->serverData.takeFirst();
        COMPARE(ba[0]!=GAME_OPERATION);
        opSelect=COperation::serverRequest(ba);
        COMPARE(!opSelect);
        COMPARE(opSelect->type!=OPERATION_SELECT);
        switch (selectionType) {
        case SELECTTYPE_YESNO:

            break;
        case SELECTTYPE_WUXIEKEJI:
            break;
        case SELECTTYPE_PLAYERCARD:
        {
            if(!w->mwCLient->dialogPlayerCard)
            {
                w->mwCLient->dialogPlayerCard=new DialogPlayerCard(w->mwCLient);
                connect(w->mwCLient->dialogPlayerCard,DialogPlayerCard::selected,this,this->handleSelected);
            }
            int i=values[0].toInt();
            CPlayer *target=game->players[i];
            w->mwCLient->dialogPlayerCard->init(target);
            startTimeCount(w->client->operationTimeout,true);
            game->smartInsertFunc([&](){
                w->mwCLient->dialogPlayerCard->finished=true;
                w->mwCLient->dialogPlayerCard->hide();
                emit GAME->newData();
            });
        }
            break;
        case SELECTTYPE_CUSTOM:
        {
            if(!w->mwCLient->dialogSelect)
            {
                w->mwCLient->dialogSelect=new DialogSelect(w->mwCLient);
                connect(w->mwCLient->dialogSelect,DialogSelect::select,this,this->handleSelected);
            }
            QVariant var;
            QStringList option;
            foreach (var, values) {
                option.append(var.toString());
            }
            w->mwCLient->dialogSelect->init(question,option);
            this->startTimeCount(w->client->operationTimeout,true);
            game->smartInsertFunc([&](){
                w->mwCLient->dialogSelect->selected=true;
                w->mwCLient->dialogSelect->hide();
                emit GAME->newData();
            });
        }
            break;
        default:
            break;
        }
    },question,selectionType,values,defaultResult,min,max);