Initial commit with basic scripts

This commit is contained in:
BackIsBachus
2017-09-17 08:20:38 +02:00
commit 68ee6a67a8
4 changed files with 295 additions and 0 deletions

11
change_week.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
OLD_DATE=`date -d '-7 day' +%Y-%m-%d`
BASE_PATH="/home/backisbachus/gw2/"
OLD_DB="stats.db"
NEW_DATE=`date +%Y-%m-%dT18:00:00+00:00`
`cp $BASE_PATH$OLD_DB $BASE_PATH$OLD_DB'_'$OLD_DATE`
`sqlite3 $BASE_PATH$OLD_DB "DELETE FROM records;"`
`sqlite3 $BASE_PATH$OLD_DB "DELETE FROM diff;"`
`sqlite3 $BASE_PATH$OLD_DB "INSERT INTO records VALUES ('$NEW_DATE',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);"`

93
diff.php Normal file
View File

@@ -0,0 +1,93 @@
<?php
$database = '/home/backisbachus/gw2/stats_bu.db';
$table_r = 'records';
$table_d = 'diff';
$score_name = array(
'eb_r_k', 'eb_r_d', 'eb_b_k', 'eb_b_d', 'eb_g_k', 'eb_g_d',
'rh_r_k', 'rh_r_d', 'rh_b_k', 'rh_b_d', 'rh_g_k', 'rh_g_d',
'bh_r_k', 'bh_r_d', 'bh_b_k', 'bh_b_d', 'bh_g_k', 'bh_g_d',
'gh_r_k', 'gh_r_d', 'gh_b_k', 'gh_b_d', 'gh_g_k', 'gh_g_d'
);
$dbhandle = new PDO('sqlite:'.$database);
$dbhandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$values = getValues($dbhandle, $table_r);
$diffs = getDiff($values, $score_name);
insertValues($dbhandle, $table_d, $diffs);
$dbhandle = null;
function getValues($h, $t) {
$query = $h -> prepare("SELECT * FROM ".$t);
$query -> execute();
$records = $query -> fetchAll();
return $records;
}
function getDiff($v, $s){
$return = array();
for($i = 1; $i<count($v); $i++) {
$prev = $v[$i-1];
$next = $v[$i];
$diff = array();
$diff['start'] = $prev['date'];
foreach($s as $c) {
$diff[$c] = $next[$c] - $prev[$c];
}
$return[] = $diff;
}
return $return;
}
function insertValues($h, $t, $d) {
$delete = $h -> prepare("DELETE FROM ".$t);
$delete -> execute();
foreach($d as $v) {
$query = $h -> prepare("INSERT INTO ".$t." VALUES (:start,
:eb_r_k, :eb_r_d, :eb_b_k, :eb_b_d, :eb_g_k, :eb_g_d,
:rh_r_k, :rh_r_d, :rh_b_k, :rh_b_d, :rh_g_k, :rh_g_d,
:bh_r_k, :bh_r_d, :bh_b_k, :bh_b_d, :bh_g_k, :bh_g_d,
:gh_r_k, :gh_r_d, :gh_b_k, :gh_b_d, :gh_g_k, :gh_g_d
)");
$query->bindParam(':start', $v['start'], PDO::PARAM_STR);
$query->bindParam(':eb_r_k', $v['eb_r_k'], PDO::PARAM_INT);
$query->bindParam(':eb_r_d', $v['eb_r_d'], PDO::PARAM_INT);
$query->bindParam(':eb_b_k', $v['eb_b_k'], PDO::PARAM_INT);
$query->bindParam(':eb_b_d', $v['eb_b_d'], PDO::PARAM_INT);
$query->bindParam(':eb_g_k', $v['eb_g_k'], PDO::PARAM_INT);
$query->bindParam(':eb_g_d', $v['eb_g_d'], PDO::PARAM_INT);
$query->bindParam(':rh_r_k', $v['rh_r_k'], PDO::PARAM_INT);
$query->bindParam(':rh_r_d', $v['rh_r_d'], PDO::PARAM_INT);
$query->bindParam(':rh_b_k', $v['rh_b_k'], PDO::PARAM_INT);
$query->bindParam(':rh_b_d', $v['rh_b_d'], PDO::PARAM_INT);
$query->bindParam(':rh_g_k', $v['rh_g_k'], PDO::PARAM_INT);
$query->bindParam(':rh_g_d', $v['rh_g_d'], PDO::PARAM_INT);
$query->bindParam(':bh_r_k', $v['bh_r_k'], PDO::PARAM_INT);
$query->bindParam(':bh_r_d', $v['bh_r_d'], PDO::PARAM_INT);
$query->bindParam(':bh_b_k', $v['bh_b_k'], PDO::PARAM_INT);
$query->bindParam(':bh_b_d', $v['bh_b_d'], PDO::PARAM_INT);
$query->bindParam(':bh_g_k', $v['bh_g_k'], PDO::PARAM_INT);
$query->bindParam(':bh_g_d', $v['bh_g_d'], PDO::PARAM_INT);
$query->bindParam(':gh_r_k', $v['gh_r_k'], PDO::PARAM_INT);
$query->bindParam(':gh_r_d', $v['gh_r_d'], PDO::PARAM_INT);
$query->bindParam(':gh_b_k', $v['gh_b_k'], PDO::PARAM_INT);
$query->bindParam(':gh_b_d', $v['gh_b_d'], PDO::PARAM_INT);
$query->bindParam(':gh_g_k', $v['gh_g_k'], PDO::PARAM_INT);
$query->bindParam(':gh_g_d', $v['gh_g_d'], PDO::PARAM_INT);
$query->execute();
}
}
?>

