Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few more JSON instances #494

Merged
merged 10 commits into from
Jul 11, 2022
Merged

Add a few more JSON instances #494

merged 10 commits into from
Jul 11, 2022

Conversation

TristanCacqueray
Copy link
Collaborator

@TristanCacqueray TristanCacqueray commented Jun 26, 2022

Copy link
Member

@xsebek xsebek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, Generic is great! 👍

Please just change the description to "- part of #347" as I think this does not add all missing ToJSON instances.

My main interest is the World which has only a parser in Scenario but is missing a serializer.

@TristanCacqueray
Copy link
Collaborator Author

I think it's worth using Generic, however it seems to double, or maybe triple, the build time. Let me try adding instances for World.

@TristanCacqueray
Copy link
Collaborator Author

I guess the alternative to Generic would be to use Data.Aeson.TH, though I'm not familiar with it.


deriving instance Show (f RID) => Show (RobotR f)
deriving instance FromJSON (f RID) => FromJSON (RobotR f)
deriving instance ToJSON (f RID) => ToJSON (RobotR f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit surprising since we have the FromJSONE instance above.

That one requires the entity map and also does not allow specifying robot ID nor some other fields like the log.

I am not sure what this ToJSON instance produces, but I would like it to match the FromJSONE.
Though if the generated instance is reasonable maybe it could be useful and we could align them later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I have no idea, I just typed on the keyboard until GHC could link the binary :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, when I made the FromJSONE instances I was just interested in putting in enough functionality to be able to describe robots in the ways that I wanted in scenario description files. But if there are fields that are still missing we can definitely add them.

@TristanCacqueray
Copy link
Collaborator Author

Here are the missing instances to be able to serialize the GameState:

instance FromJSONKey (V2 Int64)
instance ToJSONKey (V2 Int64)

instance FromJSON (Array Int Text)
instance ToJSON (Array Int Text)

instance FromJSON (W.World Int Entity)
instance ToJSON (W.World Int Entity)

instance FromJSON ScenarioCollection
instance ToJSON ScenarioCollection

instance FromJSON StdGen
instance ToJSON StdGen

@TristanCacqueray
Copy link
Collaborator Author

Oh well, build time are back to the 5min range (they went up to 15min in the previous checks)

@TristanCacqueray TristanCacqueray changed the title Add JSON instances for the CESK Add a few more JSON instances Jun 26, 2022
@xsebek
Copy link
Member

xsebek commented Jun 26, 2022

  • V2 Int64 is just a pair, should be easy to serialize it as a two-element list (which we read in scenarios)
  • Array Int Text is just an efficient Map in my mind
  • World Int Entity this one requires a bit of work
    • you need to serialize chunks of the world and the entity palette (in the scenarios we only do one chunk)
    • we would need to express our WorldFunction as a data type and use that as the generator - but maybe we can pretend that it is always either test world 2 or one entity everywhere for now 🙂
  • ScenarioCollection IMHO this should be in UI state, it is a bit annoying that a broken scenario does not let me run any test
  • StdGen its current state can be shown, but it does not have a read instance 😕

@byorgey
Copy link
Member

byorgey commented Jun 26, 2022

  • ScenarioCollection IMHO this should be in UI state, it is a bit annoying that a broken scenario does not let me run any test

Agreed, let's move it.

@byorgey
Copy link
Member

byorgey commented Jun 26, 2022

  • StdGen its current state can be shown, but it does not have a read instance confused

Relevant: https://www.reddit.com/r/haskell/comments/ponow7/saving_and_loading_a_stdgen/hcxvrcp/?utm_source=reddit&utm_medium=web2x&context=3

https://discourse.haskell.org/t/save-and-restore-random-number-generator-freeze-thaw/4751/9

@TristanCacqueray
Copy link
Collaborator Author

Actually we may not want to serialize the StdGen. What matters is to be able to recreate the world, so we should record the initial seed, and use it to regenerate the same world, but how random number are produced could be random across reload.

@xsebek
Copy link
Member

xsebek commented Jun 26, 2022

Actually we may not want to serialize the StdGen.

Counterexample: A player submits a bug report with a saved game and it depends on a random number somewhere.
If you want to randomize it again, you can just call random once more or we could ignore it at loading if the player wants to.

We already allow a custom seed, so keeping the current random generator in saves seems natural. 🤷‍♂️

@byorgey
Copy link
Member

byorgey commented Jun 26, 2022

I was going to say the same thing re: saving the StdGen. I agree it's not strictly necessary as far as gameplay goes but it would help with reproducibility, and we might as well.

@TristanCacqueray
Copy link
Collaborator Author

@byorgey if that's ok with you, i'd like to merge this PR for #537

Copy link
Member

@byorgey byorgey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

@byorgey byorgey added the merge me Trigger the merge process of the Pull request. label Jul 11, 2022
@mergify mergify bot merged commit cf6693b into main Jul 11, 2022
@mergify mergify bot deleted the cesk-json branch July 11, 2022 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants