forked from klusark/PacMacro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.cpp
85 lines (75 loc) · 1.55 KB
/
Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <libwebsockets.h>
#include <cstring>
#include "Player.hpp"
#include "Game.hpp"
#include "Connection.hpp"
std::vector<Player *> players;
std::ostream &operator<<(std::ostream &os, PlayerType const&type) {
switch (type) {
case Pacman:
os << "pacman";
break;
case Inky:
os << "inky";
break;
case Blinky:
os << "blinky";
break;
case Pinky:
os << "pinky";
break;
case Clyde:
os << "clyde";
break;
case Display:
os << "display";
break;
case Control:
os << "control";
break;
}
return os;
}
PlayerType getPlayerType(const char *data) {
char pos[32];
strcpy(pos, data);
for (int i = 0; pos[i]; ++i) {
pos[i] = tolower(pos[i]);
}
if (strcmp(pos, "pacman") == 0) {
return Pacman;
} else if (strcmp(pos, "inky") == 0) {
return Inky;
} else if (strcmp(pos, "pinky") == 0) {
return Pinky;
} else if (strcmp(pos, "blinky") == 0) {
return Blinky;
} else if (strcmp(pos, "clyde") == 0) {
return Clyde;
} else if (strcmp(pos, "display") == 0) {
return Display;
} else if (strcmp(pos, "control") == 0) {
return Control;
} else {
return InvalidType;
}
}
Player::Player() : _type(InvalidType), _pos(0) {
}
void Player::addConnection(Connection *connection) {
_connections.push_back(connection);
}
void Player::removeConnection(Connection *connection) {
for (auto it = _connections.begin(); it != _connections.end(); ++it) {
if (*it == connection) {
_connections.erase(it);
return;
}
}
}
void Player::send(const std::string &data) {
for (Connection *x : _connections) {
x->send(data);
}
}