Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
WorkAdventure Dialogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FS Info TU Dortmund
Events
WorkAdventure Dialogs
Commits
f8aa90db
Commit
f8aa90db
authored
4 years ago
by
Jonas Zohren
Browse files
Options
Downloads
Patches
Plain Diff
Implement basic (untested) gameStateSave client
parent
8b793a84
No related branches found
No related tags found
No related merge requests found
Pipeline
#19150
passed
4 years ago
Stage: deploy
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/App.svelte
+4
-2
4 additions, 2 deletions
src/App.svelte
src/SavingComponent.svelte
+45
-12
45 additions, 12 deletions
src/SavingComponent.svelte
src/gameStateServerClient.ts
+48
-0
48 additions, 0 deletions
src/gameStateServerClient.ts
src/types.ts
+13
-0
13 additions, 0 deletions
src/types.ts
with
110 additions
and
14 deletions
src/App.svelte
+
4
−
2
View file @
f8aa90db
...
...
@@ -3,7 +3,9 @@
import
{
fetchDialogSet
}
from
"
./utils
"
;
import
Debugger
from
"
./Debugger.svelte
"
;
import
type
{
Dialog
}
from
"
./types
"
;
import
SavingComponent
from
"
./SavingComponent.svelte
"
;
import
SavingComponent
from
"
./SavingComponent.svelte
"
;
const
gameStateServerBaseUrl
=
"
http://localhost:4000/
"
;
const
dialogSetPromise
=
fetchDialogSet
();
let
currentDialog
:
Dialog
;
...
...
@@ -15,7 +17,7 @@ import SavingComponent from "./SavingComponent.svelte";
<br
/>
<Debugger
{
dialogSet
}
bind:currentDialog />
<SavingComponent/>
<SavingComponent
{
gameStateServerBaseUrl
}
/>
{
:catch
_error
}
<h3>
Oh no :(
</h3>
<p>
...
...
This diff is collapsed.
Click to expand it.
src/SavingComponent.svelte
+
45
−
12
View file @
f8aa90db
<script
lang=
"ts"
>
import
{
identity
}
from
"
svelte/internal
"
;
import
{
gameFactsStore
}
from
"
./gameFacts
"
;
import
{
GameStateServerClient
}
from
"
./gameStateServerClient
"
;
import
type
{
GameState
}
from
"
./types
"
;
export
let
gameStateServerBaseUrl
:
string
;
$
:
gameStateServerClient
=
new
GameStateServerClient
(
gameStateServerBaseUrl
);
let
pretixOrderCode
:
string
=
"
XXXYYY
"
;
async
function
handleSaveGameState
():
Promise
<
void
>
{
const
gameState
:
GameState
=
{
gameFacts
:
$gameFactsStore
,
};
gameStateServerClient
.
saveState
(
pretixOrderCode
,
gameState
);
}
async
function
handleLoadGameState
():
Promise
<
void
>
{
const
newGameState
=
await
gameStateServerClient
.
loadState
(
pretixOrderCode
);
$gameFactsStore
=
newGameState
.
gameFacts
;
}
</script>
<div>
<input
type=
"text"
placeholder=
"Your Pretix order code"
maxlength=
"5"
minlength=
"5"
>
<button
style=
"width: 25rem;"
>
💾 Save game (not implemented yet)
</button
>
<input
type=
"text"
placeholder=
"Your Pretix order code"
maxlength=
"5"
minlength=
"5"
bind:value=
{
pretixOrderCode
}
/>
<button
style=
"width: 10rem;"
on:click
|
preventDefault=
{
handleSaveGameState
}
>
💾 Save game
</button>
<button
style=
"width: 10rem;"
on:click
|
preventDefault=
{
handleLoadGameState
}
>
▶️ Load game
</button>
</div>
<style>
div
{
border
:
1px
solid
;
padding
:
10px
;
padding-bottom
:
0px
;
margin
:
5px
;
}
</style>
\ No newline at end of file
div
{
border
:
1px
solid
;
padding
:
10px
;
padding-bottom
:
0px
;
margin
:
5px
;
}
</style>
This diff is collapsed.
Click to expand it.
src/gameStateServerClient.ts
0 → 100644
+
48
−
0
View file @
f8aa90db
import
{
GameState
,
isValidGameState
}
from
"
./types
"
;
export
class
GameStateServerClient
{
_apiBaseUrl
:
string
;
constructor
(
apiBaseUrl
:
string
)
{
this
.
_apiBaseUrl
=
apiBaseUrl
;
}
async
saveState
(
id
:
string
,
gameState
:
GameState
):
Promise
<
boolean
>
{
try
{
await
fetch
(
this
.
_apiBaseUrl
+
"
/game/state/
"
+
id
,
{
method
:
"
POST
"
,
mode
:
"
cors
"
,
cache
:
"
no-cache
"
,
credentials
:
"
omit
"
,
headers
:
{
"
Content-Type
"
:
"
application/json
"
,
},
referrerPolicy
:
"
no-referrer
"
,
body
:
JSON
.
stringify
(
gameState
),
});
return
true
;
}
catch
(
saveStateToServerError
)
{
console
.
error
(
"
Failed to save state to server. The following info may help to solve this problem:
"
,
saveStateToServerError
);
return
false
;
}
}
async
loadState
(
id
:
string
):
Promise
<
GameState
>
{
try
{
const
rawResponse
=
await
fetch
(
this
.
_apiBaseUrl
+
"
/game/state/
"
+
id
,
{
method
:
"
GET
"
,
mode
:
"
cors
"
,
cache
:
"
no-cache
"
,
credentials
:
"
omit
"
,
referrerPolicy
:
"
no-referrer
"
,
});
const
decodedResponse
=
await
rawResponse
.
json
();
if
(
!
isValidGameState
(
decodedResponse
))
{
throw
new
TypeError
(
"
State from server is not a valid gameState
"
);
}
return
decodedResponse
;
}
catch
(
loadGameStateFromServerError
)
{}
}
}
This diff is collapsed.
Click to expand it.
src/types.ts
+
13
−
0
View file @
f8aa90db
...
...
@@ -97,3 +97,16 @@ export interface DialogOption {
*/
forbiddenFacts
?:
String
[];
}
export
interface
GameState
{
gameFacts
:
String
[];
}
export
function
isValidGameState
(
input
:
unknown
):
input
is
GameState
{
if
(
typeof
input
!==
"
object
"
)
return
false
;
return
(
input
[
"
gameFacts
"
]
!==
undefined
&&
Array
.
isArray
(
input
[
"
gameFacts
"
])
&&
input
[
"
gameFacts
"
].
every
((
gameFact
)
=>
typeof
gameFact
===
"
string
"
)
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment