#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# GGZ Core Client with SDL fullscreen interface
# Copyright (C) 2004 Josef Spillner <josef@ggzgamingzone.org>
# Published under GNU GPL conditions

"""
Vibora
Pygame core client for GGZ
"""

import ggzcoresimple
import pygame, pygame.transform, pygame.image
import pygame.surfarray
from random import *
from math import *
from pygame.locals import *
from Numeric import *
import os, sys

try:
	import ggzsettings
	DATAPATH = ggzsettings.DATAPATH + "/vibora/"
except:
	DATAPATH = "./"

CAPTION_ALL = 3
CAPTION_TEXT = 2
CAPTION_BG = 1

SCROLL_UP = 1
SCROLL_DOWN = 2

WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
DARKRED = (150, 0, 0)
GREEN = (0, 255, 0)

def _(x):
	return unicode(x, "utf-8")

def asciify(str):
		asciistr = str.encode("ascii", "replace")
		return asciistr

def vibora_background(resolution):
	granularity = 16
	counter = 27

	try:
		bg = pygame.image.load(DATAPATH + "background.png")
	except:
		overlay = pygame.Surface((resolution[0] / granularity, resolution[1] / granularity))

		for j in range(resolution[1] / granularity):
			for i in range(resolution[0] / granularity):
				blue = ((sin(i / 2.0 - counter / 2.0) +  cos(j / 2.0 + counter / 2.0)) * 80.0)
				if blue < 0:
					blue = 0
				if blue > 50:
					blue = 50
				green = 0 
				red = 0
				overlay.fill((red, green, blue), ((i, j), (1, 1)))

		bg = pygame.transform.scale(overlay, resolution)

	return bg

def vibora_frame(surface, rect):
	((x, y), (x2, y2)) = rect
	w = x2 - x
	h = y2 - y

	if 0 == 1:
		array = pygame.surfarray.pixels2d(surface)
		for j in range(y, y2, 2):
			for i in range(x, x2, 2):
				pixel = array[i, j]
				pixel = pixel & 0xFF1F
				array[i, j] = pixel
				array[i + 1, j] = pixel
				array[i, j + 1] = pixel
				array[i + 1, j + 1] = pixel

	if 1 == 1:
		surface.lock()
		for j in range(y, y2):
			for i in range(x, x2):
				(red, green, blue, alpha) = surface.get_at((i, j))
				blue = blue / 3
				if(blue > 30):
					blue = 30
				surface.set_at((i, j), (red, green, blue, alpha))
		surface.unlock()
	surface.fill((255, 255, 255), ((x, y), (w, 1)))
	surface.fill((255, 255, 255), ((x, y), (1, h)))
	surface.fill((255, 255, 255), ((x, y2), (w, 1)))
	surface.fill((255, 255, 255), ((x2, y), (1, h)))

def vibora_scrollframe(surface, rect, direction):
	((x, y), (x2, y2)) = rect

	vibora_frame(surface, rect)
	points = ((x + 3, y2 - 3), ((x2 + x) / 2, y + 3), (x2 - 3, y2 - 3))
	if direction == SCROLL_DOWN:
		points = ((x + 3, y + 3), ((x2 + x) / 2, y2 - 3), (x2 - 3, y + 3))
	pygame.draw.polygon(surface, (255, 255, 255), points)

def vibora_caption(surface, caption, rect, what, color, line):
	((x, y), (x2, y2)) = rect

	y = y + 20 * line

	if what & CAPTION_BG:
		surface.lock()
		for j in range(y + 1, y + 19):
			for i in range(x + 1, x2 - 1):
				(red, green, blue, alpha) = surface.get_at((i, j))
				blue = blue + 30
				green = green + 30
				red = red + 30
				if blue > 255:
					blue = 255
				if green > 255:
					green = 255
				if red > 255:
					red = 255
				surface.set_at((i, j), (red, green, blue, alpha))
		surface.unlock()

	if what & CAPTION_TEXT:
		font = pygame.font.Font(None, 20)
		text = font.render(caption, 1, color)
		surface.blit(text, (x + 3, y + 1))

def fits(caption, rect):
	((x, y), (x2, y2)) = rect

	font = pygame.font.Font(None, 20)
	(w, h) = font.size(caption)

	if w > (x2 - x) - 6:
		return 0
	return 1

def background_loading():
	global screen_logo
	global screen_loading
	global screen

	screen.blit(screen_loading, (100, 10))
	screen.blit(screen_logo, (10, 15))

	pygame.display.flip()

def background_progress():
	global screen_resolution
	global screen_logo
	global screen_loading
	global screen
	global progressrect

	bg = vibora_background(screen_resolution)

	progressrect = ((100, 100), (screen_resolution[0] - 100, 300))

	vibora_frame(bg, progressrect)

	vibora_caption(bg, _("Login progress:"), progressrect, CAPTION_ALL, WHITE, 0)

	bg.blit(screen_loading, (100, 10))
	bg.blit(screen_logo, (10, 15))

	screen.blit(bg, (0, 0))

	pygame.display.flip()

	return bg

def background_firsttime():
	global screen_resolution
	global screen_logo
	global screen_loading
	global screen
	global progressrect

	bg = vibora_background(screen_resolution)

	progressrect = ((100, 100), (screen_resolution[0] - 100, 250))

	vibora_frame(bg, progressrect)

	vibora_caption(bg, _("First-time setup:"), progressrect, CAPTION_ALL, WHITE, 0)

	bg.blit(screen_loading, (100, 10))
	bg.blit(screen_logo, (10, 15))

	screen.blit(bg, (0, 0))

	pygame.display.flip()

	return bg

def firsttime():
	global progressrect
	global bg
	global screen
	global username
	global password

	username = ""
	password = ""
	firstlogin = ""

	inputready = 0
	bgdirty = 1

	try:
		os.mkdir(os.getenv("HOME") + "/.ggz")
	except:
		pass

	try:
		f = file(os.getenv("HOME") + "/.ggz/vibora.rc", "r")
		while 1:
			line = f.readline()
			if line == "":
				break
			(key, value) = line.split("=")
			value = value[:-1]
			if key == "username":
				username = value
			elif key == "password":
				password = value
			elif key == "firstlogin":
				firstlogin = value
		f.close()
	except:
		pass

	if firstlogin == "false":
		return 1

	bg = background_firsttime()
	bgshadow = pygame.Surface(screen_resolution)

	while inputready < 2:
		pygame.event.pump()

		event = pygame.event.poll()

		if event.type == KEYDOWN:
			key = event.key

			if key == K_RETURN:
				inputready += 1
				bgdirty = 1
			elif key == K_ESCAPE:
				return -1
			elif key == K_BACKSPACE:
				if inputready == 0:
					username = username[:-1]
				else:
					password = password[:-1]
				bgdirty = 10
			else:
				if inputready == 0:
					username += event.unicode
				else:
					password += event.unicode
				bgdirty = 10

		if bgdirty == 1:
			bgshadow.blit(bg, (0, 0))
			vibora_caption(bgshadow, _("* Username (can be registered at ggzcommunity.org)"), progressrect, CAPTION_ALL, YELLOW, 1)
			if username:
				vibora_caption(bgshadow, username, progressrect, CAPTION_ALL, GREEN, 2)
			if inputready > 0:
				vibora_caption(bgshadow, _("* Password (empty for guest accounts)"), progressrect, CAPTION_ALL, YELLOW, 3)
				if password:
					passwordsecret = ""
					for c in password:
						passwordsecret += "*"
					vibora_caption(bgshadow, passwordsecret, progressrect, CAPTION_ALL, GREEN, 4)
			if inputready == 2:
				vibora_caption(bgshadow, _("* Data saved"), progressrect, CAPTION_ALL, YELLOW, 5)
			screen.blit(bgshadow, (0, 0))
			pygame.display.flip()

		if bgdirty > 0:
			bgdirty -= 1

	f = file(os.getenv("HOME") + "/.ggz/vibora.rc", "w")
	f.write("username=" + username + "\n")
	f.write("password=" + password + "\n")
	f.write("firstlogin=" + "false" + "\n")
	f.close()

	return 1

