Skip to content

Commit

Permalink
link fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Sep 2, 2024
1 parent 618b595 commit 6a6e7dc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
17 changes: 16 additions & 1 deletion server/central/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl ServerStateData {

login.and_then(|user| {
// if link_code is None, or it matches, return the entry
if link_code.map(|code| code == user.link_code).unwrap_or(true) {
if link_code.map(|code| code == user.link_code && user.link_code != 0).unwrap_or(true) {
Some(user)
} else {
None
Expand All @@ -232,6 +232,21 @@ impl ServerStateData {
},
);
}

pub fn remove_login_code(&mut self, name: &str) {
let lowercase = name.trim_start().to_lowercase();

let mut hasher = DefaultHasher::new();
lowercase.hash(&mut hasher);
let hash = hasher.finish();

let data = self.last_logins.get_mut(&hash);

// reset so people cant relog again, just in case
if let Some(data) = data {
data.link_code = 0;
}
}
}

// both roa::Context and RwLock have the methods read() and write()
Expand Down
18 changes: 12 additions & 6 deletions server/central/src/web/routes/game_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,34 @@ pub struct UserLookupResponse {

#[get("/gsp/lookup?<username>&<link_code>&<bypass>")]
pub async fn p_user_lookup(
state: &State<ServerState>,
s_state: &State<ServerState>,
password: GameServerPasswordGuard,
username: &str,
link_code: u32,
bypass: Option<bool>,
) -> WebResult<Json<UserLookupResponse>> {
let state = state.state_read().await;
let state = s_state.state_read().await;

if !password.verify(&state.config.game_server_password) {
unauthorized!("invalid gameserver credentials");
}

debug!("link code: {link_code}, bypass: {bypass:?}");

if let Some(login) = state.get_login(username, if bypass.unwrap_or(false) { None } else { Some(link_code) }) {
Ok(Json(UserLookupResponse {
let response = if let Some(login) = state.get_login(username, if bypass.unwrap_or(false) { None } else { Some(link_code) }) {
UserLookupResponse {
account_id: login.account_id,
name: login.name.clone(),
}))
}
} else {
not_found!("Failed to find user by given username");
}
};

drop(state);

s_state.state_write().await.remove_login_code(username);

Ok(Json(response))
}

#[derive(Deserialize)]
Expand Down
23 changes: 17 additions & 6 deletions src/ui/menu/settings/link_code_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,28 @@ bool LinkCodePopup::setup() {

void LinkCodePopup::onLinkCodeReceived(uint32_t linkCode) {
label->setScale(0.7f);
label->setString(fmt::format("Code: {}", linkCode).c_str());
label->setString("Code: ****");

Build<ButtonSprite>::create("Copy", "bigFont.fnt", "GJ_button_01.png", 0.7)
CCMenu* btnLayout = Build<CCMenu>::create()
.layout(RowLayout::create()->setGap(5.f)->setAutoScale(false))
.pos(util::ui::getPopupLayoutAnchored(m_size).center - CCSize{0.f, 25.f})
.parent(m_mainLayer)
.collect();

Build<ButtonSprite>::create("Show", "bigFont.fnt", "GJ_button_01.png", 0.7f)
.intoMenuItem([this, linkCode] {
label->setString(fmt::format("Code: {}", linkCode).c_str());
})
.parent(btnLayout);

Build<ButtonSprite>::create("Copy", "bigFont.fnt", "GJ_button_01.png", 0.7f)
.intoMenuItem([linkCode] {
geode::utils::clipboard::write(std::to_string(linkCode));
Notification::create("Copied to clipboard", NotificationIcon::Success, 1.f);
})
.pos(util::ui::getPopupLayoutAnchored(m_size).center - CCSize{0.f, 25.f})
.intoNewParent(CCMenu::create())
.pos(0.f, 0.f)
.parent(m_mainLayer);
.parent(btnLayout);

btnLayout->updateLayout();
}

LinkCodePopup* LinkCodePopup::create() {
Expand Down

0 comments on commit 6a6e7dc

Please sign in to comment.