File: bet.c

package info (click to toggle)
bygfoot 2.3.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 12,620 kB
  • ctags: 3,441
  • sloc: xml: 113,203; ansic: 40,378; makefile: 2,345; sh: 1,816
file content (304 lines) | stat: -rw-r--r-- 7,664 bytes parent folder | download | duplicates (3)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
   bet.c

   Bygfoot Football Manager -- a small and simple GTK2-based
   football management game.

   http://bygfoot.sourceforge.net

   Copyright (C) 2005  Gyözö Both (gyboth@bygfoot.com)

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "bet.h"
#include "finance.h"
#include "fixture.h"
#include "game_gui.h"
#include "league.h"
#include "main.h"
#include "maths.h"
#include "misc.h"
#include "option.h"
#include "player.h"
#include "support.h"
#include "treeview2.h"
#include "user.h"

/** Round a betting odd to a multiple of 0.05. */
gfloat
bet_round_odd(gfloat odd)
{
#ifdef DEBUG
    printf("bet_round_odd\n");
#endif

    gint local_odd = (gint)rint(odd * 100);

    if(local_odd % 5 == 0)
	return odd;

    if(local_odd % 5 >= 3)
	return (gfloat)(local_odd + (5 - local_odd % 5)) / 100;

    return (gfloat)(local_odd - local_odd % 5) / 100;
}

/** Find the bet containing the fixture. */
BetMatch*
bet_from_fixture(gint fix_id)
{
#ifdef DEBUG
    printf("bet_from_fixture\n");
#endif

    gint i, j;

    for(i=1;i>=0;i--)
	for(j=0;j<bets[i]->len;j++)
	    if(g_array_index(bets[i], BetMatch, j).fix_id == fix_id)
		return &g_array_index(bets[i], BetMatch, j);

    main_exit_program(EXIT_BET_ERROR, 
		      "bet_from_fixture: bet going with fixture %d not found",
		      fix_id);

    return NULL;
}

/** Manage the bets made by the users. */
void
bet_update_user_bets(void)
{
#ifdef DEBUG
    printf("bet_update_user_bets\n");
#endif

    gint i, j, outcome;
    const BetMatch *bet = NULL;
    const Fixture *fix = NULL;

    for(i=0;i<users->len;i++)
    {
	g_array_free(usr(i).bets[0], TRUE);
	usr(i).bets[0] = g_array_new(FALSE, FALSE, sizeof(BetUser));
	
	for(j=0;j<usr(i).bets[1]->len;j++)
	{
	    bet = bet_from_fixture(g_array_index(usr(i).bets[1], BetUser, j).fix_id);
	    fix = fixture_from_id(g_array_index(usr(i).bets[1], BetUser, j).fix_id, TRUE);

	    if(fix->result[0][0] < fix->result[1][0])
		outcome = 2;
	    else
		outcome = (fix->result[0][0] == fix->result[1][0]);

	    if(outcome == g_array_index(usr(i).bets[1], BetUser, j).outcome)
	    {
		usr(i).money += 
		    (gint)rint((gfloat)g_array_index(usr(i).bets[1], BetUser, j).wager *
			       bet->odds[outcome]);
		usr(i).money_in[1][MON_IN_BETS] += 
		    (gint)rint((gfloat)g_array_index(usr(i).bets[1], BetUser, j).wager *
			       bet->odds[outcome]);
		g_array_index(usr(i).bets[1], BetUser, j).wager = 
		    (gint)rint((gfloat)g_array_index(usr(i).bets[1], BetUser, j).wager *
			       bet->odds[outcome]);
	    }
	    else
	    {
		usr(i).money -= 
		    g_array_index(usr(i).bets[1], BetUser, j).wager;
		usr(i).money_out[1][MON_OUT_BETS] -= 
		    g_array_index(usr(i).bets[1], BetUser, j).wager;
		g_array_index(usr(i).bets[1], BetUser, j).wager *= -1;
	    }

	    g_array_append_val(usr(i).bets[0], 
			       g_array_index(usr(i).bets[1], BetUser, j));
	}

	g_array_free(usr(i).bets[1], TRUE);
	usr(i).bets[1] = g_array_new(FALSE, FALSE, sizeof(BetUser));
    }
}