def background_problem():
	global screen_resolution
	global screen_logo
	global screen_problem
	global screen

	bg = vibora_background(screen_resolution)

	msgrect = ((100, 100), (screen_resolution[0] - 100, screen_resolution[1] - 90))

	vibora_frame(bg, msgrect)

	vibora_caption(bg, _("Message:"), msgrect, CAPTION_ALL, WHITE, 0)

	bg.blit(screen_problem, (100, 10))
	bg.blit(screen_logo, (10, 15))

	screen.blit(bg, (0, 0))

	pygame.display.flip()

def background_chat():
	global screen_resolution
	global screen_logo
	global screen_caption
	global screen
	global chatscrolluprect, chatscrolldownrect, roomscrolluprect, roomscrolldownrect
	global chatrect, roomrect, chatlinerect
	global roomwidth

	bg = vibora_background(screen_resolution)

	roomwidth = 300
	resolution = screen_resolution

	chatrect = ((100, 100), (resolution[0] - roomwidth, resolution[1] - 90))
	roomrect = ((resolution[0] - (roomwidth - 20), 100), (resolution[0] - 100, resolution[1] - 50))
	chatlinerect = ((100, resolution[1] - 70), (resolution[0] - roomwidth, resolution[1] - 50))

	chatscrolluprect = ((60, 100), (80, 120))
	chatscrolldownrect = ((60, 140), (80, 160))
	roomscrolluprect = ((resolution[0] - 80, 100), (resolution[0] - 60, 120))
	roomscrolldownrect = ((resolution[0] - 80, 140), (resolution[0] - 60, 160))

	vibora_frame(bg, chatrect)
	vibora_frame(bg, chatlinerect)
	vibora_frame(bg, roomrect)

	vibora_caption(bg, _("Chat"), chatrect, CAPTION_ALL, WHITE, 0)

	bigfont = pygame.font.Font(None, 48)
	roomname = ggzcoresimple.room.get_name()
	screen_caption = bigfont.render("GGZ Gaming Zone: " + roomname, 1, (255, 255, 255))

	bg.blit(screen_caption, (100, 10))
	bg.blit(screen_logo, (10, 15))

	screen.blit(bg, (0, 0))

	pygame.display.flip()

	return bg

def ggz_game_event(id, data):
	pass

def ggz_room_event(id, data):
	global chatoutput
	global bg_dirty
	global playerlist
	global tablelist

	if id == ggzcoresimple.ROOM_CHAT_EVENT:
		type = data[0]
		player = data[1]
		message = data[2]

		chatoutput.append(player + ": " + message)
		bg_dirty = 1
	elif id == ggzcoresimple.ROOM_TABLE_LIST:
		tablelist = data
		bg_dirty = 1
	elif id == ggzcoresimple.ROOM_PLAYER_LIST:
		playerlist = data
		bg_dirty = 1
	elif id == ggzcoresimple.ROOM_TABLE_UPDATE:
		tablelist = data
		bg_dirty = 1
	elif id == ggzcoresimple.ROOM_ENTER:
		#tablelist = data
		bg_dirty = 1
	elif id == ggzcoresimple.ROOM_LEAVE:
		#tablelist = data
		bg_dirty = 1

