source: network-game/common/Player.cpp@ cee623e

Last change on this file since cee623e was cee623e, checked in by dportnoy <dmp1488@…>, 12 years ago

Fixed a bug in serializing the player name

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#include "Player.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstring>
6
7using namespace std;
8
9Player::Player()
10{
11 this->id = 0;
12 this->name = "";
13 this->password = "";
14 this->pos.x = 0;
15 this->pos.y = 0;
16}
17
18Player::Player(const Player& p)
19{
20 this->id = p.id;
21 this->name = p.name;
22 this->password = p.password;
23 this->pos.x = p.pos.x;
24 this->pos.y = p.pos.y;
25 this->addr = p.addr;
26}
27
28Player::Player(string name, string password)
29{
30 this->id = 0;
31 this->name = name;
32 this->password = password;
33 this->pos.x = 200;
34 this->pos.y = 200;
35}
36
37Player::Player(string name, sockaddr_in addr)
38{
39 this->id = 0;
40 this->name = name;
41 this->password = "";
42 this->pos.x = 200;
43 this->pos.y = 200;
44 this->addr = addr;
45}
46
47Player::~Player()
48{
49}
50
51void Player::serialize(char* buffer)
52{
53 ostringstream oss;
54
55 cout << "Player name: " << this->name << endl;
56
57 /*
58 oss.write((char*)&(this->id), sizeof(int));
59 oss << this->name;
60 cout << "first oss str: " << oss.str() << endl;
61 oss.write("\0", 1);
62 cout << "second oss str: " << oss.str() << endl;
63 oss.write((char*)&(this->pos.x), sizeof(int));
64 cout << "third oss str: " << oss.str() << endl;
65 oss.write((char*)&(this->pos.y), sizeof(int));
66 */
67
68 oss << this->id;
69 oss << this->name;
70 oss << '\0';
71 oss << this->pos.x;
72 oss << this->pos.y;
73
74 memcpy(buffer, oss.str().c_str(), oss.str().length());
75}
76
77void Player::deserialize(char* buffer)
78{
79 istringstream iss;
80 iss.str(buffer);
81
82 /*
83 iss.read((char*)&(this->id), sizeof(int));
84 iss >> this->name;
85 iss.read((char*)&(this->pos.x), sizeof(int));
86 iss.read((char*)&(this->pos.y), sizeof(int));
87 */
88
89 iss >> this->id;
90 iss >> this->name;
91 iss >> this->pos.x;
92 iss >> this->pos.y;
93}
94
95void Player::setId(int id)
96{
97 this->id = id;
98}
99
100void Player::setAddr(sockaddr_in addr)
101{
102 this->addr = addr;
103}
Note: See TracBrowser for help on using the repository browser.