107
gw2wvw.php Normal file
View File

@@ -0,0 +1,107 @@
<?php
var_dump(gmdate('c'));
$service = 'https://api.guildwars2.com/v2/wvw/matches/stats?world=2104';
$database = '/home/backisbachus/gw2/stats.db';
$table = 'records';
$dbhandle = new PDO('sqlite:'.$database);
$dbhandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$match = getJson($service);
$values = getValues($match);
insertValues($dbhandle, $table, $values);
$dbhandle = null;
function getJson($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
curl_close($curl);
return json_decode($curl_response);
}
function getValues($m) {
$maps = $m->maps;
$records = array();
foreach($maps as $map) {
$type = $map->type;
switch($type) {
case "Center":
$records['eb_r_k'] = $map->kills->red;
$records['eb_b_k'] = $map->kills->blue;
$records['eb_g_k'] = $map->kills->green;
$records['eb_r_d'] = $map->deaths->red;
$records['eb_b_d'] = $map->deaths->blue;
$records['eb_g_d'] = $map->deaths->green;
break;
case "RedHome":
$records['rh_r_k'] = $map->kills->red;
$records['rh_b_k'] = $map->kills->blue;
$records['rh_g_k'] = $map->kills->green;
$records['rh_r_d'] = $map->deaths->red;
$records['rh_b_d'] = $map->deaths->blue;
$records['rh_g_d'] = $map->deaths->green;
break;
case "BlueHome":
$records['bh_r_k'] = $map->kills->red;
$records['bh_b_k'] = $map->kills->blue;
$records['bh_g_k'] = $map->kills->green;
$records['bh_r_d'] = $map->deaths->red;
$records['bh_b_d'] = $map->deaths->blue;
$records['bh_g_d'] = $map->deaths->green;
break;
case "GreenHome":
$records['gh_r_k'] = $map->kills->red;
$records['gh_b_k'] = $map->kills->blue;
$records['gh_g_k'] = $map->kills->green;
$records['gh_r_d'] = $map->deaths->red;
$records['gh_b_d'] = $map->deaths->blue;
$records['gh_g_d'] = $map->deaths->green;
break;
}
}
return $records;
}
function insertValues($h, $t, $v) {
$utc = gmdate('c');
$query = $h -> prepare("INSERT INTO ".$t." VALUES (:date,
:eb_r_k, :eb_r_d, :eb_b_k, :eb_b_d, :eb_g_k, :eb_g_d,
:rh_r_k, :rh_r_d, :rh_b_k, :rh_b_d, :rh_g_k, :rh_g_d,
:bh_r_k, :bh_r_d, :bh_b_k, :bh_b_d, :bh_g_k, :bh_g_d,
:gh_r_k, :gh_r_d, :gh_b_k, :gh_b_d, :gh_g_k, :gh_g_d
)");
$query->bindParam(':date', $utc, PDO::PARAM_STR);
$query->bindParam(':eb_r_k', $v['eb_r_k'], PDO::PARAM_INT);
$query->bindParam(':eb_r_d', $v['eb_r_d'], PDO::PARAM_INT);
$query->bindParam(':eb_b_k', $v['eb_b_k'], PDO::PARAM_INT);
$query->bindParam(':eb_b_d', $v['eb_b_d'], PDO::PARAM_INT);
$query->bindParam(':eb_g_k', $v['eb_g_k'], PDO::PARAM_INT);
$query->bindParam(':eb_g_d', $v['eb_g_d'], PDO::PARAM_INT);
$query->bindParam(':rh_r_k', $v['rh_r_k'], PDO::PARAM_INT);
$query->bindParam(':rh_r_d', $v['rh_r_d'], PDO::PARAM_INT);
$query->bindParam(':rh_b_k', $v['rh_b_k'], PDO::PARAM_INT);
$query->bindParam(':rh_b_d', $v['rh_b_d'], PDO::PARAM_INT);
$query->bindParam(':rh_g_k', $v['rh_g_k'], PDO::PARAM_INT);
$query->bindParam(':rh_g_d', $v['rh_g_d'], PDO::PARAM_INT);
$query->bindParam(':bh_r_k', $v['bh_r_k'], PDO::PARAM_INT);
$query->bindParam(':bh_r_d', $v['bh_r_d'], PDO::PARAM_INT);
$query->bindParam(':bh_b_k', $v['bh_b_k'], PDO::PARAM_INT);
$query->bindParam(':bh_b_d', $v['bh_b_d'], PDO::PARAM_INT);
$query->bindParam(':bh_g_k', $v['bh_g_k'], PDO::PARAM_INT);
$query->bindParam(':bh_g_d', $v['bh_g_d'], PDO::PARAM_INT);
$query->bindParam(':gh_r_k', $v['gh_r_k'], PDO::PARAM_INT);
$query->bindParam(':gh_r_d', $v['gh_r_d'], PDO::PARAM_INT);
$query->bindParam(':gh_b_k', $v['gh_b_k'], PDO::PARAM_INT);
$query->bindParam(':gh_b_d', $v['gh_b_d'], PDO::PARAM_INT);
$query->bindParam(':gh_g_k', $v['gh_g_k'], PDO::PARAM_INT);
$query->bindParam(':gh_g_d', $v['gh_g_d'], PDO::PARAM_INT);
$query->execute();
}
?>

