From a753ca67aef275f37dd320491cea25e25f5f1e7c Mon Sep 17 00:00:00 2001 From: Hielke Fellinger Date: Tue, 17 Sep 2024 22:57:53 +0200 Subject: [PATCH] Starting Character CRU(D) loop --- app/game_engine/communication.go | 15 ++-- app/game_engine/managementCrudEventHandler.go | 66 ++++++++++++++++++ web/templates/campaign.html | 22 ++++++ web/templates/campaignManageCharacters.html | 2 +- web/templates/campaignUpsertCharacter.html | 68 +++++++++++++++++++ web/templates/characterDetails.html | 23 ++++--- 6 files changed, 182 insertions(+), 14 deletions(-) create mode 100644 web/templates/campaignUpsertCharacter.html diff --git a/app/game_engine/communication.go b/app/game_engine/communication.go index 5beb8aa..15b1529 100644 --- a/app/game_engine/communication.go +++ b/app/game_engine/communication.go @@ -50,12 +50,17 @@ const ( TypeManagementOverviewEnd EventType = 560 TypeManagementCrudStart EventType = 5500 - TypeLoadUpsertMap EventType = 5511 - TypeRemoveMap EventType = 5512 - TypeUpsertMap EventType = 5513 - TypeLoadUpsertItem EventType = 5541 - TypeUpsertItem EventType = 5542 + TypeLoadUpsertMap EventType = 5511 + TypeRemoveMap EventType = 5512 + TypeUpsertMap EventType = 5513 + + TypeLoadUpsertItem EventType = 5541 + TypeUpsertItem EventType = 5542 + + TypeLoadUpsertCharacter EventType = 5521 + TypeUpsertCharacter EventType = 5522 + TypeManagementCrudEnd EventType = 5600 TypeManagementError EventType = 5999 diff --git a/app/game_engine/managementCrudEventHandler.go b/app/game_engine/managementCrudEventHandler.go index 797bd6a..c56e406 100644 --- a/app/game_engine/managementCrudEventHandler.go +++ b/app/game_engine/managementCrudEventHandler.go @@ -45,12 +45,78 @@ func (e *baseEventMessageHandler) handleManagementCrudEvents(message EventMessag handled = true } + // Characters + if message.Type == TypeLoadUpsertCharacter { + err := e.typeLoadUpsertCharacter(message, pool) + if err != nil { + return err + } + handled = true + } else if message.Type == TypeUpsertCharacter { + err := e.typeUpsertCharacter(message, pool) + if err != nil { + return err + } + handled = true + } + if !handled { return errors.New(fmt.Sprintf("message of type '%d' is not recognised by 'handleManagementCrudEvents()'", message.Type)) } return nil } +func (e *baseEventMessageHandler) typeLoadUpsertCharacter(message EventMessage, pool CampaignPool) error { + // Undo escaping + clearedBody := html.UnescapeString(message.Body) + + // Check if user is lead + if message.Source != pool.GetLeadId() { + return errors.New("modifying characters is not allowed as non-lead") + } + + data := make(map[string]any) + + // Check if there is an existing map with the supplied uuid + uuidItemFilter, err := helpers.ParseStringToUuid(clearedBody) + if err == nil { + mapCandidate, match := pool.GetEngine().GetWorld().GetEntityByUuid(uuidItemFilter) + if match && mapCandidate.HasComponentType(ecs.CharacterComponentType) { + // data["Character"] = {} + } + } + + rawJsonBytes, err := json.Marshal( + e.handleLoadHtmlBodyMultipleTemplateFiles([]string{"campaignUpsertCharacter.html", "diceSpinnerSvg.html"}, + "campaignUpsertCharacter", data)) + if err != nil { + return err + } + + loadItemMessage := NewEventMessage() + loadItemMessage.Source = message.Source + loadItemMessage.Type = TypeLoadUpsertCharacter + loadItemMessage.Body = string(rawJsonBytes) + loadItemMessage.Destinations = append(loadItemMessage.Destinations, pool.GetLeadId()) + pool.TransmitEventMessage(loadItemMessage) + + return nil +} + +func (e *baseEventMessageHandler) typeUpsertCharacter(message EventMessage, pool CampaignPool) error { + // Check if user is lead + if message.Source != pool.GetLeadId() { + return errors.New("modifying characters is not allowed as non-lead") + } + + // Undo escaping + clearedBody := html.UnescapeString(message.Body) + + log.Printf("- Game Management CRUD Events typeUpsertCharacter content: '%v'", clearedBody) + + return errors.New("NOT IMPLEMENTED!") +} + func (e *baseEventMessageHandler) typeLoadUpsertMap(message EventMessage, pool CampaignPool) error { // Undo escaping clearedBody := html.UnescapeString(message.Body) diff --git a/web/templates/campaign.html b/web/templates/campaign.html index eb20534..9b4e3cb 100644 --- a/web/templates/campaign.html +++ b/web/templates/campaign.html @@ -233,6 +233,10 @@

No active event

if (payLoad.type === 5541) { handleRightBarCrudHtml(payLoad); } + // Character + if (payLoad.type === 5521) { + handleRightBarCrudHtml(payLoad); + } return; } if (payLoad.type === 5999) { @@ -335,6 +339,11 @@

No active event

sendEvent(wrapMessage(JSON.stringify(id), 5541)); } + let upsertCharacter = (id) => { + stopLoadSpinner(); + sendEvent(wrapMessage(JSON.stringify(id), 5521)); + } + let upsertMap = (id) => { stopLoadSpinner(); sendEvent(wrapMessage(JSON.stringify(id), 5511)); @@ -359,6 +368,19 @@

No active event

} } + let submitUpsertCharacter = () => { + let formElement = document.forms['upsertCharacterForm']; + if (formElement && formElement.reportValidity()) { + let formData = new FormData(formElement); + // TODO + console.warn(formData) + let item = {} + sendEvent(wrapMessage(JSON.stringify(item), 5522)); + } else { + stopLoadSpinner(); + } + } + let submitUpsertMap = () => { let formElement = document.forms['upsertMapForm']; if (formElement && formElement.reportValidity()) { diff --git a/web/templates/campaignManageCharacters.html b/web/templates/campaignManageCharacters.html index bae4861..8c9db9c 100644 --- a/web/templates/campaignManageCharacters.html +++ b/web/templates/campaignManageCharacters.html @@ -3,7 +3,7 @@

Character Overview

| - diff --git a/web/templates/campaignUpsertCharacter.html b/web/templates/campaignUpsertCharacter.html new file mode 100644 index 0000000..e33f5c6 --- /dev/null +++ b/web/templates/campaignUpsertCharacter.html @@ -0,0 +1,68 @@ +{{define "campaignUpsertCharacter"}} + +

{{ if and .Character .Character.Id }}Update{{else}}Add{{end}} Character

+
+
+ + + + + {{ if and .Character .Character.Id }} +
+ + +
+ {{end}} +
+ + +
+
+ + +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ +
+ +
+
+
+ +{{ end }} \ No newline at end of file diff --git a/web/templates/characterDetails.html b/web/templates/characterDetails.html index 969d7dd..a44ec17 100644 --- a/web/templates/characterDetails.html +++ b/web/templates/characterDetails.html @@ -29,16 +29,23 @@

{{ .character.Description }}

-
- ... -
+
+ Stats +
+ ... +
+
+ - {{ range .character.Inventories }} -
- {{ template "inventory" . }} -
- {{ end }} +
+ Inventory + {{ range .character.Inventories }} +
+ {{ template "inventory" . }} +
+ {{ end }} +