summaryrefslogtreecommitdiff
path: root/java/demo/Freeze/casino/Client.java
blob: 994bc40a443654e1d910f0dc22c88abf77e5e822 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// **********************************************************************
//
// Copyright (c) 2003-2011 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.
//
// **********************************************************************

public class Client extends Ice.Application
{
    //
    // Number of bets placed by each player
    // You can (should) increase these values by a factor of 5 or more
    // on a fast system
    //
    static final int betCount1 = 100;
    static final int betCount2 = 20;

    private void
    printBalances(Casino.PlayerPrx[] players)
    {
        for(int i = 0; i < players.length; ++i)
        {
            Casino.PlayerPrx player = players[i];

            if(player != null)
            {
                try
                {
                    System.out.println(player.ice_getIdentity().name + ": " + player.getChips() + " chips");
                }
                catch(Ice.ObjectNotExistException one)
                {
                    //
                    // This player is gone
                    //
                    players[i] = null;
                }
            }
        }
    }

    public int
    run(String[] args)
    {
        if(args.length > 0)
        {
            System.err.println(appName() + ": too many arguments");
            return 1;
        }

        java.util.Random random = new java.util.Random();

        System.out.print("Retrieve bank and players... ");
        System.out.flush();

        Casino.BankPrx bank = Casino.BankPrxHelper.
            uncheckedCast(communicator().propertyToProxy("Bank.Proxy"));

        Casino.PlayerPrx[] players = bank.getPlayers();
        System.out.println("ok");

        System.out.println("Starting balances");
        printBalances(players);

        System.out.println("Current bank earnings: " + bank.getEarnings() + " chips");

        System.out.println("All chips accounted for? " + (bank.checkAllChips() ? "yes" : "no"));

        System.out.print("Each player buys 3,000 chips... ");
        System.out.flush();

        for(int i = 0; i < players.length; ++i)
        {
            Casino.PlayerPrx player = players[i];
            if(player != null)
            {
                if(!bank.buyChips(3000, player))
                {
                    System.out.print("(" + player.ice_getIdentity().name + " is gone) ");
                    players[i] = null;
                }
            }
        }
        System.out.println("ok");

        System.out.println("All chips accounted for? " + (bank.checkAllChips() ? "yes" : "no"));

        System.out.print("Create " + betCount1 + " 10-chips bets... ");
        System.out.flush();

        for(int b = 0; b < betCount1; ++b)
        {
            Casino.BetPrx bet = bank.createBet(10, 200 + random.nextInt(4000));
            for(int i = 0; i < players.length; ++i)
            {
                Casino.PlayerPrx player = players[i];
                if(player != null)
                {
                    try
                    {
                        bet.accept(player);
                    }
                    catch(Ice.ObjectNotExistException ex)
                    {
                        //
                        // Bet already resolved
                        //
                    }
                    catch(Casino.OutOfChipsException ex)
                    {
                        System.out.print("(" + player.ice_getIdentity().name + " is out) ");

                        players[i] = null;
                    }
                }
            }
        }
        System.out.println(" ok");

        System.out.println("Live bets: " + bank.getLiveBetCount());

        int index = random.nextInt(players.length);
        Casino.PlayerPrx gonner = players[index];
        players[index] = null;

        if(gonner != null)
        {
            System.out.print("Destroying " + gonner.ice_getIdentity().name + "... ");
            try
            {
                gonner.destroy();
            }
            catch(Ice.ObjectNotExistException e)
            {
                //
                // Ignored
                //
            }
            System.out.println("ok");
        }

        System.out.println("All chips accounted for? " + (bank.checkAllChips() ? "yes" : "no"));

        System.out.println("Sleep for 2 seconds");

        try
        {
            Thread.sleep(2000);
        }
        catch(InterruptedException ex)
        {
        }
        System.out.println("Live bets: " + bank.getLiveBetCount());

        System.out.print("Create " + betCount2 + " 10-chips bets... ");
        System.out.flush();

        for(int b = 0; b < betCount2; ++b)
        {
            Casino.BetPrx bet = bank.createBet(10, 200 + random.nextInt(4000));
            for(int i = 0; i < players.length; ++i)
            {
                Casino.PlayerPrx player = players[i];
                if(player != null)
                {
                    try
                    {
                        bet.accept(player);
                    }
                    catch(Ice.ObjectNotExistException ex)
                    {
                        //
                        // Bet already resolved
                        //
                    }
                    catch(Casino.OutOfChipsException ex)
                    {
                        System.out.print("(" + player.ice_getIdentity().name + " is out) ");

                        players[i] = null;
                    }
                }
            }
        }
        System.out.println(" ok");

        System.out.println("Live bets: " + bank.getLiveBetCount());
        System.out.println("Sleep for 10 seconds");
        try
        {
            Thread.sleep(10000);
        }
        catch(InterruptedException ex)
        {
        }
        System.out.println("Live bets: " + bank.getLiveBetCount());

        System.out.println("Ending balances");
        printBalances(players);

        System.out.println("Current bank earnings: " + bank.getEarnings() + " chips");

        System.out.println("All chips accounted for? " + (bank.checkAllChips() ? "yes" : "no"));

        return 0;
    }

    static public void
    main(String[] args)
    {
        Client app = new Client();
        app.main("demo.Freeze.casino.Client", args, "config.client");
    }
}