Fixed migration, added field for hash value.

This commit is contained in:
Sam 2021-11-14 03:24:03 -05:00
parent bf3bcd9213
commit 6ab2839eaa
4 changed files with 37 additions and 11 deletions

View File

@ -14,6 +14,7 @@ WITH RECURSIVE sp_series(earned_sp, exp) AS (
-- though slightly modified to account for sqlite lacking functions for floor and exp
-- Floor modification is replacing floor(a) with round(a - 0.5)
-- Exp mofidication is replacing exp(-a) with 1 / (2^(a*1.442695)) where 1.442695 = log(e)/log(2)
-- Bit shifting is used to emulate 2^a, though unfortunately this does have some mild accuracy issues
SELECT earned_sp + 1,
exp +
CASE
@ -34,13 +35,26 @@ FROM sp_series;
UPDATE skill_group
SET exp = skill_group.exp + (SELECT exp FROM _sp_series WHERE earned_sp = skill_group.earned_sp);
-- Progress in earned_sp is tracked in exp now
ALTER TABLE skill_group DROP COLUMN earned_sp;
-- available_sp is now useless to track, automatically recalculated when loading persisted skill groups
ALTER TABLE skill_group DROP COLUMN available_sp;
-- Skills are now tracked in skill_group table as a json blob. Json blob fine since we can just invalidate and let people respec if it doesn't deserialize
ALTER TABLE skill_group ADD COLUMN skills TEXT DEFAULT "" NOT NULL;
CREATE TABLE _skill_group
(
entity_id INTEGER NOT NULL,
skill_group_kind TEXT NOT NULL,
earned_exp INTEGER NOT NULL,
skills TEXT NOT NULL,
hash_val TEXT NOT NULL,
FOREIGN KEY(entity_id) REFERENCES entity(entity_id),
PRIMARY KEY(entity_id,skill_group_kind)
);
INSERT INTO _skill_group
SELECT sg.entity_id, sg.skill_group_kind, sg.exp, "", ""
FROM skill_group sg;
-- Skills now tracked in skill_group table, can ust drop
DROP TABLE skill;
-- Table no longer needed
DROP TABLE skill_group;
-- Rename table to proper name
ALTER TABLE _skill_group RENAME TO skill_group;
-- Temp table no longer needed, drop it
DROP TABLE _sp_series;

View File

@ -166,7 +166,8 @@ pub fn load_character_data(
"
SELECT skill_group_kind,
earned_exp,
skills
skills,
hash_val
FROM skill_group
WHERE entity_id = ?1",
)?;
@ -178,6 +179,7 @@ pub fn load_character_data(
skill_group_kind: row.get(0)?,
earned_exp: row.get(1)?,
skills: row.get(2)?,
hash_val: row.get(3)?,
})
})?
.filter_map(Result::ok)
@ -421,8 +423,10 @@ pub fn create_character(
"
INSERT INTO skill_group (entity_id,
skill_group_kind,
earned_exp)
VALUES (?1, ?2, ?3)",
earned_exp,
skills,
hash_val)
VALUES (?1, ?2, ?3, ?4, ?5)",
)?;
for skill_group in db_skill_groups {
@ -430,6 +434,8 @@ pub fn create_character(
&character_id as &dyn ToSql,
&skill_group.skill_group_kind,
&skill_group.earned_exp,
&skill_group.skills,
&skill_group.hash_val,
])?;
}
drop(stmt);
@ -998,8 +1004,10 @@ pub fn update(
REPLACE
INTO skill_group (entity_id,
skill_group_kind,
earned_exp)
VALUES (?1, ?2, ?3)",
earned_exp,
skills,
hash_val)
VALUES (?1, ?2, ?3, ?4, ?5)",
)?;
for skill_group in db_skill_groups {
@ -1007,6 +1015,8 @@ pub fn update(
&skill_group.entity_id as &dyn ToSql,
&skill_group.skill_group_kind,
&skill_group.earned_exp,
&skill_group.skills,
&skill_group.hash_val,
])?;
}

View File

@ -566,6 +566,7 @@ pub fn convert_skill_groups_to_database(
earned_exp: sg.earned_exp as i32,
// If fails to convert, just forces a respec on next login
skills: serde_json::to_string(&sg.ordered_skills).unwrap_or_else(|_| "".to_string()),
hash_val: "".to_string(),
})
.collect()
}

View File

@ -25,6 +25,7 @@ pub struct SkillGroup {
pub skill_group_kind: String,
pub earned_exp: i32,
pub skills: String,
pub hash_val: String,
}
pub struct Pet {