-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.cpp
More file actions
55 lines (45 loc) · 1.37 KB
/
game.cpp
File metadata and controls
55 lines (45 loc) · 1.37 KB
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
#include "game.h"
Game::Game(QString gameId, QString hostPlayer,QTcpSocket* hostSocket)
:gameId(gameId), hostPlayer(hostPlayer),hostSocket(hostSocket){
}
void Game::addPlayer(QString _joinPlayer, QTcpSocket *_joinSocket){
joinPlayer = _joinPlayer;
joinSocket = _joinSocket;
}
QString Game::getHostPlayer() const
{
return hostPlayer;
}
QTcpSocket *Game::getRecieverSocket(QTcpSocket *senderSocket){
if(senderSocket->peerAddress() == hostSocket->peerAddress()
&& senderSocket->peerPort() == hostSocket->peerPort()){
return joinSocket;
} else {
return hostSocket;
}
}
QString Game::getJoinPlayer() const
{
return joinPlayer;
}
bool Game::socketExists(QTcpSocket *socket){
QHostAddress socketAddr = socket->peerAddress();
int socketPort = socket->peerPort();
bool matchesHost = false;
bool matchesJoin = false;
if(hostSocket){
QHostAddress hostAddr = hostSocket->peerAddress();
int hostPort = hostSocket->peerPort();
matchesHost = (hostAddr == socketAddr && hostPort == socketPort);
}
if(joinSocket){
QHostAddress joinAddr = joinSocket->peerAddress();
int joinPort = joinSocket->peerPort();
matchesJoin = (joinAddr == socketAddr && joinPort == socketPort);
}
return matchesJoin || matchesHost;
}
QString Game::getGameId() const
{
return gameId;
}