Page 1 of 1

[PATCH] Toggle Character Online

Posted: Fri May 08, 2009 10:30 pm
by John Adams
LE, please review this before I commit it to SVN. I get a feeling there is a much better way to do this, but this is how I did it on my server.

The goal was to toggle 1/0 for when a player logs in or out of the World. The value stays persistent across zones, and only gets set back to 0 when the client loses connection (Client::Disconnect()).

Give it a look, if you think it's fine, I can check it in.

Code: Select all

Index: client.cpp
===================================================================
--- client.cpp	(revision 689)
+++ client.cpp	(working copy)
@@ -592,6 +592,7 @@
 							new_client_login = true;
 							GetCurrentZone()->AddClient(this); //add to zones client list
 							zone_list.AddClientToMap(player->GetName(), this);
+							database.ToggleCharacterOnline(this, 1);
 						}
 						else{
 							cout << "Incompatible version : " << version << endl;
@@ -1613,6 +1614,7 @@
 void Client::Disconnect(){
 	if(getConnection())
 		getConnection()->SendDisconnect(true);
+		database.ToggleCharacterOnline(this, 0);
 	eqs = 0;
 }
 
Index: WorldDatabase.cpp
===================================================================
--- WorldDatabase.cpp	(revision 689)
+++ WorldDatabase.cpp	(working copy)
@@ -3189,4 +3189,13 @@
 	}
 	columns.append("");
 	return columns;
+}
+
+void WorldDatabase::ToggleCharacterOnline(Client* client, int8 toggle) {
+	Query query;
+	Player* player = client->GetPlayer();
+	if(!player->CheckPlayerInfo())
+		return;
+
+	query.RunQuery2(Q_UPDATE, "update characters set is_online=%i where id = %lu", toggle, client->GetCharacterID());
 }
\ No newline at end of file
Index: WorldDatabase.h
===================================================================
--- WorldDatabase.h	(revision 689)
+++ WorldDatabase.h	(working copy)
@@ -208,6 +208,9 @@
 	}
 	void	LoadFogInit(string zone, PacketStruct* packet);
 	static int32		next_id;
+
+	void ToggleCharacterOnline(Client* client, int8 toggle);
+
 private:
 	map<int32, string>	zone_names;
 	string				skills;

DB Change:

Code: Select all

alter table `characters` add column `is_online` tinyint(1) UNSIGNED DEFAULT '0' NOT NULL;

Note: I am not completely sure "Player* player = client->GetPlayer();" is needed. I copied how Client::Save() worked and took out what I didn't need.

Re: [PATCH] Toggle Character Online

Posted: Sat May 09, 2009 6:22 am
by LethalEncounter
The goal is to use this outside of the emu for data display information? If so, then yes that will work. I would have added a Timer to world that updated the database with the connected characters every 5 minutes so that the DB wasn't being hit as much, but with our small populations right now it isn't going to matter. Go ahead and implement it like you have it and we can change it is the performance enhancements in 0.9.

Re: [PATCH] Toggle Character Online

Posted: Sat May 09, 2009 7:57 am
by John Adams
Yes, this is meant solely for "Who's Online" via a webpage. Scatman mentioned the same thing, using a timer, but that is way outside my capabilities (so far) so cool. I'll commit this to SVN and DB Updates now.

Thanks LE!