maybeanerd / selfhosted-api-trader-game

A self hostable game server that can connect to others instances using the ActivityPub federation protocol and offers a client API to play a city building and resource trading game.
GNU Affero General Public License v3.0
8 stars 1 forks source link

use ActivityPub for server to server API #524

Closed maybeanerd closed 6 months ago

maybeanerd commented 8 months ago

I don't want to use a custom REST based event stream. I don't want to use graphQL here.

Let's use ActivityPub We can start easy A Server is a single actor Following is the "treaty" You can Post trades, and like to "confirm" them.

Let's make it actually be "compatible" with Mastodon. I't wont be any fun to follow a Game Server But it would be possible.


Might be nice to not embed game info in posts, but rather add AP extensions for the game. (Here's sime examples from mastodon: https://docs.joinmastodon.org/spec/activitypub/#toot)

This might allow for a post to describe the Action, e.g. "Basti wants to trade 5 wood for 5 steel in GameNameHere", and add the Game and Action meta to it in extensions. This would allow others to make sense of the posts, If they were to follow.

For Likes, one could probably add an extensions for the game as well, where it specifies the trade response and which player accepted, etc?


The single actor of the instance could be merchant@trading-game.yourDomain.com


An interesting thing to solve is: who to federate with Up until now I wanted a single server to only directly connect to a few others, and then allow trade proxied by them.

Maybe this is still possible by them creating Proxy-Posts? I also thought of discovering follower instances and directly following them, but w core Game mechanic will be travel duration or taxing for Proxy even, so I don't think that's a nice solution. Of course a game server can mark other instances they follow as "No direct Connection", but i Kind of Like the idea of there actually Not being a direct connection


To allow the wider integration into the fediverse, one could limit following only for Game instances, and allow any amount of other AP services to follow and interact without Limit. Internally the Game Server will ignore all of those actions in terms of gameplay, but will replicate and federate them like any other AP system would


next TODOs

inbox/outbox handlers

maybeanerd commented 8 months ago

maybe using this lib is an option: https://jsr.io/@fedify/fedify https://fedify.dev/manual/

maybeanerd commented 7 months ago

looks like that didnt work out, it's only for deno

other intersting projects: https://github.com/immers-space/activitypub-express https://github.com/michaelcpuckett/activity-kit

maybeanerd commented 7 months ago

https://github.com/SiranWeb/activitypub-types

maybeanerd commented 7 months ago

Guides

https://socialhub.activitypub.rocks/t/guide-for-new-activitypub-implementers/479

found in the top link, but for quicker access: https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/

maybeanerd commented 7 months ago

https://github.com/digitalbazaar/jsonld.js to use json-LD compaction and stuff

maybeanerd commented 7 months ago

https://swicg.github.io/activitypub-http-signature/

maybeanerd commented 7 months ago

For testing, will finally be released later today https://pubkit.net/

maybeanerd commented 6 months ago

Idea: mark all AP related things as handled/unhandled Once applied( aka the change is reflected in the games Data tables) they are marked as handled and essentially ignored from then on This can also be used to handle worker queues that Go through unhandled activities chronologically

maybeanerd commented 6 months ago

This would mean self-created activities are Always handled when created

maybeanerd commented 6 months ago

Better idea for marking handled: Create an ActivityQueue table All AP activities that are created have an entry there with their ID as FK, and a Type Incoming|Outgoing This way workers can pick up this Queue and either Post activities to other servers, or sync them to the Server state

For now as a POC These workers could Run as cronjobs on the main thread. Later on they could become fully Independent workers. Theoretically even Independent containers

maybeanerd commented 6 months ago

Example of how to use extensions:

https://github.com/w3c/activitypub/issues/313#issuecomment-396652190

https://seb.jambor.dev/posts/understanding-activitypub/

maybeanerd commented 6 months ago

Some context for the outbox:

https://socialhub.activitypub.rocks/t/what-is-an-outbox-really-what-should-be-there-what-should-not/814/2

IMO it should return all activities of the instance actor, supporting pagination of course (and maybe a startFrom Date attribute)

The main goal should be for other instances to backfill actions , e.g. if they were down for a while, or just recently started a treaty.

maybeanerd commented 6 months ago

Ideas for Inbox: Never use actor from Incoming activity ( only it's ID) If it's a new actor, get it and it's Key to validate the request If it's known, validate with the stored key

maybeanerd commented 6 months ago

next TODOs

inbox/outbox handlers

maybeanerd commented 6 months ago

bug to look into when federating trades:

grafik
maybeanerd commented 6 months ago

Seems like this is the issue: https://github.com/maybeanerd/selfhosted-api-trader-game/blob/main/backend%2Fsrc%2Fmodules%2Ftrade%2Ftrade.service.ts#L110

Maybe move the APobject creation to a util just like the Activity creation? To enforce actorIDs being correct

maybeanerd commented 6 months ago

moved some follow up tasks to https://github.com/maybeanerd/selfhosted-api-trader-game/issues/635