Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / client / src / session_browser_impl.cpp
blob08a5dba3a039b4321450b9fb49de3139db061f7b
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2013-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "stdpch.h"
23 #include "session_browser_impl.h"
24 #include "nel/gui/lua_object.h"
25 #include "nel/gui/lua_ihm.h"
26 #include "connection.h"
27 #include "net_manager.h"
28 #include "interface_v3/interface_manager.h"
29 #include "interface_v3/skill_manager.h"
30 #include "far_tp.h"
31 #include "r2/dmc/com_lua_module.h"
33 #include "nel/misc/time_nl.h"
34 #include "nel/net/service.h"
36 #include "r2/editor.h"
37 #include "r2/dmc/client_edition_module.h"
39 #include "game_share/shard_names.h"
41 #ifdef DEBUG_NEW
42 #define new DEBUG_NEW
43 #endif
45 using namespace std;
46 using namespace NLMISC;
47 using namespace RSMGR;
48 using namespace CHARSYNC;
49 using namespace NLNET;
50 using namespace R2;
52 CVariable<uint16> SBSPortOffset("client", "SBSPortOffset", "Offset of the SBS port from the FS port", 1000, 0, true);
55 // ****************************************************************************
56 void CSessionBrowserImpl::init(CLuaState *ls)
58 if (ls != NULL)
60 nlassert(ls);
61 _Lua = ls;
62 _Lua->pushGlobalTable();
63 CLuaObject game(*_Lua);
64 game = game["game"];
65 game.setValue("getRingSessionList", luaGetRingSessionList);
66 game.setValue("getRingCharList", luaGetRingCharList);
67 game.setValue("getRingStats", luaGetRingStats);
68 game.setValue("getScenarioScores", luaGetScenarioScores);
69 game.setValue("getSessionAverageScores", luaGetSessionAverageScores);
70 game.setValue("getScenarioAverageScores", luaGetScenarioAverageScores);
71 game.setValue("updateScenarioScores", luaUpdateScenarioScores);
72 game.setValue("joinRingSession", luaJoinRingSession);
73 game.setValue("checkRingAccess", luaCheckRingAccess);
74 game.setValue("getFileHeader", luaGetFileHeader);
77 if (!ClientCfg.Local)
79 CSessionBrowserImpl::getInstance().setAuthInfo(getCookie());
80 NLNET::CInetAddress address(getFrontEndAddress());
81 address.setPort(address.port()+SBSPortOffset);
82 CSessionBrowserImpl::getInstance().connectItf(address);
85 _LastAuthorRating = 0;
86 _LastAMRating = 0;
87 _LastMasterlessRating = 0;
88 _LastRingPoints.clear();
89 _LastMaxRingPoints.clear();
93 // ***************************************************************************
94 const NLNET::CLoginCookie &CSessionBrowserImpl::getCookie()
96 /*if (CInterfaceManager::getInstance()->isInGame())
97 {*/
98 return NetMngr.getLoginCookie();
99 /*}
100 extern NLNET::CLoginCookie FakeCookie;
101 return FakeCookie; // TMP TMP : for Nico's test*/
104 // ****************************************************************************
105 const std::string &CSessionBrowserImpl::getFrontEndAddress()
107 if (!NetMngr.getFrontendAddress().empty())
109 return NetMngr.getFrontendAddress();
111 static std::string testAddress = "borisb";
112 return testAddress; // TMP TMP : for Nico's test
115 // ****************************************************************************
116 uint32 CSessionBrowserImpl::getCharId()
118 if (ClientCfg.Local) return 0;
119 return (getCookie().getUserId() << 4) + (uint32) PlayerSelectedSlot;
122 // ****************************************************************************
123 int CSessionBrowserImpl::luaGetRingSessionList(CLuaState &ls)
125 nldebug("SB: luaGetRingSessionList");
126 CLuaIHM::checkArgCount(ls, "getRingSessionList", 0);
127 CSessionBrowserImpl::getInstance().getRingRatings(getCharId());
128 CSessionBrowserImpl::getInstance().getSessionList(getCharId());
129 return 0;
132 // ****************************************************************************
133 int CSessionBrowserImpl::luaGetRingCharList(CLuaState &ls)
135 nldebug("SB: luaGetRingCharList");
136 CLuaIHM::checkArgCount(ls, "getRingCharList", 0);
137 if (R2::getEditor().getMode() != R2::CEditor::NotInitialized)
139 CSessionBrowserImpl::getInstance().getCharList(getCharId(), R2::getEditor().getDMC().getEditionModule().getCurrentAdventureId());
141 return 0;
144 // ****************************************************************************
145 int CSessionBrowserImpl::luaGetRingStats(CLuaState &ls)
147 nldebug("SB: luaGetRingStats");
148 CLuaIHM::checkArgCount(ls, "getRingStats", 0);
149 CSessionBrowserImpl::getInstance().getRingRatings(getCharId());
150 CSessionBrowserImpl::getInstance().getRingPoints(getCharId());
151 return 0;
154 // ****************************************************************************
155 int CSessionBrowserImpl::luaGetScenarioScores(CLuaState &ls)
157 nldebug("SB: luaGetScenarioScores");
158 CLuaIHM::checkArgCount(ls, "getScenarioScores", 0);
159 if (R2::getEditor().getMode() != R2::CEditor::NotInitialized)
161 CSessionBrowserImpl::getInstance().getMyRatings(getCharId(), R2::getEditor().getDMC().getEditionModule().getCurrentAdventureId());
163 return 0;
166 // ****************************************************************************
167 int CSessionBrowserImpl::luaGetSessionAverageScores(CLuaState &ls)
169 nldebug("SB: luaGetSessionAverageScores");
170 CLuaIHM::checkArgCount(ls, "getSessionAverageScores", 0);
171 if (R2::getEditor().getMode() != R2::CEditor::NotInitialized)
173 CSessionBrowserImpl::getInstance().getSessionAverageScores(R2::getEditor().getDMC().getEditionModule().getCurrentAdventureId());
175 return 0;
178 // ****************************************************************************
179 int CSessionBrowserImpl::luaGetScenarioAverageScores(CLuaState &ls)
181 nldebug("SB: luaGetScenarioAverageScores");
182 CLuaIHM::checkArgCount(ls, "getScenarioAverageScores", 1);
184 CLuaIHM::checkArgType(ls, "getScenarioAverageScores", 1, LUA_TSTRING);
186 CSessionBrowserImpl::getInstance().getScenarioAverageScores(ls.toString(1));
188 return 0;
191 // ****************************************************************************
192 int CSessionBrowserImpl::luaUpdateScenarioScores(CLuaState &ls)
194 nldebug("SB: luaUpdateScenarioScores");
195 const char *funcName = "updateScenarioScores";
196 CLuaIHM::checkArgCount(ls, funcName, 5);
197 CLuaIHM::checkArgType(ls, funcName, 1, LUA_TNUMBER);
198 CLuaIHM::checkArgType(ls, funcName, 2, LUA_TNUMBER);
199 CLuaIHM::checkArgType(ls, funcName, 3, LUA_TNUMBER);
200 CLuaIHM::checkArgType(ls, funcName, 4, LUA_TNUMBER);
201 CLuaIHM::checkArgType(ls, funcName, 5, LUA_TNUMBER);
203 if (R2::getEditor().getMode() != R2::CEditor::NotInitialized)
205 CSessionBrowserImpl::getInstance().setPlayerRating(getCharId(), R2::getEditor().getDMC().getEditionModule().getCurrentAdventureId(),
206 (uint32) ls.toInteger(1), (uint32) ls.toInteger(2), (uint32) ls.toInteger(3), (uint32) ls.toInteger(4), (uint32) ls.toInteger(5));
209 return 0;
212 // ****************************************************************************
213 int CSessionBrowserImpl::luaJoinRingSession(CLuaState &ls)
215 nldebug("SB: luaJoinRingSession");
216 const char *funcName = "joinRingSession";
217 CLuaIHM::checkArgCount(ls, funcName, 1);
218 CLuaIHM::checkArgType(ls, funcName, 1, LUA_TNUMBER);
220 CInterfaceManager *pIM = CInterfaceManager::getInstance();
221 CSessionBrowserImpl & sessionBrowser = CSessionBrowserImpl::getInstance();
222 sessionBrowser.joinSession(getCharId(), (TSessionId)(uint32) ls.toInteger(1), ClientCfg.ConfigFile.getVar("Application").asString(0));
224 if(!sessionBrowser.waitOneMessage(sessionBrowser.getMessageName("on_joinSessionResult")))
226 nlwarning("joinSession callback return false");
229 if(sessionBrowser._LastJoinSessionResult == 20)
231 CViewText* pVT = dynamic_cast<CViewText*>(CWidgetManager::getInstance()->getElementFromId("ui:interface:warning_free_trial:text"));
232 if (pVT != NULL)
233 pVT->setText(CI18N::get("uiRingWarningFreeTrial"));
234 CAHManager::getInstance()->runActionHandler("enter_modal", NULL, "group=ui:interface:warning_free_trial");
237 return 0;
240 // *****************************************************************************
241 int CSessionBrowserImpl::luaCheckRingAccess(CLuaState &ls)
243 lua_State* state = ls.getStatePointer();
244 return R2::CComLuaModule::luaGetFileHeader(state);
248 // *****************************************************************************
249 int CSessionBrowserImpl::luaGetFileHeader(CLuaState &ls)
251 lua_State* state = ls.getStatePointer();
252 return R2::CComLuaModule::luaGetFileHeader(state);
256 // ****************************************************************************
257 void CSessionBrowserImpl::on_connectionFailed()
259 nldebug("SB: on_connectionFailed");
260 callRingAccessPointMethod("onConnectionFailed", 0, 0);
263 // ****************************************************************************
264 void CSessionBrowserImpl::on_CRingSessionManagerWebClient_Disconnection(NLNET::TSockId /* from */)
266 nldebug("SB: on_CRingSessionManagerWebClient_Disconnection");
267 callRingAccessPointMethod("onDisconnection", 0, 0);
270 // ****************************************************************************
271 void CSessionBrowserImpl::on_connectionClosed()
273 nldebug("SB: on_connectionClosed");
274 callRingAccessPointMethod("onConnectionClosed", 0, 0);
277 // ****************************************************************************
278 void CSessionBrowserImpl::on_invokeResult(NLNET::TSockId /* from */, uint32 /* userId */, uint32 resultCode, const std::string &resultString)
280 nldebug("SB: on_invokeResult : result = %u, msg='%s'", resultCode, resultString.c_str());
282 _LastInvokeResult = resultCode;
283 _LastInvokeResultMsg = resultString;
286 // ****************************************************************************
287 void CSessionBrowserImpl::on_scheduleSessionResult(NLNET::TSockId /* from */, uint32 charId, TSessionId sessionId, uint8 result, const std::string &resultString)
289 nldebug("SB: on_scheduleSessionResult : result = %u, msg = '%s'", result, resultString.c_str());
290 _LastScheduleSessionCharId = charId;
291 _LastScheduleSessionResult = result;
292 _LastScheduleSessionId = sessionId;
293 _LastScheduleSessionResulMsg = resultString;
294 // if (result == 0)
295 // {
296 // // attempt real far tp
297 // if (!FarTP.requestFarTPToSession(sessionId, PlayerSelectedSlot, CFarTP::JoinSession, true))
298 // {
299 // callRingAccessPointMethod("onJoinFailed", 0, 0);
300 // }
301 // }
302 // else
303 // {
304 // nlwarning(resultString.c_str());
305 // callRingAccessPointMethod("onJoinFailed", 0, 0);
306 // }
309 // ****************************************************************************
310 void CSessionBrowserImpl::on_sessionInfoResult(NLNET::TSockId from, uint32 charId, TSessionId sessionId, const RSMGR::TRaceFilter &raceFilter, const RSMGR::TReligionFilter &religionFilter,
311 const RSMGR::TGuildFilter &guildFilter, const RSMGR::TShardFilter &shardFilter, const RSMGR::TLevelFilter &levelFilter, bool subscriptionClosed, bool autoInvite, const std::string &language,
312 const TSessionOrientation &/* orientation */, const std::string &description)
314 nldebug("SB: on_sessionInfoResult");
315 _LastRaceFilter = raceFilter;
316 _LastReligionFilter = religionFilter;
317 _LastGuildFilter = guildFilter;
318 _LastShardFilter = shardFilter;
319 _LastLevelFilter = levelFilter;
320 _LastSubscriptionClosed = subscriptionClosed;
321 _LastAutoInvite = autoInvite;
322 _LastDescription = description;
325 // ****************************************************************************
326 void CSessionBrowserImpl::on_joinSessionResult(NLNET::TSockId /* from */, uint32 /* userId */, TSessionId sessionId, uint32 result, const std::string &shardAddr, const RSMGR::TSessionPartStatus &participantStatus)
328 nldebug("SB: on_joinSessionResult : result = %u; msg = '%s'", result, shardAddr.c_str());
329 _LastJoinSessionResult = result;
330 _LastJoinSessionId = sessionId;
331 _LastJoinSessionShardAddr = shardAddr;
332 _LastJoinSessionPartStatus = participantStatus;
334 // trigger the far tp action
335 if (result == 0)
337 CInterfaceManager *pIM = CInterfaceManager::getInstance();
338 CAHManager::getInstance()->runActionHandler("on_connect_to_shard", NULL, string("cookie=")+NetMngr.getLoginCookie().toString()+"|fsAddr="+shardAddr);
342 // ****************************************************************************
343 void CSessionBrowserImpl::on_joinSessionResultExt(NLNET::TSockId from, uint32 userId, TSessionId sessionId, uint32 result, const std::string &shardAddr, const RSMGR::TSessionPartStatus &participantStatus, const CSecurityCode& securityCode)
345 nldebug("SB: on_joinSessionResultExt : result = %u, msg = '%s'", result, shardAddr.c_str());
346 FarTP.setJoinSessionResult(sessionId, securityCode); // only joinSessionResultExt grants fast disconnection
348 on_joinSessionResult(from, userId, sessionId, result, shardAddr, participantStatus);
351 // ****************************************************************************
352 void CSessionBrowserImpl::on_getShardsResult(NLNET::TSockId /* from */, uint32 /* userId */, const std::string &/* result */)
354 nldebug("SB: on_getShardsResult");
357 // ****************************************************************************
358 void CSessionBrowserImpl::on_CSessionBrowserServerWebClient_Disconnection(NLNET::TSockId /* from */)
360 nldebug("SB: on_CSessionBrowserServerWebClient_Disconnection");
361 callRingAccessPointMethod("onDisconnection", 0, 0);
364 // ****************************************************************************
365 void CSessionBrowserImpl::on_sessionList(NLNET::TSockId /* from */, uint32 /* charId */, const std::vector <RSMGR::TSessionDesc > &sessions)
367 nldebug("SB: on_sessionList");
368 fill(sessions);
371 // ****************************************************************************
372 void CSessionBrowserImpl::fill(const std::vector <RSMGR::TSessionDesc > &sessions)
374 // build datas & send to lua
375 nlassert(_Lua);
378 CLuaStackRestorer lsr(_Lua, _Lua->getTop());
379 _Lua->newTable();
380 for (uint k = 0; k < sessions.size(); ++k)
382 const RSMGR::TSessionDesc &sd = sessions[k];
383 _Lua->newTable();
384 CLuaObject session(*_Lua);
385 session.setValue("Id", sd.getSessionId().asInt());
386 session.setValue("Owner", sd.getOwnerName());
387 session.setValue("Title", sd.getTitle());
388 session.setValue("Desc", sd.getDescription());
389 session.setValue("Level", (uint32) sd.getSessionLevel().getValue());
390 session.setValue("Language", sd.getLanguage());
392 uint flags = (sd.getAnimMode().getValue() == RSMGR::TAnimMode::am_dm ? (uint) 1 : 0) |
393 (sd.getRequesterCharInvited() ? (uint) 2 : 0);
394 if(sd.getRequesterCharKicked())
395 flags = (uint) 4;
397 session.setValue("Flags", flags);
398 session.setValue("PlayerCount", sd.getNbConnectedPlayer());
399 session.setValue("AllowFreeTrial", sd.getAllowFreeTrial());
401 session.setValue("NbRating", sd.getNbRating());
402 session.setValue("RateFun", sd.getRateFun());
403 session.setValue("RateDifficulty", sd.getRateDifficulty());
404 session.setValue("RateAccessibility", sd.getRateAccessibility());
405 session.setValue("RateOriginality", sd.getRateOriginality());
406 session.setValue("RateDirection", sd.getRateDirection());
408 session.setValue("ScenarioRRPTotal", sd.getScenarioRRPTotal());
410 session.setValue("AuthorRating", _LastAuthorRating);
411 if(sd.getAnimMode().getValue() == RSMGR::TAnimMode::am_dm)
412 session.setValue("OwnerRating", _LastAMRating);
413 else
414 session.setValue("OwnerRating", _LastMasterlessRating);
416 // calculate the difference between local time and gmt
417 time_t rawtime;
418 struct tm * timeinfo;
420 rawtime= sd.getLaunchDate();
421 timeinfo = localtime ( &rawtime );
422 time_t localTime = mktime( timeinfo );
423 //nldebug("local time %02d:%02d",timeinfo->tm_hour,timeinfo->tm_min);
425 rawtime= sd.getLaunchDate();
426 timeinfo = gmtime ( &rawtime );
427 time_t gmtTime = mktime( timeinfo );
428 //nldebug("gm time %02d:%02d",timeinfo->tm_hour,timeinfo->tm_min);
430 // convert GMT time value from server to local time
431 time_t adjustedTime= sd.getLaunchDate() + localTime - gmtTime;
432 session.setValue("LaunchDate", (sint64)adjustedTime);
434 session.setValue("ScenarioType", (uint32)sd.getOrientation().getValue());
435 session.push();
436 _Lua->rawSetI(-2, k +1); // set in session list
438 // call into lua
439 callRingAccessPointMethod("onSessionListReceived", 1, 0);
441 catch(const ELuaError &)
443 // no-op (error msg already printed at exception launch)
447 // ****************************************************************************
448 void CSessionBrowserImpl::playerRatingFill(bool scenarioRated, uint32 rateFun, uint32 rateDifficulty, uint32 rateAccessibility, uint32 rateOriginality, uint32 rateDirection)
450 nlassert(_Lua);
453 _Lua->newTable();
454 CLuaObject scores(*_Lua);
456 scores.setValue("ScenarioRated", scenarioRated);
457 scores.setValue("RateFun", rateFun);
458 scores.setValue("RateDifficulty", rateDifficulty);
459 scores.setValue("RateAccessibility", rateAccessibility);
460 scores.setValue("RateOriginality", rateOriginality);
461 scores.setValue("RateDirection", rateDirection);
463 scores.push();
465 // call into lua
466 callScenarioScoresMethod("onScenarioScoresReceived", 1, 0);
468 catch(const ELuaError &)
470 // no-op (error msg already printed at exception launch)
474 // ****************************************************************************
475 void CSessionBrowserImpl::averageScoresFill(bool scenarioRated, uint32 rateFun, uint32 rateDifficulty, uint32 rateAccessibility, uint32 rateOriginality, uint32 rateDirection, uint32 rrpTotal)
477 nlassert(_Lua);
480 _Lua->newTable();
481 CLuaObject scores(*_Lua);
483 scores.setValue("ScenarioRated", scenarioRated);
484 scores.setValue("RateFun", rateFun);
485 scores.setValue("RateDifficulty", rateDifficulty);
486 scores.setValue("RateAccessibility", rateAccessibility);
487 scores.setValue("RateOriginality", rateOriginality);
488 scores.setValue("RateDirection", rateDirection);
489 scores.setValue("RRPTotal", rrpTotal);
491 scores.push();
493 // call into lua
494 callScenarioScoresMethod("onAverageScoresReceived", 1, 0);
496 catch(const ELuaError &)
498 // no-op (error msg already printed at exception launch)
503 // ****************************************************************************
504 static RSMGR::TSessionDesc buildSession(uint32 id, const std::string &owner, const std::string &title, const std::string &description, uint32 level, uint32 playerCount, const std::string &/* language */, uint32 launchTime, bool dm, bool invited)
506 RSMGR::TSessionDesc result;
507 result.setSessionId((TSessionId)id);
508 result.setRequesterCharInvited(invited);
509 result.setOwnerName(owner);
510 result.setTitle(title);
511 result.setDescription(description);
512 result.setSessionLevel(R2::TSessionLevel((R2::TSessionLevel::TValues) level));
513 result.setLaunchDate(launchTime);
514 result.setNbConnectedPlayer(playerCount);
515 result.setAnimMode(RSMGR::TAnimMode(dm ? RSMGR::TAnimMode::am_dm : RSMGR::TAnimMode::am_autonomous));
516 return result;
519 // ****************************************************************************
520 void CSessionBrowserImpl::testFill()
522 std::vector <RSMGR::TSessionDesc > sessions;
523 uint32 refTime = NLMISC::CTime::getSecondsSince1970();
524 sessions.push_back(buildSession(50, "_toto", "Scenar de toto", "Fight scenario", 0, 0, "en", refTime - 1000, true, true));
525 sessions.push_back(buildSession(51, "_titi", "Titi's scenario", "Un peu de RP", 0, 4, "en", refTime - 2000, false, true));
526 sessions.push_back(buildSession(52, "_bob", "Yubo's back", "Chasse aux yubos", 0, 10, "en", refTime - 3000, true, false));
527 sessions.push_back(buildSession(54, "_nico", "Nico test", "Scenario de test de nico", 1, 3, "fr", refTime - 10000, false, false));
528 sessions.push_back(buildSession(55, "_toto2", "Scenar de toto", "Fight scenario", 1, 0, "de", refTime - 20000, true, true));
529 sessions.push_back(buildSession(56, "_titi2", "Titi's scenario", "Un peu de RP", 1, 4, "it", refTime - 40000, true, true));
530 sessions.push_back(buildSession(57, "_bob2", "Yubo's back", "Chasse aux yubos", 2, 10, "cz", refTime - 60000, true, true));
531 sessions.push_back(buildSession(59, "_nico2", "Nico test", "Scenario de test de nico", 3, 3, "fr", refTime - 100000, true, true));
532 sessions.push_back(buildSession(510, "_toto3", "Scenar de toto", "Fight scenario", 3, 0, "de", refTime - 200000, true, true));
533 sessions.push_back(buildSession(511, "_titi3", "Titi's scenario", "Un peu de RP", 4, 4, "en", refTime - 300000, true, true));
534 sessions.push_back(buildSession(512, "_bob3", "Yubo's back", "Chasse aux yubos", 4, 10, "fr", refTime - 500000, true, true));
535 sessions.push_back(buildSession(514, "_nico3", "Nico test", "Scenario de test de nico", 5, 3, "en", refTime - 800000, true, true));
536 sessions.push_back(buildSession(515, "_toto4", "Scenar de toto", "Fight scenario", 5, 0, "fr", refTime - 1000000, true, true));
537 sessions.push_back(buildSession(516, "_titi'", "Titi's scenario", "Un peu de RP", 5, 4, "en", refTime - 2000000, true, true));
538 sessions.push_back(buildSession(517, "_bob4", "Yubo's back", "Chasse aux yubos", 5, 10, "fr", refTime - 3000000, true, true));
539 sessions.push_back(buildSession(519, "_nico4", "Nico test", "Scenario de test de nico", 5, 3, "en", refTime - 5000000, true, true));
540 fill(sessions);
543 // ****************************************************************************
544 void CSessionBrowserImpl::on_charList(NLNET::TSockId /* from */, uint32 /* charId */, TSessionId /* sessionId */, const std::vector <RSMGR::TCharDesc > &chars)
546 nldebug("SB: on_charList");
547 charsFill(chars);
550 // ****************************************************************************
551 void CSessionBrowserImpl::on_playerRatings(NLNET::TSockId /* from */, uint32 /* charId */, bool scenarioRated, uint32 rateFun, uint32 rateDifficulty, uint32 rateAccessibility, uint32 rateOriginality, uint32 rateDirection)
553 playerRatingFill(scenarioRated, rateFun, rateDifficulty, rateAccessibility, rateOriginality, rateDirection);
556 // ****************************************************************************
557 // Return average scores of a session
558 void CSessionBrowserImpl::on_sessionAverageScores(NLNET::TSockId /* from */, bool scenarioRated, uint32 rateFun, uint32 rateDifficulty, uint32 rateAccessibility, uint32 rateOriginality, uint32 rateDirection, uint32 rrpTotal)
560 averageScoresFill(scenarioRated, rateFun, rateDifficulty, rateAccessibility, rateOriginality, rateDirection, rrpTotal);
563 // ****************************************************************************
564 // Return average scores of a scenario
565 void CSessionBrowserImpl::on_scenarioAverageScores(NLNET::TSockId /* from */, bool scenarioRated, uint32 rateFun, uint32 rateDifficulty, uint32 rateAccessibility, uint32 rateOriginality, uint32 rateDirection, uint32 rrpTotal)
567 //averageScoresFill(scenarioRated, rateFun, rateDifficulty, rateAccessibility, rateOriginality, rateDirection, rrpTotal);
568 nlassert(_Lua);
571 _Lua->newTable();
572 CLuaObject scores(*_Lua);
574 scores.setValue("ScenarioRated", scenarioRated);
575 scores.setValue("RateFun", rateFun);
576 scores.setValue("RateDifficulty", rateDifficulty);
577 scores.setValue("RateAccessibility", rateAccessibility);
578 scores.setValue("RateOriginality", rateOriginality);
579 scores.setValue("RateDirection", rateDirection);
580 scores.setValue("RRPTotal", rrpTotal);
582 scores.push();
584 // call into lua
585 callScenarioScoresMethod("onScenarioAverageScoresReceived", 1, 0);
587 catch(const ELuaError &)
589 // no-op (error msg already printed at exception launch)
593 // ****************************************************************************
594 void CSessionBrowserImpl::on_ringRatings(NLNET::TSockId /* from */, uint32 /* charId */, uint32 authorRating, uint32 AMRating, uint32 masterlessRating)
596 _LastAuthorRating = authorRating;
597 _LastAMRating = AMRating;
598 _LastMasterlessRating = masterlessRating;
599 ringStatsFill();
602 // ****************************************************************************
603 void CSessionBrowserImpl::on_ringPoints(NLNET::TSockId /* from */, uint32 /* charId */, const std::string &ringPoints, const std::string &maxRingPoints)
605 _LastRingPoints = ringPoints;
606 _LastMaxRingPoints = maxRingPoints;
607 ringStatsFill();
610 // ****************************************************************************
611 void CSessionBrowserImpl::charsFill(const std::vector <RSMGR::TCharDesc > &chars)
613 // build datas & send to lua
614 nlassert(_Lua);
617 CLuaStackRestorer lsr(_Lua, _Lua->getTop());
618 _Lua->newTable();
619 for (uint k = 0; k < chars.size(); ++k)
621 const RSMGR::TCharDesc &cd = chars[k];
622 if(cd.getCharId()!=getCharId())
624 _Lua->newTable();
625 CLuaObject luaChar(*_Lua);
626 luaChar.setValue("Id", cd.getCharId());
627 luaChar.setValue("Char", cd.getCharName());
628 luaChar.setValue("Guild", cd.getGuildName());
629 luaChar.setValue("Race", (uint32) cd.getRace().getValue());
630 luaChar.setValue("Religion", (uint32) cd.getCult().getValue());
632 string shardName = toString(cd.getShardId());
633 for(uint l = 0; l < Mainlands.size(); ++l)
635 if(Mainlands[l].Id.asInt() == cd.getShardId())
637 shardName = toString(Mainlands[l].Name);
638 break;
642 luaChar.setValue("Shard", shardName);
643 // note: we do 'level-1' because the TSessionLevel enum starts at 1
644 luaChar.setValue("Level", (uint32) (cd.getLevel().getValue()-1));
646 uint32 flags = 0;
647 if (cd.getConnected()) { flags += 1; }
648 if (cd.getKicked()){ flags += 2; }
650 uint32 flags = cd.getConnected();
651 if (cd.getKicked()){ flags = 2; }
653 luaChar.setValue("Flags", flags);
654 luaChar.push();
655 _Lua->rawSetI(-2, k +1); // set in chars list
658 // call into lua
659 callRingCharTrackingMethod("onCharsListReceived", 1, 0);
661 catch(const ELuaError &)
663 // no-op (error msg already printed at exception launch)
667 // ****************************************************************************
669 inline uint32 ecoRingPoints(const std::string & ecoPoints, const char * c)
671 std::string::size_type cPlace = ecoPoints.find(c);
672 if(cPlace==string::npos)
673 return 0;
674 std::string::size_type sepPlace = ecoPoints.find(":", cPlace);
675 std::string points = ecoPoints.substr(cPlace+1, sepPlace);
676 uint32 ret;
677 fromString(points, ret);
678 return ret;
681 void CSessionBrowserImpl::ringStatsFill()
683 // build datas & send to lua
684 nlassert(_Lua);
687 CLuaStackRestorer lsr(_Lua, _Lua->getTop());
688 _Lua->newTable();
690 CLuaObject luaRingPoints(*_Lua);
692 luaRingPoints.setValue("AuthorRating", _LastAuthorRating);
693 luaRingPoints.setValue("AMRating", _LastAMRating);
694 luaRingPoints.setValue("MasterlessRating", _LastMasterlessRating);
696 luaRingPoints.setValue("MaxBasicRingPoints", ecoRingPoints(_LastMaxRingPoints, "A"));
697 luaRingPoints.setValue("BasicRingPoints", ecoRingPoints(_LastRingPoints, "A"));
698 luaRingPoints.setValue("MaxDesertRingPoints", ecoRingPoints(_LastMaxRingPoints, "D"));
699 luaRingPoints.setValue("DesertRingPoints", ecoRingPoints(_LastRingPoints, "D"));
700 luaRingPoints.setValue("MaxForestRingPoints", ecoRingPoints(_LastMaxRingPoints, "F"));
701 luaRingPoints.setValue("ForestRingPoints", ecoRingPoints(_LastRingPoints, "F"));
702 luaRingPoints.setValue("MaxJungleRingPoints", ecoRingPoints(_LastMaxRingPoints, "J"));
703 luaRingPoints.setValue("JungleRingPoints", ecoRingPoints(_LastRingPoints, "J"));
704 luaRingPoints.setValue("MaxPrimeRootRingPoints",ecoRingPoints(_LastMaxRingPoints, "R"));
705 luaRingPoints.setValue("PrimeRootRingPoints", ecoRingPoints(_LastRingPoints, "P"));
706 luaRingPoints.setValue("MaxSubtropicRingPoints",ecoRingPoints(_LastMaxRingPoints, "L"));
707 luaRingPoints.setValue("SubtropicRingPoints", ecoRingPoints(_LastRingPoints, "L"));
709 luaRingPoints.push();
711 // call into lua
712 callRingPlayerInfoMethod("onRingStatsPlayerReceving", 1, 0);
714 CSkillManager *pSM = CSkillManager::getInstance();
715 if( pSM )
717 pSM->tryToUnblockTitleFromRingRatings( _LastAuthorRating, _LastAMRating, _LastMasterlessRating );
720 catch(const ELuaError &)
722 // no-op (error msg already printed at exception launch)
726 // ****************************************************************************
727 static RSMGR::TCharDesc buildChar(uint32 id, const std::string &charName, const std::string &guild,
728 TRace race, TCult cult, uint32 shardId, TSessionLevel level, bool connected)
730 RSMGR::TCharDesc result;
731 result.setCharId(id);
732 result.setShardId(shardId);
733 result.setCharName(charName);
734 result.setGuildName(guild);
735 result.setRace(race);
736 result.setCult(cult);
737 result.setLevel(level);
738 result.setConnected(connected);
739 return result;
742 // ****************************************************************************
743 void CSessionBrowserImpl::testCharsFill()
745 std::vector <RSMGR::TCharDesc > chars;
746 chars.push_back(buildChar(50, "_toto", "Guild de toto", TRace::r_fyros, TCult::c_kami, 242, TSessionLevel::sl_a, false));
747 chars.push_back(buildChar(51, "_titi", "Titi's guild", TRace::r_matis, TCult::c_karavan, 243, TSessionLevel::sl_b, true));
748 chars.push_back(buildChar(52, "_bob", "Yubo guild", TRace::r_matis, TCult::c_karavan, 102, TSessionLevel::sl_c, false));
749 chars.push_back(buildChar(54, "_nico", "Nico guild", TRace::r_matis, TCult::c_kami, 103, TSessionLevel::sl_d, false));
750 chars.push_back(buildChar(55, "_toto2", "Guild de toto", TRace::r_tryker, TCult::c_kami, 103, TSessionLevel::sl_e, true));
751 chars.push_back(buildChar(56, "_titi2", "Titi's guild", TRace::r_matis, TCult::c_karavan, 102, TSessionLevel::sl_f, true));
752 chars.push_back(buildChar(57, "_bob2", "Yubo guild", TRace::r_fyros, TCult::c_neutral, 101, TSessionLevel::sl_a, true));
753 chars.push_back(buildChar(59, "_nico2", "Nico guild", TRace::r_fyros, TCult::c_neutral, 101, TSessionLevel::sl_b, true));
754 chars.push_back(buildChar(510, "_toto3", "Guild de toto", TRace::r_tryker, TCult::c_karavan, 103, TSessionLevel::sl_c, false));
755 chars.push_back(buildChar(511, "_titi3", "Titi's guild", TRace::r_tryker, TCult::c_karavan, 102, TSessionLevel::sl_d, true));
756 chars.push_back(buildChar(512, "_bob3", "Yubo guild", TRace::r_zorai, TCult::c_karavan, 103, TSessionLevel::sl_e, false));
757 chars.push_back(buildChar(514, "_nico3", "Nico guild", TRace::r_zorai, TCult::c_kami, 102, TSessionLevel::sl_f, true));
758 chars.push_back(buildChar(515, "_toto4", "Guild de toto", TRace::r_zorai, TCult::c_karavan, 101, TSessionLevel::sl_a, true));
759 chars.push_back(buildChar(516, "_titi'", "Titi's guild", TRace::r_tryker, TCult::c_karavan, 102, TSessionLevel::sl_b, false));
760 chars.push_back(buildChar(517, "_bob4", "Yubo guild", TRace::r_fyros, TCult::c_neutral, 102, TSessionLevel::sl_c, false));
761 chars.push_back(buildChar(519, "_nico4", "Nico guild", TRace::r_fyros, TCult::c_kami, 103, TSessionLevel::sl_d, true));
762 charsFill(chars);
765 // ****************************************************************************
766 void CSessionBrowserImpl::callRingAccessPointMethod(const char *name, int numArg, int numResult)
768 // when you load an animation, lua state isn't initialized for a short time
769 if(!_Lua) return;
770 nlassert(name);
772 CLuaStackRestorer lsr(_Lua, _Lua->getTop() + numResult);
773 _Lua->pushGlobalTable();
774 CLuaObject rap(*_Lua);
775 rap = rap["RingAccessPoint"];
776 rap.callMethodByNameNoThrow(name, numArg, numResult);
780 // ****************************************************************************
781 void CSessionBrowserImpl::callRingCharTrackingMethod(const char *name, int numArg, int numResult)
783 // when you load an animation, lua state isn't initialized for a short time
784 if(!_Lua) return;
785 nlassert(name);
787 CLuaStackRestorer lsr(_Lua, _Lua->getTop() + numResult);
788 _Lua->pushGlobalTable();
789 CLuaObject rap(*_Lua);
790 rap = rap["CharTracking"];
791 rap.callMethodByNameNoThrow(name, numArg, numResult);
795 // ****************************************************************************
796 void CSessionBrowserImpl::callRingPlayerInfoMethod(const char *name, int numArg, int numResult)
798 // when you load an animation, lua state isn't initialized for a short time
799 if(!_Lua) return;
800 nlassert(name);
802 CLuaStackRestorer lsr(_Lua, _Lua->getTop() + numResult);
803 _Lua->pushGlobalTable();
804 CLuaObject rap(*_Lua);
805 rap = rap["RingPlayerInfo"];
806 rap.callMethodByNameNoThrow(name, numArg, numResult);
810 // ****************************************************************************
811 void CSessionBrowserImpl::callScenarioScoresMethod(const char *name, int numArg, int numResult)
813 // when you load an animation, lua state isn't initialized for a short time
814 if(!_Lua) return;
815 nlassert(name);
817 CLuaStackRestorer lsr(_Lua, _Lua->getTop() + numResult);
818 _Lua->pushGlobalTable();
819 CLuaObject rap(*_Lua);
820 rap = rap["ScenarioScores"];
821 rap.callMethodByNameNoThrow(name, numArg, numResult);