Skip to content

Commit

Permalink
First Interface + counting working
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienthiery committed Nov 14, 2014
1 parent 4dddf9a commit 90f659a
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 27 deletions.
11 changes: 9 additions & 2 deletions Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ class Entity {
protected $color = "";
/* A table with the history of hits? */
protected $hits = array();
protected $proportion = 0;

public function __construct( /*string*/ $file_name, /*string*/ $color ) {
$this->name = $file_name;
$this->color = $color;
$handle = fopen($filename, "r");
$handle = fopen($file_name, "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 )
$array = explode( ":", $line );
$this->hits[ $array[1] ] = $array[0];
}
$i++;
Expand All @@ -33,6 +34,12 @@ public function get_name(){
public function get_color(){
return $this->color;
}
public function get_proportion(){
return $this->proportion;
}
public function set_proportion( /*float*/ $proportion ){
$this->proportion = $proportion;
}
public function get_hits(){
return $this->hits;
}
Expand Down
9 changes: 6 additions & 3 deletions Hit.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
class Hit {
protected $soldier = "unknown soldier";
protected $time = DateTime();
protected $time = null;

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

public function get_soldier(){
Expand Down
1 change: 1 addition & 0 deletions apple
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
score:0 %
1415989689:INIT
28 changes: 17 additions & 11 deletions callback.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<?php
function write_score( $file, $score=null ){
$handle = fopen($filename, "r");
$handle = fopen($file, "r");
if ($handle) {
$line = fgets($handle);
if( $score === null ){
$score = explode( " ", explode( ":", $line )[1])[0];
$score = int( $score ) +1;
$explode_column = explode( ":", $line );
$explode_space = explode( " ", $explode_column[1]);
$score = intval( $explode_space[0] ) +1;
}
$to_write = "score:".$score;
for( $j=0; $j< ( 49-strlen( $towrite ) ); $j++ ){
$score = "score:".$score;
$to_write = $score;
fclose( $handle );
$handle = fopen($file, "r+");
for( $j=0; $j< ( 49-strlen( $score ) ); $j++ ){
$to_write .= " ";
}
$to_write += "%";
$to_write .= "%";
fwrite( $handle, $to_write );
}
fclose( $handle );
}

$api_token = "5032dc6f-172b-4f96-9835-98b616405161";
Expand Down Expand Up @@ -41,18 +46,19 @@ function write_score( $file, $score=null ){
} elseif( $target === "google" || $target === "apple" || $target === "microsoft" || $target === "yo" ){
$now = strtotime( "now" );
$midnight = strtotime( "today midnight" );
$file = escapeshellarg($target);
$file = $target;
$latest_hit = `tail -n 1 $file`;
$score = `head -n 1 $file`;
if( $latest_hit =< $midnight ){
$file = getcwd()."/".$file;
if( $latest_hit <= $midnight ){
file_put_contents( $file, "% ".$score, FILE_APPEND );
file_put_contents( $file, $now.":".$user, FILE_APPEND );
file_put_contents( $file, $now.":".$user."\n", FILE_APPEND );
write_score( $file, 1 );
} elseif( $latest_hit > $midnight ){
file_put_contents( $file, $now.":".$user, FILE_APPEND );
file_put_contents( $file, $now.":".$user."\n", FILE_APPEND );
write_score( $file );
} else {
file_put_contents( "error", "ERROR at ".$now.":".$user, FILE_APPEND );
file_put_contents( "error", "ERROR at ".$now.":".$user."\n", FILE_APPEND );
}
}
?>
38 changes: 38 additions & 0 deletions deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
echo "Careful, with this script: "
echo "# If -d is used : you will delete your /var/www symlink or move your /var/www folder to /var/www.bak and replace it by a symlink to this folder... "
echo "# Else it will delete (if existing) the /var/www/${PWD##*/} symlink/file and create one that leads to this folder"
echo "Press any key to continue, Ctrl+C to cancel."
read -n 1 -s

OUTPUT=1
if [ "$1" == "-d" ]; then
sudo mv /var/www/ /var/www.bak/
sudo rm /var/www
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
OUTPUT=`sudo ln -s $DIR /var/www`
else
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

sudo rm /var/www/${PWD##*/}
OUTPUT=`sudo ln -s $DIR /var/www/${PWD##*/}`
fi

if [ "$OUTPUT" == 0 ]; then
echo "Open browser, and go to 127.0.0.1"
else
echo $OUTPUT
fi

1 change: 1 addition & 0 deletions google
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
score:0 %
1415989689:INIT
28 changes: 17 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
</head>
<body>
<?php
include( 'entity_class.php' );
//Read file, define entities, calculate proportions ?>
<div id='recent'></div>
<div id='battle-ground'>
include( 'Entity.php' );
//Read file, define entities, calculate proportions
$entities = array(
new Entity( 'google', "#cc0" ),
new Entity( 'apple', "#fff" ),
new Entity( 'microsoft', "#00c" ),
new Entity( 'yo', "#713b87" )
);
?>
<div id='recent'>
<?php foreach( $entities as $entity ){
var_dump( $entity );
} ?>
echo '<div style="color:'.$entity->get_color().';">'.$entity->get_name().'</div>';
} ?></div>
<div id='battle-ground'>
<?php foreach( $entities as $entity ){ ?>
<div class='<?php echo $entity->get_name(); ?>' style=''>
</div>
<?php } ?>
</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),
Expand Down
1 change: 1 addition & 0 deletions microsoft
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
score:0 %
1415989566:INIT
31 changes: 31 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
body {
color:white;
font-family: "Montserrat", arial, sans-serif;
width: 100%;
height: 100%;
text-align:center;
background:#9B59B6;
position: absolute;
top: 0;
}

*{
margin:0;
padding:0;
box-sizing: border-box;
}

#recent {
float: right;
width: 20%;
height: 100%;
vertical-align: top;
border-left: solid 2px white;
}

#battle-ground {
margin: 5%;
height: 80%;
width: 69%;
border: solid 2px white;
}
1 change: 1 addition & 0 deletions yo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
score:0 %
1415989747:INIT

0 comments on commit 90f659a

Please sign in to comment.