~ubuntu-branches/ubuntu/hardy/bygfoot/hardy

« back to all changes in this revision

Viewing changes to src/cup.c

  • Committer: Bazaar Package Importer
  • Author(s): Isaac Clerencia
  • Date: 2006-06-04 17:51:19 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060604175119-0pbfuhr617031qa9
Tags: 2.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   cup.c
 
3
 
 
4
   Bygfoot Football Manager -- a small and simple GTK2-based
 
5
   football management game.
 
6
 
 
7
   http://bygfoot.sourceforge.net
 
8
 
 
9
   Copyright (C) 2005  Gyözö Both (gyboth@bygfoot.com)
 
10
 
 
11
   This program is free software; you can redistribute it and/or
 
12
   modify it under the terms of the GNU General Public License
 
13
   as published by the Free Software Foundation; either version 2
 
14
   of the License, or (at your option) any later version.
 
15
 
 
16
   This program is distributed in the hope that it will be useful,
 
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
   GNU General Public License for more details.
 
20
 
 
21
   You should have received a copy of the GNU General Public License
 
22
   along with this program; if not, write to the Free Software
 
23
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
24
*/
 
25
 
1
26
#include "cup.h"
2
27
#include "free.h"
3
28
#include "fixture.h"
143
168
    return FALSE;
144
169
}
145
170
 
146
 
/** Write the cup or league of the chooseteam into the appropriate pointer. */
 
171
/** Write the cup or league of the chooseteam into the appropriate pointer
 
172
    and return TRUE; return FALSE if no cup/league is found. */
147
173
void
148
174
cup_get_choose_team_league_cup(const CupChooseTeam *ct, 
149
175
                               const League **league, const Cup **cup)
156
182
 
157
183
    sscanf(ct->sid, "%[^0-9]%d", trash, &idx);
158
184
 
159
 
    if(g_str_has_prefix(ct->sid, "LEAGUE"))
 
185
    if(g_str_has_prefix(ct->sid, "LEAGUE") && idx >=0 && idx <= ligs->len)
160
186
    {
161
187
        *league = &lig(idx - 1);
162
188
        *cup = NULL;
163
189
    }
164
 
    else if(g_str_has_prefix(ct->sid, "CUP"))
 
190
    else if(g_str_has_prefix(ct->sid, "CUP") && idx >=0 && idx <= cps->len)
165
191
    {
166
192
        *cup = &cp(idx - 1);
167
193
        *league = NULL;
190
216
    }
191
217
 
192
218
    if(*league == NULL && *cup == NULL)
193
 
    {
194
 
        g_warning("cup_get_choose_team_league_cup: no league nor cup found for chooseteam %s",
195
 
                  ct->sid);
196
 
        main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
197
 
    }
 
219
        main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
220
                          "cup_get_choose_team_league_cup: no league nor cup found for chooseteam %s",
 
221
                          ct->sid);
198
222
}
199
223
 
200
224
/** Load the pointers to the teams participating in the 
208
232
    GPtrArray *teams = cup_round->team_ptrs;
209
233
 
210
234
    if(debug > 60)
211
 
        printf("cup_get_team_pointers %s \n", cup->name);
 
235
        g_print("cup_get_team_pointers %s round %d\n", cup->name, round);
212
236
 
213
237
    if(teams->len > 0)
214
238
        g_warning("cup_get_team_pointers: round %d in cup %s has non-empty team pointers array.",
216
240
 
217
241
    for(i=0;i<cup_round->choose_teams->len;i++)
218
242
        if(g_array_index(cup_round->choose_teams, CupChooseTeam, i).generate)
219
 
            cup_load_choose_team_generate(cup, cup_round,
220
 
                                          &g_array_index(cup_round->choose_teams, CupChooseTeam, i));
 
243
            cup_load_choose_team_generate(
 
244
                cup, cup_round,
 
245
                &g_array_index(cup_round->choose_teams, CupChooseTeam, i));
221
246
        else
222
 
            cup_load_choose_team(cup, teams, 
223
 
                                 &g_array_index(cup_round->choose_teams, CupChooseTeam, i));
 
247
            cup_load_choose_team(
 
248
                cup, teams, 
 
249
                &g_array_index(cup_round->choose_teams, CupChooseTeam, i));
224
250
 
225
251
    if(cup_round->teams->len > 0)
226
252
        while(teams->len + cup_round->teams->len > cup_round->new_teams)
231
257
 
232
258
    for(i=0;i<cup_round->teams->len;i++)
233
259
    {   
234
 
        team_generate_players_stadium(&g_array_index(cup_round->teams, Team, i));
 
260
        team_generate_players_stadium(&g_array_index(cup_round->teams, Team, i), 0);
235
261
        g_ptr_array_add(cup->teams, &g_array_index(cup_round->teams, Team, i));     
236
262
        g_ptr_array_add(teams, &g_array_index(cup_round->teams, Team, i));
237
263
    }
238
264
 
239
265
    if(debug > 70)
240
266
        for(i=0;i<teams->len;i++)
241
 
            printf("cup_get_team_pointers: %d %s \n", i, ((Team*)g_ptr_array_index(teams, i))->name);
 
267
            g_print("cup_get_team_pointers: %d %s (%d) %s\n", i,
 
268
                   ((Team*)g_ptr_array_index(teams, i))->name,
 
269
                   ((Team*)g_ptr_array_index(teams, i))->clid,
 
270
                   cup->name);
242
271
}
243
272
 
244
273
/** Get the pointers to the teams (already generated, in one of the leagues or cups)
254
283
    const Cup *cup_temp = NULL;
255
284
 
256
285
    if(debug > 60)
257
 
        printf("cup_load_choose_team: %s, %s \n", cup->name,
 
286
        g_print("cup_load_choose_team: %s, %s \n", cup->name,
258
287
               ct->sid);
259
288
 
260
289
    cup_get_choose_team_league_cup(ct, &league, &cup_temp);
288
317
            for(j = 0; j < end; j++)
289
318
            {
290
319
                if(debug > 80)
291
 
                    printf("team %s isinint %d numteams %d\n",
 
320
                    g_print("team %s isinint %d numteams %d\n",
292
321
                           team_of_id(g_array_index(league->table.elements, 
293
322
                                                    TableElement, order[j]).team_id)->name,
294
323
                           query_team_is_in_cups(
315
344
 
316
345
        if(ct->number_of_teams != -1 &&
317
346
           number_of_teams != ct->number_of_teams)
318
 
        {
319
 
            g_warning("cup_load_choose_team (1): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ",
320
 
                      ct->sid, cup->name, ct->number_of_teams, number_of_teams, cup->group);
321
 
            main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
322
 
        }
 
347
            main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
348
                              "cup_load_choose_team (1): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ",
 
349
                              ct->sid, cup->name, ct->number_of_teams, 
 
350
                              number_of_teams, cup->group);
323
351
    }
324
352
    else
325
353
    {
326
354
        if(season == 1 && cup->add_week == 0)
327
355
        {
328
356
            if(lig(0).teams->len < ct->number_of_teams)
329
 
            {
330
 
                g_warning("cup_load_choose_team: not enough teams in league 0 for chooseteam %s (%d; required: %d) in cup %s\n",
331
 
                          ct->sid, lig(0).teams->len, ct->number_of_teams, cup->name);
332
 
                main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
333
 
            }
 
357
                main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
358
                                  "cup_load_choose_team: not enough teams in league 0 for chooseteam %s (%d; required: %d) in cup %s\n",
 
359
                                  ct->sid, lig(0).teams->len, 
 
360
                                  ct->number_of_teams, cup->name);
334
361
 
335
362
            gint permutation[lig(0).teams->len];
336
363
            math_generate_permutation(permutation, 0, lig(0).teams->len - 1);
354
381
            }
355
382
            
356
383
            if(number_of_teams != ct->number_of_teams)
357
 
            {
358
 
                g_warning("cup_load_choose_team (2): not enough teams found in league 0 for chooseteam %s (%d; required: %d) in cup %s (group %d)\n",
359
 
                          ct->sid, number_of_teams, ct->number_of_teams, cup->name, cup->group);
360
 
                main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
361
 
            }
 
384
                main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
385
                                  "cup_load_choose_team (2): not enough teams found in league 0 for chooseteam %s (%d; required: %d) in cup %s (group %d)\n",
 
386
                                  ct->sid, number_of_teams, 
 
387
                                  ct->number_of_teams, cup->name, cup->group);
362
388
        }
363
389
        else
364
390
        {
394
420
 
395
421
            if(ct->number_of_teams != -1 &&
396
422
               number_of_teams != ct->number_of_teams)
397
 
            {
398
 
                g_warning("cup_load_choose_team(3): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ",
399
 
                          ct->sid, cup->name, ct->number_of_teams, number_of_teams, cup->group);
400
 
                main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
401
 
            }
 
423
                main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
424
                                  "cup_load_choose_team(3): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ",
 
425
                                  ct->sid, cup->name,
 
426
                                  ct->number_of_teams, number_of_teams, cup->group);
402
427
        }
403
428
    }
404
429
 
405
430
    if(debug > 80)
406
431
        for(i=debug_num;i<teams->len;i++)
407
 
            printf("cup_load_choose_team: %d %s \n", i, ((Team*)g_ptr_array_index(teams, i))->name);
 
432
            g_print("cup_load_choose_team: %d %s \n", i, ((Team*)g_ptr_array_index(teams, i))->name);
408
433
}
409
434
 
410
435
/** Load the teams specified in the chooseteam from a non-country league. */
418
443
    GPtrArray *sids = NULL;