def ggz_server_event(id, data):
	global progressrect
	global bg
	global ggzready
	global roomlist
	global bg_dirty
	global chatoutput
	global username
	global playerlist
	global tablelist
	global serverstate

	if id == ggzcoresimple.SERVER_CONNECTED:
		vibora_caption(bg, _("* Connected!"), progressrect, CAPTION_ALL, GREEN, 2)
	elif id == ggzcoresimple.SERVER_CONNECT_FAIL:
		vibora_caption(bg, _("* Failure!"), progressrect, CAPTION_ALL, RED, 2)
		ggzready = -1
	elif id == ggzcoresimple.SERVER_NEGOTIATED:
		vibora_caption(bg, _("* Logging in as ") + username, progressrect, CAPTION_ALL, YELLOW, 3)
		ggzcoresimple.server.login()
	elif id == ggzcoresimple.SERVER_LOGGED_IN:
		vibora_caption(bg, _("* Logged in!"), progressrect, CAPTION_ALL, GREEN, 4)
	elif id == ggzcoresimple.SERVER_ROOM_LIST:
		roomlist = data
		vibora_caption(bg, _("* Entering the lounge"), progressrect, CAPTION_ALL, YELLOW, 5)
		ggzcoresimple.server.join_room("Lounge")
	elif id == ggzcoresimple.SERVER_ENTERED:
		if ggzready == 0:
			vibora_caption(bg, _("* Entered!"), progressrect, CAPTION_ALL, GREEN, 6)
			ggzready = 1
		else:
			chatoutput.append(_("* Entered!"))
		playerlist = ""
		tablelist = ""
	elif id == ggzcoresimple.SERVER_ENTER_FAIL:
		vibora_caption(bg, _("* Failure!"), progressrect, CAPTION_ALL, RED, 6)
		ggzready = -1
	elif id == ggzcoresimple.SERVER_LOGIN_FAIL:
		vibora_caption(bg, _("* Failure!"), progressrect, CAPTION_ALL, RED, 4)
		ggzready = -1
	elif id == ggzcoresimple.SERVER_STATE_CHANGE:
		state = ggzcoresimple.server.get_state()
		if state == ggzcoresimple.STATE_OFFLINE:
			serverstate = _("offline")
		elif state == ggzcoresimple.STATE_CONNECTING:
			serverstate = _("connecting")
		elif state == ggzcoresimple.STATE_ONLINE:
			serverstate = _("online")
		elif state == ggzcoresimple.STATE_LOGGING_IN:
			serverstate = _("logging in")
		elif state == ggzcoresimple.STATE_LOGGED_IN:
			serverstate = _("logged in")
		elif state == ggzcoresimple.STATE_ENTERING_ROOM:
			serverstate = _("entering room")
		elif state == ggzcoresimple.STATE_BETWEEN_ROOMS:
			serverstate = _("between rooms")
		elif state == ggzcoresimple.STATE_IN_ROOM:
			serverstate = _("in room")
		elif state == ggzcoresimple.STATE_LAUNCHING_TABLE:
			serverstate = _("launching table")
		elif state == ggzcoresimple.STATE_JOINING_TABLE:
			serverstate = _("joining table")
		elif state == ggzcoresimple.STATE_AT_TABLE:
			serverstate = _("at table")
		elif state == ggzcoresimple.STATE_LEAVING_TABLE:
			serverstate = _("leaving table")
		elif state == ggzcoresimple.STATE_LOGGING_OUT:
			serverstate = _("logging out")
		else:
			serverstate = _("(unknown)")

	if ggzready < 2:
		screen.blit(bg, (0, 0))
		pygame.display.flip()
		if ggzready == 1:
			ggzready = 2
	else:
		bg_dirty = 1

def readenterkey():
	while 1:
		pygame.event.pump()

		event = pygame.event.poll()

		if event.type == KEYDOWN:
			key = event.key

			if key == K_RETURN:
				return
			if key == K_ESCAPE:
				return

