Implement updating basic info for character name.
This commit is contained in:
parent
ffe4f9298c
commit
ae6d5bcfa3
21
src/db.rs
21
src/db.rs
|
@ -39,6 +39,8 @@ pub(crate) trait Dao {
|
|||
|
||||
async fn insert_character(&self, new_character: NewCharacter<'_>) -> sqlx::Result<()>;
|
||||
|
||||
async fn update_character<'a>(&self, character: &'a Character) -> sqlx::Result<()>;
|
||||
|
||||
async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()>;
|
||||
}
|
||||
|
||||
|
@ -124,6 +126,25 @@ impl Dao for SqlitePool {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_character<'a>(&self, character: &'a Character) -> sqlx::Result<()> {
|
||||
sqlx::query(
|
||||
"UPDATE characters
|
||||
set user_id = ?, viewable = ?, character_name = ?,
|
||||
data_type = ?, data_version = ?, data = ? where id = ?",
|
||||
)
|
||||
.bind(character.user_id)
|
||||
.bind(character.viewable)
|
||||
.bind(&character.character_name)
|
||||
.bind(character.data_type)
|
||||
.bind(character.data_version)
|
||||
.bind(&character.data)
|
||||
.bind(character.id)
|
||||
.execute(self)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()> {
|
||||
sqlx::query("UPDATE characters set data = ? where id = ?")
|
||||
.bind(&character.data)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as jspb from "google-protobuf";
|
||||
import { ApiResult, UpdateAttributeRequest, UpdateSkillValueRequest } from "../_proto/cofd_api_pb";
|
||||
import { ApiResult, UpdateBasicInfoRequest, UpdateAttributeRequest, UpdateSkillValueRequest } from "../_proto/cofd_api_pb";
|
||||
|
||||
const PROTOBUF_CONTENT_TYPE = { 'Content-Type': 'application/x-protobuf' };
|
||||
|
||||
|
@ -7,7 +7,6 @@ function staticImplements<T>() {
|
|||
return <U extends T>(constructor: U) => { constructor };
|
||||
}
|
||||
|
||||
|
||||
async function makeRequest<T extends jspb.Message>(uri: string, params: T): Promise<Uint8Array> {
|
||||
let resp = await fetch(uri, {
|
||||
method: 'POST',
|
||||
|
@ -28,3 +27,8 @@ export async function updateAttributeValue(params: UpdateAttributeRequest): Prom
|
|||
let data = await makeRequest('/api/rpc/cofd/update_attribute_value', params);
|
||||
return ApiResult.deserializeBinary(data);
|
||||
}
|
||||
|
||||
export async function updateBasicInfo(params: UpdateBasicInfoRequest): Promise<ApiResult> {
|
||||
let data = await makeRequest('/api/rpc/cofd/update_basic_info', params);
|
||||
return ApiResult.deserializeBinary(data);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { UpdateSkillValueRequest, UpdateAttributeRequest } from "../../_proto/cofd_api_pb";
|
||||
import { CharacterIdentifier, UpdateBasicInfoRequest, UpdateSkillValueRequest, UpdateAttributeRequest } from "../../_proto/cofd_api_pb";
|
||||
import * as api from "../api";
|
||||
|
||||
// This is the scripting for the edit character page, which submits
|
||||
|
@ -10,6 +10,13 @@ import * as api from "../api";
|
|||
|
||||
const [, , USERNAME, CHARACTER_ID] = window.location.pathname.split('/');
|
||||
|
||||
function characterId(): CharacterIdentifier {
|
||||
const id = new CharacterIdentifier();
|
||||
id.setCharacterId(parseInt(CHARACTER_ID));
|
||||
id.setOwner(USERNAME);
|
||||
return id;
|
||||
}
|
||||
|
||||
function setupAttributes() {
|
||||
const attributeInputs = document.querySelectorAll('#attributes input[type="number"]');
|
||||
|
||||
|
@ -52,7 +59,6 @@ import * as api from "../api";
|
|||
params.setSkillValue(newValue);
|
||||
|
||||
let resp = await api.updateSkillValue(params);
|
||||
|
||||
console.log("got a response back", resp);
|
||||
}
|
||||
|
||||
|
@ -61,8 +67,35 @@ import * as api from "../api";
|
|||
});
|
||||
}
|
||||
|
||||
function setupBasicInfo() {
|
||||
async function updateInfo() {
|
||||
const params = new UpdateBasicInfoRequest();
|
||||
|
||||
const name = document.querySelector<HTMLInputElement>("#characterName")?.value ?? "";
|
||||
console.log("name is", name);
|
||||
|
||||
params.setId(characterId());
|
||||
params.setName(name);
|
||||
params.setAge(50);
|
||||
params.setConcept("cool guy");
|
||||
params.setChronicle("the best one");
|
||||
params.setGender("apache attack helicopter");
|
||||
|
||||
let resp = await api.updateBasicInfo(params);
|
||||
console.log("got a response back", resp);
|
||||
}
|
||||
|
||||
const inputs = document.querySelectorAll("#basicInfo input");
|
||||
|
||||
inputs.forEach(input => {
|
||||
console.log('got an input', input);
|
||||
input.addEventListener('blur', updateInfo);
|
||||
});
|
||||
}
|
||||
|
||||
setupAttributes();
|
||||
setupSkills();
|
||||
setupBasicInfo();
|
||||
})().catch(e => {
|
||||
alert(e);
|
||||
});
|
||||
|
|
|
@ -89,8 +89,13 @@
|
|||
|
||||
<h1>Core Sheet</h1>
|
||||
<div>
|
||||
<h1>Name: <input type="text" value="{{name}}" /></h1>
|
||||
<div id="basicInfo">
|
||||
<h1>
|
||||
<label for="characterName">Name:</label>
|
||||
<input type="text" id="characterName" name="characterName" value="{{name}}" />
|
||||
</h1>
|
||||
<p>System: {{data_type}}</p>
|
||||
</div>
|
||||
<div id="attributes">
|
||||
<div class="attributes-section" id="mentalAttributes">
|
||||
{{ macros::attribute(name="Intelligence", value=sheet.intelligence) }}
|
||||
|
|
|
@ -48,12 +48,15 @@ pub(super) async fn update_basic_info<'a>(
|
|||
let mut character = load_character(&conn, logged_in_user, &id.owner, id.character_id).await?;
|
||||
let mut sheet: CofdSheet = character.try_deserialize()?;
|
||||
|
||||
println!("name will now be {}", req.name);
|
||||
character.character_name = req.name.clone(); //Should probably remove name from the sheet?
|
||||
sheet.name = req.name.clone();
|
||||
sheet.gender = req.gender.clone();
|
||||
sheet.concept = req.concept.clone();
|
||||
|
||||
character.update_data(&sheet)?;
|
||||
conn.update_character_sheet(&character).await?;
|
||||
conn.update_character(&character).await?;
|
||||
println!("Updated basic info");
|
||||
Ok(Proto(ApiResult::success()))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue