-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery.php
766 lines (671 loc) · 24.6 KB
/
lottery.php
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
<?php
/***************************************************************************
* lottery.php
* -------------------
* Version : 2.3.0
* Support forums : http://forums.zarath.com
* Website : http://www.zarath.com
*
***************************************************************************/
/***************************************************************************
*
* copyright (C) 2004, 2007 Zarath
*
* 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.
*
* http://www.gnu.org/copyleft/gpl.html
*
***************************************************************************/
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.' . $phpEx);
//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//
#
# Register action variable!
#
if ( isset($HTTP_GET_VARS['action']) || isset($HTTP_POST_VARS['action']) ) { $action = ( isset($HTTP_POST_VARS['action']) ) ? $HTTP_POST_VARS['action'] : $HTTP_GET_VARS['action']; }
else { $action = ''; }
#
# Finish registering variable
#
if ( !($board_config['lottery_status']) ) { message_die(GENERAL_MESSAGE, $lang['lottery_disabled']); }
//
// Get user's current entry information
// Check for multiple ticket purchases too
//
$sql = "SELECT *
FROM " . LOTTERY_TABLE . "
WHERE user_id = {$userdata['user_id']}";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
$sql_count = $db->sql_numrows($result);
$tickbuy = ( ($board_config['lottery_ticktype'] == 'single') && $sql_count ) ? 0 : 1;
//
// Finished checks
//
//check if lottery should be drawn
$timeleft = ( ( $board_config['lottery_start'] + $board_config['lottery_length'] ) - time() );
$thetime = time();
if ( $timeleft < 1 )
{
$sql = "SELECT *
FROM " . LOTTERY_TABLE;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
if ( $sql_count = $db->sql_numrows($result) )
{
//
// Calculate current prizepool
//
$pool = (($sql_count * $board_config['lottery_cost']) + $board_config['lottery_base']);
//
// Select winner
//
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
srand($seed);
$randnum = (rand(1, $sql_count)-1);
for ($i = 0; $i < $sql_count; $i++)
{
if (!( $row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
if ( $i == $randnum )
{
// Get winner's name and current items (incase items need to be added!)
$row2 = get_userdata($row['user_id']);
break;
}
}
if ( defined('SHOP_TABLE') )
{
#
# Explode items array, loop over items array to make sure all item exist
# Replace all RANDOM (lowercase) items with items from price to price [in store]
#
$item_array = explode(';', $board_config['lottery_win_items']);
$add_items = array();
for ($i = 0; $i < count($item_array); $i++)
{
$item_array[$i] = trim($item_array[$i]);
if ( $item_array[$i] == 'random' )
{
$shop_sql = ( !empty($board_config['lottery_random_shop']) ) ? "AND shop = '" . str_replace("'", "''", $board_config['lottery_random_shop']) . "'" : '';
$sql = "SELECT *
FROM " . SHOP_ITEMS_TABLE . "
WHERE cost > " . (int)$board_config['lottery_item_mcost'] . "
AND cost < " . (int)$board_config['lottery_item_xcost'] . "
" . $shop_sql . "
ORDER BY RAND()
LIMIT 0, 1";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'shop items'), '', __LINE__, __FILE__, $sql);
}
if ( $db->sql_numrows($result) )
{
if (!( $item_row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'shop items'), '', __LINE__, __FILE__, $sql);
}
if ( defined('USER_ITEMS_TABLE') )
{
$decay_time = ( $row['decay'] ) ? ( time() + $row['decay'] ) : 0;
$sql = "INSERT INTO " . USER_ITEMS_TABLE . "
(user_id, item_id, item_name, item_s_desc, item_l_desc, die_time)
VALUES({$row2['user_id']}, {$item_row['id']}, '" . str_replace("'", "''", $item_row['name']) . "', '" . str_replace("'", "''", $item_row['sdesc']) . "', '" . str_replace("'", "''", $item_row['ldesc']) . "', $decay_time)";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_MESSAGE, 'Fatal Error: Unable to add item to user for random');
}
}
else
{
$add_items[] = $item_row['name'];
}
}
}
elseif ( !empty($item_array[$i]) )
{
$sql = "SELECT *
FROM " . SHOP_ITEMS_TABLE . "
WHERE name = '" . str_replace("'", "''", $item_array[$i]) . "'";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'shop items'), '', __LINE__, __FILE__, $sql);
}
if ( $db->sql_numrows($result) )
{
if (!( $item_row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'shop items'), '', __LINE__, __FILE__, $sql);
}
if ( defined('USER_ITEMS_TABLE') )
{
$decay_time = ( $row['decay'] ) ? (time() + $row['decay']) : 0;
$sql = "INSERT INTO " . USER_ITEMS_TABLE . "
(user_id, item_id, item_name, item_s_desc, item_l_desc, die_time)
VALUES({$row2['user_id']}, {$item_row['id']}, '" . str_replace("'", "''", $item_row['name']) . "', '" . str_replace("'", "''", $item_row['sdesc']) . "', '" . str_replace("'", "''", $item_row['ldesc']) . "', $decay_time)";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_MESSAGE, 'Fatal Error: Unable to add normal item');
}
}
else
{
$add_items[] = $item_row['name'];
}
}
}
}
if ( count($add_items) > 0 && !defined('USER_ITEMS_TABLE') )
{
$new_items = str_replace("'", "''", $row2['user_items'] . 'ß' . implode('Þß', $add_items) . 'Þ');
#
# Add up new total & insert into database
#
$sql = "UPDATE " . USERS_TABLE . "
SET user_items = '$new_items'
WHERE user_id = {$row2['user_id']}";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'users'), '', __LINE__, __FILE__, $sql);
}
}
}
#
# Support for the cash mod, check for other currency set -- if none or failed, use user_points
#
if ( $board_config['cash_installed'] )
{
include_once($phpbb_root_path . 'includes/functions_cash.php');
$cash_sql_where = ( !empty($board_config['lottery_currency']) ) ? "WHERE cash_name = '" . str_replace("'", "''", $board_config['lottery_currency']) . "'" : '';
$sql = "SELECT `cash_dbfield`, `cash_name`
FROM " . CASH_TABLE . "
" . $cash_sql_where;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'cash'), '', __LINE__, __FILE__, $sql);
}
if ( $db->sql_numrows($result) )
{
if (!( $cash_row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'cash'), '', __LINE__, __FILE__, $sql);
}
$currency_db = $cash_row['cash_dbfield'];
$currency_name = $cash_row['cash_name'];
}
}
else
{
$currency_db = 'user_points';
$currency_name = $board_config['points_name'];
}
#
# Add up new total & insert into database
#
$sql = "UPDATE " . USERS_TABLE . "
SET `" . $currency_db . "` = " . $currency_db . " + $pool
WHERE user_id = {$row2['user_id']}";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'users'), '', __LINE__, __FILE__, $sql);
}
$sql = "INSERT INTO " . LOTTERY_HISTORY_TABLE . "
(user_id, amount, currency, time)
VALUES ({$row2['user_id']}, $pool, '" . str_replace("'", "''", $currency_name) . "', $thetime)";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_inserting'], 'lottery history'), '', __LINE__, __FILE__, $sql);
}
}
//
// Begin reset of lottery
//
$sql = "DELETE FROM " . LOTTERY_TABLE;
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_deleting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
if ($board_config['lottery_reset'])
{
$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '" . ($thetime - 30) . "'
WHERE config_name = 'lottery_start'";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'config'), '', __LINE__, __FILE__, $sql);
}
}
else
{
$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '0'
WHERE config_name = 'lottery_status'";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'config'), '', __LINE__, __FILE__, $sql);
}
$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '0'
WHERE config_name = 'lottery_start'";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'config'), '', __LINE__, __FILE__, $sql);
}
}
redirect("lottery.$phpEx");
}
//end lottery draw check
//default lottery page
if ( empty($action) )
{
$template->set_filenames(array(
'body' => 'lottery_index_body.tpl')
);
$duration = duration($timeleft);
$title = $board_config['lottery_name'] . $lang['lottery_information'];
#
# Begin action checking and toggling!
#
if ( $tickbuy || $board_config['lottery_history'] )
{
if ( $tickbuy )
{
#
# Begin switch to allow user to buy a ticket!
#
if ( $board_config['lottery_mb'] )
{
$template->assign_block_vars('switch_tickets_multi', array());
}
else
{
$template->assign_block_vars('switch_tickets_single', array());
}
}
if ( $board_config['lottery_history'] )
{
$template->assign_block_vars('switch_view_history', array());
$sql = "SELECT *
FROM " . LOTTERY_HISTORY_TABLE . "
WHERE user_id = {$userdata['user_id']}";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
if ( $db->sql_numrows($result) )
{
$template->assign_block_vars('switch_view_personal', array());
}
}
$template->assign_block_vars('switch_are_actions', array());
}
#
# Begin switch to allow full display (full pot & current entries)!
#
if ( $board_config['lottery_show_entries'] )
{
$template->assign_block_vars('switch_full_display', array());
$sql = "SELECT *
FROM " . LOTTERY_TABLE;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
$total_entries = $db->sql_numrows($result);
$total_pool = ($board_config['lottery_cost'] * $total_entries) + $board_config['lottery_base'];
}
#
# Begin currency checking (name)
#
if ( defined('CASH_TABLE') )
{
$currency_name = $board_config['lottery_currency'];
}
else
{
$currency_name = $board_config['points_name'];
}
#
# Begin checks, switch and rearrangements of items in lottery
#
if ( $board_config['lottery_items'] && !empty($board_config['lottery_win_items']) )
{
$lottery_items = str_replace(';', ', ', $board_config['lottery_win_items']);
$template->assign_block_vars('switch_items', array());
}
#
# Grab last winner from lottery -- ORDERED BY TIME
#
$sql = "SELECT t1.*, t2.username
FROM " . LOTTERY_HISTORY_TABLE . " t1, " . USERS_TABLE . " t2
WHERE t2.user_id = t1.user_id
ORDER BY time DESC";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery history'), '', __LINE__, __FILE__, $sql);
}
if ( $db->sql_numrows($result) )
{
if (!( $row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
$template->assign_block_vars('switch_last_winner', array(
'WINNER_NAME' => $row['username']));
}
$page_title = $board_config['lottery_name'];
$template->assign_vars(array(
'TICKETS_OWNED' => $sql_count,
'L_PRIZE_BASE' => $board_config['lottery_base'],
'L_TICKET_COST' => $board_config['lottery_cost'],
'L_TOTAL_PRIZE' => $total_pool,
'L_CURRENT_ENTRIES' => $total_entries,
'L_ITEM_PRIZE' => $lottery_items,
'S_CONFIG_ACTION' => append_sid('lottery.'.$phpEx),
'L_CURRENCY' => $currency_name,
'L_DURATION' => $duration,
'L_NAME' => $board_config['lottery_name'],
'L_INFO_TITLE' => $board_config['lottery_name'] . ' ' . $lang['lottery_information'],
'L_ACTIONS_TITLE' => $lang['lottery_actions'],
'L_TICKET_OWNED' => $lang['lottery_tickets_owned'],
'L_TICKETS_COST' => $lang['lottery_ticket_cost'],
'L_BASE_POOL' => $lang['lottery_base_pool'],
'L_CURRENT_POOL' => $lang['lottery_current_entries'],
'L_TOTAL_POOL' => $lang['lottery_total_pool'],
'L_ITEM_DRAW' => $lang['lottery_item_draw'],
'L_TIME_DRAW' => $lang['lottery_time_draw'],
'L_LAST_WINNER' => $lang['lottery_last_winner'],
'I_BUY_TICKET' => $lang['lottery_buy_ticket'],
'I_BUY_TICKETS' => $lang['lottery_buy_tickets'],
'I_VIEW_HISTORY' => $lang['lottery_view_history'],
'I_VIEW_PHISTORY' => $lang['lottery_view_phistory']
));
$template->assign_block_vars('', array());
}
elseif ( $action == 'options' )
{
if ( !$userdata['session_logged_in'] )
{
$redirect = "lottery.$phpEx";
redirect("login.$phpEx?redirect=$redirect");
}
#
# Register the globals for both actions [allows checks]
#
if ( isset($HTTP_GET_VARS['amount']) || isset($HTTP_POST_VARS['amount']) ) { $amount = ( isset($HTTP_POST_VARS['amount']) ) ? intval($HTTP_POST_VARS['amount']) : intval($HTTP_GET_VARS['amount']); }
else { $amount = ''; }
if ( isset($HTTP_GET_VARS['view_history']) || isset($HTTP_POST_VARS['view_history']) ) { $view_history = ( isset($HTTP_POST_VARS['view_history']) ) ? htmlspecialchars($HTTP_POST_VARS['view_history']) : htmlspecialchars($HTTP_GET_VARS['view_history']); }
else { $view_history = ''; }
if ( isset($HTTP_GET_VARS['view_personal']) || isset($HTTP_POST_VARS['view_personal']) ) { $view_personal = ( isset($HTTP_POST_VARS['view_personal']) ) ? htmlspecialchars($HTTP_POST_VARS['view_personal']) : htmlspecialchars($HTTP_GET_VARS['view_personal']); }
else { $view_personal = ''; }
if ( isset($HTTP_GET_VARS['start']) || isset($HTTP_POST_VARS['start']) ) { $start = ( isset($HTTP_POST_VARS['start']) ) ? intval($HTTP_POST_VARS['start']) : intval($HTTP_GET_VARS['start']); }
else { $start = 0; }
if ( isset($HTTP_GET_VARS['buy_ticket']) || isset($HTTP_POST_VARS['buy_ticket']) ) { $buy_ticket = ( isset($HTTP_POST_VARS['buy_ticket']) ) ? $HTTP_POST_VARS['buy_ticket'] : $HTTP_GET_VARS['buy_ticket']; }
else { $buy_ticket = ''; }
if ( isset($HTTP_GET_VARS['buy_tickets']) || isset($HTTP_POST_VARS['buy_tickets']) ) { $buy_tickets = ( isset($HTTP_POST_VARS['buy_tickets']) ) ? $HTTP_POST_VARS['buy_tickets'] : $HTTP_GET_VARS['buy_tickets']; }
else { $buy_tickets = ''; }
if ( !empty($buy_ticket) || !empty($buy_tickets) )
{
#
# Make sure they can buy a ticket & have enough gil!
#
if ( !($tickbuy) )
{
message_die(GENERAL_MESSAGE, $lang['lottery_too_many_tickets']);
}
$amount = ( ($amount < 1) || ($amount > 9999) ) ? 1 : $amount;
#
# Make sure if buying MULTIPLE tickets, they are allowed to -- and it doesn't exceed the max
#
if ( $board_config['lottery_ticktype'] != 'multiple' || !($board_config['lottery_mb']) )
{
$amount = 1;
}
else
{
if ( $amount > $board_config['lottery_mb_amount'] ) { $amount = $board_config['lottery_mb_amount']; }
}
#
# Check if total entries in the prize > $mb_amount
#
$sql = "SELECT count(*) as total_tickets
FROM " . LOTTERY_TABLE . "
WHERE user_id = {$userdata['user_id']}";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
if (!( $row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
if ( ( $row['total_tickets'] + $amount ) > $board_config['lottery_mb_amount'] )
{
$amount = ( $board_config['lottery_mb_amount'] - $row['total_tickets'] );
if ( $amount < 1 ) { message_die(GENERAL_MESSAGE, $lang['lottery_max_tickets']); }
}
$ticket_cost = ( $board_config['lottery_cost'] * $amount );
#
# Begin checks on multiple currencies, if they exist!
#
if ( $board_config['cash_installed'] )
{
include_once($phpbb_root_path . 'includes/functions_cash.php');
$cash_sql_where = ( !empty($board_config['lottery_currency']) ) ? "WHERE cash_name = '" . str_replace("'", "''", $board_config['lottery_currency']) . "'" : '';
$sql = "SELECT `cash_dbfield`
FROM " . CASH_TABLE . "
" . $cash_sql_where;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'cash'), '', __LINE__, __FILE__, $sql);
}
if ( $db->sql_numrows($result) )
{
if (!( $row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'cash'), '', __LINE__, __FILE__, $sql);
}
if ( ($userdata[$row['cash_dbfield']] - $ticket_cost) < 0 )
{
$msg = ( $amount == 1 ) ? $lang['lottery_purchased_ticket'] : sprintf($lang['lottery_purchased_tickets'], $amount);
message_die(GENERAL_MESSAGE, $lang['lottery_purchased_ne'] . $row['cash_name'] . $msg);
}
$sql = "UPDATE " . USERS_TABLE . "
SET `" . $row['cash_dbfield'] . "` = (" . $row['cash_dbfield'] . " - $ticket_cost)
WHERE user_id = {$userdata['user_id']}";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'users'), '', __LINE__, __FILE__, $sql);
}
$cash_done = 1;
}
}
if ( !($cash_done) )
{
if ( ($userdata['user_points'] - $ticket_cost) < 0 )
{
$msg = ( $amount == '1' ) ? $lang['lottery_purchased_ticket'] : sprintf($lang['lottery_purchased_tickets'], $amount);
message_die(GENERAL_MESSAGE, $lang['lottery_purchased_ne'] . $board_config['points_name'] . $msg);
}
$sql = "UPDATE " . USERS_TABLE . "
SET user_points = (user_points - $ticket_cost)
WHERE user_id = {$userdata['user_id']}";
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_updating'], 'users'), '', __LINE__, __FILE__, $sql);
}
}
$sql = "INSERT INTO " . LOTTERY_TABLE . "
(user_id)
VALUES ({$userdata['user_id']})";
for ($i = 0; $i < $amount; $i++)
{
if ( !($db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_inserting'], 'lottery'), '', __LINE__, __FILE__, $sql);
}
}
$msg = ( $amount < 2 ) ? sprintf($lang['lottery_ticket_bought'], $board_config['lottery_name']) : sprintf($lang['lottery_tickets_bought'], $amount, $board_config['lottery_name']);
message_die(GENERAL_MESSAGE, $msg);
}
elseif ( !empty($view_history) || ( !empty($view_personal) ) )
{
#
# History Layers, these will be fairly basic, just a straight check of SQL and output to a template.
# Two choices, self history (will probably be empty most of them...) and full history. Not sure how
# I'm going to implement the second, at the moment it's just view all history. :P
#
#
# Finish registering variables!
#
$template->set_filenames(array(
'body' => 'lottery_history_body.tpl')
);
#
# Make sure the lottery history is enabled! [or at least viewable]
#
if ( !($board_config['lottery_history']) ) { message_die(GENERAL_MESSAGE, $lang['lottery_history_disabled']); }
if ( !empty($view_personal) )
{
$sql = "SELECT t1.*, t2.username
FROM " . LOTTERY_HISTORY_TABLE . " t1, " . USERS_TABLE . " t2
WHERE t1.user_id = {$userdata['user_id']}
AND t2.user_id = t1.user_id
ORDER BY time DESC
LIMIT $start, {$board_config['topics_per_page']}";
# Pagination SQL Query...
$page_sql = "SELECT count(*) AS total
FROM " . LOTTERY_HISTORY_TABLE . "
WHERE user_id = {$userdata['user_id']}";
}
elseif ( !empty($view_history) )
{
$sql = "SELECT t1.*, t2.username
FROM " . LOTTERY_HISTORY_TABLE . " t1, " . USERS_TABLE . " t2
WHERE t2.user_id = t1.user_id
ORDER BY time DESC
LIMIT $start, {$board_config['topics_per_page']}";
# Pagination SQL Query...
$page_sql = "SELECT count(*) AS total
FROM " . LOTTERY_HISTORY_TABLE;
}
else { message_die(GENERAL_MESSAGE, $lang['lottery_no_history_type']); }
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery history'), '', __LINE__, __FILE__, $sql);
}
$sql_count = $db->sql_numrows($result);
if ( !($sql_count) )
{
#
# Code to toggle no history!
#
$template->assign_block_vars('switch_no_history', array(
'MESSAGE' => $lang['lottery_no_history']));
}
else
{
#
# Begin of loops over history to directly output it on the history page! :)
#
for ($i = 0; $i < $sql_count; $i++)
{
if (!( $row = $db->sql_fetchrow($result) ))
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery history'), '', __LINE__, __FILE__, $sql);
}
$rownum = ( $i % 2 ) ? 'row1' : 'row2';
$template->assign_block_vars('listrow', array(
'ROW_CLASS' => $rownum,
'HISTORY_NUM' => $i + 1 + $start,
'HISTORY_WINNER' => $row['username'],
'HISTORY_AMOUNT' => $row['amount'],
'HISTORY_CURRENCY' => $row['currency'],
'HISTORY_TIME' => create_date($board_config['default_dateformat'], $row['time'], $board_config['board_timezone']))
);
}
$template->assign_block_vars('switch_title_info', array());
}
#
# Begin pagination based on topics per page... I REALLY hate pagination, but it's the only way to stop
# Massive build ups on a single page.
#
if ( !($result = $db->sql_query($page_sql)) )
{
message_die(GENERAL_ERROR, sprintf($lang['lottery_error_selecting'], 'lottery history'), '', __LINE__, __FILE__, $sql);
}
$sql_count = $db->sql_numrows($result);
if ( ($total = $db->sql_fetchrow($result)) && ($sql_count > 0) )
{
$total_history = $total['total'];
if ( $total_history > $board_config['topics_per_page'] )
{
$pagination = generate_pagination("lottery.$phpEx?action=options&view_history=$view_history&view_personal=$view_personal", $total_history, $board_config['topics_per_page'], $start). ' ';
}
else
{
$pagination = ' ';
}
}
#
# Finished pagination, now wrapping up the page and displaying it...
#
$page_title = $board_config['lottery_name'];
$next_location = ' -> <a href="' . append_sid("lottery.$phpEx") . '" class="nav">' . $board_config['lottery_name'] . '</a> -> <a href="' . append_sid("lottery.$phpEx") . '" class="nav">' . $board_config['lottery_name'] . ' ' . $lang['lottery_history'] . '</a>';
$template->assign_vars(array(
'L_HISTORY' => $lang['lottery_current_history'],
'L_ID' => $lang['lottery_ID'],
'L_WINNER' => $lang['lottery_winner'],
'L_AMOUNT_WON' => $lang['lottery_amount_won'],
'L_TIME_WON' => $lang['lottery_time_won'],
'L_TOTAL_HISTORY' => sprintf($lang['lottery_total_history'], $total_history),
'LOCATION' => $next_location,
'PAGINATION' => $pagination,
'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $board_config['topics_per_page'] ) + 1 ), ceil( $total_history / $board_config['topics_per_page'] )),
'L_GOTO_PAGE' => $lang['Goto_page']
));
$template->assign_block_vars('', array());
}
else
{
redirect("lottery.$phpEx");
}
}
else
{
message_die(GENERAL_MESSAGE, $lang['lottery_invalid_command']);
}
//
// Start output of page
//
include($phpbb_root_path . 'includes/page_header.' . $phpEx);
//
// Generate the page
//
$template->pparse('body');
include($phpbb_root_path . 'includes/page_tail.' . $phpEx);
?>