· 8 years ago · Mar 14, 2018, 12:46 AM
1<?php
2/**
3 * Copyright 2016, AmN Tech
4 * http://amn.tech
5 * Skype: askamn786
6 */
7
8if(!defined("IN_MYBB"))
9{
10 die("Nope.");
11}
12
13define( 'FLIPCOIN_PLUGIN_NAME', 'flipcoin' );
14define( 'FLIPCOIN_PLUGIN_FRIENDLY_NAME', 'Flip Coin' );
15
16$plugins->add_hook( 'newpoints_default_menu', FLIPCOIN_PLUGIN_NAME.'_newpoints_default_menu' );
17$plugins->add_hook( 'newpoints_start', FLIPCOIN_PLUGIN_NAME.'_newpoints_start' );
18
19/**
20 * Plugin Information
21 *
22 * @return array Array of information
23 */
24function flipcoin_info()
25{
26 global $plugins_cache;
27 $pluginfilename = basename( __FILE__, ".php" );
28
29 $plugininfo = array(
30 "name" => FLIPCOIN_PLUGIN_FRIENDLY_NAME,
31 "description" => 'A Coin Flipping system, allows your users to wager credits and win/lose accordingly',
32 "website" => "http://amn.tech/",
33 "author" => "AmN",
34 "authorsite" => "http://amn.tech",
35 "version" => "1.1",
36 "compatibility" => "*"
37 );
38
39 if(is_array($plugins_cache) && is_array($plugins_cache['active']) && $plugins_cache['active'][$pluginfilename])
40 {
41 $plugininfo['description'] = FLIPCOIN_PLUGIN_FRIENDLY_NAME . " is installed and working properly.";
42 }
43
44 return $plugininfo;
45}
46
47/**
48 * Called when the plugin is installed
49 */
50function flipcoin_install()
51{
52 global $db, $lang;
53
54 $lang->load( FLIPCOIN_PLUGIN_NAME );
55
56 // Templates
57 $templatesArray = array(
58 'home' => '<html>
59 <head>
60 <title>{$mybb->settings[\'bbname\']} - {$lang->flipcoin_newpoints_menu}</title>
61 {$headerinclude}
62 <script type="text/javascript" src="jscripts/flipcoin.amn.min.js"></script>
63 <style>
64 .flipcoins-credits {
65 float: right;
66 border: 1px solid #1C58A2;
67 padding: 5px 10px;
68 border-radius: 2px;
69 cursor: pointer;
70 background: rgba(0,0,0,0.5);
71 }
72 .flip {
73 position: relative;
74 overflow: hidden;
75 margin: 40px auto;
76 width: 100px;
77 height: 100px;
78 padding: 10px;
79 }
80 .flip, .flip a {
81 width: 100px;
82 height: 100px;
83 text-align: center;
84 }
85 .flip a {
86 transition: none!important;
87 font-size: 20px;
88 font-weight: bold;
89 line-height: 100px;
90 position: absolute;
91 top: 0px;
92 left: 0px;
93 display: block;
94 overflow: hidden;
95 -webkit-user-select: none;
96 -moz-user-select: none;
97 -ms-user-select: none;
98 user-select: none;
99 text-decoration: none;
100 border-width: 6px;
101 border-style: double;
102 -webkit-border-radius: 50%;
103 -moz-border-radius: 50%;
104 border-radius: 50%;
105 outline: none !important;
106 -khtml-border-radius: 50%;
107 -webkit-text-size-adjust: none;
108 -webkit-touch-callout: none;
109 -khtml-user-select: none;
110 white-space: nowrap;
111 color: rgba(242,215,50,1);
112 background: linear-gradient(45deg, rgba(242,215,12,1) 0%,rgba(255,255,255,1) 56%,rgba(252,235,0,1) 96%);
113 box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, .1);
114 text-shadow: none;
115 font-family: monospace;
116 }
117 .flip a::after {
118 position: absolute;
119 top: 0;
120 left: 0;
121 width: 100%;
122 height: 50%;
123 content: \'\';
124 }
125 .flip a.flipping:hover {
126 cursor: default;
127 }
128 .notification {
129 padding: 10px 15px!important;
130 background: transparent!important;
131 }
132 .notification.success {
133 border: 1px solid green;
134 }
135 .notification.error{
136 border: 1px solid #f00;
137 }
138 .flipcoin-rules {
139 font-style: italic;
140 font-size: 12px;
141 }
142 .flipcoin-rules div {
143 }
144 </style>
145 </head>
146 <body>
147 {$header}
148 <table width="100%" border="0" align="center">
149 <tr>
150 <td valign="top" width="180">
151 <table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
152 <tr>
153 <td class="thead"><strong>{$lang->newpoints_menu}</strong></td>
154 </tr>
155 {$options}
156 </table>
157 </td>
158 <td valign="top">
159 <input type="hidden" name="csrfkey" value="{$mybb->post_code}" />
160 <table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
161 <tr>
162 <td class="thead" colspan="2"><strong>{$lang->flipcoin_newpoints_home}</strong></td>
163 </tr>
164 <tr>
165 <td class="trow1" colspan="2">
166 <div class="flipcoins-credits">{$mypoints}</div>
167 <div class="flip">
168 <a title="Flip me!" class="save start" href="javascript:void(0);">HEADS</a>
169 </div>
170 <div class="notification">
171 </div>
172 </td>
173 </tr>
174 <tr>
175 <td class="trow1" width="50%"><strong>Bet Amount:</strong></td>
176 <td class="trow1" width="50%">
177 <input type="text" name="bet" value="1" class="textbox" />
178 <div class="flipcoin-rules">
179 <div>Maximum Bet Amount: {$mybb->settings[\'flipcoin_maxwager\']}</div>
180 <div>Minimum Bet Amound: {$mybb->settings[\'flipcoin_minwager\']}</div>
181 </div>
182 </td>
183 </tr>
184 <tr>
185 <td class="trow1" width="50%"><strong>Heads/Tails:</strong></td>
186 <td class="trow1" width="50%">
187 <select name="choice">
188 <option value="HEADS" selected="selected">Heads</option>
189 <option value="TAILS">Tails</option>
190 </select>
191 </td>
192 </tr>
193 </table>
194 </td>
195 </tr>
196 </table>
197 {$footer}
198 </body>
199</html>',
200 );
201
202 // Insert them in Global Templates
203 foreach( $templatesArray AS $name => $data )
204 {
205 $template = array(
206 'title' => $db->escape_string( FLIPCOIN_PLUGIN_NAME . '_' . $name),
207 'template' => $db->escape_string( $data ),
208 'version' => 1,
209 'sid' => -1,
210 'dateline' => TIME_NOW
211 );
212
213 $db->insert_query( 'templates', $template );
214 }
215
216 $name = FLIPCOIN_PLUGIN_NAME . '_setting_group_name';
217 $desc = FLIPCOIN_PLUGIN_NAME . '_setting_group_desc';
218
219 $group = array(
220 'name' => FLIPCOIN_PLUGIN_NAME,
221 'title' => $db->escape_string( $lang->{$name} ),
222 'description' => $db->escape_string( $lang->{$desc} ),
223 'isdefault' => 0
224 );
225
226 // Check if the group already exists.
227 $query = $db->simple_select('settinggroups', 'gid', "name='".FLIPCOIN_PLUGIN_NAME."'");
228
229 if($gid = (int)$db->fetch_field($query, 'gid'))
230 {
231 // We already have a group. Update title and description.
232 $db->update_query( 'settinggroups', $group, "gid='{$gid}'" );
233 }
234 else
235 {
236 // We don't have a group. Create one with proper disporder.
237 $query = $db->simple_select('settinggroups', 'MAX(disporder) AS disporder');
238
239 $group['disporder'] = (int)$db->fetch_field($query, 'disporder');
240 $group['disporder']++;
241
242 $gid = (int)$db->insert_query('settinggroups', $group);
243 }
244
245 // Deprecate all the old entries in settings.
246 $db->update_query('settings', array('description' => 'FLIPCOINDELETE'), "gid='{$gid}'");
247
248 // New settings
249 $settings = array(
250 // Max Wager
251 'maxwager' => array(
252 'optionscode' => 'text',
253 'value' => '100'
254 ),
255 // Min Wager
256 'minwager' => array(
257 'optionscode' => 'text',
258 'value' => '1'
259 ),
260 // Winning Multiplier
261 'multiplier_win' => array(
262 'optionscode' => 'text',
263 'value' => '2'
264 ),
265 // Loss Multiplier
266 'multiplier_loss' => array(
267 'optionscode' => 'text',
268 'value' => '1'
269 ),
270 );
271
272 $disporder = 0;
273
274 // Create and/or update settings.
275 foreach($settings AS $key => $setting)
276 {
277 // Prefix all keys with group name.
278 $title = FLIPCOIN_PLUGIN_NAME."_setting_{$key}";
279 $description = FLIPCOIN_PLUGIN_NAME."_setting_{$key}_desc";
280
281 $key = FLIPCOIN_PLUGIN_NAME."_{$key}";
282
283 $setting['title'] = $lang->{$title};
284 $setting['description'] = $lang->{$description};
285
286 // Filter valid entries.
287 $setting = array_intersect_key($setting,
288 array(
289 'title' => 0,
290 'description' => 0,
291 'optionscode' => 0,
292 'value' => 0,
293 )
294 );
295
296 // Escape input values.
297 $setting = array_map(array($db, 'escape_string'), $setting);
298
299 // Add missing default values.
300 ++$disporder;
301
302 $setting = array_merge(
303 array(
304 'description' => '',
305 'optionscode' => 'yesno',
306 'value' => 0,
307 'disporder' => $disporder
308 ),
309 $setting
310 );
311
312 $setting['name'] = $db->escape_string($key);
313 $setting['gid'] = $gid;
314
315 // Check if the setting already exists.
316 $query = $db->simple_select('settings', 'sid', "gid='{$gid}' AND name='{$setting['name']}'");
317
318 if($sid = $db->fetch_field($query, 'sid'))
319 {
320 // It exists, update it, but keep value intact.
321 unset($setting['value']);
322 $db->update_query('settings', $setting, "sid='{$sid}'");
323 }
324 else
325 {
326 // It doesn't exist, create it.
327 $db->insert_query('settings', $setting);
328 }
329 }
330
331 // Delete deprecated entries.
332 $db->delete_query('settings', "gid='{$gid}' AND description='FLIPCOINDELETE'");
333
334 // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
335 rebuild_settings();
336}
337
338/**
339 * Called when plugin is activated
340 *
341 * @return void
342 */
343
344function flipcoin_activate()
345{
346 global $db;
347
348 // Status Row: 0 = Loss, 1 = Win
349 $db->query( '
350 CREATE TABLE IF NOT EXISTS '.TABLE_PREFIX.'flipcoin(
351 `gid` int(10) PRIMARY KEY AUTO_INCREMENT,
352 `uid` int(10) NOT NULL,
353 `bet` int(10) NOT NULL,
354 `choice` varchar(196) NOT NULL,
355 `status` int(1) DEFAULT \'0\' NOT NULL,
356 `dateline` varchar(196) NOT NULL
357 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci'
358 );
359}
360
361/**
362 * Called when plugin is de-activated
363 *
364 * @return void
365 */
366function flipcoin_deactivate()
367{
368 global $db;
369
370 $db->drop_table( 'flipcoin' );
371}
372
373/**
374 * Called when plugin is uninstalled
375 *
376 * @return void
377 */
378function flipcoin_uninstall()
379{
380 global $db, $mybb;
381
382 // Show confirmation
383 if($mybb->request_method != 'post')
384 {
385 global $page, $lang;
386 $lang->load( FLIPCOIN_PLUGIN_NAME );
387
388 $page->output_confirm_action('index.php?module=config-plugins&action=deactivate&uninstall=1&plugin=flipcoin', $lang->flipcoin_uninstall_message, $lang->flipcoin_uninstall);
389 }
390
391 // Delete settings group
392 $gid = $db->fetch_field( $db->simple_select( 'settinggroups', 'gid', 'name=\''.FLIPCOIN_PLUGIN_NAME.'\'' ), 'gid' );
393 $db->delete_query('settinggroups', "name='".FLIPCOIN_PLUGIN_NAME."'");
394
395 // Remove settings
396 $db->delete_query( 'settings', 'gid=\''.intval( $gid ).'\'' );
397 rebuild_settings();
398
399 // Remove Templates
400 $db->delete_query( 'templates', 'title LIKE \''.FLIPCOIN_PLUGIN_NAME.'%\'' );
401}
402
403/**
404 * Is the plugin installed?
405 */
406function flipcoin_is_installed()
407{
408 global $db;
409 return $db->table_exists( 'flipcoin' );
410}
411
412// Newpoints Menu
413function flipcoin_newpoints_default_menu( $menu )
414{
415 global $lang, $mybb;
416
417 $lang->load( FLIPCOIN_PLUGIN_NAME );
418 $link = array( '<a href="'.$mybb->settings['bburl'].'/newpoints.php?action=flipcoin">'.$lang->flipcoin_newpoints_menu.'</a>' );
419
420 if( $mybb->input['action'] == 'flipcoin' )
421 {
422 $link[0] = '» ' . $link[0];
423 }
424
425 return array_merge( $menu, $link );
426}
427
428// Newpoints Action
429function flipcoin_newpoints_start()
430{
431 global $mybb, $db, $lang, $templates, $header, $footer, $headerinclude, $menu, $plugins, $theme, $options, $mypoints;
432
433 if ($mybb->user['usergroup'] == 7) {
434 error('Banned users cannot visit this page');
435 }
436
437 if( $mybb->input['action'] != 'flipcoin' )
438 {
439 return;
440 }
441
442 if( $mybb->input['do'] == 'bet' )
443 {
444 verify_post_check( $mybb->input['key'] );
445
446 $mybb->input['bet'] = intval( $mybb->input['bet'] );
447
448 if( $mybb->user['newpoints'] < $mybb->input['bet'] )
449 {
450 die( json_encode( array( 'success' => 2, 'message' => 'You don\'t have enough points.' ) ) );
451 }
452 else if( $mybb->input['bet'] < $mybb->settings['flipcoin_minwager'] )
453 {
454 die( json_encode( array( 'success' => 2, 'message' => 'Minimum bet amount is: ' . $mybb->settings['flipcoin_minwager'] ) ) );
455 }
456 else if( $mybb->input['bet'] > $mybb->settings['flipcoin_maxwager'] )
457 {
458 die( json_encode( array( 'success' => 2, 'message' => 'Maximum bet amount is: ' . $mybb->settings['flipcoin_maxwager'] ) ) );
459 }
460
461 // Check if the user is spamming
462 $spamTime = TIME_NOW - 60;
463 $count = $db->fetch_field( $db->simple_select( 'flipcoin', 'COUNT(*) AS `count`', 'uid=\''.$mybb->user['uid'].'\' AND `dateline` > \''.$spamTime.'\'' ), 'count' );
464
465 if( $count > 25 )
466 {
467 die( json_encode( array( 'success' => 2, 'message' => 'You can play 25 games at most in a minute! Please try again after sometime.' ) ) );
468 }
469
470 $choices = array(
471 0 => 'HEADS',
472 1 => 'TAILS'
473 );
474
475 if( !in_array( $mybb->input['choice'], $choices ) )
476 {
477 die( json_encode( array( 'success' => 2, 'message' => 'Invalid choice.' ) ) );
478 }
479
480 $win = rand(0, 1);
481
482 $inserts = array(
483 'dateline' => TIME_NOW,
484 'uid' => $mybb->user['uid'],
485 'status' => 0,
486 'choice' => $db->escape_string( $mybb->input['choice'] ),
487 'bet' => $mybb->input['bet'],
488 );
489
490 // Won
491 if( $mybb->input['choice'] == $choices[$win] )
492 {
493 newpoints_addpoints( $mybb->user['uid'], $mybb->input['bet']*$mybb->settings['flipcoin_multiplier_win'] );
494 $totalcredits = $mybb->input['bet']*$mybb->settings['flipcoin_multiplier_win'] + $mybb->user['newpoints'];
495 $winorloss = 1;
496 $inserts['status'] = 1;
497 }
498 // Lost
499 else
500 {
501 newpoints_addpoints( $mybb->user['uid'], -1*$mybb->input['bet']*$mybb->settings['flipcoin_multiplier_loss'] );
502 $totalcredits = -1*$mybb->input['bet']*$mybb->settings['flipcoin_multiplier_loss'] + $mybb->user['newpoints'];
503 $winorloss = 0;
504 }
505
506 $db->insert_query( 'flipcoin', $inserts );
507
508 die( json_encode( array( 'success' => $winorloss, 'winchoice' => $choices[$win], 'points' => newpoints_format_points( $totalcredits ) ) ) );
509 }
510
511
512 //JOIN QUERY :'(
513/* $joinquery = $db->write_query("
514 SELECT u.uid,username,usergroup,displaygroup,dateline,choice,bet,status FROM ".TABLE_PREFIX."users b
515 LEFT JOIN ".TABLE_PREFIX."flipcoin u ON (b.uid=u.uid)
516 ORDER BY u.dateline
517 DESC LIMIT 5
518 ");
519
520foreach ($joinquery as $lastfive)
521 {
522 if ($lastfive['status'] == 1) {
523 $lastfive['status'] = '<span style="color:#449947">Win</span>';
524 }
525 else{
526 $lastfive['status'] = '<span style="color:#E61A1A">Loss</span>';
527 }
528 $username = format_name($lastfive['username'], $lastfive['usergroup'], $lastfive['displaygroup']);
529 $user_link = build_profile_link($username, $lastfive['uid']);
530
531
532 $lastfivebets .= '
533 <tr>
534 <td class="trow1" id="" >
535 <span class="smalltext">'.$user_link.'</span>
536 </td>
537 <td class="trow1" id="" >
538 <span class="smalltext"><strong>'.$lastfive['bet'].'</strong></span>
539 </td>
540 <td class="trow1" id="" >
541 <span class="smalltext"><strong>'.$lastfive['choice'].'</strong></span>
542 </td>
543 <td class="trow1" id="" >
544 <span class="smalltext"><strong>'.$lastfive['status'].'</strong></span>
545 </td>
546 </tr>
547 ';
548 }*/
549
550
551
552 $joinquery = $db->write_query("
553 SELECT u.uid,username,usergroup,displaygroup,dateline,choice,bet,status FROM ".TABLE_PREFIX."users b
554 LEFT JOIN ".TABLE_PREFIX."flipcoin u ON (b.uid=u.uid)
555 ORDER BY u.dateline
556 DESC LIMIT 5
557 ");
558
559foreach ((array) $joinquery as $lastfive)
560 {
561 $lastfive = array();
562 if ($lastfive['status'] == 1) {
563 $lastfive['status'] = '<span style="color:#449947">Win</span>';
564 }
565 else{
566 $lastfive['status'] = '<span style="color:#E61A1A">Loss</span>';
567 }
568 $username = format_name($lastfive['username'], $lastfive['usergroup'], $lastfive['displaygroup']);
569 $user_link = build_profile_link($username, $lastfive['uid']);
570
571
572 $lastfivebets .= '
573 <tr>
574 <td class="trow1" id="" >
575 <span class="smalltext">'.$user_link.'</span>
576 </td>
577 <td class="trow1" id="" >
578 <span class="smalltext"><strong>'.$lastfive['bet'].'</strong></span>
579 </td>
580 <td class="trow1" id="" >
581 <span class="smalltext"><strong>'.$lastfive['choice'].'</strong></span>
582 </td>
583 <td class="trow1" id="" >
584 <span class="smalltext"><strong>'.$lastfive['status'].'</strong></span>
585 </td>
586 </tr>
587 ';
588
589 }
590
591
592 $lang->load( FLIPCOIN_PLUGIN_NAME );
593
594 eval("\$page = \"".$templates->get('flipcoin_home')."\";");
595 output_page( $page );
596}
597?>