Exemple #1
0
			/**
			* This function will write into json file
			*/
			void JSONReader::update(entityx::ptr<entityx::EntityManager> es, entityx::ptr<entityx::EventManager> events, double dt)
			{
				entityx::ptr<Comp::File> file;
				entityx::ptr<Comp::Read> read;
				for (auto entity : es->entities_with_components(file, read))
				{
					rapidjson::Document doc;

					bool fileread = file->read();

					//we try to read
					if (fileread && file->getContents() != "")
					{
						doc.Parse<0>(file->getContents().c_str());
						if (doc.HasParseError()) {
							CCLOG("GetParseError %s\n", doc.GetParseError());
							//signal error
							events->emit<Events::Error>(entity, "JSONReader system file read error");
							//WHAT TO DO ?
						}
						//else everything is ok with this json file
					}
					else // read fail : lets create first content
					{
						doc.SetObject();
						rapidjson::StringBuffer strbuf;
						rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
						doc.Accept(writer);
						file->setContents( strbuf.GetString());
						//CCLOG("new contents :  %s", file->getContents().c_str());
					}

					entity.remove<Comp::Read>();
				}
			}
Exemple #2
0
			/**
			* This function will call the callback with the current progress of the timer.
			*/
			void AlarmRinger::update(entityx::ptr<entityx::EntityManager> es, entityx::ptr<entityx::EventManager> events, double dt)
			{
				entityx::ptr<Comp::ID> id;
				entityx::ptr<Comp::Alarm> alarm;
				for (auto entity : es->entities_with_components(id, alarm))
				{
					time_t start = mktime(&alarm->m_end);
					time_t nowtime = mktime(&alarm->m_now);
					// actually returns long long, despite double declaration
					double delta = difftime(start, nowtime); 
					if (delta < 0) // becomes true when delta is -1, this adds 1 sec to alarm length
					{
						events->emit<Events::AlarmOff>(entity);
					}
				}
			};
Exemple #3
0
			void Timer::update(entityx::ptr<entityx::EntityManager> entities, entityx::ptr<entityx::EventManager> events, double dt)
			{

				entityx::ptr<Comp::ServerTime> st;
				for (auto entity : entities->entities_with_components(st))
				{
					if (st->done)
					{
						entity.remove<Comp::ServerTime>();
						//if mask at 0 no request in this entity anymore
						if (entity.component_mask() == 0)
							entity.destroy();
					}
					else if (!st->in_progress)
					{
						m_timer_service->GetCurrentTimeNoWinBase(st->m_cb);
						st->in_progress = true;
					}
				}

			}
			/**
			* This function will write into json file
			*/
			void JSONPlayerData::update(entityx::ptr<entityx::EntityManager> es, entityx::ptr<entityx::EventManager> events, double dt)
			{
				entityx::ptr<Comp::File> file;
				entityx::ptr<Comp::PlayerData_v1> playerData;
				for (auto entity : es->entities_with_components(file, playerData))
				{
					if (!entity.component<Comp::Read>())
					{
						rapidjson::Document doc;
						doc.Parse<0>(file->getContents().c_str());
						if (doc.HasParseError()) {
							CCLOG("GetParseError %s\n", doc.GetParseError());
							//callback empty on invalid json
							if (playerData->m_load_cb)
							{
								playerData->m_load_cb("");
							}
						}
						else if (playerData->m_load_cb) //if we want to load data
						{
							if (doc.HasMember(playerData->m_name.c_str()))
							{
								rapidjson::Value& playerdatavalue = doc[playerData->m_name.c_str()];
								//writing json structure in a string
								rapidjson::StringBuffer strbuf;
								rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
								playerdatavalue.Accept(writer);
								//passing the string to the callback
								playerData->m_data = strbuf.GetString();
								playerData->m_load_cb(playerData->m_data);

							}
							else
							{
								playerData->m_load_cb("");
							}
						}
						else //write request
						{
							//clearing original saved data
							doc.SetObject();

							rapidjson::Document data;
							data.Parse<0>(playerData->m_data.c_str());
							if (data.HasParseError()) {
								CCLOG("GetParseError %s\n", data.GetParseError());
								//signal error
								events->emit<Events::Error>(entity, "JSONPlayerData system parse error");
								//WHAT TO DO ?
							}
							else
							{
								rapidjson::Value vholder;
								vholder.SetObject();
								for (auto dm = data.MemberonBegin(); dm != data.MemberonEnd(); ++dm)
								{
									vholder.AddMember(dm->name, dm->value, doc.GetAllocator());
								}
								doc.AddMember(playerData->m_name.c_str(), vholder, doc.GetAllocator());

								//TMP debug
								rapidjson::StringBuffer strbuf;
								rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
								doc.Accept(writer);

								file->setContents( strbuf.GetString());
								//CCLOG("new contents :  %s", file->getContents().c_str());
							}
						}

						//we finished working with this component.
						entity.remove<Comp::PlayerData_v1>();
					}
				}
			};
Exemple #5
0
			/**
			* This function will write into json file
			*/
			void JSONLoginID::update(entityx::ptr<entityx::EntityManager> es, entityx::ptr<entityx::EventManager> events, double dt)
			{
				entityx::ptr<Comp::File> file;
				entityx::ptr<Comp::LoginID_v1> loginid;
				for (auto entity : es->entities_with_components(file, loginid))
				{
					if (!entity.component<Comp::Read>())
					{
						rapidjson::Document doc;
						doc.Parse<0>(file->getContents().c_str());
						
						if (loginid->m_load_cb)//if we want to load data
						{
							if (doc.HasParseError()) {
								CCLOG("GetParseError %s\n", doc.GetParseError());
								events->emit<Events::Error>(entity, "JSONLogin system parse error");
								loginid->m_load_cb("","");
							}
							else
							{
								rapidjson::Value& loginvalue = doc[loginid->m_name.c_str()];
								if (!loginvalue.IsNull()){
									if (loginvalue.HasMember("user"))
									{
										loginid->m_user = loginvalue["user"].GetString();
									}
									if (loginvalue.HasMember("passwd"))
									{
										loginid->m_passwd = loginvalue["passwd"].GetString();
									}
								}
								loginid->m_load_cb(loginid->m_user, loginid->m_passwd);
							}
						}
						else //write request
						{
							// must pass an allocator when the object may need to allocate memory
							rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();

							if (!doc.HasParseError() && doc.HasMember(loginid->m_name.c_str()))
							{
								//if we already have a login value in the file, we need to replace it
								rapidjson::Value& vholder = doc[loginid->m_name.c_str()];
								if (vholder.HasMember("user"))
								{
									vholder["user"] = loginid->m_user.c_str();
								}
								if (vholder.HasMember("passwd"))
								{
									vholder["passwd"] = loginid->m_passwd.c_str();
								}
							}
							else
							{
								if (doc.HasParseError())
								{
									//CCLOG("GetParseError %s\n", doc.GetParseError());
									//no big deal we replace everything.
									doc.SetObject();
								}
								rapidjson::Value vholder;
								vholder.SetObject();
								vholder.AddMember("user", loginid->m_user.c_str(), allocator);
								vholder.AddMember("passwd", loginid->m_passwd.c_str(), allocator);
								doc.AddMember(loginid->m_name.c_str(), vholder, allocator);
							}
							
							//TMP debug
							rapidjson::StringBuffer strbuf;
							rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
							doc.Accept(writer);

							file->setContents(strbuf.GetString());
							//CCLOG("new contents :  %s", file->getContents().c_str());
						}

						//we finished working with this component.
						entity.remove<Comp::LoginID_v1>();
					}
				}
			};