Skip to content

Commit

Permalink
feat(effect): you can now add effects to events
Browse files Browse the repository at this point in the history
  • Loading branch information
AcevedoR committed Mar 29, 2024
1 parent 34e7e5e commit 88bb086
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 31 deletions.
7 changes: 6 additions & 1 deletion packages/renderer/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {
createChoice,
createChoiceOutcome,
createEffect,
createEvent,
editChoice,
editEvent,
Expand All @@ -18,6 +19,7 @@
import type {CreateEvent} from '/@/file-synchronization/CreateEvent';
import type {CreateChoice} from '/@/file-synchronization/CreateChoice';
import type {CreateChoiceOutcome} from '/@/file-synchronization/CreateChoiceOutcome';
import type {CreateEffect} from '/@/file-synchronization/CreateEffect';
let selectedContentToEdit: ChoiceToDisplay | EventToDisplay | undefined;
Expand Down Expand Up @@ -46,6 +48,9 @@
case 'createChoiceOutcome':
createChoiceOutcome(chainFileAbsolutePath, chain, modificationEvent.detail.content as CreateChoiceOutcome).then(v => onChainSelectionChange(chainFileAbsolutePath));
break;
case 'createEffect':
createEffect(chainFileAbsolutePath, chain, modificationEvent.detail.content as CreateEffect).then(v => onChainSelectionChange(chainFileAbsolutePath));
break;
default:
throw Error('unhandled modification event: ' + modificationEvent.detail.type);
}
Expand All @@ -60,7 +65,7 @@
};
</script>

<svelte:window on:error={(errorEvent) => alert(errorEvent.message)}/>
<svelte:window on:error={(errorEvent) => alert(errorEvent.message)} />

{#await currentChainsDirectory}
Getting directory path, this show be instantaneous
Expand Down
173 changes: 173 additions & 0 deletions packages/renderer/src/admin-view/EffectCreationForm.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<script lang="ts">
import {createEventDispatcher} from 'svelte';
import Input from '/@/admin-view/Input.svelte';
import type {EventId} from '../model/todisplay/EventId';
import {ChoiceToDisplayId, isChoiceToDisplayId} from '../model/todisplay/ChoiceToDisplay';
import type {ChangeOperation} from '../model/ChangeOperation';
import type {ChangeTarget} from '../model/ChangeTarget';
import type {EffectType} from '../model/EffectType';
import Select from '/@/admin-view/Select.svelte';
import type {CreateEffect} from '/@/file-synchronization/CreateEffect';
import type {Effect} from '/@/model/Effect';
export let parentId: EventId | ChoiceToDisplayId;
let effectName: string;
let description: string | null = null;
let operation: ChangeOperation;
let target: ChangeTarget;
let value: number;
let type: EffectType;
let activated: boolean;
const dispatch = createEventDispatcher();
$: formResult = {
effectName, description, operation, target, value, type, activated,
};
$: errors = validate(formResult);
interface EventCreationFormErrors {
effectName: string | undefined,
description: string | undefined
operation: string | undefined
target: string | undefined
value: string | undefined
type: string | undefined
activated: string | undefined
}
const validate = (formResult: {
effectName: string,
description: string,
operation: string,
target: string,
value: number,
type: string,
activated: boolean
}): EventCreationFormErrors => {
console.log('validate: ' + formResult.effectName);
let errorOnEffectName;
let errorOnDescription;
let errorOnOperation;
let errorOnTarget;
let errorOnValue;
let errorOnType;
let errorOnActivated;
let regexp = '^[A-Za-z\-\_0-9]*$';
if (!formResult.effectName || (formResult.effectName && !formResult.effectName.match(regexp))) {
errorOnEffectName = 'should match regexp ' + regexp;
}
if (!formResult.value || formResult.value < 0) {
errorOnValue = 'positive value is required';
}
return {
effectName: errorOnEffectName,
description: errorOnDescription,
operation: errorOnOperation,
target: errorOnTarget,
value: errorOnValue,
type: errorOnType,
activated: errorOnActivated,
} as EventCreationFormErrors;
};
function isErrorObjectEmpty() {
return !Object.values(errors).find(x => x);
}
const validateAndSubmitOnCreateEvent = (
parentEventId: EventId | ChoiceToDisplayId,
effectName: string,
description: string | null,
operation: ChangeOperation,
target: ChangeTarget,
value: number,
type: EffectType,
activated: boolean,
) => {
if (isErrorObjectEmpty()) {
const createEffect: CreateEffect = {
effectName,
parentId: parentEventId as EventId,
newEffect: {
operation,
value,
type,
target,
description,
} as Effect,
activated,
};
dispatch('createEffect', createEffect);
}
};
</script>
<div id="event-creation-form">
<h2>Create a new effect from parent: </h2>
{#if isChoiceToDisplayId(parentId)}
<h3>choice: {parentId.get()}</h3>
{:else}
<h3>{parentId.value}</h3>
{/if}
<form on:submit|preventDefault={() => validateAndSubmitOnCreateEvent(parentId, effectName, description, operation, target,value,type,activated)}>
<Input
type="text"
label="effect name"
placeholder="your-effect-name"
bind:value={effectName}
error={errors.effectName}
/>
<Input
type="text"
label="description"
placeholder="desc"
bind:value={description}
error={errors.description}
/>
<Select
label="operation"
bind:value={operation}
>
<option value="add">add</option>
<option value="substract">substract</option>
</Select>
<Select
label="target"
bind:value={target}
>
<option value="population">population</option>
<option value="ecology">ecology</option>
<option value="money">money</option>
</Select>
<Input
type="number"
label="value"
placeholder="3"
bind:value={value}
error={errors.value}
/>
<Select
label="type"
bind:value={type}
>
<option value="instant">instant</option>
<option value="permanent">permanent</option>
</Select>
<Select
label="activated"
bind:value={activated}
>
<option value="true">true</option>
<option value="false">false</option>
</Select>
<button type="submit">create effect</button>
</form>
</div>
<style>
#event-creation-form button {
text-align: center;
}
</style>
43 changes: 34 additions & 9 deletions packages/renderer/src/admin-view/EventEditionSidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import type {CreateEvent} from '/@/file-synchronization/CreateEvent';
import ChoiceCreationForm from '/@/admin-view/ChoiceCreationForm.svelte';
import type {CreateChoice} from '/@/file-synchronization/CreateChoice';
import EffectCreationForm from '/@/admin-view/EffectCreationForm.svelte';
import type {CreateEffect} from '/@/file-synchronization/CreateEffect';
export let selectedContentToEdit: EventToDisplay;
let isEventCreationFormDisplayed = false;
let isChoiceCreationFormDisplayed = false;
enum CreationFormDisplayed {
none, createEvent, createChoice, createEffect
}
let currentCreationFormDisplayed: CreationFormDisplayed = CreationFormDisplayed.none;
enum EventOutcomeType {
NONE, CHOICES, EVENTS
Expand Down Expand Up @@ -47,7 +52,7 @@
id: createEvent.id,
content: createEvent,
});
isEventCreationFormDisplayed = false;
currentCreationFormDisplayed = CreationFormDisplayed.none;
};
const onChoiceCreation = (createChoice: CreateChoice) => {
Expand All @@ -56,25 +61,39 @@
type: 'createChoice',
content: createChoice,
});
isChoiceCreationFormDisplayed = false;
currentCreationFormDisplayed = CreationFormDisplayed.none;
};
const onEffectCreation = (createEffect: CreateEffect) => {
console.log('creating effect');
dispatch('save', {
type: 'createEffect',
content: createEffect,
});
currentCreationFormDisplayed = CreationFormDisplayed.none;
};
</script>
<div id="event-edition-sidebar">