419
444
 
420
445
    if(debug > 60)
421
 
        printf("cup_load_choose_team_generate: %s, %s \n", cup->name,
 
446
        g_print("cup_load_choose_team_generate: %s, %s \n", cup->name,
422
447
               ct->sid);
423
448
 
424
449
    leagues = g_array_new(FALSE, FALSE, sizeof(League));
471
496
 
472
497
    if(teams_local->len < end_idx)
473
498
    {
474
 
        g_warning("cup_load_choose_team_generate: not enough teams (%d) in chooseteam %s in cup %s (%d are specified) \n",
475
 
                  teams_local->len, ct->sid, cup->name, end_idx);
476
 
        
477
499
        free_teams_array(&teams_local, FALSE);
478
 
        main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
 
500
        main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
501
                          "cup_load_choose_team_generate: not enough teams (%d) in chooseteam %s in cup %s (%d are specified) \n",
 
502
                          teams_local->len, ct->sid, 
 
503
                          cup->name, end_idx);
479
504
    }
480
505
 
481
506
    for(j = 0; j < end_idx; j++)
497
522
    if(((ct->number_of_teams != -1 && number_of_teams != ct->number_of_teams) ||
498
523
        (ct->number_of_teams == -1 && number_of_teams != teams_local->len)) &&
499
524
       teams_local->len > 0)
500
 
    {
501
 
        g_warning("cup_load_choose_team_generate: not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found).\n ", ct->sid, cup->name, ct->number_of_teams, number_of_teams);
502
 
        main_exit_program(EXIT_CHOOSE_TEAM_ERROR, NULL);
503
 
    }
504
 
 
 
525
        main_exit_program(EXIT_CHOOSE_TEAM_ERROR, 
 
526
                          "cup_load_choose_team_generate: not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found).\n ", 
 
527
                          ct->sid, cup->name, 
 
528
                          ct->number_of_teams, number_of_teams);
 
529
    
505
530
    for(j=teams_local->len - 1; j>=0;j--)
506
531
        if(query_team_is_in_cup(&g_array_index(teams_local, Team, j), cup))
507
532
            g_array_remove_index(teams_local, j);
668
693
            cup_get_matchdays_in_cup_round(cup, cup_round) * cup->week_gap;
669
694
 
670
695
    if(week_number <= 0)
671
 
    {
672
 
        g_warning("cup_get_first_week_of_cup_round: first week of cup %s cup round %d is not positive (%d).\nPlease lower the week gap or set a later last week.\n", cup->name, cup_round, week_number);
673
 
        main_exit_program(EXIT_FIRST_WEEK_ERROR, NULL);
674
 
    }
675
 
 
 
696
        main_exit_program(EXIT_FIRST_WEEK_ERROR, 
 
697
                          "cup_get_first_week_of_cup_round: first week of cup %s cup round %d is not positive (%d).\nPlease lower the week gap or set a later last week.\n", 
 
698
                          cup->name, cup_round, week_number);
 
699
    
676
700
    return week_number;
677
701
}
678
702
 
703
727
    if(cup_round->round_robin_number_of_groups > 0)
704
728
    {
705
729
        number_of_teams = cup_round_get_number_of_teams(cup, round);
 
730
        number_of_teams += cup_round->round_robin_number_of_groups - 1;
 
731
        number_of_teams /= cup_round->round_robin_number_of_groups;
 
732
        /* Now, number_of_teams = number of teams in largest group */
 
733
        if (number_of_teams % 2 == 0)
 
734
            number_of_matchdays = number_of_teams - 1;
 
735
        else
 
736
            number_of_matchdays = number_of_teams;
706
737
        
707
 
        if(number_of_teams % cup_round->round_robin_number_of_groups == 0)
708
 
            number_of_matchdays = 
709
 
                ((number_of_teams / cup_round->round_robin_number_of_groups) - 1) * 2;
710
 
        else
711
 
            number_of_matchdays =
712
 
                ((number_of_teams - 
713
 
                  (number_of_teams % cup_round->round_robin_number_of_groups)) /
714
 
                 cup_round->round_robin_number_of_groups) * 2;
 
738
        if (g_array_index(cup->rounds, CupRound, round).home_away)
 
739
            number_of_matchdays *= 2;
715
740
    }
716
741
    else if(g_array_index(cup->rounds, CupRound, round).home_away)
717
742
        number_of_matchdays = 2;
836
861
        if(cp(i).id == clid)
837
862
            return &cp(i);
838
863
 
839
 
    g_warning("cup_from_clid: didn't find cup with id %d\n", clid);
840
 
 
841
 
    main_exit_program(EXIT_POINTER_NOT_FOUND, NULL);
 
