view libserver/malikania/server/db/spell.hpp @ 116:d7025649d85c

Server: add database account Implement accounts using a abstract factory mechanism, the database object creates abstract account which are implemented differently depending on the backend. See: - test_database, - test_account - broken_account Refs #687, #682
author David Demelier <markand@malikania.fr>
date Mon, 11 Sep 2017 13:18:43 +0200
parents f8c6d2244795
children 1359e09fb3c8
line wrap: on
line source

/*
 * spell.hpp -- database spell object
 *
 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef MALIKANIA_SERVER_DB_SPELL_HPP
#define MALIKANIA_SERVER_DB_SPELL_HPP

/**
 * \file spell.hpp
 * \brief Database spell object.
 */

#include <memory>
#include <unordered_set>

#include "model.hpp"

namespace mlk {

/**
 * \brief Database spell object.
 */
namespace server {

/**
 * \brief Describe a spell.
 */
class spell : public model {
private:
    friend class character;

protected:
    std::int64_t character_id_{-1};     //!< parent character
    std::string classname_;             //!< class type to instanciate
    std::uint8_t level_{1};             //!< spell level

    /**
     * Save this spell.
     *
     * The implementation must save the spell and update id, character_id
     * member variables.
     *
     * Then it will be added into character.spells_ variable.
     *
     * \note called by character::add_spell.
     */
    virtual void do_save(std::int64_t character_id) = 0;

    /**
     * Remove this spell.
     *
     * The implementation must remove the spell from the database and update
     * id, character_id member variables.
     *
     * \note called by character::remove_spell
     */
    virtual void do_remove() = 0;

    /**
     * Update the spell level in database.
     *
     * Only called when the level needs to be changed, the implementation does
     * not need to update level_ field.
     *
     * \note called from set_level helper
     * \throw std::exception if the operation could not succeed
     */
    virtual void do_set_level(std::uint8_t level) = 0;

    /**
     * Constructor.
     *
     * \pre !classname.empty()
     * \param classname the class name
     */
    inline spell(std::string classname)
        : classname_(std::move(classname))
    {
        assert(!classname_.empty());
    }

public:
    /**
     * Get the level.
     *
     * \return the level
     */
    inline std::uint8_t level() const noexcept
    {
        return level_;
    }

    /**
     * Set the spell level.
     *
     * \param level the level
     */
    inline void set_level(std::uint8_t level)
    {
        if (is_published() && level_ != level)
            do_set_level(level);

        level_ = level;
    }
};

/**
 * Type for storing spells.
 */
using spell_set = std::unordered_set<std::unique_ptr<spell>>;

} // !server

} // !mlk

#endif // !MALIKANIA_SERVER_DB_SPELL_HPP