Page 1 of 1

A quick way to obtain game instead of Singletons.getModel().

PostPosted: 28 Mar 2013, 17:44
by Max mtg
There was such lines in a recent commit

Code: Select all
        if (this.isOpponentTurn() && Singletons.getModel().getGame().getPhaseHandler().isPlayerTurn(activator)) {
            return false;
_
Aside from the fact that two parts of condition on a first glance look a bit excessive, I just want to note that player instances now hold reference to a game object. That is the game that player is involved in.

So if you have a reference to player and have to recieve its game, you don't need to get the game from sinletons... just invoke player.getGame(). That is a bit shorter and removes reference to that singletons, incompatible with multiple games in one application (when we build the server).
Same is true when you need game given an abiliy or card - derive player from there and you know what to do next.

Re: A quick way to obtain game instead of Singletons.getMode

PostPosted: 29 Mar 2013, 07:42
by moomarc
Thanks Max. Just to clarify, in the example you used there isn't actually a double check in the condition. 'IsOpponentTurn' is actually just the name of the restriction set if "OpponentTurn" is a restriction, not a reference to player or turn. So for this example I assume it ould now read
Code: Select all
if (this.isOpponentTurn() && activator.getGame().getPhaseHandler().isPlayerTurn(activator)) { return false; }

Re: A quick way to obtain game instead of Singletons.getMode

PostPosted: 29 Mar 2013, 09:04
by Max mtg
Marc, that sample code was from an older revision and it was changed already.
Probably from left side of that code - http://svn.slightlymagic.net/websvn/dif ... &peg=20636

I am writing that just to remind of easier ways of doing common things.