864
    main_exit_program(EXIT_POINTER_NOT_FOUND, 
 
865
                      "cup_from_clid: didn't find cup with id %d\n", clid);
842
866
 
843
867
    return NULL;
844
868
}
853
877
        if(strcmp(cp(i).sid, sid) == 0)
854
878
            return &cp(i);
855
879
 
856
 
    g_warning("cup_from_sid: didn't find cup with sid %s \n", sid);
857
 
 
858
 
    main_exit_program(EXIT_POINTER_NOT_FOUND, NULL);
 
880
    main_exit_program(EXIT_POINTER_NOT_FOUND, 
 
881
                      "cup_from_sid: didn't find cup with sid %s \n", sid);
859
882
 
860
883
    return NULL;
861
884
}
949
972
gboolean
950
973
query_cup_begins(const Cup *cup)
951
974
{
952
 
    gint i;
 
975
    gint i, j;
953
976
    const League *league = NULL;
954
977
    const Cup *cup_temp = NULL;
955
 
    gboolean proceed = FALSE;
956
 
    const CupRound *cup_round = &g_array_index(cup->rounds, CupRound, 0);
957
 
 
958
 
    for(i=0;i<cup_round->choose_teams->len;i++)
959
 
        if(!g_array_index(cup_round->choose_teams,CupChooseTeam, i).generate)
960
 
        {
961
 
            
962
 
            cup_get_choose_team_league_cup(
963
 
                &g_array_index(cup_round->choose_teams,
964
 
                               CupChooseTeam, i), &league, &cup_temp);
965
 
 
966
 
            if((cup_temp == NULL && 
967
 
                (!league->active || 
968
 
                 (g_array_index(league->fixtures, Fixture,
969
 
                                league->fixtures->len - 1).week_number == week &&
970
 
                  g_array_index(league->fixtures, Fixture,
971
 
                                league->fixtures->len - 1).week_round_number == week_round))) ||
972
 
               (league == NULL && 
973
 
                (cup_temp->fixtures->len > 0 &&
974
 
                 g_array_index(cup_temp->fixtures, Fixture,
975
 
                               cup_temp->fixtures->len - 1).week_number == week &&
976
 
                 g_array_index(cup_temp->fixtures, Fixture,
977
 
                               cup_temp->fixtures->len - 1).week_round_number == week_round)))
978
 
                proceed = TRUE;
979
 
        }
980
 
    
981
 
    if(!proceed)
982
 
        return FALSE;
983
 
 
984
 
    for(i=0;i<cup_round->choose_teams->len;i++)
985
 
        if(!g_array_index(cup_round->choose_teams,CupChooseTeam, i).generate)
986
 
        {
987
 
            cup_get_choose_team_league_cup(
988
 
                &g_array_index(cup_round->choose_teams,
989
 
                               CupChooseTeam, i), &league, &cup_temp);
990
 
            if((cup_temp == NULL &&
991
 
                (league->active &&
992
 
                 g_array_index(league->fixtures, Fixture,
993
 
                               league->fixtures->len - 1).attendance == -1)) ||
994
 
               (league == NULL &&
995
 
                (cup_temp->fixtures->len == 0 || 
996
 
                 (cup_temp->fixtures->len > 0 && 
997
 
                  g_array_index(cup_temp->fixtures, Fixture,
998
 
                                cup_temp->fixtures->len - 1).attendance == -1))))
999
 
                return FALSE;
1000
 
        }
 
978
    const CupRound *cup_round = NULL;
 
979
 
 
980
    for(j=0;j<cup->rounds->len;j++)
 
981
    {
 
982
        cup_round = &g_array_index(cup->rounds, CupRound, j);
 
983
 
 
984
        for(i=0;i<cup_round->choose_teams->len;i++)
 
985
            if(!g_array_index(cup_round->choose_teams,CupChooseTeam, i).generate)
 
986
            {           
 
987
                cup_get_choose_team_league_cup(
 
988
                    &g_array_index(cup_round->choose_teams,
 
989
                                   CupChooseTeam, i), &league, &cup_temp);
 
990
                
 
991
                if((cup_temp == NULL &&
 
992
                    league->active &&
 
993
                    g_array_index(league->fixtures, Fixture,
 
994
                                  league->fixtures->len - 1).attendance == -1) ||
 
995
                   (league == NULL &&
 
996
                    ((cup_temp->fixtures->len > 0 &&
 
997
                      g_array_index(cup_temp->fixtures, Fixture,
 
998
                                    cup_temp->fixtures->len - 1).attendance == -1) ||
 
999
                     cup_temp->fixtures->len == 0)))
 
1000
                    return FALSE;
 
1001
            }
 
1002
    }
1001
1003
 
1002
1004
    return TRUE;
1003
1005
}