Skip to content

Commit

Permalink
1.1 - Local and serverside caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ripexz committed Aug 26, 2014
1 parent 5c666c1 commit b7edcb4
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
*.min.js
*.zip
node_modules
build
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ CS:GO Bet Alert
A Chrome extension to keep track of active CS:GO Lounge matches.

Add to Chrome: https://chrome.google.com/webstore/detail/csgo-bet-alert/eaijhjfdoohbhheggpgmdobmdidebamm

### Requirements
- Node.JS
- gulp

### Setup
- `npm i`

### Build
- `gulp build` or just `gulp`

### Deploy
1. Build
2. `gulp deploy` to create the ZIP file
5 changes: 0 additions & 5 deletions badge.min.js

This file was deleted.

35 changes: 35 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();

var paths = {
src: "./src",
build: "./build"
};

gulp.task('manifest', function() {
gulp.src(paths.src + '/manifest.json')
.pipe(gulp.dest(paths.build));
});
gulp.task('scripts', function() {
gulp.src(paths.src + '/*.js')
.pipe($.uglify({ mangle: true }))
.pipe(gulp.dest(paths.build));
});
gulp.task('markup', function() {
gulp.src(paths.src + '/*.html')
.pipe(gulp.dest(paths.build));
});
gulp.task('images', function() {
gulp.src(paths.src + '/images/**')
.pipe($.imagemin())
.pipe(gulp.dest(paths.build));
});

gulp.task('deploy', function(){
gulp.src(paths.build + '/**')
.pipe($.zip('csgobetalert.zip'))
.pipe(gulp.dest('./'));
});

gulp.task('build', ['manifest', 'scripts', 'markup', 'images']);
gulp.task('default', ['build']);
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "csgo-bet-alert",
"version": "1.1.0",
"description": "A Chrome extension to keep track of active CS:GO Lounge matches.",
"main": "popup.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/ripexz/csgo-bet-alert.git"
},
"keywords": [
"csgo",
"counter-strike",
"counter",
"strike",
"global",
"offensive",
"chrome",
"extension"
],
"author": "ripexz <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/ripexz/csgo-bet-alert/issues"
},
"homepage": "https://github.com/ripexz/csgo-bet-alert",
"devDependencies": {
"gulp": "^3.8.7",
"gulp-imagemin": "^1.0.1",
"gulp-load-plugins": "^0.5.3",
"gulp-uglify": "^0.3.1",
"gulp-zip": "^2.0.1"
}
}
8 changes: 8 additions & 0 deletions php/get_data.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

$memcached = new Memcached();
$memcached->addServer('localhost', 11211);

//check for this message and stop work if servers are overloaded
$errMsg = "Servers are under very heavy load or item draft is under progress.";
$nfMsg = "Looks like there's no site that you´re looking for.";
Expand Down Expand Up @@ -122,6 +125,11 @@
}
}

if ($next != $last || mysqli_num_rows($result2) > 0) {
// Invalidate cache:
$memcached->delete("csgo_match_data");
}

function file_get_contents_curl($url, $timeout = 0) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
Expand Down
12 changes: 12 additions & 0 deletions php/get_json.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

header('Content-Type: application/json');

$memcached = new Memcached();
$memcached->addServer('localhost', 11211);

$mdata = $memcached->get("csgo_match_data");
if ($mdata) {
echo $mdata;
exit();
}

$db = mysqli_connect( 'localhost', 'username', 'password', 'dbname' );
if ( !$db ) {
die();
Expand All @@ -24,6 +35,7 @@
}

$json .= "]}";
$memcached->set("csgo_match_data", $json);
echo $json;

?>
5 changes: 0 additions & 5 deletions popup.min.js

This file was deleted.

19 changes: 11 additions & 8 deletions badge.js → src/badge.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
function updateBadge(data) {
var matchObj = JSON.parse(data.target.responseText);
var str_num = matchObj.matches.length + '';
chrome.browserAction.setBadgeText({text:str_num});
function saveData(data) {
var matchObj = JSON.parse(data.target.responseText),
countStr = matchObj.matches.length + '';
chrome.browserAction.setBadgeText({text: countStr});

matchObj.valid = true;
chrome.storage.local.set({'csgomatches': matchObj});
}
function getMatchCount() {
function getMatchData() {
var req = new XMLHttpRequest();
req.open("GET", 'http://www.ripexz.com/csgobetalert/get_json.php', true);
req.onload = updateBadge.bind(this);
req.open("GET", 'http://eyeur.com/csgo/get_json.php', true);
req.onload = saveData.bind(this);
req.send(null);
}

Expand All @@ -22,7 +25,7 @@ function addAlarm() {
}
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name == "csgobetalert") {
getMatchCount();
getMatchData();
}
});
chrome.runtime.onStartup.addListener(function() {
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
7 changes: 4 additions & 3 deletions manifest.json → src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "CS:GO Bet Alert",
"description": "CS:GO Bet Alert lists any active games on CS:GO Lounge so that you never miss the chance to lose your rares!",
"version": "1.0.1",
"version": "1.1.0",

"icons": {
"16": "icon16.png",
Expand All @@ -14,11 +14,12 @@
"permissions": [
"http://*/*",
"tabs",
"alarms"
"alarms",
"storage"
],

"background": {
"scripts": ["badge.min.js"]
"scripts": ["badge.js"]
},

"browser_action": {
Expand Down
2 changes: 1 addition & 1 deletion popup.html → src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<body>
<h1>CS:GO Bet Alert</h1>
<div id="match_list"></div>
<script src="popup.min.js"></script>
<script src="popup.js"></script>
</body>
</html>
38 changes: 29 additions & 9 deletions popup.js → src/popup.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
var csgobetalert = {

reqUrl: 'http://www.ripexz.com/csgobetalert/get_json.php',
reqUrl: 'http://eyeur.com/csgo/get_json.php',

getData: function() {
chrome.storage.local.get('csgomatches', function(matchObj){
if (matchObj.csgomatches && matchObj.csgomatches.valid) {
csgobetalert.renderMatches(matchObj.csgomatches);
}
else {
csgobetalert.getDataViaAjax();
}
});
},

getDataViaAjax: function() {
var req = new XMLHttpRequest();
req.open("GET", this.reqUrl, true);
req.onload = this.listMatches.bind(this);
req.onload = this.renderMatches.bind(this);
req.send(null);
},

listMatches: function(data) {
var matchObj = JSON.parse(data.target.responseText);
if ( matchObj.matches && matchObj.matches.length > 0 ) {
for ( var i = 0; i < matchObj.matches.length; i++ ) {
renderMatches: function(data) {
var matchObj, countStr;
if (data.valid) {
matchObj = data;
}
else {
matchObj = JSON.parse(data.target.responseText);
matchObj.valid = true;
chrome.storage.local.set({'csgomatches': matchObj});
}

if (matchObj.matches && matchObj.matches.length > 0) {
for (var i = 0; i < matchObj.matches.length; i++) {
var p = document.createElement('p');
p.id = matchObj.matches[i].id + '';
p.innerHTML = '<span class="t1">'+matchObj.matches[i].team1+'</span><span class="vs">vs</span><span class="t2">'+matchObj.matches[i].team2+'</span>';
document.getElementById('match_list').appendChild(p);
}
var str_num = i + '';
countStr = i + '';
this.applyBindings();
}
else {
var p = document.createElement('p');
p.innerHTML = 'No active matches found.';
document.getElementById('match_list').appendChild(p);
var str_num ='0';
countStr = '0';
}
chrome.browserAction.setBadgeText({text:str_num});
chrome.browserAction.setBadgeText({text: countStr});
},

applyBindings: function() {
Expand Down

0 comments on commit b7edcb4

Please sign in to comment.