Skip to content

Commit 3a7e91d

Browse files
committed
api & team badge
1 parent 9357627 commit 3a7e91d

15 files changed

+261
-208
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ project(AvalancheIndex VERSION 1.2.2)
1212

1313
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.cpp incl/*.cpp)
1414
add_library(${PROJECT_NAME} SHARED ${SOURCES})
15-
target_include_directories(AvalancheIndex PRIVATE ${CMAKE_SOURCE_DIR})
15+
16+
target_include_directories(AvalancheIndex PUBLIC ${CMAKE_SOURCE_DIR}/incl)
17+
target_include_directories(AvalancheIndex PRIVATE ${CMAKE_SOURCE_DIR}/src)
1618

1719
if(NOT DEFINED ENV{GEODE_SDK})
1820
message(FATAL_ERROR "Unable to find Geode SDK! Please define GEODE_SDK environment variable to point to Geode")

changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#### Changes
55
- **Added** team member name in Badge information pop-up
66
- **Fixed** team data cache handling from API requests
7-
- Minor code cleanup
7+
- Major code cleanup
88

99
#### Developers
1010
- **Tweaked** constructors for `Profile` and `Project` classes

incl/Avalanche.hpp

Lines changed: 33 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <Geode/Geode.hpp>
1313

1414
#include <Geode/utils/web.hpp>
15-
#include <Geode/utils/terminate.hpp>
1615

1716
using namespace geode::prelude;
1817
using namespace matjson;
@@ -50,7 +49,7 @@ namespace avalanche { // Avalanche Index mod namespace
5049

5150
// Node IDs for badges
5251
namespace Nodes {
53-
inline constexpr auto None = "";
52+
inline constexpr auto None = "none"_spr;
5453
inline constexpr auto Cubic = "cubic-studios-badge"_spr;
5554
inline constexpr auto Director = "director-badge"_spr;
5655
inline constexpr auto Manager = "team-manager-badge"_spr;
@@ -60,7 +59,7 @@ namespace avalanche { // Avalanche Index mod namespace
6059

6160
// Sprite IDs for badges
6261
namespace Sprites {
63-
inline constexpr auto None = "";
62+
inline constexpr auto None = "no-badge"_spr;
6463
inline constexpr auto Cubic = "cubic-studios.png"_spr;
6564
inline constexpr auto Director = "director.png"_spr;
6665
inline constexpr auto Manager = "team-manager.png"_spr;
@@ -84,6 +83,7 @@ namespace avalanche { // Avalanche Index mod namespace
8483

8584
// Avalanche member profile class
8685
struct Profile {
86+
// The type of Avalanche badge
8787
enum class Badge {
8888
NONE, // No badge
8989
CUBIC, // Staff of Cubic Studios
@@ -106,6 +106,7 @@ namespace avalanche { // Avalanche Index mod namespace
106106

107107
// Avalanche project level class
108108
struct Project {
109+
// The type of Avalanche project
109110
enum class Type {
110111
NONE, // Not a project
111112
SOLO, // A project that a member worked on by themself
@@ -184,98 +185,42 @@ namespace avalanche { // Avalanche Index mod namespace
184185
static constexpr const char* toString(Project::Type type);
185186
};
186187

187-
// Fetch all remote data on badges and levels, automatically checks "Fetch Data Once" setting
188+
/**
189+
* Fetch all remote data on badges and levels, automatically checks "Fetch Data Once" setting
190+
*/
188191
static void scanAll();
189192

190-
// Get profile data on a player
191-
static Profile GetProfile(
192-
int id // The player's account ID
193-
);
194-
// Get project data on a level
195-
static Project GetProject(
196-
int id // The level's ID
197-
);
198-
199-
// Check if the profile belongs to a team member
193+
/**
194+
* Get profile data on a player
195+
*
196+
* @param id The player's account ID
197+
*
198+
* @returns The player's team profile data
199+
*/
200+
static Profile GetProfile(int id);
201+
202+
/**
203+
* Get project data on a level
204+
*
205+
* @param id The level's ID
206+
*
207+
* @returns The level's team project data
208+
*/
209+
static Project GetProject(int id);
210+
211+
/**
212+
* Check if the profile belongs to a team member
213+
*
214+
* @param badge The team member's badge type
215+
*
216+
* @returns Whether this member is actually a part of Avalanche
217+
*/
200218
static bool isTeamMember(Profile::Badge badge);
201219

202220
// Get the comment text color for a certain badge type
203221
static ccColor3B getCommentColor(Profile::Badge badge);
204222

205-
static void getBadgeInfo(Profile::Badge badge, CCString* name);
206-
void onInfoBadge(CCObject* sender);
207-
208-
// Create badge and format comment for a player
209-
template <typename T>
210-
void createBadge(
211-
T pointer, // Parent class
212-
Profile profile, // Member's profile object
213-
CCMenu* cell_menu, // Username menu to add the badge to
214-
TextArea* cmntText = nullptr, // Comment text node
215-
CCLabelBMFont* cmntFont = nullptr, // Comment font node
216-
float size = 0.625f // Scale of the badge sprite
217-
) {
218-
log::debug("Creating badge for {}...", profile.name);
219-
auto idString = Handler::Badges::getBadgeID(profile.badge); // gets the string equivalent
220-
221-
if (idString) {
222-
// get the badge item
223-
auto badge = Handler::Badges::fromBadgeID(std::string(idString));
224-
225-
if (cell_menu == nullptr) {
226-
log::debug("No username menu provided");
227-
} else {
228-
log::debug("Found username menu for {}...", profile.name);
229-
230-
try {
231-
// prevent dupes
232-
if (auto alreadyBadge = cell_menu->getChildByID(idString)) alreadyBadge->removeMeAndCleanup();
233-
234-
auto newBadge = Handler::Badges::getSpriteName(badge); // gets sprite filename
235-
236-
CCSprite* badgeBtnSprite = CCSprite::createWithSpriteFrameName(newBadge);
237-
badgeBtnSprite->setScale(size);
238-
239-
CCMenuItemSpriteExtra* badgeBtn = CCMenuItemSpriteExtra::create(
240-
badgeBtnSprite,
241-
pointer,
242-
menu_selector(Handler::onInfoBadge));
243-
badgeBtn->setID(idString);
244-
badgeBtn->setZOrder(1);
245-
246-
badgeBtn->setUserObject("profile"_spr, CCString::create(profile.name));
247-
248-
cell_menu->addChild(badgeBtn);
249-
cell_menu->updateLayout();
250-
} catch (std::exception& e) {
251-
log::error("Failed to create badge for {}...", profile.name);
252-
};
253-
254-
log::info("Finished creating badge for {}", profile.name);
255-
};
256-
257-
if (cmntText == nullptr && cmntFont == nullptr) {
258-
log::debug("No comment text node provided");
259-
} else {
260-
log::debug("Found comment text node for {}...", profile.name);
261-
auto col = Handler::Badges::getBadgeColor(badge);
262-
263-
if (cmntText) {
264-
cmntText->colorAllCharactersTo(col);
265-
cmntText->setOpacity(255);
266-
} else if (cmntFont) {
267-
cmntFont->setColor(col);
268-
cmntFont->setOpacity(255);
269-
} else {
270-
log::error("No comment text node found");
271-
};
272-
273-
log::info("Finished changing comment text color for {}", profile.name);
274-
};
275-
} else {
276-
log::error("Badge is invalid.");
277-
};
278-
};
223+
static void getBadgeInfo(Profile::Badge badge, std::string name);
279224
};
280225
};
281226

