Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
8.9 kB
3
Indexable
Never
void save_player(Player* p_, bool on_exit = true) {
	if (p_->tankIDName.empty() or p_->discord.empty()) return;
	if (saving_ and p_->saved_on_close) {
		cout << "already saved" << endl;
		return;
	}
	//saveall
	struct tm newtime;
	time_t now = time(0);
	localtime_s(&newtime, &now);
	p_->lo = "" + to_string(newtime.tm_mon + 1) + "/" + to_string(today_day) + "/2022 " + to_string(newtime.tm_hour) + ":" + (newtime.tm_min < 10 ? "0" + to_string(newtime.tm_min) + "" : "" + to_string(newtime.tm_min)) + ":" + to_string(newtime.tm_sec) + "";
	json save_, achievements_ = json::array(), friends_ = json::array(), inv_ = json::array(), visited_worlds_ = json::array(), worlds_owned_ = json::array(), playmods_ = json::array();
	save_["name"] = p_->tankIDName;
	save_["n"] = p_->n;
	save_["adminLevel"] = p_->adminLevel;
	save_["m_h"] = p_->m_h;
	save_["cc"] = p_->cc;
	save_["isNew"] = p_->isNew;
	save_["breakPoint"] = p_->breakPoint;
	save_["bans"] = p_->bans;
	save_["discord_code"] = p_->discord_code;
	save_["promo"] = p_->promo;
	save_["license"] = p_->license;
	save_["clist"] = p_->clist;
	save_["w_w"] = p_->w_w;
	save_["w_d"] = p_->w_d;
	save_["mer"] = p_->mercy;
	save_["drtt"] = p_->drtitle;
	save_["_drt"] = p_->drt;
	save_["dnr"] = p_->donor;
	save_["mst"] = p_->master;
	save_["lvl125"] = p_->lvl125;
	save_["radio"] = p_->radio;
	save_["fa_ip"] = p_->fa_ip;
	save_["2fa"] = p_->fa2;
	save_["modname"] = p_->modName;
	save_["autofarm"] = p_->autofarm;
	save_["find_item_collect"] = p_->find_item_collect;
	save_["flagset"] = p_->flagset;
	save_["skl"] = p_->surgery_skill;
	save_["guild_id"] = p_->guild_id;
	save_["sgt"] = p_->surgery_type;
	save_["sd"] = p_->surgery_done;
	save_["su1"] = p_->su_8552_1;
	save_["su2"] = p_->su_8552_2;
	save_["vipused_potion"] = p_->vipused_potion;
	save_["egg"] = p_->egg;
	save_["glo"] = p_->glo;
	//save_["logs"] = p_->logs;
	save_["b_t"] = p_->b_t;
	save_["b_b"] = p_->b_b;
	save_["dd"] = p_->dd;
	save_["lo"] = p_->lo;
	save_["ip"] = p_->ip;
	save_["b_s"] = p_->b_s;
	save_["b_r"] = p_->b_r;
	save_["b_ra"] = p_->b_ra;
	save_["i_11818_1"] = p_->i_11818_1;
	save_["i_11818_2"] = p_->i_11818_2;
	save_["b_lvl"] = p_->b_lvl;
	save_["b_i"] = p_->b_i;
	save_["b_a"] = p_->b_a;
	save_["b_w"] = p_->b_w;
	save_["b_p"] = p_->b_p;
	save_["m_r"] = p_->m_r;
	save_["m_b"] = p_->m_b;
	save_["note"] = p_->note;
	save_["supp"] = p_->supp;
	save_["hs"] = p_->hs;
	save_["dds"] = p_->dds;
	save_["tk"] = p_->tk;
	save_["bb"] = p_->bb;
	save_["gtwl"] = p_->gtwl;
	save_["rb"] = p_->rb;
	save_["mds"] = p_->mds;
	save_["gr"] = p_->gr;
	save_["gp"] = p_->gp;
	save_["gd"] = p_->gd;
	save_["glo_p"] = p_->glo_p;
	save_["lvl_p"] = p_->lvl_p;
	save_["t_p"] = p_->t_p;
	save_["bb_p"] = p_->bb_p;
	save_["ff_p"] = p_->ff_p;
	save_["p_p"] = p_->p_p;
	save_["g_p"] = p_->g_p;
	save_["t_xp"] = p_->t_xp;
	save_["bb_xp"] = p_->bb_xp;
	save_["ff_xp"] = p_->ff_xp;
	save_["s_xp"] = p_->s_xp;
	save_["g_xp"] = p_->g_xp;
	save_["p_xp"] = p_->p_xp;
	save_["t_lvl"] = p_->t_lvl;
	save_["bb_lvl"] = p_->bb_lvl;
	save_["ff_lvl"] = p_->ff_lvl;
	save_["g_lvl"] = p_->g_lvl;
	save_["p_lvl"] = p_->p_lvl;
	save_["s_lvl"] = p_->s_lvl;
	save_["b_l"] = p_->b_l;
	save_["bp"] = p_->bp;
	save_["flagmay"] = p_->flagmay;
	save_["pass"] = p_->tankIDPass, save_["discord"] = p_->discord;
	save_["gems"] = p_->gems;
	save_["punch"] = p_->punch_count;
	save_["home_world"] = p_->home_world;
	save_["show_location"] = p_->show_location_;
	save_["show_notifications"] = p_->show_friend_notifications_;
	save_["xp"] = p_->xp;
	save_["d_name"] = p_->d_name;
	save_["level"] = p_->level;
	save_["skin"] = p_->skin;
	save_["geiger"] = p_->geiger_;
	save_["ghost"] = p_->ghost;
	save_["invis"] = p_->invis;
	save_["date"] = p_->account_created;
	save_["booty_broken"] = p_->booty_broken;
	save_["playtime"] = p_->seconds + (time(NULL) - p_->playtime);
	save_["legend"] = p_->is_legend;
	save_["roleSkin"] = p_->roleSkin;
	save_["roleIcon"] = p_->roleIcon;
	save_["mac"] = p_->mac;
	save_["rid"] = p_->rid;
	save_["vid"] = p_->vid;
	save_["aid"] = p_->aid;
	save_["gid"] = p_->gid;
	save_["pid"] = p_->platformid;
	save_["wk"] = p_->wk;
	for (int i_ = 0; i_ < p_->inv.size(); i_++) {
		json item_;
		item_["i"] = p_->inv[i_].id;
		item_["c"] = p_->inv[i_].count;
		inv_.push_back(item_);
	}
	map<string, int>::iterator it;
	for (it = p_->achievements.begin(); it != p_->achievements.end(); it++) {
		json achievement_;
		achievement_["title"] = it->first;
		achievement_["progress"] = it->second;
		achievements_.push_back(achievement_);
	} for (int i_ = 0; i_ < p_->friends.size(); i_++) {
		json friend_;
		friend_["name"] = p_->friends[i_].name;
		friend_["mute"] = p_->friends[i_].mute;
		friend_["block_trade"] = p_->friends[i_].block_trade;
		friend_["last_seen"] = p_->friends[i_].last_seen;
		friends_.push_back(friend_);
	} for (int i_ = 0; i_ < p_->last_visited_worlds.size(); i_++) {
		json world_;
		world_["name"] = p_->last_visited_worlds[i_];
		visited_worlds_.push_back(world_);
	} for (int i_ = 0; i_ < p_->worlds_owned.size(); i_++) {
		json world_;
		world_["name"] = p_->worlds_owned[i_];
		worlds_owned_.push_back(world_);
	} for (int i_ = 0; i_ < p_->playmods.size(); i_++) {
		json playmod_;
		playmod_["id"] = p_->playmods[i_].id;
		playmod_["time"] = p_->playmods[i_].time;
		playmod_["user"] = p_->playmods[i_].user;
		playmods_.push_back(playmod_);
	} 
	json blarneys_ = json::array();
	for (int i_ = 0; i_ < p_->completed_blarneys.size(); i_++) {
		json blarney_;
		blarney_["world"] = p_->completed_blarneys[i_][0];
		blarney_["time"] = p_->completed_blarneys[i_][1];
		blarneys_.push_back(blarney_);
	}
	save_["blarney"] = blarneys_;
	save_["playmods"] = playmods_;
	save_["worlds_owned"] = worlds_owned_;
	save_["last_worlds"] = visited_worlds_;
	save_["inv"] = inv_;
	save_["achievements"] = achievements_;
	save_["friends"] = friends_;
	save_["hair"] = p_->hair, save_["shirt"] = p_->shirt, save_["pants"] = p_->pants, save_["feet"] = p_->feet, save_["face"] = p_->face, save_["hand"] = p_->hand, save_["back"] = p_->back, save_["mask"] = p_->mask, save_["necklace"] = p_->necklace, save_["ances"] = p_->ances;
	ofstream w_("database/players/" + p_->tankIDName + "_.json");
	if (w_.fail()) {
		cout << "failed to save player " << p_->tankIDName << endl;
		return;
	}
	w_ << save_ << endl;
	w_.close();
	{
		if (on_exit) {
			vector<string> friends_;
			long long time_t = time(NULL);
			for (int c_ = 0; c_ < p_->friends.size(); c_++) {
				friends_.push_back(p_->friends[c_].name);
			} for (ENetPeer* currentPeer = server->peers; currentPeer < &server->peers[server->peerCount]; ++currentPeer) {
				if (currentPeer->state != ENET_PEER_STATE_CONNECTED or currentPeer->data == NULL) continue;
				if (pInfo(currentPeer)->m_h == 1) continue;
				if (pInfo(currentPeer)->invis == true) continue;
				if (find(friends_.begin(), friends_.end(), pInfo(currentPeer)->tankIDName) != friends_.end()) {
					for (int d_ = 0; d_ < pInfo(currentPeer)->friends.size(); d_++) {
						if (pInfo(currentPeer)->friends[d_].name == p_->tankIDName) {
							pInfo(currentPeer)->friends[d_].last_seen = time_t;
							break;
						}
					} 
					if (not p_->invis and not p_->m_h) {
						if (pInfo(currentPeer)->show_friend_notifications_) {
							packet_(currentPeer, "action|play_sfx\nfile|audio/friend_logoff.wav\ndelayMS|0");
							string color = "`o";
							if (p_->name_color == "`6@" or p_->name_color == "`#@") color = p_->name_color;
							gamepacket_t p;
							p.Insert("OnConsoleMessage");
							p.Insert("`3FRIEND ALERT:`` " + color + p_->tankIDName + "`` has `4logged off``.");
							p.CreatePacket(currentPeer);
						}
					}
					friends_.erase(remove(friends_.begin(), friends_.end(), pInfo(currentPeer)->tankIDName), friends_.end());
				}
			} 
			try {
				for (int c_ = 0; c_ < friends_.size(); c_++) {
					string path_ = "database/players/" + friends_.at(c_) + "_.json";
					//if (exists(path_)) {
					if (_access_s(path_.c_str(), 0) == 0) {
						json r_;
						ifstream f_(path_, ifstream::binary);
						if (f_.fail()) {
							cout << "failed reading data for " << friends_.at(c_) << endl;
							return;
						}
						f_ >> r_;
						f_.close();
						json f_g = r_.at("friends").get<json>();
						for (int i_ = 0; i_ < f_g.size(); i_++) {
							if (f_g.at(i_).at("name") == p_->tankIDName) {
								f_g.at(i_).at("last_seen") = time_t;
								break;
							}
						}
						r_.at("friends") = f_g;
						{
							ofstream f_(path_, ifstream::binary);
							f_ << r_;
							f_.close();
						}
					}
				}
			}
			catch (exception)
			{
				return;
			}
		}
	}
}