Fully implement /api/live/ranking
This commit is contained in:
parent
5a67be8182
commit
880758b8e1
@ -131,7 +131,7 @@ async fn live_guest(req: HttpRequest, body: String) -> HttpResponse { router::li
|
||||
async fn live_mission(req: HttpRequest, body: String) -> HttpResponse { router::live::mission(req, body) }
|
||||
|
||||
#[post("/api/live/ranking")]
|
||||
async fn live_ranking(req: HttpRequest, body: String) -> HttpResponse { router::live::ranking(req, body) }
|
||||
async fn live_ranking(req: HttpRequest, body: String) -> HttpResponse { router::clear_rate::ranking(req, body) }
|
||||
|
||||
#[post("/api/event")]
|
||||
async fn event(req: HttpRequest, body: String) -> HttpResponse { router::event::event(req, body) }
|
||||
|
@ -5,6 +5,7 @@ use rusqlite::{Connection, params, ToSql};
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use lazy_static::lazy_static;
|
||||
use std::thread;
|
||||
use crate::encryption;
|
||||
|
||||
lazy_static! {
|
||||
static ref ENGINE: Mutex<Option<Connection>> = Mutex::new(None);
|
||||
@ -32,6 +33,28 @@ fn lock_and_exec(command: &str, args: &[&dyn ToSql]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn lock_and_select(command: &str, args: &[&dyn ToSql]) -> Result<String, rusqlite::Error> {
|
||||
loop {
|
||||
match ENGINE.lock() {
|
||||
Ok(mut result) => {
|
||||
if result.is_none() {
|
||||
init(&mut result);
|
||||
}
|
||||
let conn = result.as_ref().unwrap();
|
||||
let mut stmt = conn.prepare(command)?;
|
||||
return stmt.query_row(args, |row| {
|
||||
match row.get::<usize, i64>(0) {
|
||||
Ok(val) => Ok(val.to_string()),
|
||||
Err(_) => row.get(0)
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(_) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn lock_and_select_all(command: &str, args: &[&dyn ToSql]) -> Result<JsonValue, rusqlite::Error> {
|
||||
loop {
|
||||
match ENGINE.lock() {
|
||||
@ -121,8 +144,55 @@ fn get_live_data(id: i64) -> Result<Live, rusqlite::Error> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn live_completed(id: i64, level: i32, failed: bool) {
|
||||
//let live = get_live_id(id);
|
||||
fn update_live_score(id: i64, uid: i64, score: i64) {
|
||||
lock_and_exec("CREATE TABLE IF NOT EXISTS scores (
|
||||
live_id INT NOT NULL PRIMARY KEY,
|
||||
score_data TEXT NOT NULL
|
||||
)", params!());
|
||||
|
||||
let info = lock_and_select("SELECT score_data FROM scores WHERE live_id=?1", params!(id)).unwrap_or(String::from("[]"));
|
||||
let scores = json::parse(&info).unwrap();
|
||||
|
||||
let mut result = array![];
|
||||
let mut current = 0;
|
||||
let mut added = false;
|
||||
for _i in 0..11 {
|
||||
if current >= 10 {
|
||||
break;
|
||||
}
|
||||
if scores[current].is_empty() && !added {
|
||||
added = true;
|
||||
result.push(object!{user: uid, score: score}).unwrap();
|
||||
}
|
||||
if scores[current].is_empty() {
|
||||
break;
|
||||
}
|
||||
if scores[current]["score"].as_i64().unwrap() < score {
|
||||
added = true;
|
||||
result.push(object!{user: uid, score: score}).unwrap();
|
||||
current += 1;
|
||||
}
|
||||
if scores[current]["user"].as_i64().unwrap() == uid && !added {
|
||||
return;
|
||||
}
|
||||
if scores[current]["user"].as_i64().unwrap() == uid {
|
||||
continue;
|
||||
}
|
||||
result.push(scores[current].clone()).unwrap();
|
||||
current += 1;
|
||||
}
|
||||
|
||||
if added {
|
||||
if lock_and_select("SELECT live_id FROM scores WHERE live_id=?1", params!(id)).is_ok() {
|
||||
lock_and_exec("UPDATE scores SET score_data=?1 WHERE live_id=?2", params!(json::stringify(result), id));
|
||||
} else {
|
||||
lock_and_exec("INSERT INTO scores (score_data, live_id) VALUES (?1, ?2)", params!(json::stringify(result), id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn live_completed(id: i64, level: i32, failed: bool, score: i64, uid: i64) {
|
||||
update_live_score(id, uid, score);
|
||||
match get_live_data(id) {
|
||||
Ok(info) => {
|
||||
let value = format!("{}_{}",
|
||||
@ -260,3 +330,35 @@ pub fn clearrate(_req: HttpRequest) -> HttpResponse {
|
||||
};
|
||||
global::send(resp)
|
||||
}
|
||||
|
||||
pub fn ranking(_req: HttpRequest, body: String) -> HttpResponse {
|
||||
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
|
||||
let live = body["master_live_id"].as_i64().unwrap();
|
||||
|
||||
let info = lock_and_select("SELECT score_data FROM scores WHERE live_id=?1", params!(live)).unwrap_or(String::from("[]"));
|
||||
let scores = json::parse(&info).unwrap();
|
||||
|
||||
let mut rank = array![];
|
||||
|
||||
for (i, data) in scores.members().enumerate() {
|
||||
let user = global::get_user(data["user"].as_i64().unwrap(), &object![], false);
|
||||
rank.push(object!{
|
||||
rank: i + 1,
|
||||
user: user["user"].clone(),
|
||||
score: data["score"].as_i64().unwrap(),
|
||||
favorite_card: user["favorite_card"].clone(),
|
||||
guest_smile_card: user["guest_smile_card"].clone(),
|
||||
guest_cool_card: user["guest_cool_card"].clone(),
|
||||
guest_pure_card: user["guest_pure_card"].clone()
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
let resp = object!{
|
||||
"code": 0,
|
||||
"server_time": global::timestamp(),
|
||||
"data": {
|
||||
"ranking_list": rank
|
||||
}
|
||||
};
|
||||
global::send(resp)
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ pub fn friend(req: HttpRequest, body: String) -> HttpResponse {
|
||||
};
|
||||
|
||||
for (_i, uid) in rv_data.members().enumerate() {
|
||||
rv.push(global::get_user(uid.as_i64().unwrap(), &friends)).unwrap();
|
||||
rv.push(global::get_user(uid.as_i64().unwrap(), &friends, false)).unwrap();
|
||||
}
|
||||
|
||||
let resp = object!{
|
||||
@ -60,7 +60,7 @@ pub fn recommend(req: HttpRequest, body: String) -> HttpResponse {
|
||||
|
||||
let mut rv = array![];
|
||||
for (_i, uid) in random.members().enumerate() {
|
||||
let user = global::get_user(uid.as_i64().unwrap(), &friends);
|
||||
let user = global::get_user(uid.as_i64().unwrap(), &friends, false);
|
||||
if user["user"]["friend_request_disabled"].to_string() == "1" || user.is_empty() {
|
||||
continue;
|
||||
}
|
||||
@ -83,7 +83,7 @@ pub fn search(req: HttpRequest, body: String) -> HttpResponse {
|
||||
let friends = userdata::get_acc_friends(&key);
|
||||
|
||||
let uid = body["user_id"].as_i64().unwrap();
|
||||
let user = global::get_user(uid, &friends);
|
||||
let user = global::get_user(uid, &friends, false);
|
||||
|
||||
let resp = object!{
|
||||
"code": 0,
|
||||
|
@ -353,11 +353,12 @@ fn get_cards(arr: JsonValue, user: &JsonValue) -> JsonValue {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
pub fn get_user(id: i64, friends: &JsonValue) -> JsonValue {
|
||||
pub fn get_user(id: i64, friends: &JsonValue, live_data: bool) -> JsonValue {
|
||||
let user = userdata::get_acc_from_uid(id);
|
||||
if !user["error"].is_empty() {
|
||||
return object!{};
|
||||
}
|
||||
|
||||
let mut rv = object!{
|
||||
user: user["user"].clone(),
|
||||
live_data_summary: {
|
||||
@ -384,15 +385,17 @@ pub fn get_user(id: i64, friends: &JsonValue) -> JsonValue {
|
||||
rv["user"].remove("ss_user_id");
|
||||
rv["user"].remove("birthday");
|
||||
|
||||
rv["status"] = if friends["friend_user_id_list"].contains(id) {
|
||||
3
|
||||
} else if friends["pending_user_id_list"].contains(id) {
|
||||
2
|
||||
} else if friends["request_user_id_list"].contains(id) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}.into();
|
||||
if !friends.is_empty() {
|
||||
rv["status"] = if friends["friend_user_id_list"].contains(id) {
|
||||
3
|
||||
} else if friends["pending_user_id_list"].contains(id) {
|
||||
2
|
||||
} else if friends["request_user_id_list"].contains(id) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}.into();
|
||||
}
|
||||
|
||||
rv
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use crate::router::clear_rate::live_completed;
|
||||
|
||||
pub fn retire(_req: HttpRequest, body: String) -> HttpResponse {
|
||||
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
|
||||
live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), true);
|
||||
live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), true, 0, 0);
|
||||
let resp = object!{
|
||||
"code": 0,
|
||||
"server_time": global::timestamp(),
|
||||
@ -86,7 +86,7 @@ pub fn guest(req: HttpRequest, body: String) -> HttpResponse {
|
||||
}).unwrap();
|
||||
} else {
|
||||
if friends["friend_user_id_list"].len() != 0 {
|
||||
guest_list.push(global::get_user(friends["friend_user_id_list"][random_number(0, friends["friend_user_id_list"].len() - 1)].as_i64().unwrap(), &friends)).unwrap();
|
||||
guest_list.push(global::get_user(friends["friend_user_id_list"][random_number(0, friends["friend_user_id_list"].len() - 1)].as_i64().unwrap(), &friends, false)).unwrap();
|
||||
}
|
||||
let expected: usize = 5;
|
||||
if guest_list.len() < expected {
|
||||
@ -97,7 +97,7 @@ pub fn guest(req: HttpRequest, body: String) -> HttpResponse {
|
||||
}
|
||||
|
||||
for (_i, uid) in random.members().enumerate() {
|
||||
let guest = global::get_user(uid.as_i64().unwrap(), &friends);
|
||||
let guest = global::get_user(uid.as_i64().unwrap(), &friends, false);
|
||||
if guest["user"]["friend_request_disabled"].to_string() == "1" || guest.is_empty() {
|
||||
continue;
|
||||
}
|
||||
@ -178,19 +178,6 @@ pub fn mission(_req: HttpRequest, _body: String) -> HttpResponse {
|
||||
global::send(resp)
|
||||
}
|
||||
|
||||
// /api/live/ranking
|
||||
pub fn ranking(_req: HttpRequest, _body: String) -> HttpResponse {
|
||||
//todo
|
||||
let resp = object!{
|
||||
"code": 0,
|
||||
"server_time": global::timestamp(),
|
||||
"data": {
|
||||
"ranking_list": []
|
||||
}
|
||||
};
|
||||
global::send(resp)
|
||||
}
|
||||
|
||||
pub fn start(_req: HttpRequest, _body: String) -> HttpResponse {
|
||||
let resp = object!{
|
||||
"code": 0,
|
||||
@ -265,7 +252,7 @@ pub fn end(req: HttpRequest, body: String) -> HttpResponse {
|
||||
let mut user2 = userdata::get_acc_home(&key);
|
||||
let mut user = userdata::get_acc(&key);
|
||||
|
||||
live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), false);
|
||||
live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), false, body["live_score"]["score"].as_i64().unwrap(), user["user"]["id"].as_i64().unwrap());
|
||||
|
||||
global::gift_item_basic(1, 10000, 4, "You completed a live!", &mut user2);
|
||||
global::gift_item_basic(16005003, 10, 3, "You completed a live!", &mut user2);
|
||||
|
@ -351,7 +351,7 @@ pub fn detail(req: HttpRequest, body: String) -> HttpResponse {
|
||||
let mut user_detail_list = array![];
|
||||
for (_i, data) in body["user_ids"].members().enumerate() {
|
||||
let uid = data.as_i64().unwrap();
|
||||
let user = global::get_user(uid, &friends);
|
||||
let user = global::get_user(uid, &friends, true);
|
||||
user_detail_list.push(user).unwrap();
|
||||
}
|
||||
let resp = object!{
|
||||
|
Loading…
x
Reference in New Issue
Block a user