Compare commits

..

2 commits

Author SHA1 Message Date
b60e632e80
remove init code from db.rs 2020-05-04 19:47:48 -04:00
d8a5542abc
move things into aux file 2020-05-04 19:47:27 -04:00
5 changed files with 121 additions and 59 deletions

3
.gitignore vendored
View file

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

76
aux/filter_raw_character_data Executable file
View file

@ -0,0 +1,76 @@
#!/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))

8
aux/get_recruit_data Executable file
View file

@ -0,0 +1,8 @@
#!/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"

32
aux/init_db Executable file
View file

@ -0,0 +1,32 @@
#!/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,4 +1,3 @@
use log::debug;
use serenity::prelude::TypeMapKey;
use sqlx::{
sqlite::{SqliteConnection, SqlitePool},
@ -15,7 +14,9 @@ pub(crate) struct DbConnPool {
impl DbConnPool {
pub async fn new() -> Result<Self, Error> {
Ok(Self {
pool: init_pool().await?,
pool: SqlitePool::builder()
.build(&env::var("DATABASE_URL").unwrap())
.await?,
})
}
}
@ -23,59 +24,3 @@ impl DbConnPool {
impl TypeMapKey for DbConnPool {
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)
}