def ggz_progress(host):
	global progressrect
	global bg
	global ggzready
	global username
	global password

	ggzready = 0

	ggzcoresimple.setHandler(ggzcoresimple.EVENT_SERVER, ggz_server_event)
	ggzcoresimple.setHandler(ggzcoresimple.EVENT_ROOM, ggz_room_event)
	ggzcoresimple.setHandler(ggzcoresimple.EVENT_GAME, ggz_game_event)

	ggzcoresimple.server.set_hostinfo(host, 5688, 1)
	if password:
		ggzcoresimple.server.set_logininfo(ggzcoresimple.LOGIN, username, password)
	else:
		ggzcoresimple.server.set_logininfo(ggzcoresimple.LOGIN_GUEST, username, "")

	vibora_caption(bg, _("* Connecting to ") + host, progressrect, CAPTION_ALL, YELLOW, 1)
	screen.blit(bg, (0, 0))
	pygame.display.flip()

	ggzcoresimple.server.connect()

	while not ggzready:
		pygame.event.pump()

		event = pygame.event.poll()

		if event.type == KEYDOWN:
			key = event.key

			if key == K_ESCAPE:
				vibora_caption(bg, _("* User interrupt!"), progressrect, CAPTION_ALL, RED, 2)
				ggzready = -1

				screen.blit(bg, (0, 0))
				pygame.display.flip()

		ggzcoresimple.server.process()

	if ggzready == -1:
		readenterkey()
	return ggzready