/** Calculate the odds for the bet. */
void
bet_get_odds(BetMatch *bet)
{
#ifdef DEBUG
    printf("bet_get_odds\n");
#endif

    const Fixture *fix = fixture_from_id(bet->fix_id, TRUE);
    gfloat home_advantage = (fix->home_advantage) ?
	(const_float("float_game_home_advantage_lower") +
	 const_float("float_game_home_advantage_upper")) / 2 : 0;
    gfloat av_skill[2] = {0, 0}, skilldiff;
    gint i, j, better_idx;

    for(i=0;i<2;i++)
    {
	for(j=0;j<11;j++)
	    av_skill[i] += 
		player_get_game_skill(player_of_idx_team(fix->teams[i], j),
				      FALSE, TRUE);

	av_skill[i] /= 11;
    }

    av_skill[0] *= (1 + home_advantage);

    skilldiff = ABS(av_skill[0] - av_skill[1]);

    better_idx = 2 * (av_skill[0] < av_skill[1]);

    bet->odds[better_idx] = 
	0.33 + skilldiff * const_float("float_bet_better_factor");
    bet->odds[2 - better_idx] = 
	0.33 + skilldiff * const_float("float_bet_worse_factor");

    for(i=0;i<2;i++)
	bet->odds[i * 2] = CLAMP(bet->odds[i * 2],
				 const_float("float_bet_lower_limit"),
				 1 - const_float("float_bet_lower_limit"));
    
    bet->odds[1] = (1 - bet->odds[0] - bet->odds[2]);
    bet->odds[1] = CLAMP(bet->odds[1],
			 const_float("float_bet_lower_limit"),
			 1 - const_float("float_bet_lower_limit"));

    for(i=0;i<3;i++)
	bet->odds[i] = 
	    bet_round_odd((1 / bet->odds[i]) * 
			  (1 - const_float("float_bet_commission_decrease")));
}

/** Write the bets for the current week round. */
void
bet_update(void)
{
#ifdef DEBUG
    printf("bet_update\n");
#endif

    gint i;
    GPtrArray *fixtures = fixture_get_week_list(week, week_round);
    BetMatch new_bet;

    bet_update_user_bets();

    g_array_free(bets[0], TRUE);
    bets[0] = g_array_new(FALSE, FALSE, sizeof(BetMatch));

    for(i=0;i<bets[1]->len;i++)
	g_array_append_val(bets[0],
			   g_array_index(bets[1], BetMatch, i));

    g_array_free(bets[1], TRUE);
    bets[1] = g_array_new(FALSE, FALSE, sizeof(BetMatch));

    for(i=0;i<fixtures->len;i++)
	if(fixture_user_team_involved((Fixture*)g_ptr_array_index(fixtures, i)) == -1)
	{
	    new_bet.fix_id = ((Fixture*)g_ptr_array_index(fixtures, i))->id;
	    bet_get_odds(&new_bet);
	    g_array_append_val(bets[1], new_bet);
	}

    g_ptr_array_free(fixtures, TRUE);
}

/** Return the user bet if the user betted on the bet or NULL. */
BetUser*
bet_is_user(const BetMatch *bet)
{
#ifdef DEBUG
    printf("bet_is_user\n");
#endif

    gint i, j;

    for(i=1;i>=0;i--)
	for(j=0;j<current_user.bets[i]->len;j++)
	    if(bet->fix_id == g_array_index(current_user.bets[i], BetUser, j).fix_id)
		return &g_array_index(current_user.bets[i], BetUser, j);

    return NULL;
}

/** Place a new bet. 
    @return TRUE on success, FALSE otherwise. */
gboolean
bet_place(gint fix_id, gint outcome, gint wager)
{
#ifdef DEBUG
    printf("bet_place\n");
#endif

    gint max_wager = (gint)rint(finance_wage_unit(current_user.tm) * 
				const_float("float_bet_wager_limit_factor"));
    BetUser new_bet;
    gchar buf[SMALL];

    if(wager <= 0)
	return TRUE;

    if(wager > BUDGET(cur_user))
    {
	game_gui_show_warning(_("You don't have the money."));
	return FALSE;
    }

    if(wager > max_wager)
    {
	misc_print_grouped_int(max_wager, buf);
	game_gui_show_warning(_("The betting office doesn't allow you to wager more than %s."), buf);
	gtk_spin_button_set_value(
	    GTK_SPIN_BUTTON(lookup_widget(window.digits, "spinbutton1")),
	    (gdouble)max_wager);
	return FALSE;
    }

    new_bet.fix_id = fix_id;
    new_bet.outcome = outcome;
    new_bet.wager = wager;

    g_array_append_val(current_user.bets[1], new_bet);

    if(window.bets != NULL)
	treeview2_show_bets();

    return TRUE;
}

/** Remove the bet on the given fixture. */
void
bet_remove(gint fix_id)
{
#ifdef DEBUG
    printf("bet_remove\n");
#endif

    gint i;

    for(i=0;i<current_user.bets[1]->len;i++)
	if(g_array_index(current_user.bets[1], BetUser, i).fix_id == fix_id)
	{
	    g_array_remove_index(current_user.bets[1], i);
	    return;
	}
}