<?php
require_once '../config.php';
require_once '../app/includes/constant.php';
require_once '../app/includes/app_start.php';

$game_id = mysqli_real_escape_string($socket, $_POST['gi']);
$game = dataBy('zon_games', "id=$game_id");
$ui = $_SERVER['REMOTE_ADDR'];

if (num_rows(T_ZON_LIKES, "game_id=$game_id && user_id='$ui'") > 0) {
    if (mysqli_query($socket, "DELETE FROM " . T_ZON_LIKES . " WHERE game_id=$game_id && user_id='$ui'")) {
        $like = num_rows(T_ZON_LIKES, "game_id=$game_id");
        $dislike = num_rows(T_ZON_UNLIKES, "game_id=$game_id");
        $total = $like + $dislike;
        $percentOfDislikes = ($dislike / $total) * 100;
        $percentOfLikes = ($like / $total) * 100;
        echo json_encode(["like" => number_format($percentOfLikes, 0), "unlike" => number_format($percentOfDislikes, 0)]);
    }
} else {
    if (mysqli_query($socket, "INSERT INTO " . T_ZON_LIKES . " (user_id, game_id) VALUES ('$ui', $game_id) ")) {
        mysqli_query($socket, "DELETE FROM " . T_ZON_UNLIKES . " WHERE game_id=$game_id && user_id='$ui'");
        $like = num_rows(T_ZON_LIKES, "game_id=$game_id");
        $dislike = num_rows(T_ZON_UNLIKES, "game_id=$game_id");
        $total = $like + $dislike;
        $percentOfDislikes = ($dislike / $total) * 100;
        $percentOfLikes = ($like / $total) * 100;
        echo json_encode(["like" => number_format($percentOfLikes, 0), "unlike" => number_format($percentOfDislikes, 0)]);
    }
}