From 32eb8f65f76cf680d555ba7f6db558cbff2c1263 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Sun, 6 Apr 2008 00:54:41 +0200 Subject: [PATCH] Add the Core files for Cluster. --- src/Core/Cluster.cpp | 111 ++++++++++++++++ src/Core/Cluster.h | 79 +++++++++++ src/Core/ClusterManager.cpp | 66 ++++++++++ src/Core/ClusterManager.h | 44 +++++++ src/Core/EditorCluster.cpp | 312 ++++++++++++++++++++++++++++++++++++++++++++ src/Core/EditorCluster.h | 76 +++++++++++ 6 files changed, 688 insertions(+) create mode 100644 src/Core/Cluster.cpp create mode 100644 src/Core/Cluster.h create mode 100644 src/Core/ClusterManager.cpp create mode 100644 src/Core/ClusterManager.h create mode 100644 src/Core/EditorCluster.cpp create mode 100644 src/Core/EditorCluster.h diff --git a/src/Core/Cluster.cpp b/src/Core/Cluster.cpp new file mode 100644 index 0000000..3e03b2d --- /dev/null +++ b/src/Core/Cluster.cpp @@ -0,0 +1,111 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "Cluster.h" +#include "Global.h" +#include "FieldImpls.h" +#include "TableImpls.h" + +using mud::Cluster; + +Cluster::Cluster(SavableManagerPtr cluster) : +m_cluster(cluster) +{ + Assert(cluster); +} + +Cluster::~Cluster(void) +{ + Unlock(); +} + +value_type Cluster::getID() const +{ + return m_cluster->getValue(db::TableImpls::Get()->CLUSTERS->CLUSTERID)->getIntegerValue(); +} + +const std::string& Cluster::getName() const +{ + return m_cluster->getValue(db::TableImpls::Get()->CLUSTERS->NAME)->getStringValue(); +} + +const std::string& Cluster::getDescription() const +{ + return m_cluster->getValue(db::TableImpls::Get()->CLUSTERS->DESCRIPTION)->getStringValue(); +} + +value_type Cluster::getArea() const +{ + return m_cluster->getValue(db::TableImpls::Get()->CLUSTERS->FKAREAS)->getIntegerValue(); +} + +void Cluster::setName(const std::string& name) +{ + Lock(); + ValuePtr value(new FieldValue(db::TableImpls::Get()->CLUSTERS->NAME, name)); + m_cluster->setValue(value); +} + +void Cluster::setDescription(const std::string& description) +{ + Lock(); + ValuePtr value(new FieldValue(db::TableImpls::Get()->CLUSTERS->DESCRIPTION, description)); + m_cluster->setValue(value); +} + +void Cluster::setArea(value_type area) +{ + Lock(); + ValuePtr value(new FieldValue(db::TableImpls::Get()->CLUSTERS->FKAREAS, area)); + m_cluster->setValue(value); +} + +void Cluster::Delete() +{ + Lock(); + m_cluster->erase(); +} + +void Cluster::Save() +{ + Lock(); + m_cluster->save(); +} + +void Cluster::Discard() +{ + Lock(); + m_cluster->discard(); +} + +bool Cluster::Exists() +{ + return m_cluster->exists(); +} + +SavableManagerPtr Cluster::getManager() const +{ + return m_cluster; +} + +TableImplPtr Cluster::getTable() const +{ + return m_cluster->getTable(); +} diff --git a/src/Core/Cluster.h b/src/Core/Cluster.h new file mode 100644 index 0000000..f779bb2 --- /dev/null +++ b/src/Core/Cluster.h @@ -0,0 +1,79 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#pragma once + +#include "SavableHeaders.h" +#include "SavableTypes.h" + +namespace mud +{ + class ClusterManager; + + class Cluster : public Savable + { + public: + /* + * Getters + */ + value_type getID() const; + const std::string& getName() const; + const std::string& getDescription() const; + value_type getArea() const; + + /* + * Setters + */ + void setName(const std::string& name); + void setDescription(const std::string& description); + void setArea(value_type area); + + /* + * Utilities + */ + SavableManagerPtr getManager() const; + TableImplPtr getTable() const; + + /* + * brief Database operations + */ + void Delete(); + void Save(); + void Discard(); + bool Exists(); + + private: + friend class mud::ClusterManager; // For constructor + friend SmartPtrDelete(mud::Cluster); + + SavableManagerPtr m_cluster; + + /** + * Constructor + * @param cluster The DB object + */ + Cluster(SavableManagerPtr cluster); + + Cluster(const Cluster& rhs); + Cluster operator=(const Cluster& rhs); + + /** Destructor, unlocks the savable. */ + ~Cluster(void); + }; +} diff --git a/src/Core/ClusterManager.cpp b/src/Core/ClusterManager.cpp new file mode 100644 index 0000000..7b163d3 --- /dev/null +++ b/src/Core/ClusterManager.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "ClusterManager.h" +#include "Cluster.h" +#include "TableImpls.h" +#include "FieldImpls.h" +#include "Area.h" + +using mud::ClusterManager; +using mud::Cluster; +using mud::ClusterPtr; + +std::vector ClusterManager::List(mud::AreaPtr parentArea) +{ + SelectionMaskPtr mask(new SelectionMask(GetTable())); + if(parentArea) + { + ValuePtr value(new FieldValue(db::TableImpls::Get()->CLUSTERS->FKAREAS, parentArea->getID())); + mask->addField(value); + } + return GetTable()->tableList(mask); +} + +TableImplPtr ClusterManager::GetTable() +{ + return db::TableImpls::Get()->CLUSTERS; +} + +KeysPtr ClusterManager::Add() +{ + SavableManagerPtr manager = SavableManager::getnew(db::TableImpls::Get()->CLUSTERS); + + bool locked = manager->lock(); + Assert(locked); // new manager, should always be unlocked + + manager->save(); + + manager->unlock(); + return manager->getKeys(); +} + +mud::ClusterPtr ClusterManager::GetByKey(value_type id) +{ + KeyValuePtr key(new KeyValue(db::TableImpls::Get()->CLUSTERS->CLUSTERID, id)); + SavableManagerPtr manager = SavableManager::bykey(key); + ClusterPtr p(new Cluster(manager)); + return p; +} diff --git a/src/Core/ClusterManager.h b/src/Core/ClusterManager.h new file mode 100644 index 0000000..1647a25 --- /dev/null +++ b/src/Core/ClusterManager.h @@ -0,0 +1,44 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#pragma once + +#include "SavableHeaders.h" +#include "SavableTypes.h" + +namespace mud +{ + class ClusterManager : public Singleton + { + public: + TableImplPtr GetTable(); + std::vector List(mud::AreaPtr parentArea = mud::AreaPtr()); + + KeysPtr Add(); + mud::ClusterPtr GetByKey(value_type id); + + private: + ClusterManager(void) {}; + ClusterManager(const ClusterManager& rhs); + ClusterManager operator=(const ClusterManager& rhs); + ~ClusterManager(void) {}; + + friend class Singleton; + }; +} diff --git a/src/Core/EditorCluster.cpp b/src/Core/EditorCluster.cpp new file mode 100644 index 0000000..7178c9d --- /dev/null +++ b/src/Core/EditorCluster.cpp @@ -0,0 +1,312 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "EditorCluster.h" +#include "EditorRoom.h" + +#include "UBSocket.h" + +#include "StringUtilities.h" +#include "TableImpls.h" +#include "Array.h" + +#include "Account.h" +#include "Cluster.h" +#include "ClusterManager.h" +#include "Area.h" +#include "AreaManager.h" + +typedef EditorCluster E; +typedef CommandInfoObject O; +typedef CommandBinding B; + +// name function need: object lock +static O editName( "Name", &E::editName, true, true); +static O editDescription("Description", &E::editDescription, true, true); +static O editArea( "Area", &E::editArea, true, true); +static O showCluster( "Show", &E::showCluster, true, false); +static O saveCluster( "Save", &E::saveCluster, true, true); +static O startRooms("Rooms",&E::startRooms); +static O clearParentArea("ClearParentArea", &E::clearParentArea); +static O setParentArea("SetParentArea", &E::setParentArea); + +static const B commands[] = { + B("all", clearParentArea), + B("area", editArea), + B("description", editDescription), + B("filter", setParentArea), + B("name", editName), + B("rooms", startRooms), + B("save", saveCluster), + B("show", showCluster), +}; + +EditorCluster::EditorCluster(UBSocket* sock) : +OLCEditor(sock), +m_commands(commands, array_size(commands)), +m_cluster(), +m_target(M_NONE) +{ + listCommands(Global::Get()->EmptyString); +} + +EditorCluster::EditorCluster(UBSocket* sock, mud::AreaPtr parentArea) : +OLCEditor(sock), +m_commands(commands, array_size(commands)), +m_cluster(), +m_parentArea(parentArea), +m_target(M_NONE) +{ + listCommands(Global::Get()->EmptyString); + m_sock->Sendf("Only Clusters that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea->getID()); +} + +EditorCluster::~EditorCluster(void) +{ + +} + +void EditorCluster::OnFocus() +{ + switch(m_target) + { + case M_NONE: + return; + + case M_DESCRIPTION: + editDescription(m_value); + break; + } + + m_target = M_NONE; +} + +std::string EditorCluster::lookup(const std::string& action) +{ + std::string name = OLCEditor::lookup(action); + if(name.size() != 0) + return name; + + const ClusterCommand* act = (ClusterCommand*)m_commands.getObject(action); + if(act) + return act->getName(); + + return Global::Get()->EmptyString; +} + +void EditorCluster::dispatch(const std::string& action, const std::string& argument) +{ + const ClusterCommand* act = (ClusterCommand*)m_commands.getObject(action); + + if(!act) + { + OLCEditor::dispatch(action, argument); + return; + } + + if(!m_cluster) + { + m_sock->Send("You need to be editing a cluster first.\n"); + m_sock->Send("(Use the 'edit' command.)\n"); + return; + } + + if(act->needLock()) + { + try { + m_cluster->Lock(); + } catch(SavableLocked& e) { + m_sock->Send("The cluster you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n"); + m_sock->Send("Please try again later.\n"); + return; + } + } + + act->Run(this, argument); + return; +} + +SavablePtr EditorCluster::getEditing() +{ + return m_cluster; +} + +TableImplPtr EditorCluster::getTable() +{ + return db::TableImpls::Get()->CHUNKS; +} + +KeysPtr EditorCluster::addNew() +{ + return mud::ClusterManager::Get()->Add(); +} + +std::vector EditorCluster::getList() +{ + return mud::ClusterManager::Get()->List(m_parentArea); +} + +void EditorCluster::setEditing(KeysPtr keys) +{ + if(!keys->size()) + { + m_cluster.reset(); + return; + } + + m_cluster = mud::ClusterManager::Get()->GetByKey(keys->first()->getIntegerValue()); + return; +} + +std::vector EditorCluster::getCommands() +{ + return m_commands.getCommandsVector(); +} + +void EditorCluster::editName(const std::string& argument) +{ + if(argument.size() == 0) + { + m_sock->Send("Cluster name can't be zero length!\n"); + return; + } + + m_sock->Sendf("Cluster name changed from '%s' to '%s'.\n", m_cluster->getName().c_str(), argument.c_str()); + m_cluster->setName(argument); + return; +} + +void EditorCluster::editDescription(const std::string& argument) +{ + if(argument.size() == 0) + { + m_sock->Send("No argument, dropping you into the string editor!\n"); + return; + } + + m_sock->Sendf("Cluster description changed from '%s' to '%s'.\n", m_cluster->getDescription().c_str(), argument.c_str()); + m_cluster->setDescription(argument); + return; +} + +void EditorCluster::editArea(const std::string& argument) +{ + int id = atoi(argument.c_str()); + if(id <= 0) + { + m_sock->Send("Please specify a area this Cluster belongs to.\n"); + } + + std::string oldname; + try { + mud::AreaPtr oldarea = mud::AreaManager::Get()->GetByKey(m_cluster->getArea()); + oldname = oldarea->getName(); + } catch(RowNotFoundException& e) { + oldname = String::Get()->fromInt(m_cluster->getArea()); + } + + try + { + mud::AreaPtr area = mud::AreaManager::Get()->GetByKey(id); + m_sock->Sendf("Area changed from '%s' to '%s'.\n", oldname.c_str(), area->getName().c_str()); + m_cluster->setArea(id); + } + catch(RowNotFoundException& e) + { + m_sock->Sendf("'%s' is not a valid area!\n", argument.c_str()); + m_sock->Send(String::Get()->box(mud::AreaManager::Get()->List(), "Areas")); + return; + } +} + +void EditorCluster::startRooms(const std::string& argument) +{ + if(!argument.compare(".")) + { + if(!m_cluster) + { + m_sock->Send("You can only use '.' when you are already editing a cluster.\n"); + m_sock->Send("You may specifiy a cluster to filter the Room Editor on by it's id.\n"); + return; + } + + m_sock->Send("Dropping you into filtered Room Edit mode!\n"); + m_sock->SetEditor(new EditorRoom(m_sock, m_cluster)); + return; + } + + if(argument.size()) + { + value_type id = atoi(argument.c_str()); + + try { + mud::ClusterPtr cluster = mud::ClusterManager::Get()->GetByKey(id); + m_sock->Send("Dropping you into filtered Room Edit mode!\n"); + m_sock->SetEditor(new EditorRoom(m_sock, cluster)); + } catch(RowNotFoundException& e) { + m_sock->Sendf("Unknown cluster '%s'.\n", argument.c_str()); + } + + return; + } + + m_sock->Send("Dropping you into Room Edit mode!\n"); + m_sock->SetEditor(new EditorRoom(m_sock)); + return; +} + +void EditorCluster::showCluster(const std::string& argument) +{ + m_sock->Send(m_cluster->toString()); +} + +void EditorCluster::saveCluster(const std::string& argument) +{ + m_sock->Sendf("Saving cluster '%s'.\n", m_cluster->getName().c_str()); + m_cluster->Save(); + m_sock->Send("Saved.\n"); + return; +} + +void EditorCluster::clearParentArea(const std::string& argument) +{ + m_sock->Send("Ok.\n"); + m_parentArea.reset(); + return; +} + +void EditorCluster::setParentArea(const std::string& argument) +{ + if(argument.size()) + { + m_sock->Send("Please provide a area to filter on.\n"); + return; + } + + try { + int id = atoi(argument.c_str()); + mud::AreaPtr area = mud::AreaManager::Get()->GetByKey(id); + m_parentArea = area; + m_sock->Send("Ok.\n"); + m_sock->Sendf("Only Clusters that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea->getID()); + } catch(RowNotFoundException& e) { + m_sock->Sendf("'%s' is not a area.\n", argument.c_str()); + } +} diff --git a/src/Core/EditorCluster.h b/src/Core/EditorCluster.h new file mode 100644 index 0000000..d844987 --- /dev/null +++ b/src/Core/EditorCluster.h @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#pragma once + +#include "Types.h" + +#include "OLCEditor.h" +#include "CommandInfoObject.h" +#include "CommandTable.h" + +class EditorCluster : public OLCEditor +{ +public: + typedef CommandInfoObject ClusterCommand; /**< The type of a CommandInfoObject for this Editor. */ + + EditorCluster(UBSocket* sock); + EditorCluster(UBSocket* sock, mud::AreaPtr parentArea); + ~EditorCluster(void); + + void OnFocus(); + + std::string name() { return "Cluster"; }; + std::string prompt() { return "Cluster> "; }; + + std::string lookup(const std::string& action); + void dispatch(const std::string& action, const std::string& argument); + + SavablePtr getEditing(); + TableImplPtr getTable(); + KeysPtr addNew(); + std::vector getList(); + std::vector getCommands(); + void setEditing(KeysPtr keys); + + void editName(const std::string& argument); + void editDescription(const std::string& argument); + void editArea(const std::string& argument); + void startRooms(const std::string& argument); + void showCluster(const std::string& argument); + void saveCluster(const std::string& argument); + void clearParentArea(const std::string& argument); + void setParentArea(const std::string& argument); + +private: + enum E_TARGET + { + M_NONE, + M_DESCRIPTION, + }; + + CommandTable m_commands; + mud::ClusterPtr m_cluster; + mud::AreaPtr m_parentArea; + EditorCluster::E_TARGET m_target; + std::string m_value; + + EditorCluster(const EditorCluster& rhs); + EditorCluster operator=(const EditorCluster& rhs); +}; -- 2.11.4.GIT