diff options
author | Aaron Marks <nymacro@gmail.com> | 2005-07-16 10:00:53 +0000 |
---|---|---|
committer | Aaron Marks <nymacro@gmail.com> | 2005-07-16 10:00:53 +0000 |
commit | ce87adec648c69af2313e6077dad467d9ca8af3f (patch) | |
tree | f74727012cd5f1f646e4b75c7a8c47c80424af4c /src | |
parent | 34e887895276242efaf2e0b5f1700c1ab1d6b3db (diff) | |
download | manaserv-ce87adec648c69af2313e6077dad467d9ca8af3f.tar.gz manaserv-ce87adec648c69af2313e6077dad467d9ca8af3f.tar.bz2 manaserv-ce87adec648c69af2313e6077dad467d9ca8af3f.tar.xz manaserv-ce87adec648c69af2313e6077dad467d9ca8af3f.zip |
Added chat message handler placeholder (still not fully functional).
Updated PostgreSQL SQL support - although there is still problem with primary key being initialized to null.
Updated message enumeration.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/accounthandler.cpp | 16 | ||||
-rw-r--r-- | src/chathandler.cpp | 50 | ||||
-rw-r--r-- | src/chathandler.h | 44 | ||||
-rw-r--r-- | src/client.cpp | 24 | ||||
-rw-r--r-- | src/dalstoragesql.h | 65 | ||||
-rw-r--r-- | src/defines.h | 67 | ||||
-rw-r--r-- | src/main.cpp | 10 | ||||
-rw-r--r-- | src/messagein.cpp | 4 | ||||
-rw-r--r-- | src/messagein.h | 4 | ||||
-rw-r--r-- | src/messageout.cpp | 16 | ||||
-rw-r--r-- | src/messageout.h | 4 |
12 files changed, 245 insertions, 61 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 56003d26..37528879 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,8 @@ tmwserv_SOURCES = main.cpp \ configuration.cpp \ accounthandler.h \ accounthandler.cpp \ + chathandler.h \ + chathandler.cpp \ connectionhandler.h \ connectionhandler.cpp \ debug.h \ diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 266dfddf..84f1c553 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -40,17 +40,13 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { int result = 0; - // strip message type byte - message.readByte(); - Storage &store = Storage::instance("tmw"); - // get message type char type = message.readByte(); switch(type) { - case MSG_ACCOUNT_LOGIN: + case CMSG_LOGIN: { std::string username = message.readString(); std::string password = message.readString(); @@ -73,7 +69,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) std::cout << "Login OK by " << username << std::endl; } break; - case MSG_ACCOUNT_REGISTER: + case CMSG_REGISTER: { std::string username = message.readString(); std::string password = message.readString(); @@ -86,12 +82,18 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) store.flush(); // flush changes } break; + case CMSG_CHAR_CREATE: + { + std::string name = message.readString(); + // TODO: Finish this message type (should a player customize stats + // slightly?) + } break; + default: std::cout << "Invalid message type" << std::endl; break; } - debugCatch(result); } diff --git a/src/chathandler.cpp b/src/chathandler.cpp new file mode 100644 index 00000000..4d901f19 --- /dev/null +++ b/src/chathandler.cpp @@ -0,0 +1,50 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 2 of the License, or + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "chathandler.h" +#include "defines.h" +#include <iostream> + +void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) +{ + char type = message.readByte(); + + switch (type) { + case CMSG_SAY: + { + std::string text = message.readString(); + short channel = message.readShort(); + std::cout << "Say (" << channel << "): " << text << std::endl; + } break; + + case CMSG_ANNOUNCE: + { + std::string text = message.readString(); + std::cout << "Announce: " << text << std::endl; + } break; + + default: + std::cout << "Invalid message type" << std::endl; + break; + } +} diff --git a/src/chathandler.h b/src/chathandler.h new file mode 100644 index 00000000..597d61b0 --- /dev/null +++ b/src/chathandler.h @@ -0,0 +1,44 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 2 of the License, or + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_SERVER_CHATHANDLER_ +#define _TMW_SERVER_CHATHANDLER_ + +#include "messagehandler.h" +#include "netcomputer.h" +#include "messagein.h" + +/** + * Manages all chat related + * + */ +class ChatHandler : public MessageHandler +{ + public: + /** + * Recieves chat related messages. + */ + void receiveMessage(NetComputer &computer, MessageIn &message); +}; + +#endif diff --git a/src/client.cpp b/src/client.cpp index 19e0fa46..e3b010b2 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -41,27 +41,35 @@ int main(int argc, char *argv[]) // Send a login message MessageOut msg; + // Register + /* + msg.writeByte(CMSG_REGISTER); + msg.writeString("test"); + msg.writeString("password"); + msg.writeString("test@email.addr"); + */ + // Login ///* - msg.writeByte(MSG_ACCOUNT); - msg.writeByte(MSG_ACCOUNT_LOGIN); + msg.writeByte(CMSG_LOGIN); msg.writeString("test"); msg.writeString("password"); //*/ - // Register + // Chat /* - msg.writeByte(MSG_ACCOUNT); - msg.writeByte(MSG_ACCOUNT_REGISTER); - msg.writeString("test"); - msg.writeString("password"); - msg.writeString("test@email.addr"); + msg.writeByte(CMSG_SAY); + msg.writeString("Hello World!"); + msg.writeShort(0); */ + // message hex + /* for (unsigned int i = 0; i < msg.getPacket()->length; i++) { printf("%x ", msg.getPacket()->data[i]); } printf("\n"); + */ SDLNet_TCP_Send(tcpsock, msg.getPacket()->data, msg.getPacket()->length); diff --git a/src/dalstoragesql.h b/src/dalstoragesql.h index eacb165d..a9cb8f82 100644 --- a/src/dalstoragesql.h +++ b/src/dalstoragesql.h @@ -34,6 +34,7 @@ #include <string> +// TODO: Fix problem with PostgreSQL null primary key's. /** * MySQL specificities: @@ -74,6 +75,9 @@ const std::string SQL_MAPS_TABLE( #elif defined (SQLITE_SUPPORT) "id INTEGER PRIMARY KEY," "map TEXT NOT NULL" +#elif defined (POSTGRESQL_SUPPORT) + "id INTEGER PRIMARY KEY," + "map TEXT NOT NULL" #endif ");" ); @@ -107,6 +111,13 @@ const std::string SQL_ACCOUNTS_TABLE( "email TEXT NOT NULL," "level INTEGER NOT NULL," "banned INTEGER NOT NULL" +#elif defined (POSTGRESQL_SUPPORT) + "id INTEGER PRIMARY KEY," + "username TEXT NOT NULL UNIQUE," + "password TEXT NOT NULL," + "email TEXT NOT NULL," + "level INTEGER NOT NULL," + "banned INTEGER NOT NULL" #endif ");" ); @@ -165,7 +176,30 @@ const std::string SQL_CHARACTERS_TABLE( "vit INTEGER NOT NULL," "int INTEGER NOT NULL," "dex INTEGER NOT NULL," - "luck INTEGER NOT NULL" + "luck INTEGER NOT NULL," + "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id)," + "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" +#elif defined (POSTGRESQL_SUPPORT) + "id INTEGER PRIMARY KEY," + "user_id INTEGER NOT NULL," + "name TEXT NOT NULL UNIQUE," + // general information about the character + "gender INTEGER NOT NULL," + "level INTEGER NOT NULL," + "money INTEGER NOT NULL," + // location on the map + "x INTEGER NOT NULL," + "y INTEGER NOT NULL," + "map_id INTEGER NOT NULL," + // stats + "str INTEGER NOT NULL," + "agi INTEGER NOT NULL," + "vit INTEGER NOT NULL," + "int INTEGER NOT NULL," + "dex INTEGER NOT NULL," + "luck INTEGER NOT NULL," + "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id)," + "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" #endif ");" ); @@ -192,6 +226,11 @@ const std::string SQL_ITEMS_TABLE( "amount INTEGER NOT NULL," "type INTEGER NOT NULL," "state TEXT" +#elif defined (POSTGRESQL_SUPPORT) + "id INTEGER PRIMARY KEY," + "amount INTEGER NOT NULL," + "type INTEGER NOT NULL," + "state TEXT" #endif ");" ); @@ -225,7 +264,20 @@ const std::string SQL_WORLD_ITEMS_TABLE( "map_id INTEGER NOT NULL," // time to die (UNIX time) "deathtime INTEGER NOT NULL," - "PRIMARY KEY (id, map_id)" + "PRIMARY KEY (id, map_id)," + "FOREIGN KEY (id) REFERENCES tmw_items(id)," + "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" +#elif defined (POSTGRESQL_SUPPORT) + "id INTEGER NOT NULL," + // location on the map + "x INTEGER NOT NULL," + "y INTEGER NOT NULL," + "map_id INTEGER NOT NULL," + // time to die (UNIX time) + "deathtime INTEGER NOT NULL," + "PRIMARY KEY (id, map_id)," + "FOREIGN KEY (id) REFERENCES tmw_items(id)," + "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" #endif ");" ); @@ -244,7 +296,14 @@ const std::string SQL_INVENTORIES_TABLE( "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)" #elif defined (SQLITE_SUPPORT) "id INTEGER NOT NULL," - "owner_id INTEGER NOT NULL" + "owner_id INTEGER NOT NULL," + "FOREIGN KEY (id) REFERENCES tmw_items(id)," + "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" +#elif defined (POSTGRESQL_SUPPORT) + "id INTEGER NOT NULL," + "owner_id INTEGER NOT NULL," + "FOREIGN KEY (id) REFERENCES tmw_items(id)," + "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)" #endif ");" ); diff --git a/src/defines.h b/src/defines.h index a0d3273a..2119b9d7 100644 --- a/src/defines.h +++ b/src/defines.h @@ -55,39 +55,52 @@ typedef enum { } Genders; /** - * Enumerated type for received server messages + * Enumerated type for communicated messages */ enum { - MSG_ACCOUNT = 0, - MSG_MOVE, - MSG_ATTACK, - MSG_PICKUP, - MSG_DROP, - MSG_TRADE, - MSG_CHAT -}; + // Login/Register + CMSG_REGISTER = 0, + CMSG_ENCRYPTED_REGISTER, + SMSG_REGISTER_RESPONSE, + CMSG_LOGIN, + CMSG_ENCRYPTED_LOGIN, + SMSG_LOGIN_ERROR, + SMSG_LOGIN_CONFIRM, + CMSG_CHAR_CREATE, + SMSG_CHAR_CREATE_RESPONSE, -enum { - MSG_ACCOUNT_LOGIN = 0, - MSG_ACCOUNT_REGISTER -}; + // Objects + SMSG_NEW_OBJECT = 20, + SMSG_REMOVE_OBJECT, + SMSG_CHANGE_OBJECT, + CMSG_PICKUP, + CMSG_USER_OBJECT, -// NOTE: Maybe it would be better to reuse some enumerated types with both -// server and client? + // Beings + SMSG_NEW_BEING = 30, + SMSG_REMOVE_BEING, + SMSG_INVENTORY_UPD, + SMSG_EQUIPMENT_UPD, + SMSG_ATTACK, + SMSG_PATH, + CMSG_TARGET, + CMSG_WALK, + CMSG_START_TRADE, + CMSG_START_TALK, + CMSG_REQ_TRADE, -/** - * Enumerated type for messages sent to client - */ -enum { - CMSG_SPAWN = 0, // spawn object - CMSG_DESTROY, // destroy object - CMSG_MOVE, // move object - CMSG_ATTACK, // Player attacked/got attacked by object - CMSG_PICKUP, // Player picked up object - CMSG_DROP, // Player dropped object - CMSG_CHAT, // Another player chatted - CMSG_DIALOG // Message dialog + // Items + CMSG_USE_ITEM = 40, + CMSG_EQUIP, + + // Chat + SMSG_CHAT = 60, + SMSG_SYSTEM, + SMSG_ANNOUNCEMENT, + CMSG_SAY, + CMSG_ANNOUNCE, }; + #endif // _TMWSERV_DEFINES_H_ diff --git a/src/main.cpp b/src/main.cpp index 815d6b4b..d6e07837 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,7 @@ #include "netsession.h" #include "connectionhandler.h" #include "accounthandler.h" +#include "chathandler.h" #include "storage.h" #include "configuration.h" @@ -72,6 +73,7 @@ Skill skillTree("base"); /**< Skill tree */ Configuration config; /**< XML config reader */ AccountHandler *accountHandler = new AccountHandler(); /**< Account message handler */ +ChatHandler *chatHandler = new ChatHandler(); /** * SDL timer callback, sends a <code>TMW_WORLD_TICK</code> event. @@ -160,8 +162,9 @@ void deinitialize() delete script; #endif - // destro account handler + // destroy message handlers delete accountHandler; + delete chatHandler; // Get rid of persistent data storage tmwserv::Storage::destroy(); @@ -197,7 +200,10 @@ int main(int argc, char *argv[]) // // Register message handlers - connectionHandler->registerHandler(MSG_ACCOUNT, accountHandler); + connectionHandler->registerHandler(CMSG_LOGIN, accountHandler); + connectionHandler->registerHandler(CMSG_REGISTER, accountHandler); + connectionHandler->registerHandler(CMSG_SAY, chatHandler); + connectionHandler->registerHandler(CMSG_ANNOUNCE, chatHandler); //LOG_INFO("The Mana World Server v" << PACKAGE_VERSION) PACKAGE_VERSION undeclared session->startListen(connectionHandler.get(), SERVER_PORT); diff --git a/src/messagein.cpp b/src/messagein.cpp index 065c7052..f2faaf2c 100644 --- a/src/messagein.cpp +++ b/src/messagein.cpp @@ -52,7 +52,7 @@ char MessageIn::readByte() return -1; } -unsigned short MessageIn::readShort() +short MessageIn::readShort() { if (packet) // if Packet exists { @@ -70,7 +70,7 @@ unsigned short MessageIn::readShort() return -1; } -unsigned long MessageIn::readLong() +long MessageIn::readLong() { if (packet) // if Packet exists { diff --git a/src/messagein.h b/src/messagein.h index 0de64d46..beb20310 100644 --- a/src/messagein.h +++ b/src/messagein.h @@ -45,8 +45,8 @@ class MessageIn char readByte(); /**< Reads a byte. */ - unsigned short readShort(); /**< Reads a short. */ - unsigned long readLong(); /**< Reads a long. */ + short readShort(); /**< Reads a short. */ + long readLong(); /**< Reads a long. */ /** * Reads a string. If a length is not given (-1), it is assumed diff --git a/src/messageout.cpp b/src/messageout.cpp index 2aca011a..9c3ea453 100644 --- a/src/messageout.cpp +++ b/src/messageout.cpp @@ -45,18 +45,18 @@ void MessageOut::writeByte(char value) packet->length += sizeof(char); } -void MessageOut::writeShort(unsigned short value) +void MessageOut::writeShort(short value) { - packet->expand(sizeof(unsigned short)); - memcpy(&packet->data[packet->length], (void*)&value, sizeof(unsigned short)); - packet->length += sizeof(unsigned short); + packet->expand(sizeof(short)); + memcpy(&packet->data[packet->length], (void*)&value, sizeof(short)); + packet->length += sizeof(short); } -void MessageOut::writeLong(unsigned long value) +void MessageOut::writeLong(long value) { - packet->expand(sizeof(unsigned long)); - memcpy(&packet->data[packet->length], (void*)&value, sizeof(unsigned long)); - packet->length += sizeof(unsigned long); + packet->expand(sizeof(long)); + memcpy(&packet->data[packet->length], (void*)&value, sizeof(long)); + packet->length += sizeof(long); } void MessageOut::writeString(const std::string &string, int length) diff --git a/src/messageout.h b/src/messageout.h index fd7d1752..d48cb899 100644 --- a/src/messageout.h +++ b/src/messageout.h @@ -42,8 +42,8 @@ class MessageOut ~MessageOut(); void writeByte(char value); /**< Reads a byte. */ - void writeShort(unsigned short value);/**< Reads a short. */ - void writeLong(unsigned long value); /**< Reads a long. */ + void writeShort(short value); /**< Reads a short. */ + void writeLong(long value); /**< Reads a long. */ /** * Writes a string. If a fixed length is not given (-1), it is stored |