84
udiff.php Normal file
View File

@@ -0,0 +1,84 @@
<?php
$database = '/home/backisbachus/gw2/stats.db';
$table_r = 'records';
$table_d = 'diff';
$score_name = array(
'eb_r_k', 'eb_r_d', 'eb_b_k', 'eb_b_d', 'eb_g_k', 'eb_g_d',
'rh_r_k', 'rh_r_d', 'rh_b_k', 'rh_b_d', 'rh_g_k', 'rh_g_d',
'bh_r_k', 'bh_r_d', 'bh_b_k', 'bh_b_d', 'bh_g_k', 'bh_g_d',
'gh_r_k', 'gh_r_d', 'gh_b_k', 'gh_b_d', 'gh_g_k', 'gh_g_d'
);
$dbhandle = new PDO('sqlite:'.$database);
$dbhandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$values = getValues($dbhandle, $table_r);
$diffs = getDiff($values, $score_name);
insertValues($dbhandle, $table_d, $diffs);
$dbhandle = null;
function getValues($h, $t) {
$query = $h -> prepare("SELECT * FROM ".$t." order by date desc limit 2");
$query -> execute();
$records = $query -> fetchAll();
return $records;
}
function getDiff($v, $s){
$prev = $v[1];
$next = $v[0];
$diff = array();
$diff['start'] = $prev['date'];
foreach($s as $c) {
$diff[$c] = $next[$c] - $prev[$c];
}
return $diff;
}
function insertValues($h, $t, $v) {
$query = $h -> prepare("INSERT INTO ".$t." VALUES (:start,
:eb_r_k, :eb_r_d, :eb_b_k, :eb_b_d, :eb_g_k, :eb_g_d,
:rh_r_k, :rh_r_d, :rh_b_k, :rh_b_d, :rh_g_k, :rh_g_d,
:bh_r_k, :bh_r_d, :bh_b_k, :bh_b_d, :bh_g_k, :bh_g_d,
:gh_r_k, :gh_r_d, :gh_b_k, :gh_b_d, :gh_g_k, :gh_g_d
)");
$query->bindParam(':start', $v['start'], PDO::PARAM_STR);
$query->bindParam(':eb_r_k', $v['eb_r_k'], PDO::PARAM_INT);
$query->bindParam(':eb_r_d', $v['eb_r_d'], PDO::PARAM_INT);
$query->bindParam(':eb_b_k', $v['eb_b_k'], PDO::PARAM_INT);
$query->bindParam(':eb_b_d', $v['eb_b_d'], PDO::PARAM_INT);
$query->bindParam(':eb_g_k', $v['eb_g_k'], PDO::PARAM_INT);
$query->bindParam(':eb_g_d', $v['eb_g_d'], PDO::PARAM_INT);
$query->bindParam(':rh_r_k', $v['rh_r_k'], PDO::PARAM_INT);
$query->bindParam(':rh_r_d', $v['rh_r_d'], PDO::PARAM_INT);
$query->bindParam(':rh_b_k', $v['rh_b_k'], PDO::PARAM_INT);
$query->bindParam(':rh_b_d', $v['rh_b_d'], PDO::PARAM_INT);
$query->bindParam(':rh_g_k', $v['rh_g_k'], PDO::PARAM_INT);
$query->bindParam(':rh_g_d', $v['rh_g_d'], PDO::PARAM_INT);
$query->bindParam(':bh_r_k', $v['bh_r_k'], PDO::PARAM_INT);
$query->bindParam(':bh_r_d', $v['bh_r_d'], PDO::PARAM_INT);
$query->bindParam(':bh_b_k', $v['bh_b_k'], PDO::PARAM_INT);
$query->bindParam(':bh_b_d', $v['bh_b_d'], PDO::PARAM_INT);
$query->bindParam(':bh_g_k', $v['bh_g_k'], PDO::PARAM_INT);
$query->bindParam(':bh_g_d', $v['bh_g_d'], PDO::PARAM_INT);
$query->bindParam(':gh_r_k', $v['gh_r_k'], PDO::PARAM_INT);
$query->bindParam(':gh_r_d', $v['gh_r_d'], PDO::PARAM_INT);
$query->bindParam(':gh_b_k', $v['gh_b_k'], PDO::PARAM_INT);
$query->bindParam(':gh_b_d', $v['gh_b_d'], PDO::PARAM_INT);
$query->bindParam(':gh_g_k', $v['gh_g_k'], PDO::PARAM_INT);
$query->bindParam(':gh_g_d', $v['gh_g_d'], PDO::PARAM_INT);
$query->execute();
}
?>