Implement shop endpoints

This commit is contained in:
Ethan O'Brien 2024-04-28 20:43:58 -05:00
parent 4fc0cbcfa3
commit 2133f2236d
7 changed files with 156 additions and 23 deletions

View file

@ -217,6 +217,12 @@ async fn card_skill_reinforce(req: HttpRequest, body: String) -> HttpResponse {
#[post("/api/card/evolve")]
async fn card_evolve(req: HttpRequest, body: String) -> HttpResponse { router::card::evolve(req, body) }
#[get("/api/shop")]
async fn shop(req: HttpRequest) -> HttpResponse { router::shop::shop(req) }
#[post("/api/shop/buy")]
async fn shop_buy(req: HttpRequest, body: String) -> HttpResponse { router::shop::buy(req, body) }
#[post("/api/webui/login")]
async fn webui_login(req: HttpRequest, body: String) -> HttpResponse { router::webui::login(req, body) }
@ -270,6 +276,8 @@ async fn main() -> std::io::Result<()> {
println!("Request: {}", req.path());
srv.call(req)
})
.service(shop)
.service(shop_buy)
.service(css)
.service(js)
.service(webui_start_loginbonus)

View file

@ -19,4 +19,5 @@ pub mod gree;
pub mod serial_code;
pub mod web;
pub mod card;
pub mod shop;
pub mod webui;

View file

