gameontext / sample-room-java

Walkthrough for creating a room in java for Game On!
https://gameontext.org
Apache License 2.0
20 stars 105 forks source link

Refactor sample Java room #34

Closed ebullient closed 7 years ago

ebullient commented 7 years ago

Signed-off-by: Erin Schnabel schnabel@us.ibm.com


This change is Reviewable

BarDweller commented 7 years ago

Reviewed 11 of 26 files at r1. Review status: 11 of 24 files reviewed at latest revision, 12 unresolved discussions.


gojava-application/src/main/java/org/gameontext/sample/Log.java, line 63 at r1 (raw file):

        // much easier to read if the default behaviour is to have this level
        // promotion switched on
        if ( level.intValue() < Level.INFO.intValue() && !NO_LOG_LEVEL_PROMOTION) {

Wondering here about stuff javac would likely optimise away, but I'd normally put the fixed constant part of the && statement first, and have the level int comparison after, as conceptually, the comparison will only need to be performed if the bool expression is false.. I'd hope a good compiler could imply that from this statement, but even so.. easy change..


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 19 at r1 (raw file):

 * This is how our room is described.
 *  a) Use post-construct to go fill some of this in by asking the map
 *  b) Assign this dynamically on the fly as the room is used.

what are a) b) here.. ? order of operation? mutually exclusive usage patterns? #confusingcomment I can't find reference to a) b) later in this file.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 94 at r1 (raw file):

    }

    /**

There's no synchronization protection over get/add/remove, and get iterates the entryset, which is modified by add/remove .. caution needed here if these are driven in response to external events on concurrent threads.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 128 at r1 (raw file):

    /**
     * Room inventory objects are optional. Build/cache/return a JsonArray listing
     * items in the room for use in location messages.

same issue re commands / concurrency


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 93 at r1 (raw file):


        // Who doesn't love switch on strings in Java 8?
        switch(message.getTarget()) {

This feels nicer than before.. but the length of this switch statement is unfortunate.. admittedly a good 50% of that is the json commenting..


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 179 at r1 (raw file):

        String contentToLower = content.toLowerCase(Locale.ENGLISH).trim();

        String firstWord;

String parts[] = contentToLower.split(" ",2); //split limit.. shiny ;p String firstWord = parts[0]; String remainder = parts.length > 1 ? parts[1] : null; //array will only have 1 element if string had no spaces..

(also look at java.util.Scanner it can make token processing string input a lot simpler.. although it doesn't easily handle the 'remainder' pattern here, you could pass the Scanner instance around allowing getDirection etc to process the remaining string as required.. {can sort of emulate remainder if you add \n to the string then use scanner nextLine to glob the rest of the line.. ymmv} .. )


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 249 at r1 (raw file):

    }

the direction stuff here feels.. awkward.. like direction should be some sort of construct that handles all this mapping between text and short & long for you, so it's not duplicated between the two methods (note that getDirection will return null for up / down, while long direction supports those concepts.. if that's intentional, it could do with a comment explaining why)


gojava-application/src/main/java/org/gameontext/sample/map/client/MapResponseReader.java, line 58 at r1 (raw file):

        try {
            rdr = Json.createReader(entityStream);
            JsonStructure json = rdr.read();

JsonObject returnedJson = rdr.readObject(); ??


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 42 at r1 (raw file):

    private static final String PREFIX = "room-" + RoomImplementation.ROOM_ID + "-";

    /** Incrementing message id for bookmark */

