So I'm from faroeisland a small country and i've made a software for Dota 2, Counter strike and League of legends where people are able to sit down in a lobby on my webpage(wordpress). But I need some improvements in my code and some additional features.
I'm not sure what the budget is needed but If you know your PhP and mysql it's an easy task.
This is my code so far :)
PHP
[login to view URL]
<?php
/*Plugin Name: Lobby manager
Description: Show a lobby section
Version: 0.1
Author: Jóannes á Fløtti
Author URI: [login to view URL]
License: GPLv2
*/
/* Lobby lists
/* ------------------------------------ */
function generate_lobby_list($game) {
global $wpdb;
$list = [];
$query = $wpdb->get_results( "SELECT ID, time FROM esport_event where game = '".$game."' order by time");
foreach($query as $row) {
$list[$row->ID] = ['time' => $row->time];
}
//$query = $wpdb->get_results( "SELECT user_id, event_id FROM esport_user_event where date = '" . date('Y-m-d') ."'" );
return $list;
}
function my_assets() {
wp_enqueue_style('lobby-manager', plugins_url( '/css/[login to view URL]', __FILE__ ));
wp_enqueue_script( 'lobby-manager', plugins_url( '/js/[login to view URL]', __FILE__ ), array( 'jquery' ) );
wp_localize_script( 'lobby-manager', 'sitdown', array(
'ajax_url' => admin_url( '[login to view URL]' )
));
}
add_action( 'wp_enqueue_scripts', 'my_assets' );
class ESport_Lobby_Manager_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'esport_lobby_mananger_widget',
// name of the widget
__('Lobby manager', 'esport' ),
// widget options
array (
'description' => __( 'Creates a lobby section', 'esport' )
)
);
}
function form( $instance ) {
// markup for form
$time = $instance[ 'lobbytime' ];
$game = $instance[ 'lobbygame' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'lobbygame' ); ?>">Spæl:</label>
<select class="widefat" name="<?php echo $this->get_field_name( 'lobbygame' ); ?>" id="<?php echo $this->get_field_id( 'lobbygame' ); ?>">
<option value="dota2">Dota 2</option>
<option value="cs">Counter Strike: Global Offensive</option>
<option value="lol">League of legends</option>
<option value="rocketleague">Rocket League</option>
</select>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'lobbygame' ] = strip_tags( $new_instance[ 'lobbygame' ] );
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
$date = new DateTime();
$list = generate_lobby_list($instance['lobbygame']);
echo '<div class="row" style="z-index:10">';
foreach($list as $key => $lobby) {
$timeArr = explode(':',$lobby['time']);
$date->setTime($timeArr[0],$timeArr[1]);
?>
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 lobby-<?php echo $key; ?>">
<h2 class="lead">
Klokkan <?php echo substr($lobby['time'],0,-3); ?> FO tíð
<img src="<?php echo plugins_url( '/img/[login to view URL]', __FILE__); ?>" class="loading pull-right">
</h2>
<form class="form-inline lobby-container">
<?php
if ( is_user_logged_in()) {
if($date->getTimestamp() > time()) {
$user = wp_get_current_user();
?>
<div class="regForm hide">
<div class="form-group">
<label class="sr-only" for="playerProfileName">Eyðkennis navn</label>
<input type="text" class="form-control" name="playerProfileName" value="<?php echo esc_attr( get_the_author_meta( 'dotaprofile', $user->ID ) ); ?>" placeholder="Eyknevni">
<input type="hidden" name="playerProfileID" value="<?php echo $user->ID; ?>" placeholder="Eyðkennis navn">
</div>
<button type="button" class="btn btn-success sitdown">Set teg</button>
</div>
<button type="button" value="<?php echo $user->ID; ?>" class="btn btn-warning leave hide">Reis teg</button>
<?php } else { ?>
<p>
<strong>
Ikki møguligt at seta seg!
</strong>
</p>
<?php
}
} else { ?>
<p>
<strong>
Logga á fyri at seta teg!
</strong>
</p>
<?php }
?>
<ul class="lobby-list fade-in" id="playersInLobby-<?php echo $key; ?>"></ul>
<!-- <button type="button" class="btn btn-default update hide">Dagfør</button>-->
</form>
<script>
jQuery(function(){
jQuery('lobby-<?php echo $key; ?>').lobbyManager({
lobby: 'div.lobby-<?php echo $key; ?>',
lobbyID: <?php echo $key; ?>,
container: 'form.lobby-container',
list: '#playersInLobby-<?php echo $key; ?>'
});
});
</script>
</div>
<?php
}
echo '</div>';
}
}
function esport_register_lobby_manager_widget() {
register_widget( 'ESport_Lobby_Manager_Widget' );
}
add_action( 'widgets_init', 'esport_register_lobby_manager_widget' );
function esport_lobby_widgets_init() {
register_sidebar( array(
'name' => 'Dota lobby',
'id' => 'dota_lobby_section',
'before_widget' => '<div id="esport-dota-lobby-manager">',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'esport_lobby_widgets_init' );
add_action( 'wp_ajax_take_a_seat', 'prefix_ajax_take_a_seat' );
//add_action( 'wp_ajax_nopriv_take_a_seat', 'prefix_ajax_take_a_seat' );
function prefix_ajax_take_a_seat() {
global $wpdb;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
try {
$wpdb->insert(
'esport_user_event',
array(
'user_id' => filter_input(INPUT_POST, 'id'),
'event_id' => filter_input(INPUT_POST, 'event'),
'name' => filter_input(INPUT_POST, 'name'),
'date' => date('Y-m-d')
),
array(
'%d',
'%d',
'%s',
'%s',
)
);
echo json_encode(['status' => $wpdb->insert_id, 'message' => ($wpdb->insert_id > 0) ? 'Welcome to lobby' : 'No mana left!']);
} catch (Exception $e) {
echo $e->getMessage();
}
die();
} else {
echo json_encode(['status' => 0, 'message' => 'No funky business here!']);
die();
}
}
add_action( 'wp_ajax_leave_a_seat', 'prefix_ajax_leave_a_seat' );
//add_action( 'wp_ajax_nopriv_take_a_seat', 'prefix_ajax_take_a_seat' );
function prefix_ajax_leave_a_seat() {
global $wpdb;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
try {
$wpdb->query( $wpdb->prepare(
"
DELETE FROM esport_user_event
WHERE
user_id = %d
AND
event_id = %d
AND
date = %s
",
filter_input(INPUT_POST, 'id'),
filter_input(INPUT_POST, 'event'),
date('Y-m-d')
) );
echo json_encode(['status' => 1, 'message' => 'Regeneration!']);
} catch (Exception $e) {
echo $e->getMessage();
}
die();
} else {
echo json_encode(['status' => 0, 'message' => 'No funky business here!']);
die();
}
}
add_action( 'wp_ajax_update_a_lobby', 'prefix_ajax_update_a_lobby' );
add_action( 'wp_ajax_nopriv_update_a_lobby', 'prefix_ajax_update_a_lobby' );
function prefix_ajax_update_a_lobby() {
global $wpdb, $current_user;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$user = wp_get_current_user();
$query = $wpdb->get_results( $wpdb->prepare(
"SELECT
user_id, name
FROM
esport_user_event
WHERE
event_id = %d
AND
date = %s
ORDER BY
id asc
",
filter_input(INPUT_POST, 'event'),
date('Y-m-d')
));
$list = "";
if($wpdb->num_rows) {
$i = 1;
$seated = false;
foreach($query as $row) {
if($i <= 10) {
$list .= '<li class="player-'.$row->user_id.'">' . $row->name . '</li>';
}
if($row->user_id == $current_user->ID) {
$seated = $i;
}
$i++;
}
} else {
$list .= '<li><strong>Eingin í lobby</strong></li>';
}
echo json_encode(['status' => 1, 'count' => $wpdb->num_rows, 'list' => $list, 'seated' => $seated]);
die();
} else {
echo json_encode(['status' => 0, 'message' => 'No funky business here!']);
die();
}
}