File: /var/www/uileague/wp-content/themes/uileague/helper/uileague_socket.php
<?php
class WebSocketHelper
{
// 保存用户和socket之间的联系
public static $pb_table = "orm_sock_fd";
const TYPE_TEST = "test";
const TYPE_ERROR = "error";
const TYPE_LOGIN_ANOTHER_DEVICE = "loginOtherDevice";
const TYPE_ONLINE_HEARTBEAT = "onlineHeartbeat";
const TYPE_USER_UPDATE = "updaeUser";
const TYPE_UPDATE_MATCH = "updateMatch";
const TYPE_GET_MATCHES = "getMatches";
const TYPE_GET_PLAYER_FIRST = "getPlayerFirst";
const TYPE_GET_PLAYER_REPLACE = "getPlayerReplace";
const TYPE_GET_REFEREES = "getReferees";
const TYPE_GET_PLAYER_LIST = "getPlayerList";
public static function set($uid, $wpuid, $fd, $isMobile = true)
{
$row = self::get_by_uid($uid, $wpuid);
global $wpdb;
if ($row != null) {
if ($isMobile) {
$res = $wpdb->update(
self::$pb_table,
array(
'fd' => $fd,
'updated' => current_time('mysql'),
),
array('id' => $row->id),
array('%d', '%s'),
array('%d')
);
} else {
$res = $wpdb->update(
self::$pb_table,
array(
'fd_web' => $fd,
'updated' => current_time('mysql'),
),
array('id' => $row->id),
array('%d', '%s'),
array('%d')
);
}
} else {
$res = $wpdb->insert(
self::$pb_table,
array(
'uid' => $uid,
'wpuid' => $wpuid,
'fd' => $isMobile ? $fd : 0,
'fd_web' => $isMobile ? 0 : $fd,
'created' => current_time('mysql'),
'updated' => current_time('mysql'),
),
array('%d', '%d', '%d', '%d', '%s', '%s')
);
}
return $res !== false;
}
public static function reset_by_fd($fd)
{
$row = self::get_by_fd($fd);
if ($row != null) {
if ($fd == $row->fd) { // mobile
$res = self::set($row->uid, $row->wpuid, 0, true);
} else if ($fd == $row->fd_web) { // web
$res = self::set($row->uid, $row->wpuid, 0, false);
}
} else {
$res = true;
}
return $res !== false;
}
public static function get_by_uid($uid, $wpuid)
{
global $wpdb;
return $wpdb->get_row($wpdb->prepare("select * from `" . self::$pb_table . "` where uid = %d and wpuid = %d", $uid, $wpuid));
}
public static function get_by_fd($fd)
{
global $wpdb;
return $wpdb->get_row($wpdb->prepare("select * from `" . self::$pb_table . "` where fd = %d or fd_web = %d", $fd, $fd));
}
}