Add dynmap time queries through api, and add a time widget

This commit is contained in:
Trever Fischer
2012-03-04 10:57:37 -05:00
parent e0c9e9775c
commit 3d317ad0b3
3 changed files with 32 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ from piston.handler import AnonymousBaseHandler, BaseHandler
from profiles.models import MinecraftProfile, Quote
from minecraft.models import MOTD, Server
from django.http import HttpResponse
from urllib2 import urlopen
import json
class WhitelistHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
@@ -39,4 +41,9 @@ class ServerHandler(AnonymousBaseHandler):
def read(self, request, hostname):
s = Server.objects.get(hostname__exact=hostname)
return {"hostname":hostname, "port":s.port, "players": map(lambda x:x.mc_username, s.online_players())}
try:
dynMapJS = json.load(urlopen("http://%s/map/up/world/world/0"%(hostname)))
serverTime = dynMapJS["servertime"]
except Exception, e:
serverTime = -1
return {"hostname":hostname, "port":s.port, "players": map(lambda x:x.mc_username, s.online_players()), "time":serverTime}

20
static/js/server-poll.js Normal file
View File

@@ -0,0 +1,20 @@
function updateServer() {
$.get("/api/server/dev.camin.us", function(data) {
hours = parseInt((data['time']/1000)+8)%24;
minutes = parseInt(((data['time']/1000)%1)*60);
var day = hours < 12;
minutes = ""+minutes;
while(minutes.length<2)
minutes = "0"+minutes;
$("#time-display").html(hours+":"+minutes+" "+(day ? "am":"pm"));
});
}
function pollServer() {
updateServer();
window.setTimeout(pollServer, 1000);
}
$(document).ready(function () {
pollServer();
});

View File

@@ -12,6 +12,7 @@
<link rel="stylesheet" type="text/css" href="{{ STATIC_PREFIX }}/css/main.css"/>
</style>
<script type="text/javascript" language="javascript" src="{{STATIC_PREFIX}}/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="{{STATIC_PREFIX}}/js/server-poll.js"></script>
{% if user.is_authenticated %}
<script type="text/javascript" language="javascript" src="{{STATIC_PREFIX}}/js/balance-poll.js"></script>
{% endif %}
@@ -39,6 +40,9 @@
<div class="balance">
<span id="balance-display">{{ user.get_profile.currencyaccount.balance|floatformat:2 }}</span> grist
</div>
<div class="time">
<span id="time-display"></span>
</div>
</div>
<ul>
<li><a href="{% url user_profile %}">Profile</a></li>