def main(host):
	global screen_resolution
	global screen_problem
	global screen_caption
	global screen_loading
	global screen_logo
	global screen
	global chatscrolluprect, chatscrolldownrect, roomscrolluprect, roomscrolldownrect
	global chatrect, roomrect, chatlinerect
	global progressrect
	global roomwidth
	global bg
	global chatoutput
	global roomlist
	global bg_dirty
	global playerlist
	global tablelist
	global tablerects
	global serverstate

	""" Initialization """

	screen_resolution = (1024, 768)
	#resolution = (640, 480)

	pygame.init()
	pygame.display.set_caption(_("GGZ Gaming Zone (Vibora Client)"))

	screen = pygame.display.set_mode(screen_resolution, HWSURFACE | DOUBLEBUF | FULLSCREEN)

	bigfont = pygame.font.Font(None, 48)
	font = pygame.font.Font(None, 20)
	smallfont = pygame.font.Font(None, 14)

	screen_loading = bigfont.render(_("Loading Víbora 0.0.2 (GGZ 0.0.10)..."), 1, (255, 255, 255))
	screen_problem = bigfont.render(_("Problem!"), 1, (255, 255, 255))
	screen_caption = bigfont.render(_("GGZ Gaming Zone: Lounge"), 1, (255, 255, 255))

	screen_logo = pygame.image.load(DATAPATH + "logo.png")

	clock = pygame.time.Clock()

	""" Intro screen """

	serverstate = ""

	background_loading()

	clock.tick()

	chatoutput = []
	tablerects = []
	roomlist = []
	startroom = 0

	""" Username/password entry on first startup """

	ret = firsttime()
	if ret < 0:
		return

	""" Progress """

	bg = background_progress()
	ret = ggz_progress(host)
	if ret < 0:
		return

	#""" Simulate problem """

	#background_problem()

	""" Create background """

	bg = background_chat()

	chatline = ""
	#chatoutput = ["Hello", "World"]
	#roomlist = ["TicTacToe (0)", "NetSpades (3)"]

	bg_dirty = 1
	bgshadow = pygame.Surface(screen_resolution)

	switchto_room = -1
	switchto_table = -1
	launch_table = 0
	current_room = 0

	maxline = (screen_resolution[1] - 50 - 100) / 20
	if (screen_resolution[1] - 50 - 100) % 20 > 0:
		maxline = maxline - 1
	maxchatline = (screen_resolution[1] - 120 - 100) / 20
	if (screen_resolution[1] - 120 - 100) % 20 > 0:
		maxchatline = maxchatline - 1

	""" Main loop """

	while 1:
		oldchatline = chatline
		dchatline = ""

		ggzcoresimple.server.process()

		pygame.event.pump()

		while 1:
			event = pygame.event.poll()

			if event.type == NOEVENT:
				break

			if event.type == QUIT:
				return

			if event.type == MOUSEBUTTONDOWN:
				(x, y) = event.pos
				((rx, ry), (rx2, ry2)) = roomrect
				if x > rx and x < rx2 and y > ry and y < ry2:
					line = (y - ry) / 20
					if line == maxline:
						return
					if line > 0 and line < maxline - 1:
						if current_room == 0:
							switchto_room = line + startroom - 1
						else:
							if line < maxline - 2:
								i = 0
								for r in tablerects:
									(ypos, yheight) = r
									if y >= ypos and y <= ypos + yheight:
										switchto_table = i
									i += 1
					if line == maxline - 1:
						if current_room != 0:
							switchto_room = 0
					if line == maxline - 2:
						if current_room != 0:
							launch_table = 1

				((rx, ry), (rx2, ry2)) = roomscrolluprect
				if x > rx and x < rx2 and y > ry and y < ry2:
					if startroom > 0:
						startroom -= 1
						bg_dirty = 1
				((rx, ry), (rx2, ry2)) = roomscrolldownrect
				if x > rx and x < rx2 and y > ry and y < ry2:
					if startroom < len(roomlist) - 1:
						startroom += 1
						bg_dirty = 1

			if event.type == KEYDOWN:
				key = event.key
				#print event, pygame.key.name(key)

				if key == K_ESCAPE:
					return
				elif key == K_RETURN:
					if chatline != "":
						chatline = asciify(chatline)
						ggzcoresimple.room.chat(ggzcoresimple.CHAT_NORMAL, "", chatline)
						#chatoutput.append(chatline)
						chatline = ""
						bg_dirty = 1
				elif key == K_SPACE:
					chatline += " "
				elif key == K_TAB:
					ignore = 1
				elif key == K_BACKSPACE:
					chatline = chatline[:-1]
					bg_dirty = 1
				else:
					chatline += event.unicode

		if chatline != oldchatline:
			if oldchatline == "":
				bg_dirty = 1
			dchatline = chatline
			while not fits(dchatline, chatlinerect):
				dchatline = dchatline[1:]
				bg_dirty = 1

		if switchto_room >= 0:
			chatoutput.append(_("* Entering room ") + roomlist[switchto_room])
			ggzcoresimple.server.join_room(roomlist[switchto_room])
			current_room = switchto_room
			switchto_room = -1
			#bg_dirty = 1
			playerlist = ""
			tablelist = ""

		if switchto_table >= 0:
			chatoutput.append(_("* Joining table ") + str(switchto_table))
			ggzcoresimple.room.join(switchto_table)
			switchto_table = -1
			bg_dirty = 1

		if launch_table == 1:
			chatoutput.append(_("* Launching table "))
			ggzcoresimple.room.launch()
			launch_table = 0
			bg_dirty = 1

		if bg_dirty:
			bgshadow.blit(bg, (0, 0))

			vibora_scrollframe(bgshadow, chatscrolluprect, SCROLL_UP)
			vibora_scrollframe(bgshadow, chatscrolldownrect, SCROLL_DOWN)

			vibora_scrollframe(bgshadow, roomscrolluprect, SCROLL_UP)
			vibora_scrollframe(bgshadow, roomscrolldownrect, SCROLL_DOWN)

			if current_room != 0:
				vibora_caption(bgshadow, _("Players and Tables"), roomrect, CAPTION_ALL, WHITE, 0)
			else:
				vibora_caption(bgshadow, _("Rooms"), roomrect, CAPTION_ALL, WHITE, 0)

			if chatline != "":
				vibora_caption(bgshadow, chatline, chatlinerect, CAPTION_BG, YELLOW, 0)

			i = len(chatoutput)
			j = 0
			tmp = []
			for l in chatoutput:
				tmp.append(l)
			tmp.reverse()
			((x, y), (x2, y2)) = chatrect
			for line in tmp:
				(w, h) = font.size(line)
				i = i - 1
				j = j + w / (x2 - x) + 1
				if j > maxchatline:
					i = i + 1
					break
			j = 1
			while i < len(chatoutput):
				line = chatoutput[i]
				while len(line) > 0:
					tmpline = line
					(w, h) = font.size(tmpline)
					while w > x2 - x:
						tmpline = tmpline[:-1]
						(w, h) = font.size(tmpline)
					vibora_caption(bgshadow, tmpline, chatrect, CAPTION_ALL, YELLOW, j)
					j = j + 1
					line = line[len(tmpline):]
				i = i + 1

			if current_room == 0:
				i = 0
				for line in roomlist:
					if i >= startroom:
						vibora_caption(bgshadow, line, roomrect, CAPTION_ALL, RED, i - startroom + 1)
					i = i + 1
					if i == 27 + startroom:
						break
			else:
				base = 150

				tableplayers = []

				if len(tablelist) > 0:
					index = 0
					tablerects = []
					while index < len(tablelist):
						(tablename, playercount) = tablelist[index]
						if not tablename:
							tablename = _("Table (untitled)")
						else:
							tablename = _("Table ") + tablename

						tablerects.append((base, 30 + 10 * playercount))
						
						rect = ((screen_resolution[0] - (roomwidth - 20) + 20, base), (roomwidth - 20 - 100 - 20 * 2, 30 + 10 * playercount))
						pygame.draw.rect(bgshadow, (55, 150, 127), rect)
						titletext = smallfont.render(tablename, 1, (255, 255, 255))
						bgshadow.blit(titletext, (screen_resolution[0] - (roomwidth - 20) + 20 + 3, base + 1))

						i = 2
						for j in range(index + 1, index + 1 + playercount):
							player = tablelist[index + j]
							tableplayers.append(player)
							if player == "":
								player == _("(open)")
							name = smallfont.render(player, 1, (255, 255, 255))
							if i % 2 == 0:
								bgshadow.blit(name, (screen_resolution[0] - (roomwidth - 20) + 20 + 3, base + i * 10 + 1))
							else:
								w = name.get_width()
								bgshadow.blit(name, (screen_resolution[0] - 100 - 20 * 2 - w + 3, base + i * 10 + 1))
							i = i + 1
						base += 20 + 20 * playercount + 10
						index += playercount + 1

				if len(playerlist) > 0:
					players = []
					for p in playerlist:
						(name, status) = p
						if name not in tableplayers:
							namestr = name
							if status == "guest":
								namestr += _(" (guest)")
							elif status == "admin":
								namestr += _(" (admin)")
							elif status == "bot":
								namestr += _(" (bot)")
							players.append(namestr)

					rect = ((screen_resolution[0] - (roomwidth - 20) + 20, base), (roomwidth - 20 - 100 - 20 * 2, 30 + 10 * len(players)))
					pygame.draw.rect(bgshadow, (155, 150, 127), rect)
					titletext = smallfont.render(_("Not playing"), 1, (255, 255, 255))
					bgshadow.blit(titletext, (screen_resolution[0] - (roomwidth - 20) + 20 + 3, base + 1))

					i = 2
					for player in players:
						name = smallfont.render(player, 1, (255, 255, 255))
						bgshadow.blit(name, (screen_resolution[0] - (roomwidth - 20) + 20 + 3, base + i * 10 + 1))
						i = i + 1

				i = maxline - 2
				vibora_caption(bgshadow, _("Launch new game"), roomrect, CAPTION_ALL, DARKRED, i)
				i = maxline - 1
				vibora_caption(bgshadow, _("Back to Lounge"), roomrect, CAPTION_ALL, RED, i)
			i = maxline
			vibora_caption(bgshadow, _("Logout"), roomrect, CAPTION_ALL, RED, i)

			bg_dirty = 0

		screen.blit(bgshadow, (0, 0))

		fps = _("Fps: ") + str(int(clock.get_fps() * 10) / 10.0)
		state = _("State: ") + serverstate
		lag = _("Lag: ") + str(0)
		subcaption = font.render(state + " " + lag + " " + fps, 1, (255, 255, 255))

		screen.blit(subcaption, (100, 50))

		if dchatline != "":
			vibora_caption(bgshadow, dchatline, chatlinerect, CAPTION_TEXT, YELLOW, 0)

		pygame.display.flip()
		clock.tick()

if __name__ == '__main__':
	host = None

	if len(sys.argv) == 2:
		host = sys.argv[1]

	if host == "--help":
		print _("víbora [hostname]")
		sys.exit(0)
	
	if not host:
		host = "live.ggzgamingzone.org"

	main(host)

