Implement updating basic info for character name.
This commit is contained in:
parent
fe53a0d531
commit
6d9cdd0269
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 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<()>;
|
async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,6 +126,25 @@ impl Dao for SqlitePool {
|
||||||
Ok(())
|
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<()> {
|
async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()> {
|
||||||
sqlx::query("UPDATE characters set data = ? where id = ?")
|
sqlx::query("UPDATE characters set data = ? where id = ?")
|
||||||
.bind(&character.data)
|
.bind(&character.data)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import * as jspb from "google-protobuf";
|
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' };
|
const PROTOBUF_CONTENT_TYPE = { 'Content-Type': 'application/x-protobuf' };
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ function staticImplements<T>() {
|
||||||
return <U extends T>(constructor: U) => { constructor };
|
return <U extends T>(constructor: U) => { constructor };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function makeRequest<T extends jspb.Message>(uri: string, params: T): Promise<Uint8Array> {
|
async function makeRequest<T extends jspb.Message>(uri: string, params: T): Promise<Uint8Array> {
|
||||||
let resp = await fetch(uri, {
|
let resp = await fetch(uri, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -28,3 +27,8 @@ export async function updateAttributeValue(params: UpdateAttributeRequest): Prom
|
||||||
let data = await makeRequest('/api/rpc/cofd/update_attribute_value', params);
|
let data = await makeRequest('/api/rpc/cofd/update_attribute_value', params);
|
||||||
return ApiResult.deserializeBinary(data);
|
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";
|
import * as api from "../api";
|
||||||
|
|
||||||
// This is the scripting for the edit character page, which submits
|
// 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('/');
|
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() {
|
function setupAttributes() {
|
||||||
const attributeInputs = document.querySelectorAll('#attributes input[type="number"]');
|
const attributeInputs = document.querySelectorAll('#attributes input[type="number"]');
|
||||||
|
|
||||||
|
@ -52,7 +59,6 @@ import * as api from "../api";
|
||||||
params.setSkillValue(newValue);
|
params.setSkillValue(newValue);
|
||||||
|
|
||||||
let resp = await api.updateSkillValue(params);
|
let resp = await api.updateSkillValue(params);
|
||||||
|
|
||||||
console.log("got a response back", resp);
|
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();
|
setupAttributes();
|
||||||
setupSkills();
|
setupSkills();
|
||||||
|
setupBasicInfo();
|
||||||
})().catch(e => {
|
})().catch(e => {
|
||||||
alert(e);
|
alert(e);
|
||||||
});
|
});
|
||||||
|
|
|
@ -89,8 +89,13 @@
|
||||||
|
|
||||||
<h1>Core Sheet</h1>
|
<h1>Core Sheet</h1>
|
||||||
<div>
|
<div>
|
||||||
<h1>Name: <input type="text" value="{{name}}" /></h1>
|
<div id="basicInfo">
|
||||||
<p>System: {{data_type}}</p>
|
<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 id="attributes">
|
||||||
<div class="attributes-section" id="mentalAttributes">
|
<div class="attributes-section" id="mentalAttributes">
|
||||||
{{ macros::attribute(name="Intelligence", value=sheet.intelligence) }}
|
{{ 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 character = load_character(&conn, logged_in_user, &id.owner, id.character_id).await?;
|
||||||
let mut sheet: CofdSheet = character.try_deserialize()?;
|
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.name = req.name.clone();
|
||||||
sheet.gender = req.gender.clone();
|
sheet.gender = req.gender.clone();
|
||||||
sheet.concept = req.concept.clone();
|
sheet.concept = req.concept.clone();
|
||||||
|
|
||||||
character.update_data(&sheet)?;
|
character.update_data(&sheet)?;
|
||||||
conn.update_character_sheet(&character).await?;
|
conn.update_character(&character).await?;
|
||||||
|
println!("Updated basic info");
|
||||||
Ok(Proto(ApiResult::success()))
|
Ok(Proto(ApiResult::success()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue