#!/usr/bin/env python

import pg
import sys

def norm((longitude, latitude), weight):
	mapwidth = 596
	mapheight = 331
	longnorm = longitude + 180
	latnorm = latitude + 90
	calibrate_x = -10
	calibrate_y = -20
	xpos = int((longnorm + calibrate_x) * mapwidth / 360)
	ypos = int((latnorm + calibrate_y) * mapheight / 180)
	xpos -= int(weight / 2)
	ypos -= int(weight / 2)
	return (xpos, ypos)

def run(factorx, factory):
	mapwidth = 596
	mapheight = 331
	radius = 10

	if factorx == 4:
		radius = 4
	if factorx == 1:
		radius = 8

	cns = pg.DB(host="XXX", dbname="XXX", user="XXX", passwd="XXX")
	q = cns.query("SELECT handle, longitude, latitude, handle FROM userinfo WHERE latitude IS NOT NULL")

	print "<map name='ggzmap'>"

	for row in q.dictresult():
		longitude = int(row['longitude'])
		latitude = int(row['latitude'])
		handle = row['handle']
		#print longitude, latitude, radius
		(x, y) = norm((longitude, latitude), radius)
		#print "norm", x, y
		x = x / factorx
		y = (mapheight - y) / factory
		coords = str(x) + "," + str(y) + "," + str(x + radius) + "," + str(y + radius)
		coords = "coords='" + coords + "'"
		alt = "alt='" + handle + "'"
		title = "title='" + handle + "'"
		href = "href='/db/players/?lookup=" + handle + "'"
		print "<area " + coords + " " + alt + " " + title + " " + href + ">"
	print "</map>"

factor = 1
if len(sys.argv) == 2:
	factor = int(sys.argv[1])
run(factor, factor)

