From: Ondrej H. <tan...@us...> - 2004-03-04 22:01:25
|
Update of /cvsroot/planeshift/planeshift/src/server/bulkobjects In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23525/src/server/bulkobjects Modified Files: psguildinfo.cpp psguildinfo.h Log Message: Added firt version guild alliances. It can be accessed from command line only (no GUI yet). Invites don't work. Index: psguildinfo.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/bulkobjects/psguildinfo.cpp,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** psguildinfo.cpp 26 Feb 2004 05:31:05 -0000 1.15 --- psguildinfo.cpp 4 Mar 2004 21:47:32 -0000 1.16 *************** *** 32,35 **** --- 32,43 ---- #include "pscharacter.h" + + /******************************************************************************* + * + * Guilds + * + *******************************************************************************/ + + psGuildInfo::psGuildInfo() { *************** *** 37,41 **** psGuildInfo::psGuildInfo(csString name, unsigned int founder) ! :id(0),name(name),founder(founder),karma_points(0),secret(false) { } --- 45,49 ---- psGuildInfo::psGuildInfo(csString name, unsigned int founder) ! :id(0),name(name),founder(founder),karma_points(0),secret(false),alliance(0) { } *************** *** 74,77 **** --- 82,86 ---- karma_points = row.GetInt("karma_points"); web_page = row["web_page"]; + alliance = row.GetInt("alliance"); if (row["secret_ind"]) *************** *** 246,254 **** if (db->Command("update characters " " set guild_member_of=%d, " ! " guild_level=1," " guild_points=0," " guild_public_notes=NULL," " guild_private_notes=NULL" ! " where id=%d",id, player->characterid ) != 1) { Error3("Couldn't update guild for player.\nCommand was <%s>.\nError returned was <%s>\n", --- 255,263 ---- if (db->Command("update characters " " set guild_member_of=%d, " ! " guild_level=%d," " guild_points=0," " guild_public_notes=NULL," " guild_private_notes=NULL" ! " where id=%d",id, level, player->characterid ) != 1) { Error3("Couldn't update guild for player.\nCommand was <%s>.\nError returned was <%s>\n", *************** *** 515,516 **** --- 524,707 ---- return true; } + + /******************************************************************************* + * + * Guild alliances + * + *******************************************************************************/ + + csString psGuildAlliance::lastError; + + psGuildAlliance::psGuildAlliance() + { + id = 0; + leader = NULL; + } + + psGuildAlliance::psGuildAlliance(const csString & name) + { + id = 0; + this->name = name; + leader = NULL; + } + + bool psGuildAlliance::InsertNew() + { + int leaderID; + + if (leader != NULL) + leaderID = leader->id; + else + leaderID = 0; + + if (db->Command("insert into alliances (name, leading_guild) values ('%s', %d)",name.GetData(),leaderID) != 1) + { + lastError = db->GetLastError(); + return false; + } + id = db->GetLastInsertID(); + return true; + } + + bool psGuildAlliance::RemoveAlliance() + { + for (int i=0; i < members.Length(); i++) + members[i]->alliance = 0; + + db->Command("update guilds set alliance=0 where alliance=%d",id); + db->Command("delete from alliances where id=%d",id); + return true; + } + + bool psGuildAlliance::AddNewMember(psGuildInfo * member) + { + int index; + + index = members.Find(member); + if (index != -1) + return false; + + members.Push(member); + + member->alliance = id; + + if (db->Command("update guilds set alliance=%d where id=%d",id,member->id) != 1) + { + lastError = db->GetLastError(); + return false; + } + return true; + } + + bool psGuildAlliance::CheckMembership(psGuildInfo * member) + { + return members.Find(member) != -1; + } + + bool psGuildAlliance::RemoveMember(psGuildInfo * member) + { + int index; + + if (leader == member) + { + lastError = "You can't remove leader of alliance."; + return false; + } + + index = members.Find(member); + if (index != -1) + { + members[index]->alliance = 0; + members.DeleteIndex(index); + } + else + { + lastError = "Guild not found in members"; + return false; + } + + member->alliance = 0; + + if (db->Command("update guilds set alliance=NULL where id=%d",member->id) != 1) + { + lastError = db->GetLastError(); + return false; + } + return true; + } + + bool psGuildAlliance::Load(int id) + { + iResultSet * result; + psGuildInfo * member; + int leaderID; + int memberNum; + + this->id = id; + + // Load name and leader of alliance + + result = db->Select("select name, leading_guild from alliances where id=%d", id); + if (result == NULL) + { + lastError = db->GetLastError(); + return false; + } + + if (result->Count() == 0) + { + lastError = "Alliance not found in database"; + return false; + } + + name = (*result)[0]["name"]; + leaderID = (*result)[0].GetInt("leading_guild"); + delete result; + + leader = psServer::cachemanager.FindGuild(leaderID); + if (leader == NULL) + { + lastError = "ID of leader read from alliances.leading_guild not found in cachemanager"; + return false; + } + + + // Load other members of alliance + + result = db->Select("select id from guilds where alliance=%d", id); + if (result == NULL) + { + lastError = db->GetLastError(); + return false; + } + + for (memberNum=0; memberNum < result->Count(); memberNum++) + { + member = psServer::cachemanager.FindGuild( (*result)[memberNum].GetInt("id") ); + if (member == NULL) + { + lastError = "Member of alliance loaded from DB couln't be found in cachemanager"; + return false; + } + members.Push(member); + } + + delete result; + + return true; + } + + bool psGuildAlliance::SetLeader(psGuildInfo * newLeader) + { + if ( ! CheckMembership(newLeader)) + return false; + + leader = newLeader; + + if (db->Command("update alliances set leading_guild=%d where id=%d",newLeader->id, id) != 1) + { + lastError = db->GetLastError(); + return false; + } + return true; + } Index: psguildinfo.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/bulkobjects/psguildinfo.h,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** psguildinfo.h 1 Mar 2004 11:24:18 -0000 1.9 --- psguildinfo.h 4 Mar 2004 21:47:32 -0000 1.10 *************** *** 24,27 **** --- 24,28 ---- #include "csutil/ref.h" + #include "csutil/refarr.h" #include "csutil/refcount.h" #include "util/psconst.h" *************** *** 98,101 **** --- 99,103 ---- int karma_points; csString web_page; + int alliance; csArray<psGuildMember*> members; csArray<psGuildLevel*> levels; *************** *** 137,140 **** --- 139,180 ---- }; + class psGuildAlliance : public csRefCount + { + public: + psGuildAlliance(); + psGuildAlliance(const csString & name); + + /** INSERTs alliance data in database */ + bool InsertNew(); + + /** Removes alliance data from database and sets psGuildInfo::alliance of members to 0 */ + bool RemoveAlliance(); + + /** Loads alliance data from database */ + bool Load(int id); + + /** Adds new member to alliance (saves to database) */ + bool AddNewMember(psGuildInfo * member); + + /** Checks if 'member' is in our alliance */ + bool CheckMembership(psGuildInfo * member); + + /** Removes a member from alliance (saves to database) */ + bool RemoveMember(psGuildInfo * member); + + int GetID() { return id; } + + psGuildInfo * GetLeader() { return leader; } + bool SetLeader(psGuildInfo * newLeader); + + static csString lastError; // When a psGuildAlliance method fails (returns false), + // this contains description of the problem + + protected: + int id; + csString name; + csRef<psGuildInfo> leader; + csRefArray<psGuildInfo> members; + }; #endif |