]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/testdriver/main.cpp
Added the asio framework to start developing a GameServ server
[irc/gameservirc.git] / gameserv-2.0 / testdriver / main.cpp
1 #include <iostream>
2 using namespace std;
3 #include <GameServ/GameLayer/GameObjects/PlayerGO.h>
4 using GameServ::GameLayer::GameObjects::PlayerGO;
5 #include <GameServ/GameLayer/PlayerGL.h>
6 using GameServ::GameLayer::PlayerGL;
7 #include <GameServ/GameLayer/ForestGL.h>
8 using GameServ::GameLayer::ForestGL;
9 #include <GameServ/GameLayer/FightGL.h>
10 using GameServ::GameLayer::FightGL;
11
12 #include <GameServ/GameLayer/GameObjects/ItemGO.h>
13 using GameServ::GameLayer::GameObjects::ItemGO;
14 #include <GameServ/GameLayer/GameObjects/WeaponGO.h>
15 using GameServ::GameLayer::GameObjects::WeaponGO;
16 #include <GameServ/GameLayer/GameObjects/ArmorGO.h>
17 using GameServ::GameLayer::GameObjects::ArmorGO;
18 #include <GameServ/GameLayer/GameObjects/PotionGO.h>
19 using GameServ::GameLayer::GameObjects::PotionGO;
20 #include <GameServ/GameLayer/GameObjects/MonsterGO.h>
21 using GameServ::GameLayer::GameObjects::MonsterGO;
22
23
24 #include <GameServ/GameServException.h>
25 using GameServ::Exceptions::GameServException;
26
27 #include <GameServ/Types.h>
28 using GameServ::Types::IntRange;
29 using GameServ::Types::ItemTypes;
30 using GameServ::Types::Modifiers;
31
32 #include <GameServ/GameLayer/Helpers/InventoryManager.h>
33 using namespace GameServ::GameLayer::Helpers;
34
35 #include <boost/algorithm/string.hpp>
36 #include <boost/format.hpp>
37 using boost::str;
38 using boost::format;
39
40 #include <boost/smart_ptr/shared_ptr.hpp>
41 #include <boost/bind.hpp>
42 #include <boost/enable_shared_from_this.hpp>
43 #include <asio.hpp>
44 using asio::ip::tcp;
45
46 // just to test
47 #include <GameServ/DataLayer/MySQL/MySQLPlayerDAO.h>
48 using GameServ::DataLayer::MySQL::MySQLPlayerDAO;
49 #include <GameServ/DataLayer/MySQL/MySQLItemDAO.h>
50 using GameServ::DataLayer::MySQL::MySQLItemDAO;
51
52 string ItemInfo(boost::shared_ptr<ItemGO> spItem);
53 string MonsterInfo(boost::shared_ptr<MonsterGO> spMonster);
54 string PlayerInfo(boost::shared_ptr<PlayerGO> spPlayer);
55 void DisplayMonster(boost::shared_ptr<MonsterGO> spMonster);
56 void DisplayPlayer(boost::shared_ptr<PlayerGO> spPlayer);
57
58 std::string make_daytime_string()
59 {
60 using namespace std; // For time_t, time and ctime;
61 time_t now = time(0);
62 return ctime(&now);
63 }
64
65 class tcp_connection
66 : public boost::enable_shared_from_this<tcp_connection>
67 {
68 public:
69 typedef boost::shared_ptr<tcp_connection> pointer;
70
71 static pointer create(asio::io_service& io_service)
72 {
73 return pointer(new tcp_connection(io_service));
74 }
75
76 tcp::socket& socket()
77 {
78 return socket_;
79 }
80
81 void start()
82 {
83 message_ = make_daytime_string();
84
85 asio::async_write(socket_, asio::buffer(message_),
86 boost::bind(&tcp_connection::handle_write, shared_from_this(),
87 asio::placeholders::error,
88 asio::placeholders::bytes_transferred));
89 }
90
91 private:
92 tcp_connection(asio::io_service& io_service)
93 : socket_(io_service)
94 {
95 }
96
97 void handle_write(const asio::error_code& /*error*/,
98 size_t /*bytes_transferred*/)
99 {
100 }
101
102 tcp::socket socket_;
103 std::string message_;
104 };
105
106 class tcp_server
107 {
108 public:
109 tcp_server(asio::io_service& io_service)
110 : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
111 {
112 start_accept();
113 }
114
115 private:
116 void start_accept()
117 {
118 tcp_connection::pointer new_connection =
119 tcp_connection::create(acceptor_.io_service());
120
121 acceptor_.async_accept(new_connection->socket(),
122 boost::bind(&tcp_server::handle_accept, this, new_connection,
123 asio::placeholders::error));
124 }
125
126 void handle_accept(tcp_connection::pointer new_connection,
127 const asio::error_code& error)
128 {
129 if (!error)
130 {
131 new_connection->start();
132 start_accept();
133 }
134 }
135
136 tcp::acceptor acceptor_;
137 };
138
139
140 int main()
141 {
142 try
143 {
144 /*PlayerGL pgl;
145 boost::shared_ptr<PlayerGO> spPlayer = pgl.GetById("Kain");
146
147 cout << "Name: " << spPlayer->Name() << endl
148 << "Level: " << spPlayer->LevelNumber() << endl
149 << "Exp: " << spPlayer->Experience() << endl
150 << "Strength: " << spPlayer->Strength() << endl
151 << "Defense: " << spPlayer->Defense() << endl
152 << "Gold: " << spPlayer->Gold() << endl
153 << "Bank: " << spPlayer->Bank() << endl
154 << "Items: " << endl;
155
156 boost::shared_ptr<InventoryManager> spInventory = InventoryManager::Instance();
157 ItemList inventory = spInventory->GetInventory(spPlayer->Id());
158 ItemList::const_iterator iter;
159 for (iter = inventory.begin(); iter != inventory.end(); iter++)
160 {
161 boost::shared_ptr<ItemGO> spItem = (*iter);
162 cout << ItemInfo(spItem) << endl;
163 }
164
165 spInventory->AddItem(spPlayer->Id(), boost::shared_ptr<ItemGO>(new PotionGO("Test potion", 123, 10, 10, 10, 0)));
166 inventory = spInventory->GetInventory(spPlayer->Id());
167 for (iter = inventory.begin(); iter != inventory.end(); iter++)
168 {
169 boost::shared_ptr<ItemGO> spItem = (*iter);
170 cout << ItemInfo(spItem) << endl;
171 }
172
173 boost::shared_ptr<InventoryManager> spInventory2 = InventoryManager::Instance();
174 inventory = spInventory2->GetInventory(spPlayer->Id());
175 for (iter = inventory.begin(); iter != inventory.end(); iter++)
176 {
177 boost::shared_ptr<ItemGO> spItem = (*iter);
178 cout << ItemInfo(spItem) << endl;
179 }
180
181
182 ForestGL forestGL;
183 FightGL fightGL;
184 string cmd = "";
185 while (cmd.compare("quit") != 0)
186 {
187 spPlayer->Health(spPlayer->MaxHealth());
188 spPlayer->Alive(true);
189 DisplayPlayer(spPlayer);
190 getline(cin, cmd);
191
192 if (cmd == "search")
193 {
194 cout << "Searching the forest..." << endl;
195 boost::shared_ptr<MonsterGO> spMonster = forestGL.GetRandomMonsterForPlayer(spPlayer);
196
197 while (spPlayer->Alive() && spMonster->Alive())
198 {
199 DisplayMonster(spMonster);
200 cout << "Hit Enter to attack: " << endl;
201 getline(cin, cmd);
202 unsigned int attack = fightGL.PlayerAttackMonster(spPlayer, spMonster);
203 cout << "You hit " << spMonster->Name() << " for " << attack << endl;
204 if (!spMonster->Alive())
205 {
206 cout << "You have killed " << spMonster->Name() << endl;
207 cout << "He shouts: " << spMonster->DeathCry() << endl;
208 }
209 else
210 {
211 unsigned int mAttack = fightGL.MonsterAttackPlayer(spMonster, spPlayer);
212 cout << spMonster->Name() << " hits you with their "
213 << spMonster->WeaponName() << " for " << mAttack
214 << endl;
215 if (!spPlayer->Alive())
216 {
217 cout << spMonster->Name() << " has killed you!" << endl;
218 }
219 }
220 }
221 }
222 }
223
224 string what = "what";*/
225 //MySQLItemDAO test;
226 //boost::shared_ptr<ItemGO> spItem = test.GetById("3");
227 //spItem->Name("Testin' again!!");
228 //
229 //test.Insert(spItem);
230 //
231 //MySQLPlayerDAO playerTest;
232
233 //map <int, int> frequencies;
234 //int x;
235 //for (x = 0; x < 10000; x++)
236 //{
237 // IntRangetestrange(-100, 100);
238 // int randnum = testrange.Random();
239 // frequencies[randnum]++;
240 //}
241 //map<int, int>::const_iterator iter;
242 //for (iter = frequencies.begin(); iter != frequencies.end(); iter++)
243 //{
244 // cout << iter->first << ": ";
245 // cout << (iter->second / (double)(x + 1)) * 100 << '%' << endl;
246 //}
247
248
249 try
250 {
251 asio::io_service io_service;
252 tcp_server server(io_service);
253 io_service.run();
254 }
255 catch (std::exception& e)
256 {
257 std::cerr << e.what() << std::endl;
258 }
259 }
260 catch (GameServException &e)
261 {
262 cout << e.VerboseError() << endl;
263
264 }
265 cout << "Done. Hit enter to quit." << endl;
266 string temp;
267 getline(cin, temp);
268 return 0;
269 }
270
271 string ItemInfo(boost::shared_ptr<ItemGO> spItem)
272 {
273 return str(format("Name: %1% Type: %2%") % spItem->Name() % ItemTypes::GetName(spItem->Type()));
274 }
275
276 string MonsterInfo(boost::shared_ptr<MonsterGO> spMonster)
277 {
278 string info = str(format("Name: %1% Weapon: %2%\nStrength: %3% Defense %4% ") %
279 spMonster->Name() % spMonster->WeaponName() % spMonster->Strength() %
280 spMonster->Defense());
281 info += str(format("Health: %1%/%2%") % spMonster->Health() % spMonster->MaxHealth());
282 return info;
283 }
284
285 string PlayerInfo(boost::shared_ptr<PlayerGO> spPlayer)
286 {
287 string info = str(format("Name: %1% Level: %2%\nHealth: %3%/%4%") %
288 spPlayer->Name() % spPlayer->LevelNumber() % spPlayer->Health() %
289 spPlayer->MaxHealth());
290 return info;
291 }
292
293 void DisplayMonster(boost::shared_ptr<MonsterGO> spMonster)
294 {
295 cout << "Monster:\n" << MonsterInfo(spMonster) << endl;
296 }
297
298 void DisplayPlayer(boost::shared_ptr<PlayerGO> spPlayer)
299 {
300 cout << "Player:\n" << PlayerInfo(spPlayer) << endl;
301 }