I wonder if we shouldn't init this long with say, System.currentMillis() / 100 or something.. to ensure each run of the same room generates differing bookmark ids in the ui


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 71 at r1 (raw file):

    /**
     * Ack message: this supports version 1 & 2
     * {@code ack,{\"version\":[1]}}

hmm.. should this comment say 1|2 ?


gojava-application/src/main/java/org/gameontext/sample/protocol/MessageDecoder.java, line 45 at r1 (raw file):

    @Override
    public boolean willDecode(String s) {
        return true;

meh.. suspect this should only return true if new Message(s) won't through a DecodeException ? not sure though.


gojava-application/src/main/java/org/gameontext/sample/protocol/MessageEncoder.java, line 40 at r1 (raw file):

    @Override
    public String encode(Message msg) throws EncodeException {
        return msg.toString();

wondering if this shouldn't call a more appropriately named method to encode msg to json, I know toString is javadoc'd to say it returns json, but I kinda prefer having toString, and formatted outputters be distinct, to allow toString to be embellished for debug/log without affecting operation


Comments from Reviewable

BarDweller commented 7 years ago

Reviewed 12 of 26 files at r1. Review status: 21 of 24 files reviewed at latest revision, 12 unresolved discussions.


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 255 at r1 (raw file):

        }

        switch(lowerDirection) {

Below you include up and down, here you don't. Not sure to me why?


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/map/client/MapClient.java, line 78 at r1 (raw file):

        if (mapLocation == null) {
            MapClientLog.log(Level.FINER, this, "No MAP_URL environment variable provided. Will use default.");
            mapLocation = "https://game-on.org/map/v1/sites";

Should this be a constant?


Comments from Reviewable

ebullient commented 7 years ago

Review status: 21 of 24 files reviewed at latest revision, 14 unresolved discussions.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 19 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > what are a) b) here.. ? order of operation? mutually exclusive usage patterns? #confusingcomment I can't find reference to a) b) later in this file.

Changes would be made in RoomImplementation (where this is a member variable), not here.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 94 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > There's no synchronization protection over get/add/remove, and get iterates the entryset, which is modified by add/remove .. caution needed here if these are driven in response to external events on concurrent threads.

Wasn't sure I should have to deal with all of that up front, or leave some as exercises for the reader. But I suppose I should just go do that...


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 179 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > String parts[] = contentToLower.split(" ",2); //split limit.. shiny ;p > String firstWord = parts[0]; > String remainder = parts.length > 1 ? parts[1] : null; //array will only have 1 element if string had no spaces.. > > (also look at java.util.Scanner it can make token processing string input a lot simpler.. although it doesn't easily handle the 'remainder' pattern here, you could pass the Scanner instance around allowing getDirection etc to process the remaining string as required.. {can sort of emulate remainder if you add \n to the string then use scanner nextLine to glob the rest of the line.. ymmv} .. )

Not sure it matters that much? Splitting into an array is not necessarily more readable.. We'll see what Ilan says. :-P


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 249 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > the direction stuff here feels.. awkward.. like direction should be some sort of construct that handles all this mapping between text and short & long for you, so it's not duplicated between the two methods (note that getDirection will return null for up / down, while long direction supports those concepts.. if that's intentional, it could do with a comment explaining why)

Yes.. first one is a filter (is it something I know about or not). Second one makes it pretty for display. I'll clarify.


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 255 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > Below you include up and down, here you don't. Not sure to me why?

I can remove Up/Down here (cut/paste from other code elsewhere)


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 71 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > hmm.. should this comment say 1|2 ?

or 1 || 2


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 139 at r1 (raw file):

        //        }
        JsonObjectBuilder oayload = Json.createObjectBuilder();
        oayload.add(TYPE, EVENT);

should this be 'payload' ?


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 396 at r1 (raw file):

     * @see MessageDecoder#decode(String)
     */
    public Message(String s) throws DecodeException {

maybe worth considering looking at the format of the message again so it becomes trivial and doesn't require decoding. Make the whole thing JSON, instead of this hybrid CSV, JSON?


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 494 at r1 (raw file):

    }

    @Override

This is a bit of a head scratcher.


Comments from Reviewable

ebullient commented 7 years ago

Review status: 21 of 24 files reviewed at latest revision, 17 unresolved discussions.


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 93 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > This feels nicer than before.. but the length of this switch statement is unfortunate.. admittedly a good 50% of that is the json commenting..

Y.. if you removed all the commenting, it would be teeny. I don't feel compelled to change it.. ?


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 42 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > I wonder if we shouldn't init this long with say, System.currentMillis() / 100 or something.. to ensure each run of the same room generates differing bookmark ids in the ui

y.. I wondered about that. I was half going for something readable. Having something unique to the room in the bookmark is a nice visual indicator in the client console..


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 71 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > or 1 || 2

No. it supports both versions.


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 139 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > should this be 'payload' ?

LOL. yes.


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 396 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > maybe worth considering looking at the format of the message again so it becomes trivial and doesn't require decoding. Make the whole thing JSON, instead of this hybrid CSV, JSON?

harder to stop parsing midstream, isn't it? I know this is the single biggest concern.. but there isn't a good way (otherwise) for me to direct traffic in the middle without parsing more of the JSON message than I want to.

With message queues you have a topic. With websockets, we don't. The content-before the JSON payload is the rough equivalent of a topic.


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 494 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > This is a bit of a head scratcher.

Generated.

Equals used to validate that created messages get parsed properly


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 94 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > Wasn't sure I should have to deal with all of that up front, or leave some as exercises for the reader. But I suppose I should just go do that...

If you want to leave as an exercise for the reader, just add a comment indicating that it may be needed under some condition and has been left consciously as an exercise for the reader?


Comments from Reviewable

ebullient commented 7 years ago

Review status: 21 of 24 files reviewed at latest revision, 17 unresolved discussions.


gojava-application/src/main/java/org/gameontext/sample/map/client/MapResponseReader.java, line 58 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > JsonObject returnedJson = rdr.readObject(); ??

Yes. let me simplify. ALso need tests for this one.


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 179 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > Not sure it matters that much? Splitting into an array is not necessarily more readable.. We'll see what Ilan says. :-P

I find the current way easier to read and understand.


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 255 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > I can remove Up/Down here (cut/paste from other code elsewhere)

I like having more directions though, but Map can't handle that right?


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 494 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > Generated. > > Equals used to validate that created messages get parsed properly

I wondered if you wrote that out manually, and then wondered the mental space you would have been in, in the middle of that process.


Comments from Reviewable

BarDweller commented 7 years ago

Review status: 15 of 24 files reviewed at latest revision, 16 unresolved discussions.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 19 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > Changes would be made in RoomImplementation (where this is a member variable), not here.

Okie, so mebbe just clean up the comment to avoid the confusion.. it's just the use of a) b) that threw me, because it wasn't explained later, or here what the a & b were for.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 94 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > If you want to leave as an exercise for the reader, just add a comment indicating that it may be needed under some condition and has been left consciously as an exercise for the reader?

A comment warning it's not threadsafe / or similar would at least give people the heads up.


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 93 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > Y.. if you removed all the commenting, it would be teeny. I don't feel compelled to change it.. ?

I know.. I guess It'll have to live here.. the only choice left is to move the switch bodies into their own methods, but that carries a negative that I feel is roughly equiv to the over long switch anyways..


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 71 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > No. it supports both versions.

the code supports both.. the comment doesn't allow for the existence of 2


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 139 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > LOL. yes.

Nice catch! =)


Comments from Reviewable

BarDweller commented 7 years ago

Review status: 15 of 24 files reviewed at latest revision, 13 unresolved discussions, some commit checks failed.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 32 at r2 (raw file):

    private String description = "An undescribed room (or perhaps the data hasn't been fetched from the map)";

    private Map<String, String> commands = new ConcurrentHashMap<>();

Yay ;p


Comments from Reviewable

BarDweller commented 7 years ago

Reviewed 1 of 26 files at r1, 5 of 7 files at r2. Review status: 20 of 24 files reviewed at latest revision, 8 unresolved discussions, some commit checks failed.


Comments from Reviewable

BarDweller commented 7 years ago

Reviewed 1 of 7 files at r2. Review status: 21 of 24 files reviewed at latest revision, 8 unresolved discussions, some commit checks failed.


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 42 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > y.. I wondered about that. > I was half going for something readable. Having something unique to the room in the bookmark is a nice visual indicator in the client console..

Well that wouldn't be incrementing then, though I guess its not incrementing now either. Unless the message id is incrementing and not the bookmark. What is the message id?


Comments from Reviewable

ebullient commented 7 years ago

Review status: 21 of 24 files reviewed at latest revision, 9 unresolved discussions, some commit checks failed.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 94 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > A comment warning it's not threadsafe / or similar would at least give people the heads up.

Meh. I made them threadsafe.


gojava-application/src/main/java/org/gameontext/sample/RoomImplementation.java, line 255 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > I like having more directions though, but Map can't handle that right?

Map can't.. yet. This should hopefully make it clearer for us to explain how to get wormholes to work. There is a bit through the mediator I have to make sure works.. and then I think a room could manufacture its own wormholes.


gojava-application/src/main/java/org/gameontext/sample/map/client/MapClient.java, line 78 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > Should this be a constant?

Done.


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 71 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > the code supports both.. the comment doesn't allow for the existence of 2

Comment changed


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 494 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > I wondered if you wrote that out manually, and then wondered the mental space you would have been in, in the middle of that process.

No way. Eclipse generates those.


gojava-application/src/main/java/org/gameontext/sample/protocol/MessageDecoder.java, line 45 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > meh.. suspect this should only return true if new Message(s) won't through a DecodeException ? not sure though.

You'd have to pre-parse the message to be able to tell. I could pre-check that the string starts with "room"...


Comments from Reviewable

ebullient commented 7 years ago

Review status: 21 of 24 files reviewed at latest revision, 7 unresolved discussions, some commit checks failed.


gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 42 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > Well that wouldn't be incrementing then, though I guess its not incrementing now either. Unless the message id is incrementing and not the bookmark. What is the message id?

The last chunk is incrementing (the AtomicLong, just below)


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/Message.java, line 494 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > No way. Eclipse generates those.

Aww.. You burst my bubble.


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/MessageDecoder.java, line 45 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > You'd have to pre-parse the message to be able to tell. I *could* pre-check that the string starts with "room"...

depends on what "willDecode" means semantically. If it just means you can use the decode method and its implemented then its correct. How is this method intended to be used?


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/MessageEncoder.java, line 40 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > wondering if this shouldn't call a more appropriately named method to encode msg to json, I know toString is javadoc'd to say it returns json, but I kinda prefer having toString, and formatted outputters be distinct, to allow toString to be embellished for debug/log without affecting operation

I've been using Jackson lately, and this all has just been happening transparently. But maybe should be toJson? but the I guess decode should be fromJson?


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/MessageEncoder.java, line 40 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > I've been using Jackson lately, and this all has just been happening transparently. But maybe should be `toJson`? but the I guess decode should be `fromJson`?

oops..I should have written instead of toString. I cant seem to edit previous comments.


Comments from Reviewable

ebullient commented 7 years ago

Review status: 17 of 24 files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


gojava-application/src/main/java/org/gameontext/sample/protocol/MessageDecoder.java, line 45 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > depends on what "willDecode" means semantically. If it just means you can use the decode method and its implemented then its correct. How is this method intended to be used?

https://javaee-spec.java.net/nonav/javadocs/javax/websocket/Decoder.Text.html#willDecode(java.lang.String)

Given the usage, I'm inclined to agree with myself and answer true. Anything the decoder won't decode flowing on the websocket is an exception


Comments from Reviewable

ebullient commented 7 years ago

Review status: 17 of 24 files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


gojava-application/src/main/java/org/gameontext/sample/protocol/MessageEncoder.java, line 40 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > oops..I should have written instead of `toString`. I cant seem to edit previous comments.

I moved to encode. Because that's fine. the json as a string is perfectly readable. If/when it needs to be changed, it can be.


Comments from Reviewable

ebullient commented 7 years ago

Review status: 17 of 24 files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


gojava-application/src/main/java/org/gameontext/sample/RoomDescription.java, line 19 at r1 (raw file):

Previously, BarDweller (Ozzy Osborne) wrote… > Okie, so mebbe just clean up the comment to avoid the confusion.. it's just the use of a) b) that threw me, because it wasn't explained later, or here what the a & b were for.

Done.


Comments from Reviewable

ilanpillemer commented 7 years ago

gojava-application/src/main/java/org/gameontext/sample/protocol/MessageDecoder.java, line 45 at r1 (raw file):

Previously, ebullient (Erin Schnabel) wrote… > https://javaee-spec.java.net/nonav/javadocs/javax/websocket/Decoder.Text.html#willDecode(java.lang.String) > > Given the usage, I'm inclined to agree with myself and answer true. Anything the decoder won't decode flowing on the websocket is an exception >

The JavaDoc is a bit self referential. 'Answer whether the given String can be decoded into an object of type T.' Is it meant to be called before attempting to decode, so you know there won't be an Exception; or is it to say you can attempt and won't get an 'Unsupported' Exception, but may get others.


Comments from Reviewable

ebullient commented 7 years ago

Review status: 17 of 24 files reviewed at latest revision, 4 unresolved discussions.


gojava-application/src/main/java/org/gameontext/sample/protocol/MessageDecoder.java, line 45 at r1 (raw file):

Previously, ilanpillemer (Ilan Pillemer) wrote… > The JavaDoc is a bit self referential. 'Answer whether the given String can be decoded into an object of type T.' Is it meant to be called before attempting to decode, so you know there won't be an Exception; or is it to say you can attempt and won't get an 'Unsupported' Exception, but may get others.

So. in my experience, this is useful when you have many different types of things flying around on the socket, and there is some marker you can use to determine whether or not the string should be decoded by this decoder, instead of one of the others...

We aren't in that boat here.


Comments from Reviewable

ilanpillemer commented 7 years ago

Reviewed 15 of 26 files at r1, 4 of 7 files at r2, 7 of 7 files at r3. Review status: all files reviewed at latest revision, 4 unresolved discussions.


Comments from Reviewable

ilanpillemer commented 7 years ago
:lgtm:

Comments from Reviewable