Last change
on this file since 8f438a5 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
|
Rev | Line | |
---|
[8e540f4] | 1 | #include "Player.h"
|
---|
[2488852] | 2 |
|
---|
| 3 | #include <iostream>
|
---|
[3b8adee] | 4 | #include <sstream>
|
---|
| 5 | #include <cstring>
|
---|
[2488852] | 6 |
|
---|
| 7 | using namespace std;
|
---|
| 8 |
|
---|
[01d0d00] | 9 | Player::Player()
|
---|
| 10 | {
|
---|
| 11 | this->id = 0;
|
---|
| 12 | this->name = "";
|
---|
| 13 | this->password = "";
|
---|
| 14 | this->pos.x = 0;
|
---|
| 15 | this->pos.y = 0;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | Player::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 |
|
---|
[59061f6] | 28 | Player::Player(string name, string password)
|
---|
| 29 | {
|
---|
[01d0d00] | 30 | this->id = 0;
|
---|
[59061f6] | 31 | this->name = name;
|
---|
| 32 | this->password = password;
|
---|
[edfd1d0] | 33 | this->pos.x = 200;
|
---|
| 34 | this->pos.y = 200;
|
---|
[59061f6] | 35 | }
|
---|
| 36 |
|
---|
[8e540f4] | 37 | Player::Player(string name, sockaddr_in addr)
|
---|
[2488852] | 38 | {
|
---|
[01d0d00] | 39 | this->id = 0;
|
---|
[2488852] | 40 | this->name = name;
|
---|
[59061f6] | 41 | this->password = "";
|
---|
[edfd1d0] | 42 | this->pos.x = 200;
|
---|
| 43 | this->pos.y = 200;
|
---|
[2488852] | 44 | this->addr = addr;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[8e540f4] | 47 | Player::~Player()
|
---|
[2488852] | 48 | {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[3b8adee] | 51 | void Player::serialize(char* buffer)
|
---|
[59061f6] | 52 | {
|
---|
[3b8adee] | 53 | ostringstream oss;
|
---|
| 54 |
|
---|
[1106210] | 55 | cout << "Player name: " << this->name << endl;
|
---|
| 56 |
|
---|
| 57 | /*
|
---|
[01d0d00] | 58 | oss.write((char*)&(this->id), sizeof(int));
|
---|
[3b8adee] | 59 | oss << this->name;
|
---|
[1106210] | 60 | cout << "first oss str: " << oss.str() << endl;
|
---|
[5066e27] | 61 | oss.write("\0", 1);
|
---|
[1106210] | 62 | cout << "second oss str: " << oss.str() << endl;
|
---|
[60776f2] | 63 | oss.write((char*)&(this->pos.x), sizeof(int));
|
---|
[1106210] | 64 | cout << "third oss str: " << oss.str() << endl;
|
---|
[60776f2] | 65 | oss.write((char*)&(this->pos.y), sizeof(int));
|
---|
[1106210] | 66 | */
|
---|
| 67 |
|
---|
| 68 | oss << this->id;
|
---|
| 69 | oss << this->name;
|
---|
[cee623e] | 70 | oss << '\0';
|
---|
[3535088] | 71 | oss << this->pos.x;
|
---|
| 72 | oss << this->pos.y;
|
---|
[3b8adee] | 73 |
|
---|
[3535088] | 74 | memcpy(buffer, oss.str().c_str(), oss.str().length());
|
---|
[59061f6] | 75 | }
|
---|
[edfd1d0] | 76 |
|
---|
[3b8adee] | 77 | void Player::deserialize(char* buffer)
|
---|
[edfd1d0] | 78 | {
|
---|
[3b8adee] | 79 | istringstream iss;
|
---|
[60776f2] | 80 | iss.str(buffer);
|
---|
[3b8adee] | 81 |
|
---|
[1106210] | 82 | /*
|
---|
[01d0d00] | 83 | iss.read((char*)&(this->id), sizeof(int));
|
---|
[3b8adee] | 84 | iss >> this->name;
|
---|
[60776f2] | 85 | iss.read((char*)&(this->pos.x), sizeof(int));
|
---|
| 86 | iss.read((char*)&(this->pos.y), sizeof(int));
|
---|
[1106210] | 87 | */
|
---|
| 88 |
|
---|
[3535088] | 89 | iss >> this->id;
|
---|
[1106210] | 90 | iss >> this->name;
|
---|
| 91 | iss >> this->pos.x;
|
---|
| 92 | iss >> this->pos.y;
|
---|
[3b8adee] | 93 | }
|
---|
| 94 |
|
---|
[01d0d00] | 95 | void Player::setId(int id)
|
---|
| 96 | {
|
---|
| 97 | this->id = id;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[3b8adee] | 100 | void Player::setAddr(sockaddr_in addr)
|
---|
| 101 | {
|
---|
| 102 | this->addr = addr;
|
---|
[edfd1d0] | 103 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.