-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalGameplayController.cpp
More file actions
157 lines (139 loc) · 6.71 KB
/
LocalGameplayController.cpp
File metadata and controls
157 lines (139 loc) · 6.71 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "Constants.h"
#include "GameController.h"
#include "GameServer.h"
#include "LocalGameplayController.h"
#include "LocalGameplayMessenger.h"
#include "NetworkController.h"
#include "PlayerController.h"
#include "PopupController.h"
#include "RoomController.h"
#include "UDPMulticastClient.h"
#include "UDPMulticastServer.h"
#include "LocalNetworkManager.h"
#include "UserConfiguration.h"
#include "StatsController.h"
LocalGameplayController* LocalGameplayController::mLocalGameplayController = nullptr;
LocalGameplayController::LocalGameplayController() { this->lanGamesHosted = new LANGameHostedOnNetwork(); }
LocalGameplayController::~LocalGameplayController() {}
LocalGameplayController* LocalGameplayController::getInstance() {
if (mLocalGameplayController == nullptr) {
mLocalGameplayController = new LocalGameplayController();
mLocalGameplayController->onEnter();
mLocalGameplayController->initListeners(NULL);
mLocalGameplayController->isHostedGameRefreshRequestInProgress = false;
mLocalGameplayController->hostTablePressed = false;
}
return mLocalGameplayController;
}
bool LocalGameplayController::hostGame() {
/*
* Once the gameserver and the room controller is up then we join the current player first
* only then we enable the udp broadcast
*/
this->hostTablePressed = true;
NotificationInterface::getInstance()->sendCrashlyticsLog("Hosting Local Game Start");
this->stopDiscovery();
GameServer* gameServer = GameServer::getInstance();
RoomController* roomController = RoomController::getInstance();
LocalGameplayMessenger* messenger = LocalGameplayMessenger::getInstance();
if (gameServer->start(messenger) && roomController->start()) {
int gameMode = roomController->getRoomConfig()->getGameMode(); // game mode will
std::string address = LocalNetworkManager::getInstance()->getWifiIPDetails(); // get address fron jni interface
CCLOG("AYUSH LOCAL HOST TABLE address: %s", address.c_str());
if (address != "") {
std::string ipAaddress = "ws://" + address + ":" + std::to_string(gameServer->getPort());
std::string tableName = LocalGameplayController::getHostNameFromRoomId(roomController->getRoomId());
int avatarId = UserConfiguration::getInstance()->getOfflineAvatarID();
if (GameController::sharedController()->joinLocalRoom(gameMode, "LocalGameplayController", ipAaddress, true)) {
std::shared_ptr<UDPMessage> udpMessage = std::make_shared<UDPMessage>(
gameMode, ipAaddress, tableName, avatarId);
if (UDPMulticastServer::getInstance()->start(udpMessage, address)) {
NotificationInterface::getInstance()->sendCrashlyticsLog("Hosting Local Game Success");
StatsController::sharedController()->debug("lan", "LocalGameplayController", "hostGame", "Success");
return true;
}
}
}
}
CCLOGERROR("LocalGameplayController: Error while hosting game");
NotificationInterface::getInstance()->sendCrashlyticsLog("Hosting Local Game Fail");
StatsController::sharedController()->debug("lan", "LocalGameplayController", "hostGame", "Error");
this->stopGameWithError();
return false;
}
void LocalGameplayController::stopGameWithError() {
this->stopGame();
this->startDiscovery();
}
void LocalGameplayController::stopGame() {
UDPMulticastServer::getInstance()->stop();
RoomController::getInstance()->stop();
this->stopDiscovery();
}
bool LocalGameplayController::startDiscovery() {
std::string address = LocalNetworkManager::getInstance()->getWifiIPDetails();
if (address != "") {
StatsController::sharedController()->debug(
"lan", "LocalGameplayController", "startDiscovery", "Success");
return UDPMulticastClient::getInstance()->start(address);
}
StatsController::sharedController()->debug(
"lan", "LocalGameplayController", "startDiscovery", "Error");
return false;
}
void LocalGameplayController::stopDiscovery() {
UDPMulticastClient::getInstance()->stop();
}
void LocalGameplayController::joinLocalRoom(const std::string ipAddress) {
this->hostTablePressed = false;
GameController::sharedController()->joinLocalRoom(GameMode::GAME_MODE_NORMAL, "LocalGameplayController", ipAddress);
}
void LocalGameplayController::refreshLANGamesHosted() {
this->lanGamesHosted->clearAllLANServer();
if (this->isHostedGameRefreshRequestInProgress) {
CCLOG("LAN Game Refresh request already in progress");
} else {
UDPMulticastClient::getInstance()->getMessages();
this->isHostedGameRefreshRequestInProgress = true;
this->scheduleOnce(schedule_selector(LocalGameplayController::checkAndPopulateLANGameHosted),
(UDPMulticastServer::INTERVAL * 5) / (float) 1000);
}
}
void LocalGameplayController::checkAndPopulateLANGameHosted(float) {
auto messages = UDPMulticastClient::getInstance()->getMessages();
std::string address = LocalNetworkManager::getInstance()->getWifiIPDetails();
for (const auto& message : *messages) {
if (message->getAddress().find(address) == std::string::npos) {
this->lanGamesHosted->addLANServer(message->getAddress(), message->getAvatarId(), message->getTableName());
}
}
NotificationCenter::getInstance()->postNotification(Events::EVENT_UPDATED_LANGAME_HOSTED, this->lanGamesHosted);
this->isHostedGameRefreshRequestInProgress = false;
}
void LocalGameplayController::setLocalGameplayShowExperimentValue(Ref* obj) {
if (Globals::activeSession && !Globals::activeSession->is_offline) {
UserConfiguration::getInstance()->setLANGamePlayExp(
ExperimentController::sharedController()->getVariantForExperiment(Experiments::TPG_SHOW_LAN_GAME));
}
}
bool LocalGameplayController::isToShowLANGame() {
return (UserConfiguration::getInstance()->getLANGamePlayExp() == 2);
}
void LocalGameplayController::initListeners(Ref* target) {
NotificationCenter::getInstance()->addObserver(this,
callfuncO_selector(LocalGameplayController::setLocalGameplayShowExperimentValue),
Events::EVENT_CONFIG_LOADED, NULL);
}
std::string LocalGameplayController::getHostNameFromRoomId(const std::string roomId) {
if (roomId.length() > 40) {
return roomId.substr(40);
} else {
return roomId;
}
}
bool LocalGameplayController::isHostTablePressed() {
return this->hostTablePressed;
}
void LocalGameplayController::removeListeners() {}
void LocalGameplayController::registerListener(Ref* target, const char* event) {}
void LocalGameplayController::deregisterListener(const char* event) {}