summaryrefslogtreecommitdiff
path: root/java/demo/Freeze/casino/BetI.java
blob: 2fdd5a9091177f812be07f69659ac771b1141364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

class BetI extends CasinoStore.PersistentBet
{
    public int 
    getAmount(Ice.Current current)
    {
        return amount;
    }

    public void 
    accept(Casino.PlayerPrx p, Ice.Current current) throws Casino.OutOfChipsException
    {
        CasinoStore.PersistentPlayerPrx player = CasinoStore.PersistentPlayerPrxHelper.uncheckedCast(p);
        if(player == null)
        {
            throw new Casino.OutOfChipsException();
        }
        
        try
        {
            player.withdraw(amount);
            potentialWinners.add(player);
        }
        catch(Ice.ObjectNotExistException one)
        {
            throw new Casino.OutOfChipsException();
        }
    }

    public int 
    getChipsInPlay(Ice.Current current)
    {
        return amount * potentialWinners.size();
    }

    public long
    getCloseTime(Ice.Current current)
    {
        return closeTime;
    }

    public void
    complete(int random, Ice.Current current)
    {
        if(random < 0)
        {
            random = -random;
        }

        int size = potentialWinners.size();

        //
        // Pick a winner using random
        //
        int winnerIndex = random % (size + (_bankEdge - 1));
        
        if(winnerIndex >= size)
        {
            winnerIndex = 0;
        }
        
        CasinoStore.WinnerPrx winner = (CasinoStore.WinnerPrx)potentialWinners.elementAt(winnerIndex);
        
        try
        {
            winner.win(amount * size);
        }
        catch(Ice.ObjectNotExistException ex)
        {
            //
            // Goes to the bank
            //
            winner = (CasinoStore.WinnerPrx)potentialWinners.elementAt(0);
            winner.win(amount * size);
        }
        
        //
        // Self-destroys
        //
        _evictor.remove(current.id);
    }

    BetI()
    {
    }

    BetI(int amount, long closeTime, CasinoStore.PersistentBankPrx bank, Freeze.TransactionalEvictor evictor,
         int bankEdge)
    {
        this.amount = amount;
        this.closeTime = closeTime;
        init(evictor, bankEdge);
        potentialWinners = new java.util.Vector<CasinoStore.WinnerPrx>();
        potentialWinners.add(bank);
    }

    void
    init(Freeze.TransactionalEvictor evictor, int bankEdge)
    {
        _evictor = evictor;
        _bankEdge = bankEdge;
    }

    private Freeze.TransactionalEvictor _evictor;    
    private int _bankEdge;
}