Skip to content

Commit

Permalink
To TEST
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienthiery committed Nov 14, 2014
1 parent a3e6bf1 commit 4dddf9a
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
include( "Hit.php" );

class Entity {
protected $name = "";
protected $color = "";
/* A table with the history of hits? */
protected $hits = array();

public function __construct( /*string*/ $file_name, /*string*/ $color ) {
$this->name = $file_name;
$this->color = $color;
$handle = fopen($filename, "r");
$i = 0;
if ($handle) {
while (($line = fgets($handle)) !== false) {
if( $i !== 0 && substr( $line, 0, 1 ) != "%" ){
// process the hit line read.
$array = explode( ":", $line )
$this->hits[ $array[1] ] = $array[0];
}
$i++;
}
} else {
// error opening the file.
}
fclose($handle);
}

public function get_name(){
return $this->name;
}
public function get_color(){
return $this->color;
}
public function get_hits(){
return $this->hits;
}
public function get_latest_hit(){
return new Hit( end( $this->hits ), new DateTime( key( $this->hits ) ) );
}
public function get_first_hit(){
return new Hit( reset($this->hits) , new DateTime( key( $this->hits ) ) );
}
public function get_todays_hits(){
$arr = $this->hits;
$input = strtotime('today midnight');
$the_key = get_closest_key( $arr, $input );
return array_slice( $arr, $the_key );
}
private function get_closest_key( $arr, $input){
if (isset($arr[$input])) {
return $input;
}

foreach ($arr as $key => $value) {
if ($key > $input) {
if (prev($arr) === FALSE) {
// If the input is smaller than the first key
return $key;
}
$prevKey = key($arr);

if (abs($key - $input) < abs($prevKey - $input)) {
return $key;
} else {
return $prevKey;
}
}
}
}

}
17 changes: 17 additions & 0 deletions Hit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
class Hit {
protected $soldier = "unknown soldier";
protected $time = DateTime();

public function __construct( /*string*/ $soldier, /*DateTime*/ $time=DateTime() ) {
$this->soldier = $soldier;
$this->time = $time;
}

public function get_soldier(){
return $this->soldier;
}
public function get_time(){
return $this->time;
}
}
1 change: 1 addition & 0 deletions apple
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
score:0 %
58 changes: 58 additions & 0 deletions callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
function write_score( $file, $score=null ){
$handle = fopen($filename, "r");
if ($handle) {
$line = fgets($handle);
if( $score === null ){
$score = explode( " ", explode( ":", $line )[1])[0];
$score = int( $score ) +1;
}
$to_write = "score:".$score;
for( $j=0; $j< ( 49-strlen( $towrite ) ); $j++ ){
$to_write .= " ";
}
$to_write += "%";
fwrite( $handle, $to_write );
}
}

$api_token = "5032dc6f-172b-4f96-9835-98b616405161";

// Receive YO's and increment hits
// Get parameters from url
$target = $_GET[ 'target' ];
$user = $_GET[ 'username' ];

// Respond on YOFANSWAR
if( $target === "YOFANSWAR" ){
$url = "http://api.justyo.co/yo";
$link = "http://yofanswar.thiery.io/";
$data = array('api_token' => $api_token, 'username' => $user, 'link' => $link);

$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
} elseif( $target === "google" || $target === "apple" || $target === "microsoft" || $target === "yo" ){
$now = strtotime( "now" );
$midnight = strtotime( "today midnight" );
$file = escapeshellarg($target);
$latest_hit = `tail -n 1 $file`;
$score = `head -n 1 $file`;
if( $latest_hit =< $midnight ){
file_put_contents( $file, "% ".$score, FILE_APPEND );
file_put_contents( $file, $now.":".$user, FILE_APPEND );
write_score( $file, 1 );
} elseif( $latest_hit > $midnight ){
file_put_contents( $file, $now.":".$user, FILE_APPEND );
write_score( $file );
} else {
file_put_contents( "error", "ERROR at ".$now.":".$user, FILE_APPEND );
}
}
?>
1 change: 1 addition & 0 deletions google
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
score:0 %
38 changes: 38 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<title>YoFansWar</title>
<meta charset="UTF-8"/>
<link rel="icon" type="image/png" href="assets/img/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes"/>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Montserrat:700,400">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
include( 'entity_class.php' );
//Read file, define entities, calculate proportions ?>
<div id='recent'></div>
<div id='battle-ground'>
<?php foreach( $entities as $entity ){
var_dump( $entity );
} ?>
</div>
<div class='legend'>
<?php foreach( $entities as $entity ){
echo $entity->color; echo $entity->name;
} ?>
<div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', '', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');

</script>
</body>
</html>
1 change: 1 addition & 0 deletions microsoft
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
score:0 %
1 change: 1 addition & 0 deletions yo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
score:0 %

0 comments on commit 4dddf9a

Please sign in to comment.