<h1>Admin sidebar</h1>

{#if isEventCreationFormDisplayed}
{#if currentCreationFormDisplayed === CreationFormDisplayed.createEvent }
<EventCreationForm
parentEventId={selectedContentToEdit.id}
on:createEvent={(createEvent) => onEventCreation(createEvent.detail)}
></EventCreationForm>
{:else if isChoiceCreationFormDisplayed}
{:else if currentCreationFormDisplayed === CreationFormDisplayed.createChoice }
<ChoiceCreationForm
parentEventId={selectedContentToEdit.id}
on:createChoice={(createChoice) => onChoiceCreation(createChoice.detail)}
></ChoiceCreationForm>
{:else}
{:else if currentCreationFormDisplayed === CreationFormDisplayed.createEffect}
<EffectCreationForm
parentId={{value: selectedContentToEdit.id}}
on:createEffect={(createEffect) => onEffectCreation(createEffect.detail)}
></EffectCreationForm>
{:else if currentCreationFormDisplayed === CreationFormDisplayed.none}

<div id="selected-content-display">
<h2>Modifying event: </h2>
Expand All @@ -84,14 +103,20 @@
<hr>
{#if eventOutcomeType === EventOutcomeType.EVENTS || eventOutcomeType === EventOutcomeType.NONE}
<div id="event-creation-section">
<button on:click={() => isEventCreationFormDisplayed = true}>Link a new event</button>
<button on:click={() => currentCreationFormDisplayed = CreationFormDisplayed.createEvent}>Link a new event
</button>
</div>
{/if}
{#if eventOutcomeType === EventOutcomeType.CHOICES || eventOutcomeType === EventOutcomeType.NONE}
<div id="choice-creation-section">
<button on:click={() => isChoiceCreationFormDisplayed = true}>Link a new choice</button>
<button on:click={() => currentCreationFormDisplayed = CreationFormDisplayed.createChoice}>Link a new choice
</button>
</div>
{/if}
<div id="choice-effect-creation-section">
<button on:click={() => currentCreationFormDisplayed = CreationFormDisplayed.createEffect}>Link a new effect
</button>
</div>
{/if}
</div>

Expand Down
45 changes: 45 additions & 0 deletions packages/renderer/src/admin-view/Select.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script>
export let value;
export let error = '';
export let label = '';
</script>

<div class="select">
{#if label}
<span class="block">{label}</span>
{/if}

<select class="block select" class:error bind:value on:blur>
<slot />
</select>

{#if error}
<span class="block error-text">{error}</span>
{/if}
</div>

<style>
.select {
width: 70%;
margin: 10px;
text-align: left;
}
.select span {
max-width: 10px;
}
.select select {
border-radius: 4px;
padding: 6px 10px;
margin: 0;
}
.error {
border-color: #f55;
}
.error-text {
color: #f55;
}
</style>
Loading

0 comments on commit 88bb086

Please sign in to comment.