Skip to content

Instantly share code, notes, and snippets.

@malte-laukoetter
Created June 24, 2017 19:28
Show Gist options
  • Save malte-laukoetter/e6e2ace92f3f677ca49ef06c42b382e7 to your computer and use it in GitHub Desktop.
Save malte-laukoetter/e6e2ace92f3f677ca49ef06c42b382e7 to your computer and use it in GitHub Desktop.
Hive Gold Medal Leaderboard
{
"name": "Hive Gold Medal Leaderboard",
"main": "index.html",
"dependencies": {
"polymer": "Polymer/polymer#^2.0.0",
"polymerfire": "^2.1.0",
"paper-item": "^2.0.0",
"paper-avatar": "^2.0.1",
"paper-input": "^2.0.1",
"paper-button": "^2.0.0"
},
"devDependencies": {
"web-component-tester": "Polymer/web-component-tester#^6.0.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^1.0.0"
}
}
<link rel="import" href="bower_components/polymer/polymer-element.html">
<link rel="import" href="bower_components/polymerfire/polymerfire.html">
<link rel="import" href="bower_components/paper-item/paper-icon-item.html">
<link rel="import" href="bower_components/paper-item/paper-item-body.html">
<link rel="import" href="bower_components/paper-avatar/paper-avatar.html">
<link rel="import" href="bower_components/paper-input/paper-input.html">
<link rel="import" href="bower_components/paper-button/paper-button.html">
<dom-module id="hive-medal-leaderboard">
<template>
<style>
:host {
display: block;
--paper-item-icon-width: 70px;
}
</style>
<firebase-app
auth-domain="dashboard-196e4.firebaseapp.com"
database-url="https://dashboard-196e4.firebaseio.com/"
api-key="AIzaSyBr2_BiRET53JK44D3WBDqGx6HKqmAVatg"
storage-bucket="dashboard-196e4.appspot.com">
</firebase-app>
<firebase-query
id="query"
path="/hive/medalLeaderboard/data"
order-by-child="medals"
limit-to-last=100
data="{{data}}">
</firebase-query>
<div role="listbox">
<template is="dom-repeat" items="{{data}}" as="player" sort="sortByMedals" observe="medals">
<paper-icon-item>
<a href="https://hivemc.com/player/[[player.name]]" slot="item-icon"><paper-avatar image-src="http://cravatar.eu/avatar/[[player.name]]/128">
</paper-avatar></a>
<paper-item-body two-line>
<div>[[player.name]]</div>
<div secondary>[[player.medals]] Gold Medals ([[addOne(index)]]. place)</div>
</paper-item-body>
</paper-icon-item>
</template>
</div>
<div style="display: flex">
<paper-input style="flex-grow: 1" label="New Name / UUID" value="{{newName}}"></paper-input>
<paper-button style="flex-basis: 200px" raised on-tap="addNewName">Add to List</paper-button>
</div>
<div style="width: 100%"><a href="https://lergin.de/imprint">Imprint</a> - <a href="https://lergin.de/privacy">Privacy</a> - by <a href="https://lergin.de">Lergin</a></div>
</template>
<script>
/**
* @customElement
* @polymer
*/
class HiveMedalLeaderboard extends Polymer.Element {
static get is() { return 'hive-medal-leaderboard'; }
static get properties() {
return {
newName: {
type: "string",
value: ""
}
};
}
sortByMedals(a,b){
return b.medals - a.medals;
}
addOne(a){
return a + 1;
}
addNewName(){
firebase.database().ref('hive').child('medalLeaderboard').child('newPlayers').child(this.newName.replace(" ", "")).set(true);
this.newName = "";
}
}
window.customElements.define(HiveMedalLeaderboard.is, HiveMedalLeaderboard);
</script>
</dom-module>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>Hive Gold Medal Leaderboard</title>
<meta name="description" content="Hive Gold Medal Leaderboard description">
<!-- See https://goo.gl/OOhYW5 -->
<link rel="manifest" href="/manifest.json">
<script src="/bower_components/webcomponentsjs/webcomponents-loader.js"></script>
<link rel="import" href="/hive-medal-leaderboard.html">
</head>
<body>
<hive-medal-leaderboard></hive-medal-leaderboard>
</body>
</html>
{
"name": "Hive Gold Medal Leaderboard",
"short_name": "Hive Gold Medal Leaderboard",
"start_url": "/",
"display": "standalone"
}
import {Player} from "hive-api" //a not jet really published node wrapper for the hive api
import * as admin from "firebase-admin"
const serviceAccount = require("../firebase_service_account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://dashboard-196e4.firebaseio.com"
});
const db = admin.database();
let ref = db.ref("hive/medalLeaderboard");
let newPlayersRef = ref.child("newPlayers");
let dataRef = ref.child("data");
let timeoutPlayers: Map<string, any> = new Map();
let waitingPlayers: Set<Player> = new Set();
newPlayersRef.on("child_added", (snap) => {
let player: Player = new Player(snap.key);
player.info().then((info)=>{
dataRef.child(player.uuid).update({
medals: info.medals,
name: info.name
});
}).catch(console.error);
newPlayersRef.child(snap.key).remove();
});
dataRef.orderByChild("medals").limitToLast(100).on("child_added", (snap) => {
let player: Player = new Player(snap.key);
waitingPlayers.add(player);
});
dataRef.on("child_removed", (snap)=>{
clearTimeout(timeoutPlayers.get(snap.key));
timeoutPlayers.delete(snap.key);
});
setInterval(()=>{
let a = waitingPlayers.values().next();
if(!a.done){
a.value.info().then(info => {
return dataRef.child(a.value.uuid).update({
medals: info.medals,
name: info.name
});
}).then(() => {
timeoutPlayers.set(a.value.uuid, setTimeout(()=>{
waitingPlayers.add(a.value);
return;
}, 1000 * 60 * 10));
}).catch(console.error);
waitingPlayers.delete(a.value);
}
}, 5 * 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment