Skip to content

Commit

Permalink
Add networking (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomperoo committed Apr 18, 2021
1 parent ffaf409 commit da499bf
Show file tree
Hide file tree
Showing 71 changed files with 8,237 additions and 61 deletions.
31 changes: 0 additions & 31 deletions .codecov.yml

This file was deleted.

7 changes: 0 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ jobs:
yarn build
env:
CI: true
- uses: codecov/codecov-action@v1.0.3
if: github.repository == 'jamjarlabs/jamjar'
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage/clover.xml
flags: unittests
name: jamjar-unittests
- name: Publish to NPM
if: github.event_name == 'release' && github.repository == 'jamjarlabs/jamjar'
run: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
lib
dist
/coverage
yarn-error.log
/specs
yarn-error.log
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
docs
coverage
*.md
src/network
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Serialization to and from JSON.
- Networking using JamJar relay protocol.

## [v0.10.0] - 2021-02-25

### Added
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[![Build
Status](https://github.com/jamjarlabs/jamjar/workflows/JamJar/badge.svg)](https://github.com/jamjarlabs/JamJar/actions)
[![CodeCov
Report](https://codecov.io/gh/jamjarlabs/jamjar/branch/master/graph/badge.svg)](https://codecov.io/gh/jamjarlabs/jamjar)
[![License - Apache
2.0](https://img.shields.io/:license-apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![Documentation
Expand Down Expand Up @@ -34,8 +32,9 @@ JamJar provides the following:
- Text - processing text before rendering, handling fonts.
- Sprite Animation - animation through sprites.
- Audio - audio playback.
- Scripting - runtime execution of scripts, interface for interacting with
core the game logic
- Scripting - runtime execution of scripts, interface for interacting with core the game logic
- Networking - networking through a standardised relay server protocol.
- Serialization - serialization to and from JSON.

## More information

Expand All @@ -51,6 +50,7 @@ Dependencies for developing this project:

- [`node`](https://nodejs.org/en/) == `12.18.3`
- [`yarn`](https://legacy.yarnpkg.com/en/) == `1.22.10`
- [`protoc`](http://google.github.io/proto-lens/installing-protoc.html) == `3.15.6`

The docs are generated using a Python tool, requiring Python and pip:

Expand Down
14 changes: 14 additions & 0 deletions docs/architecture/networking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Networking

JamJar uses the [jamjar-relay-server](https://github.com/jamjarlabs/jamjar-relay-server) protocol to handle networking,
allowing use of a central relay server to allow client hosted games. This protocol defines a number of messages and
actions that can be done (e.g. connecting, listing clients, migrating hosts) while also providing a mechanism to
relay messages to other clients. This allows for client-hosted servers, with one client acting as a host and
deciding how the game logic is processed.

The networking that JamJar supports is `v1` of the relay server protocol, there is a [WebSocketNetworkSystem] that
is provided with JamJar which implements the protocol, converting messages and actions from the protocol into JamJar
messages, while also providing the ability to relay JamJar messages to other clients. The [WebSocketNetworkSystem] uses
a tick rate for outbound messages, meaning that outbound messages are stored in a queue and then published together
on every tick. The inbound messages are also stored in a list, but are propogated to the rest of the game engine on
every game update.
90 changes: 90 additions & 0 deletions docs/architecture/serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Serialization

JamJar classes can be serialized and deserialized into and from JSON (useful for networking/saving game states).

Since there are no types in the transpiled JavaScript generated from TypeScript JamJar adds in a pseudo type system
which allows a centralized store of types and deserializing functionality. This basically works by having a global
mapping of a type identifier (e.g. `jamjar.Vector`) to a function that will take in JSON and output an instance of the
type (e.g. [Vector.Deserialize]). This means that JSON can be fed into this that includes some type which can be
looked up against the centralised store, and then if there's a matching function it can be used to convert the JSON
into a concrete type.

## Serialization

To make a class support serialization the class just needs to implement the [ISerializable] interface, providing a
`Serialize` method which should return the current instance serialized into a JSON string. The JSON should be in the
format:

```json
{
"type": "jamjar.Vector",
"value": {
"x": 5,
"y": 3
}
}
```

The `type` should be the identifer that will be stored with the centralized serialization storage and used to
identify a class (see the rest of how this works under the Deserialization section). The `value` is a JSON object that
contains any information that defines the instance, in this case it is the `x` and `y` values for the [Vector].

## Deserialization

To make it possible for JSON to be deserialized into a concrete type there are a couple things a class needs to
provide, first it needs to provide a function that takes in a JSON object and returns the concrete type (e.g
[Vector.Deserialize]). The second thing that needs to be provided is a class decorator, called `Serialize`, for example:

```ts
@Serialize("my_custom_type", my_deserialization_function)
```

This decorator registers the custom type to the global centralized serialization storage, allowing a lookup at runtime
and retrieval of our `Vector.Deserialize` function that we can feed JSON into to get a concrete type.


## Primitives

Primitives are serialized in a special form to be handled by the centralized deserialization logic, it looks like this:

```json
{
"primitive": true,
"type": "js.string",
"value": "this is a primitive value!"
}
```

This will be deserialized into the concrete type of a string with the value `this is a primitive value!`.

## Collections

Currently the only supported serialization collection is an array, which looks like this:

```json
{
"type": "js.array",
"value": [
{
"type": "jamjar.Vector",
"value": {
"x": 5,
"y": 3
}
},
{
"type": "jamjar.Vector",
"value": {
"x": 3,
"y": 5
}
}
]
}
```

This will be deserialized at runtime into the concrete type of an array of [Vector] objects.

[Vector.Deserialize]: ../../reference/classes/vector/#deserialize
[Vector]: ../../reference/classes/vector
[ISerializable]: ../../reference/interfaces/iserializable
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[![Build
Status](https://github.com/jamjarlabs/jamjar/workflows/JamJar/badge.svg)](https://github.com/jamjarlabs/JamJar/actions)
[![CodeCov
Report](https://codecov.io/gh/jamjarlabs/jamjar/branch/master/graph/badge.svg)](https://codecov.io/gh/jamjarlabs/jamjar)
[![License - Apache
2.0](https://img.shields.io/:license-apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![Documentation
Expand Down Expand Up @@ -39,3 +37,5 @@ JamJar provides the following:
* Audio - audio playback.
* Scripting - runtime execution of scripts, interface for interacting with
core the game logic
* Networking - networking through a standardised relay server protocol.
* Serialization - serialization to and from JSON.
38 changes: 38 additions & 0 deletions docs/reference/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions docs/reference/classes/message.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da499bf

Please sign in to comment.