incl/src/Avalanche.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ namespace avalanche {
209209
if (id > 0) {
210210
log::info("Fetching profile of ID {}", id);
211211

212-
auto cacheKey = fmt::format("cache-badge-p{}", (int)id); // format the save key string
212+
auto cacheKey = fmt::format("cache-badge-p{}", id); // format the save key string
213213
Value cacheStd = AVAL_MOD->getSavedValue<Value>(cacheKey, Value()); // gets locally saved badge json
214214

215215
if (AVAL_MOD->getSettingValue<bool>("web-once")) { // only fetch from cached web data if this setting is on
@@ -228,7 +228,7 @@ namespace avalanche {
228228
};
229229

230230
if (cacheStd.isNull()) {
231-
log::error("Player {} is no longer associated with Avalanche", (int)id);
231+
log::error("Player {} is no longer associated with Avalanche", id);
232232
return Profile();
233233
} else {
234234
log::debug("Processing cache for player {}", id);
@@ -243,7 +243,7 @@ namespace avalanche {
243243
return res;
244244
};
245245
} else {
246-
log::error("Profile ID {} is invalid", (int)id);
246+
log::error("Profile ID {} is invalid", id);
247247
return Profile();
248248
};
249249
};
@@ -252,7 +252,7 @@ namespace avalanche {
252252
if (id > 0) {
253253
log::info("Fetching project of ID {}", id);
254254

255-
auto cacheKey = fmt::format("cache-level-p{}", (int)id); // format the save key string
255+
auto cacheKey = fmt::format("cache-level-p{}", id); // format the save key string
256256
Value cacheStd = AVAL_MOD->getSavedValue<Value>(cacheKey, Value()); // gets locally saved level json
257257

258258
if (AVAL_MOD->getSettingValue<bool>("web-once")) { // only fetch from cached web data if this setting is on
@@ -324,7 +324,7 @@ namespace avalanche {
324324
return Handler::Badges::getBadgeColor(badge);
325325
};
326326

327-
void Handler::getBadgeInfo(Profile::Badge badge, CCString* name) {
327+
void Handler::getBadgeInfo(Profile::Badge badge, std::string name) {
328328
auto title = "Oops!";
329329
auto description = "This badge has <cr>no available information</c>. This is likely unintentional, please report it as an issue in the mod's repository.";
330330
auto button = "Learn More";
@@ -333,27 +333,27 @@ namespace avalanche {
333333
switch (badge) {
334334
case Profile::Badge::CUBIC:
335335
title = "Cubic Studios";
336-
description = fmt::format("<cg>{}</c> is a <cy>staff member</c> of <cj>Cubic Studios</c>. They partake in the activities of a department of Cubic, and may supervise or join projects such as <cl>Avalanche</c>.", name->getCString()).c_str();
336+
description = fmt::format("<cg>{}</c> is a <cy>staff member</c> of <cj>Cubic Studios</c>. They partake in the activities of a department of Cubic, and may supervise or join projects such as <cl>Avalanche</c>.", name).c_str();
337337
break;
338338

339339
case Profile::Badge::DIRECTOR:
340340
title = "Avalanche Director";
341-
description = fmt::format("<cg>{}</c> is the <co>director</c> of <cl>Avalanche</c>. They run the whole team.", name->getCString()).c_str();
341+
description = fmt::format("<cg>{}</c> is the <co>director</c> of <cl>Avalanche</c>. They run the whole team.", name).c_str();
342342
break;
343343

344344
case Profile::Badge::MANAGER:
345345
title = "Avalanche Manager";
346-
description = fmt::format("<cg>{}</c> is a <cy>manager</c> of <cl>Avalanche</c>. They manage team projects and collaborations.", name->getCString()).c_str();
346+
description = fmt::format("<cg>{}</c> is a <cy>manager</c> of <cl>Avalanche</c>. They manage team projects and collaborations.", name).c_str();
347347
break;
348348

349349
case Profile::Badge::MEMBER:
350350
title = "Avalanche Team Member";
351-
description = fmt::format("<cg>{}</c> is a <cg>member</c> of <cl>Avalanche</c>. They partake in team projects and collaborations.", name->getCString()).c_str();
351+
description = fmt::format("<cg>{}</c> is a <cg>member</c> of <cl>Avalanche</c>. They partake in team projects and collaborations.", name).c_str();
352352
break;
353353

354354
case Profile::Badge::COLLABORATOR:
355355
title = "Team Collaborator";
356-
description = fmt::format("<cg>{}</c> is a <cg>collaborator</c> of <cl>Avalanche</c>. They've directly worked on the crew's or team's projects as an outsider.", name->getCString()).c_str();
356+
description = fmt::format("<cg>{}</c> is a <cg>collaborator</c> of <cl>Avalanche</c>. They've directly worked on the crew's or team's projects as an outsider.", name).c_str();
357357
break;
358358

359359
default:
@@ -370,15 +370,4 @@ namespace avalanche {
370370
if (btn2) web::openLinkInBrowser(url);
371371
}, true);
372372
};
373-
374-
// badge button event
375-
void Handler::onInfoBadge(CCObject* sender) {
376-
// gets the node that triggered the function
377-
auto ptr = static_cast<CCMenuItemSpriteExtra*>(sender);
378-
379-
auto badge = Handler::Badges::fromBadgeID(ptr->getID());
380-
auto name = static_cast<CCString*>(ptr->getUserObject("profile"_spr));
381-
382-
Handler::getBadgeInfo(badge, name);
383-
};
384373
};

mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"community": "https://www.dsc.gg/cubic"
2424
},
2525
"issues": {
26-
"info": "You may report any significant issues you experience with this mod in the AvalancheIndex repository.",
26+
"info": "You may report any significant issues you experience with this mod to us at the AvalancheIndex repository.",
2727
"url": "https://www.github.com/CubicCommunity/AvalancheIndex/issues/"
2828
},
2929
"dependencies": {

src/BadgeHelper.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <Debugger.hpp>
2+
3+
#include <Avalanche.hpp>
4+
5+
#include "./headers/TeamBadgeItem.hpp"
6+
7+
#include <Geode/Geode.hpp>
8+
9+
using namespace geode::prelude;
10+
using namespace avalanche;
11+
12+
class BadgeHelper {
13+
public:
14+
// Get the BadgeHelper functions
15+
static BadgeHelper* get() {
16+
static BadgeHelper ptr;
17+
return &ptr;
18+
};
19+
20+
/**
21+
* Create badge and format comment for a player
22+
*
23+
* @param profile Member's profile object
24+
* @param cell_menu Username menu to add the badge to
25+
* @param cmntText Comment text node
26+
* @param cmntFont Comment font node
27+
* @param size Scale of the badge sprite
28+
*/
29+
void createBadge(
30+
Profile profile,
31+
CCMenu* cell_menu,
32+
TextArea* cmntText = nullptr,
33+
CCLabelBMFont* cmntFont = nullptr,
34+
float size = 0.625f
35+
) {
36+
AVAL_LOG_DEBUG("Creating badge for {}...", profile.name);
37+
auto idString = Handler::Badges::getBadgeID(profile.badge); // gets the string equivalent
38+
39+
if (idString) {
40+
// get the badge item
41+
auto badge = Handler::Badges::fromBadgeID(std::string(idString));
42+
43+
if (cell_menu == nullptr) {
44+
AVAL_LOG_DEBUG("No username menu provided");
45+
} else {
46+
AVAL_LOG_DEBUG("Found username menu for {}...", profile.name);
47+
48+
try {
49+
// prevent dupes
50+
if (auto alreadyBadge = cell_menu->getChildByID(idString)) alreadyBadge->removeMeAndCleanup();
51+
52+
auto newBadge = Handler::Badges::getSpriteName(badge); // gets sprite filename
53+
54+
CCSprite* badgeBtnSprite = CCSprite::createWithSpriteFrameName(newBadge);
55+
badgeBtnSprite->setScale(size);
56+
57+
CCMenuItemSpriteExtra* badgeBtn = TeamBadgeItem::create(profile, size);
58+
59+
cell_menu->addChild(badgeBtn);
60+
cell_menu->updateLayout();
61+
} catch (std::exception& e) {
62+
AVAL_LOG_ERROR("Failed to create badge for {}...", profile.name);
63+
};
64+
65+
AVAL_LOG_INFO("Finished creating badge for {}", profile.name);
66+
};
67+
68+
if (cmntText == nullptr && cmntFont == nullptr) {
69+
AVAL_LOG_DEBUG("No comment text node provided");
70+
} else {
71+
AVAL_LOG_DEBUG("Found comment text node for {}...", profile.name);
72+
auto col = Handler::Badges::getBadgeColor(badge);
73+
74+
if (cmntText) {
75+
cmntText->colorAllCharactersTo(col);
76+
cmntText->setOpacity(255);
77+
} else if (cmntFont) {
78+
cmntFont->setColor(col);
79+
cmntFont->setOpacity(255);
80+
} else {
81+
AVAL_LOG_ERROR("No comment text node found");
82+
};
83+
84+
AVAL_LOG_INFO("Finished changing comment text color for {}", profile.name);
85+
};
86+
} else {
87+
AVAL_LOG_ERROR("Badge is invalid.");
88+
};
89+
};
90+
};

src/Debugger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include <incl/Avalanche.hpp>
2+
#include <Avalanche.hpp>
33

44
using namespace avalanche;
55

0 commit comments

Comments
 (0)