jifunks / botany

command line virtual plant buddy
ISC License
438 stars 32 forks source link

Do not crash with malformed visitors.json files #35

Open marado opened 4 years ago

marado commented 4 years ago

If the visitors.json file does not contain a valid JSON file, reset it, and act as if it was empty.

This crash was mentioned initially in #34 .

Lucidiot commented 4 years ago

Doesn't this imply someone could just kill someone else's plant by making the JSON file invalid if their owner did not visit in 5 days, since the game will act as if nobody visited at all? the visit option is made to allow others to babysit your plant if you're away for more than 5 days so that would be a problem.

What about 'just' crashing but with a readable error message, or just preventing the death detection from triggering in this case and giving the owner a last chance?

marado commented 3 years ago

Well, I think it is all a question of... design, more than implementation. visitors.json is writable by anyone, and so anyone can write invalid json in there, either on purpose or by mistake. We can deal with an invalid json in two different ways:

I prefer the first one, just because (a) relying on others to water your plants is always a risk, and (b) if we "assume the best" this is also a cheating mechanism (just mess up with the visitor.json file to make sure your plan never dies).

I don't have strong feelings about this matter, I just did what seemed to make most sense to me. If we want a different behavior than the one currently implemented (eg, preventing death detection on this case), I can make the needed changes, but I suppose a decision should be made before I throw more code into the PR.

jmdejong commented 3 years ago

I think the best would be not to take any automatic action but leave it up to the user.

Maybe it would be best to make a prompt if visitors.json is malformed: the user can choose to either have it deleted automatically, or to try to fix it manually (which will close botany while printing instructions on how the file should look)