Skip to content

Commit

Permalink
Merge pull request #78 from dev-osrose/componentUpdate
Browse files Browse the repository at this point in the history
Component update and implementation of inventory
  • Loading branch information
RavenX8 committed Nov 29, 2016
2 parents 62c388b + 0238193 commit 28a7d90
Show file tree
Hide file tree
Showing 41 changed files with 659 additions and 277 deletions.
12 changes: 6 additions & 6 deletions Database/osirose.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CREATE TABLE `accounts` (
`lasttime` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand Down Expand Up @@ -94,7 +94,7 @@ CREATE TABLE `characters` (
UNIQUE KEY `name_UNIQUE` (`name`),
KEY `user_idx` (`userid`),
CONSTRAINT `user` FOREIGN KEY (`userid`) REFERENCES `accounts` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand Down Expand Up @@ -134,7 +134,7 @@ CREATE TABLE `inventory` (
UNIQUE KEY `id_UNIQUE` (`uid`),
KEY `char_id_idx` (`char_id`),
CONSTRAINT `char_id` FOREIGN KEY (`char_id`) REFERENCES `characters` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand All @@ -145,10 +145,10 @@ DROP TABLE IF EXISTS `item_db`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `item_db` (
`type` tinyint(2) NOT NULL DEFAULT '0',
`id` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(50) NOT NULL DEFAULT '',
`desc` varchar(500) NOT NULL DEFAULT '',
`type` tinyint(2) NOT NULL DEFAULT '0',
`subtype` smallint(3) NOT NULL DEFAULT '0',
`price_buy` mediumint(10) unsigned DEFAULT NULL,
`price_sell` mediumint(10) unsigned DEFAULT NULL,
Expand All @@ -163,7 +163,7 @@ CREATE TABLE `item_db` (
`refinable` tinyint(1) unsigned DEFAULT NULL,
`view_id` smallint(3) unsigned DEFAULT NULL,
`script` text,
PRIMARY KEY (`id`,`type`)
PRIMARY KEY (`type`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

Expand Down Expand Up @@ -443,7 +443,7 @@ DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `delete_character`(IN `_charid` INT, IN `_name` VARCHAR(20))
MODIFIES SQL DATA
BEGIN
DELETE FROM characters WHERE characters.userid = _charid AND characters.name = _name;
DELETE FROM characters WHERE characters.id = _charid AND characters.name = _name;
END ;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
Expand Down
19 changes: 10 additions & 9 deletions src/char/src/ccharclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ bool CCharClient::JoinServerReply(

uint32_t sessionID = P->sessionId();
std::string password = Core::CMySQL_Database::escapeData(P->password());
logger_->info("user {}, password {} bla", sessionID, password);

std::unique_ptr<Core::IResult> res;
std::string query = fmt::format("CALL get_session({}, '{}');", sessionID, password);
Expand Down Expand Up @@ -134,12 +133,7 @@ bool CCharClient::SendCharListReply() {
res->getInt("face", face);
res->getInt("hair", hair);

packet->addCharacter(_name, race, level, job, delete_time);
packet->addEquipItem(
idx, SrvCharacterListReply::equipped_position::EQUIP_FACE, face);
packet->addEquipItem(
idx, SrvCharacterListReply::equipped_position::EQUIP_HAIR, hair);

packet->addCharacter(_name, race, level, job, face, hair, delete_time);
{
res->getInt("id", id);
characterRealId_.push_back(id);
Expand All @@ -153,7 +147,7 @@ bool CCharClient::SendCharListReply() {
itemres->getInt("itemid", itemid);

packet->addEquipItem(
idx, (SrvCharacterListReply::equipped_position)slot,
idx, SrvCharacterListReply::getPosition(slot),
itemid);
itemres->incrementRow();
}
Expand Down Expand Up @@ -243,8 +237,15 @@ bool CCharClient::SendCharSelectReply(

loginState_ = eSTATE::TRANSFERING;

uint8_t selected_id = P->charId();
if(selected_id > characterRealId_.size()) {
logger_->warn("Client {} is attempting to select a invalid character.",
GetId());
return false;
}

std::string query = fmt::format("CALL update_session_with_character({}, '{}');",
sessionId_, characterRealId_[P->charId()]);
sessionId_, characterRealId_[selected_id]);

Core::IDatabase& database = Core::databasePool.getDatabase();
database.QExecute(query);
Expand Down
2 changes: 2 additions & 0 deletions src/map/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SET( SOURCES_HEADER
include/systems/timesystem
include/systems/updatesystem
include/systems/chatsystem
include/systems/inventorysystem
)

SET( SOURCES_MAIN
Expand All @@ -48,6 +49,7 @@ SET( SOURCES_MAIN
src/systems/timesystem
src/systems/updatesystem
src/systems/chatsystem
src/systems/inventorysystem
)

find_package( Threads REQUIRED )
Expand Down
20 changes: 20 additions & 0 deletions src/map/include/systems/inventorysystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "system.h"

namespace Systems {

class InventorySystem : public System {
public:
InventorySystem(SystemManager &manager);
virtual ~InventorySystem() = default;

virtual void update(EntityManager &es, double dt);

static uint8_t findNextEmptySlot(Entity entity);
static bool swapItems(Entity entity, uint8_t &a, uint8_t &b);

void processEquip(CMapClient *client, Entity entity, const RoseCommon::CliEquipItem &packet);
};

}
6 changes: 3 additions & 3 deletions src/map/include/systems/updatesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class UpdateSystem : public System {

virtual void update(EntityManager &es, double);

void calculateSpeed(Entity entity);
static void calculateSpeed(Entity entity);

void calculateAtkSpeed(Entity entity);
static void calculateAtkSpeed(Entity entity);

void calculateCommand(Entity entity);
static void calculateCommand(Entity entity);
};

}
3 changes: 3 additions & 0 deletions src/map/src/cmapclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ bool CMapClient::JoinServerReply(

} else {
logger_->debug("Something wrong happened when creating the entity");
auto packet = makePacket<ePacketType::PAKSC_JOIN_SERVER_REPLY>(
SrvJoinServerReply::FAILED, 0);
Send(*packet);
}
} else {
logger_->debug("Client {} auth INVALID_PASS.", GetId());
Expand Down
35 changes: 15 additions & 20 deletions src/map/src/entitysystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
#include "systems/movementsystem.h"
#include "systems/updatesystem.h"
#include "systems/chatsystem.h"
#include "systems/inventorysystem.h"
#include "database.h"

using namespace RoseCommon;

EntitySystem::EntitySystem() : systemManager_(*this) {
// TODO : use on_component_removed for Destination
systemManager_.add<Systems::MovementSystem>();
systemManager_.add<Systems::UpdateSystem>();
systemManager_.add<Systems::ChatSystem>();
systemManager_.add<Systems::InventorySystem>();
}

EntityManager &EntitySystem::getEntityManager() {
Expand Down Expand Up @@ -151,34 +155,25 @@ Entity EntitySystem::loadCharacter(uint32_t charId, bool platinium) {
// TODO : write the hotbar table and loading code
entity.assign<Hotbar>();

auto equipped = entity.assign<EquippedItems>();
res = database.QStore(fmt::format("CALL get_equipped({});", charId));
entity.assign<StatusEffects>();
entity.assign<RidingItems>();
entity.assign<BulletItems>();

// TODO : write the inventory code
auto inventory = entity.assign<Inventory>();
res = database.QStore(fmt::format("CALL get_inventory({});", charId));
if (!res) {
entity.destroy();
return Entity();
}
for (auto &it : *res) {
size_t slot;
uint16_t id, gemOpt;
uint8_t refine;
it->getInt("slot", slot);
it->getInt("itemid", id);
it->getInt("gem_opt", gemOpt);
it->getInt("socket", equipped->items_[slot].wearable_.hasSocket_);
it->getInt("refine", refine);
equipped->items_[slot].wearable_.id_ = id;
equipped->items_[slot].wearable_.gemOpt_ = gemOpt;
equipped->items_[slot].wearable_.refine_ = refine;
if (slot >= Inventory::maxItems)
continue; // TODO : add a warning about that slot
inventory->items_[slot].loadFromDatabase(*it);
}
if (!equipped->items_[EquippedItems::FACE].wearable_.id_)
equipped->items_[EquippedItems::FACE].wearable_.id_ = graphics->face_;
entity.assign<StatusEffects>();
entity.assign<RidingItems>();
entity.assign<BulletItems>();

// TODO : write the inventory code
entity.assign<Inventory>();
get<Systems::UpdateSystem>().calculateSpeed(entity);
Systems::UpdateSystem::calculateSpeed(entity);

registerEntity(entity);
return entity;
Expand Down
56 changes: 56 additions & 0 deletions src/map/src/systems/inventorysystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "systems/inventorysystem.h"
#include "cmapserver.h"
#include "cmapclient.h"

using namespace Systems;
using namespace RoseCommon;

InventorySystem::InventorySystem(SystemManager &manager) : System(manager) {
manager.registerDispatcher(ePacketType::PAKCS_EQUIP_ITEM, &InventorySystem::processEquip);
}

void InventorySystem::update(EntityManager&, double) {}

uint8_t InventorySystem::findNextEmptySlot(Entity entity) {
if (!entity || !entity.component<Inventory>())
return 0;
const auto &inventory = entity.component<Inventory>()->items_;
for (size_t i = Inventory::MAX_EQUIP_ITEMS; i < Inventory::maxItems; ++i)
if (!inventory[i].id_)
return i;
return 0;
}

bool InventorySystem::swapItems(Entity entity, uint8_t &a, uint8_t &b) {
if (!entity || !entity.component<Inventory>())
return false;
if (a >= Inventory::maxItems || b >= Inventory::maxItems)
return false;
if (a == b || (!a && !b))
return false;
if (!a)
a = findNextEmptySlot(entity);
if (!b)
b = findNextEmptySlot(entity);
auto inventory = entity.component<Inventory>();
std::swap(inventory->items_[a], inventory->items_[b]);
return true;
}

void InventorySystem::processEquip(CMapClient *client, Entity entity, const RoseCommon::CliEquipItem &packet) {
logger_->trace("InventorySystem::processEquip");
if (packet.slotTo() < 1 || packet.slotTo() >= Inventory::MAX_EQUIP_ITEMS) {
logger_->warn("When requesting to change equipped item, the destination wasn't good");
return;
}
uint8_t to = packet.slotTo();
uint8_t from = packet.slotFrom();
if (!swapItems(entity, to, from)) {
logger_->warn("There was an error while swapping items for client {}", entity.component<BasicInfo>()->id_);
return;
}
CMapServer::SendPacket(client, CMapServer::eSendType::EVERYONE,
*makePacket<ePacketType::PAKWC_EQUIP_ITEM>(entity, packet.slotTo()));
auto list = {to, from};
client->Send(*makePacket<ePacketType::PAKWC_SET_ITEM>(entity, list));
}
9 changes: 5 additions & 4 deletions src/map/src/systems/updatesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using namespace Systems;

void UpdateSystem::update(EntityManager &es, double) {
for (Entity entity : es.entities_with_components<AdvancedInfo, EquippedItems, Stats>())
for (Entity entity : es.entities_with_components<AdvancedInfo, Inventory, Stats>())
calculateSpeed(entity);
for (Entity entity : es.entities_with_components<BasicInfo>())
calculateCommand(entity);
Expand All @@ -15,11 +15,12 @@ void UpdateSystem::calculateSpeed(Entity entity) {
if (advanced->moveMode_ == AdvancedInfo::WALK)
advanced->runSpeed_ = 200;
if (advanced->moveMode_ == AdvancedInfo::RUN) {
auto equipped = entity.component<EquippedItems>();
auto inventory = entity.component<Inventory>();
auto stats = entity.component<Stats>();
advanced->runSpeed_ += stats->dex_ * .8500001;
if (equipped->items_[EquippedItems::BOOTS].wearable_.id_ && equipped->items_[EquippedItems::BOOTS].wearable_.life_) {
uint16_t realSpeed = equipped->items_[EquippedItems::BOOTS].runSpeed_ - 65;
if (inventory->items_[Inventory::BOOTS].id_ && inventory->items_[Inventory::BOOTS].life_) {
// FIXME : realSpeed = boots.runSpeed - 65
uint16_t realSpeed = -65;
uint16_t realMod = realSpeed / 5;
uint16_t dexMod = realMod * (stats->dex_ + realMod) / 23.222;
advanced->runSpeed_ += (realSpeed * 5) + realMod + dexMod;
Expand Down

0 comments on commit 28a7d90

Please sign in to comment.