//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.ezgame.server;

import com.samskivert.util.ListUtil;
import com.samskivert.util.RandomUtil;

import com.threerings.util.Name;

import com.threerings.parlor.turn.server.TurnGameManagerDelegate;

/**
 * A special turn delegate for ez games.
 */
public class EZGameTurnDelegate extends TurnGameManagerDelegate
{
    /**
     * A form of endTurn where you can specify the next turn holder oid.
     */
    public void endTurn (Name nextPlayer)
    {
        _nextPlayer = nextPlayer;
        endTurn();
    }

    @Override
    protected void setFirstTurnHolder ()
    {
        // make it nobody's turn
        _turnIdx = -1;
    }

    @Override
    protected void setNextTurnHolder ()
    {
        // if the user-supplied value seems to make sense, use it!
        if (_nextPlayer != null) {
            // copy the value, clear out _nextPlayer.
            Name nextPlayer = _nextPlayer;
            _nextPlayer = null;

            int index = ListUtil.indexOf(_turnGame.getPlayers(), nextPlayer);
            if (index != -1) {
                _turnIdx = index;
                return;
            }

        }
        
        // Otherwise, if it's nobody's turn randomly pick a turn holder
        if (_turnIdx == -1) {
            assignTurnRandomly();
            return;
        }

        // otherwise, do the default behavior
        super.setNextTurnHolder();
    }

    /** An override next turn holder, or null. */
    protected Name _nextPlayer;
}

@@ -1436,32 +1436,196 @@
 dTurn();%0A    %7D%0A%0A
+    @Override%0A    public void gameWillReset ()%0A    %7B%0A        super.gameWillReset();%0A%0A        // make it nobody's turn%0A        _turnGame.setTurnHolder(null);%0A    %7D%0A%0A
     @Override%0A  

Clear out the turn when the game restarts.