示例#1
0
	void ServerSession::HandleJoinMessage(Network::Slot clientSlot, const std::string& name)
	{
		bool playerValid = true;
		RefuseReason::RefuseReason reason;

		// Find an open slot
		PlayerID openPlayerID = GetPlayerSlot(C_STATUS_OPEN);
		if (openPlayerID == C_PLAYER_NONE)
		{
			// There are no open slots
			playerValid = false;
			reason = RefuseReason::TooManyPlayers;
		}
		
		// If an open slot is found, check the player's name
		if (playerValid)
		{
			for (PlayerID i = 0; i < mPlayers.size(); ++i)
			{
				if (mPlayers[i] != NULL && mPlayers[i]->GetName() == name)
				{
					playerValid = false;
					reason = RefuseReason::InvalidName;
					break;
				}
			}
		}		

		// If the player is valid, add them to the open slot
		if (playerValid)
		{
			mServer->Send(clientSlot, AcceptMessage(mPlayers.size(), openPlayerID));
			AddPlayer(openPlayerID, clientSlot, name, 0);
		}
		else
		{
			// Refuse the player
			mClientsToRemove.push_back(clientSlot);
			mServer->Send(clientSlot, RefuseMessage(reason));
		}

		std::vector<ClientSlot>::iterator it = std::find(mPendingClients.begin(), mPendingClients.end(), clientSlot);
		if (it != mPendingClients.end())
			mPendingClients.erase(it);
	}
void EditorFileList::AcceptMessage(const Message &message)
{
#ifdef ENGINE_TARGET_WIN32
	if (message.is("KeyPress"))
	{
		int data = utils::lexical_cast<int>(message.getData());
		char key = data;

		if (key == -VK_LEFT)
		{
			if (_cursorPos > 0)
			{
				_cursorPos--;
			}
			_cursorTime = 0.f;
		} else if (key == -VK_RIGHT)
		{
			if (_cursorPos < static_cast<int>(_fileName.length()))
			{
				_cursorPos++;
			}
			_cursorTime = 0.f;
		} else if (key == -VK_UP)
		{
			_cursorPos = static_cast<int>(_fileName.length());
			_cursorTime = 0.f;
		} else if (key == -VK_DOWN)
		{
			_cursorPos = 0;
			_cursorTime = 0.f;
		} else if (data > 0 && key > ' ' && _fileName.length() < 20)
		{
			_fileName.insert(static_cast<size_t>(_cursorPos), 1, key);
			_cursorPos++;
			_cursorTime = 0.f;
		} else if (key == 8)
		{
			if (_cursorPos > 0)
			{
				_cursorPos--;
				_fileName.erase(static_cast<size_t>(_cursorPos), 1);
			}
		}
	} else
#endif
	if (message.is("Init"))
	{
		_names.clear();
		StringVector vec = File::DirectoryListing::Get("Levels/*.*");
		size_t count = vec.size();
		_names.reserve(count);
		int fnd0;
		int fnd1;
		for(size_t i=0;i<count;i++){
			fnd0 = vec[i].find("/");
			fnd1 = vec[i].find(".xml");
			if( fnd0>0 && fnd1 > 0){
				_names.push_back( vec[i].substr(fnd0+1,fnd1-fnd0-1));
			}
		}
	/*} else if (message.is("SetDestLayer"))
	{
		_destLayer = message.getData();
	} else if (message.is("SetDestWidget"))
	{
		_destWidget = message.getData();*/
	} else if (message.is("SetLevelName"))
	{
		_fileName = message.getData();
		_cursorPos = (int) _fileName.length();
	} else if (message.is("AddItem"))
	{
		_names.push_back(message.getData());
	} else if (message.is("Load")){
		if (_currentName >= 0)
		{
			_fileName = _names[_currentName];
			_cursorPos = (int) _fileName.length();

			GUI::Widget *target = Core::guiManager.getLayer(_destLayer) -> getWidget(_destWidget);
			target -> AcceptMessage(Message("LoadLevelForEdit", _fileName));
			Core::messageManager.putMessage(Message("Return", "press"));
		}
	} else if (message.is("Save"))
	{
		bool f = false;
		for (size_t i = 0; i<_names.size(); i++)
		{
			if (_fileName == _names[i])
			{
				f = true;
			}
		}
		if (f)
		{
			Core::guiManager.getLayer("ConfirmRewriteLevel")->getWidget("ConfirmText")->
				AcceptMessage(Message("SetString", "Do you really want to rewrite " + _fileName + "?"));
			Core::messageManager.putMessage(Message("ShowConfirmRewrite"));
		} else {
			Core::guiManager.getLayer(_destLayer)->getWidget(_destWidget)->AcceptMessage(Message("SaveLevel", _fileName));
			Core::messageManager.putMessage(Message("Return", "press"));

		}
	} else if (message.is("SaveAlways"))
	{
		Core::guiManager.getLayer(_destLayer)->getWidget(_destWidget)->AcceptMessage(Message("SaveLevel", _fileName));
		Core::messageManager.putMessage(Message("Return", "press"));
	} else if (message.is("Delete"))
	{
		size_t i = static_cast<size_t>(_currentName);
		if(i >= _names.size())
		{
			return;
		}
		std::string filename = _names[i];
		if (filename == _fileName)
		{
			Log::log.WriteInfo("Вы не можете удалить текущий загруженый уровень!");
			return;
		}
		std::string full_name = "Levels/" + filename + ".xml";
		if(File::Exists(full_name)){
			File::rm(full_name);
			AcceptMessage(Message("Init"));
		}
	} else if (message.is("ResaveAll")) {  //открыть и пересохранить все файлы

		GUI::Widget *target = Core::guiManager.getLayer(_destLayer) -> getWidget(_destWidget);

		for (auto filename: _names)
		{
			target -> AcceptMessage(Message("LoadLevelForEdit", filename));
			target -> AcceptMessage(Message("SaveLevel", filename));
		}
		//target -> AcceptMessage(Message("LoadLevelForEdit", _fileName));

	}
}