@ -121,6 +121,28 @@ const LIMIT_ITEMS: i64 = 200000000;
const LIMIT_COINS: i64 = 2000000000;
const LIMIT_PRIMOGEMS: i64 = 2000000000;
pub fn give_shop(master_item_id: i64, count: i64, user: &mut JsonValue) -> bool {
let mut has = false;
for (_j, dataa) in user["shop_list"].members_mut().enumerate() {
if dataa["master_shop_item_id"].as_i64().unwrap() == master_item_id {
has = true;
let new_amount = dataa["count"].as_i64().unwrap() + count;
if new_amount > LIMIT_ITEMS {
return true;
}
dataa["count"] = new_amount.into();
break;
}
}
if !has {
user["shop_list"].push(object!{
master_shop_item_id: master_item_id,
count: count
}).unwrap();
}
false
}
pub fn give_item(master_item_id: i64, amount: i64, user: &mut JsonValue) -> bool {
let mut has = false;
for (_j, dataa) in user["item_list"].members_mut().enumerate() {
@ -213,6 +235,32 @@ pub fn gift_item(item: &JsonValue, reason: &str, user: &mut JsonValue) {
user["home"]["gift_list"].push(to_push).unwrap();
}
pub fn lp_modification(user: &mut JsonValue, change_amount: u64, remove: bool) {
let max = get_user_rank_data(user["user"]["exp"].as_i64().unwrap())["maxLp"].as_u64().unwrap();
let speed = 285; //4 mins, 45 sec
let since_last = timestamp() - user["stamina"]["last_updated_time"].as_u64().unwrap();
let diff = since_last % speed;
let restored = (since_last - diff) / speed;
user["stamina"]["last_updated_time"] = (timestamp() - diff).into();
let mut stamina = user["stamina"]["stamina"].as_u64().unwrap();
if stamina < max {
stamina += restored;
if stamina > max {
stamina = max;
}
}
if remove {
stamina -= change_amount;
} else {
stamina += change_amount;
}
user["stamina"]["stamina"] = stamina.into();
}
// true - added
// false - already has

View file

@ -0,0 +1,32 @@
[
{
"id": 40100011,
"name": "ラブカ×20個",
"masterBillingShopId": 4010001,
"consumeType": 2,
"price": 20,
"masterShopRewardId": 40100011,
"buyLimit": 0,
"startdashTime": 0,
"startdashExpireTime": 0,
"priority": 1,
"spriteName": "0",
"timeResetType": 2,
"masterReleaseLabelId": 1
},
{
"id": 40100012,
"name": "ラブカ×200個",
"masterBillingShopId": 4010001,
"consumeType": 1,
"price": 200,
"masterShopRewardId": 40100012,
"buyLimit": 0,
"startdashTime": 0,
"startdashExpireTime": 0,
"priority": 1,
"spriteName": "0",
"timeResetType": 0,
"masterReleaseLabelId": 1
}
]

View file

@ -267,11 +267,7 @@ pub fn end(req: HttpRequest, body: String) -> HttpResponse {
global::give_item(16005003, 10, &mut user);
global::give_item(17001003, 2, &mut user);
user["stamina"]["stamina"] = (user["stamina"]["stamina"].as_i32().unwrap() - body["use_lp"].as_i32().unwrap()).into();
if user["stamina"]["stamina"].as_i32().unwrap() < 0 {
user["stamina"]["stamina"] = (0).into();
}
user["stamina"]["last_updated_time"] = global::timestamp().into();
global::lp_modification(&mut user, body["use_lp"].as_u64().unwrap(), true);
global::give_exp(body["use_lp"].as_i32().unwrap(), &mut user);

65
src/router/shop.rs Normal file
View file

@ -0,0 +1,65 @@
use crate::encryption;
use json::object;
use crate::router::global;
use json::JsonValue;
use actix_web::{HttpResponse, HttpRequest};
use lazy_static::lazy_static;
use crate::router::userdata;
lazy_static! {
static ref SHOP_INFO: JsonValue = {
let mut info = object!{};
let items = json::parse(include_str!("json/shop_item.json")).unwrap();
for (_i, data) in items.members().enumerate() {
info[data["id"].to_string()] = data.clone();
}
info
};
}
fn get_item_info(id: i64) -> JsonValue {
SHOP_INFO[id.to_string()].clone()
}
pub fn shop(req: HttpRequest) -> HttpResponse {
let key = global::get_login(req.headers(), "");
let user = userdata::get_acc(&key);
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": {
"shop_list": user["shop_list"].clone()
}
};
global::send(resp)
}
pub fn buy(req: HttpRequest, body: String) -> HttpResponse {
let key = global::get_login(req.headers(), &body);
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
let mut user = userdata::get_acc(&key);
let user_home = userdata::get_acc_home(&key);
let item = get_item_info(body["master_shop_item_id"].as_i64().unwrap());
global::remove_gems(&mut user, item["price"].as_i64().unwrap());
global::give_shop(item["masterShopRewardId"].as_i64().unwrap(), item["price"].as_i64().unwrap(), &mut user);
global::lp_modification(&mut user, item["price"].as_u64().unwrap() / 2, false);
userdata::save_acc(&key, user.clone());
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": {
"gem": user["gem"].clone(),
"shop_list": user["shop_list"].clone(),
"gift_list": user_home["home"]["gift_list"].clone(),
"updated_value_list": {
"stamina": user["stamina"].clone()
}
}
};
global::send(resp)
}

View file

@ -208,24 +208,7 @@ fn get_data(auth_key: &str, row: &str) -> JsonValue {
pub fn get_acc(auth_key: &str) -> JsonValue {
let mut user = get_data(auth_key, "userdata");
user["gem"]["total"] = (user["gem"]["charge"].as_i64().unwrap() + user["gem"]["free"].as_i64().unwrap()).into();
let max = global::get_user_rank_data(user["user"]["exp"].as_i64().unwrap())["maxLp"].as_u64().unwrap();
let speed = 285; //4 mins, 45 sec
let since_last = global::timestamp() - user["stamina"]["last_updated_time"].as_u64().unwrap();
let diff = since_last % speed;
let restored = (since_last - diff) / speed;
user["stamina"]["last_updated_time"] = (global::timestamp() - diff).into();
let mut stamina = user["stamina"]["stamina"].as_u64().unwrap();
if stamina < max {
stamina += restored;
if stamina > max {
stamina = max;
}
}
user["stamina"]["stamina"] = stamina.into();
global::lp_modification(&mut user, 0, false);
return user;
}