Skip to content

Commit

Permalink
Add OTServers.eu voting (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elime1 authored and Znote committed Aug 6, 2018
1 parent 8788aa4 commit 0d51f87
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
15 changes: 14 additions & 1 deletion config.php
Expand Up @@ -1000,4 +1000,17 @@ function getClock($time = false, $format = false, $adjust = true) {
'points' => 20,
),
);
?>

//////////////////////////
/// OTServers.eu voting
//
// Start by creating an account at OTServers.eu and add your server.
// You can find your secret token by logging in on OTServers.eu and go to 'MY SERVER' then 'Encourage players to vote'.
$config['otservers_eu_voting'] = [
'enabled' => false,
'voteUrl' => 'https://api.otservers.eu/vote_link.php',
'voteCheckUrl' => 'https://api.otservers.eu/vote_check.php',
'secretToken' => '', //Enter your secret token. Do not share with anyone!
'landingPage' => '/voting.php?action=reward', //The user will be redirected to this page after voting
'points' => '1' //Amount of points to give as reward
];
9 changes: 5 additions & 4 deletions layout/aside.php
@@ -1,11 +1,12 @@
<div id="sidebar_container">
<?php
<?php
if (user_logged_in() === true) {
include 'layout/widgets/loggedin.php';
include 'layout/widgets/loggedin.php';
} else {
include 'layout/widgets/login.php';
include 'layout/widgets/login.php';
}
if (user_logged_in() && is_admin($user_data)) include 'layout/widgets/Wadmin.php';
if (user_logged_in() && is_admin($user_data)) include 'layout/widgets/Wadmin.php';
if (user_logged_in()) include 'layout/widgets/vote.php';
include 'layout/widgets/charactersearch.php';
include 'layout/widgets/topplayers.php';
include 'layout/widgets/highscore.php';
Expand Down
9 changes: 9 additions & 0 deletions layout/widgets/vote.php
@@ -0,0 +1,9 @@
<div class="sidebar">
<h2>Vote for us!</h2>
<div class="inner">
<form type="submit" action="voting.php" method="GET">
Get points by voting at OTServers.eu
<input type="submit" value="Vote">
</form>
</div>
</div>
69 changes: 69 additions & 0 deletions voting.php
@@ -0,0 +1,69 @@
<?php require_once 'engine/init.php';
protect_page();
include 'layout/overall/header.php';

$otservers_eu_voting = $config['otservers_eu_voting'];

if ($otservers_eu_voting['enabled']) {
$isRewardRequest = isset($_GET['action']) && $_GET['action'] === 'reward';

if (!$isRewardRequest) {
$result = vote($user_data['id'], $otservers_eu_voting);
if ($result === false) {
echo '<p>Something went wrong! Could not make a vote request.</p>';
} else {
header('Location: ' . $result['voteLink']);
die;
}
} else {
$result = checkHasVoted($user_data['id'], $otservers_eu_voting);
if ($result !== false) {
if ($result['voted'] === true) {
$points = $otservers_eu_voting['points'];
$pointsText = $points === '1' ? 'point' : 'points';
mysql_update("UPDATE `znote_accounts` SET `points` = `points` + '$points' WHERE `account_id`=" . $user_data['id']);
echo "<p>Thank you for voting! You have been rewarded with $points $pointsText!</p>";
} else {
echo '<p>It does not seem like you have voted.</p>';
}
} else {
echo '<p>Could not verify that you have voted.</p>';
}
}
} else {
echo '<p>Voting is not enabled.</p>';
}

include 'layout/overall/footer.php';

function vote($otUserId, $otservers_eu_voting) {
$context = stream_context_create([
'http' => [
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => json_encode([
'otUserId' => $otUserId,
'secretToken' => $otservers_eu_voting['secretToken'],
'landingPage' => $otservers_eu_voting['landingPage']
])
]
]);
$result = file_get_contents($otservers_eu_voting['voteUrl'], false, $context);
return $result !== false ? json_decode($result, true) : false;
}

function checkHasVoted($otUserId, $otservers_eu_voting) {
$context = stream_context_create([
'http' => [
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => json_encode([
'otUserId' => $otUserId,
'secretToken' => $otservers_eu_voting['secretToken'],
'consume' => true
])
]
]);
$result = file_get_contents($otservers_eu_voting['voteCheckUrl'], false, $context);
return $result !== false ? json_decode($result, true) : false;
}

0 comments on commit 0d51f87

Please sign in to comment.