Compare commits

..

No commits in common. "b60e632e807c31a1c462ccdf54dc188d06f429b7" and "ced50b363c9bb65778506022180fb8752c4ac377" have entirely different histories.

5 changed files with 59 additions and 121 deletions

1
.gitignore vendored
View file

@ -3,4 +3,3 @@
*.sqlite-shm *.sqlite-shm
*.sqlite-wal *.sqlite-wal
.env .env
aux/**.json

View file

@ -1,76 +0,0 @@
#!/usr/bin/env python
import json
import sys
raw_json = json.load(sys.stdin)
tags = {
1: "Guard",
2: "Sniper",
3: "Defender",
4: "Medic",
5: "Supporter",
6: "Caster",
7: "Specialist",
8: "Vanguard",
9: "Melee",
10: "Ranged",
11: "Top Operator",
12: "Crowd-Control",
13: "Nuker",
14: "Senior Operator",
15: "Healing",
16: "Support",
17: "Starter",
18: "DP-Recovery",
19: "DPS",
20: "Survival",
21: "AoE",
22: "Defense",
23: "Slow",
24: "Debuff",
25: "Fast-Redeploy",
26: "Shift",
27: "Summon",
28: "Robot"
}
char_data = {}
en_recruitable = {}
cn_recruitable = {}
for key, entry in raw_json.items():
parsed_data = {
"stars": entry["star"],
"tags": entry["tags"],
# 0 = headhunt only, 1 = china only, 2 = global
"recruitment": min(len(entry["recruitment"]), 2)
}
op_name = entry["appellation"]
char_data[op_name] = parsed_data
if parsed_data["recruitment"] == 2:
for tag in entry["tags"]:
if tags[tag] not in en_recruitable:
en_recruitable[tags[tag]] = []
if tags[tag] not in cn_recruitable:
cn_recruitable[tags[tag]] = []
en_recruitable[tags[tag]].append(op_name)
cn_recruitable[tags[tag]].append(op_name)
elif parsed_data["recruitment"] == 1:
for tag in entry["tags"]:
if tags[tag] not in cn_recruitable:
cn_recruitable[tags[tag]] = []
cn_recruitable[tags[tag]].append(op_name)
project_root = sys.argv[1]
with open(project_root + "/aux/char_data.json", "w") as f:
f.write(json.dumps(char_data))
with open(project_root + "/aux/en_recruitable.json", "w") as f:
f.write(json.dumps(en_recruitable))
with open(project_root + "/aux/cn_recruitable.json", "w") as f:
f.write(json.dumps(cn_recruitable))

View file

@ -1,8 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
PROJECT_ROOT=$(cargo locate-project | sed -E 's#.*"(/.*)/Cargo.toml"}#\1#')
DATASET="https://raw.githubusercontent.com/Tsuk1ko/arknights-toolbox/master/src/data/character.json"
curl "$DATASET" | cat | "$PROJECT_ROOT"/aux/filter_raw_character_data "$PROJECT_ROOT"

View file

@ -1,32 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
DB_NAME="data.sqlite"
PROJECT_ROOT=$(cargo locate-project | sed -E 's#.*"(/.*)/Cargo.toml"}#\1#')
if [ ! -f "$PROJECT_ROOT/$DB_NAME" ]; then
sqlite3 "$PROJECT_ROOT/$DB_NAME" <<EOF
CREATE TABLE IF NOT EXISTS Heck (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
count INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS RollCount (
user_id TEXT PRIMARY KEY UNIQUE NOT NULL,
count INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS OperatorCount (
user_id TEXT NOT NULL,
operator TEXT NOT NULL,
count INTEGER NOT NULL,
UNIQUE(user_id, operator)
);
INSERT INTO Heck (id, count) VALUES (1, 0)
ON CONFLICT(id) DO NOTHING;
EOF
else
echo "$DB_NAME exists, exiting."
fi

View file

@ -1,3 +1,4 @@
use log::debug;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use sqlx::{ use sqlx::{
sqlite::{SqliteConnection, SqlitePool}, sqlite::{SqliteConnection, SqlitePool},
@ -14,9 +15,7 @@ pub(crate) struct DbConnPool {
impl DbConnPool { impl DbConnPool {
pub async fn new() -> Result<Self, Error> { pub async fn new() -> Result<Self, Error> {
Ok(Self { Ok(Self {
pool: SqlitePool::builder() pool: init_pool().await?,
.build(&env::var("DATABASE_URL").unwrap())
.await?,
}) })
} }
} }
@ -24,3 +23,59 @@ impl DbConnPool {
impl TypeMapKey for DbConnPool { impl TypeMapKey for DbConnPool {
type Value = Self; type Value = Self;
} }
async fn init_pool() -> Result<DbPool, Error> {
let pool = SqlitePool::builder()
.build(&env::var("DATABASE_URL").unwrap())
.await?;
// Heck table
sqlx::query!(
"CREATE TABLE IF NOT EXISTS Heck (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
count INTEGER NOT NULL
)"
)
.execute(&pool)
.await?;
debug!("Table Heck exists or was created");
// Arknights row roll counting
sqlx::query!(
"CREATE TABLE IF NOT EXISTS RollCount (
user_id TEXT PRIMARY KEY UNIQUE NOT NULL,
count INTEGER NOT NULL
)"
)
.execute(&pool)
.await?;
debug!("Table RollCount exists or was created");
sqlx::query!(
"CREATE TABLE IF NOT EXISTS OperatorCount (
user_id TEXT NOT NULL,
operator TEXT NOT NULL,
count INTEGER NOT NULL,
UNIQUE(user_id, operator)
)"
)
.execute(&pool)
.await?;
debug!("Table OperatorCount exists or was created");
if sqlx::query!("SELECT count FROM Heck")
.fetch_all(&pool)
.await?
.is_empty()
{
debug!("No entries in Heck, inserting default one");
sqlx::query!("INSERT INTO Heck (count) VALUES (0)")
.execute(&pool)
.await?;
}
Ok(pool)
}