#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import pygame

def load_multiframe(datei,size):
	img = pygame.image.load(datei).convert_alpha()
	rect = img.get_rect()
	maxx = rect.width
	frames = []
	for x in range(0,maxx/size[0]):
		frames.append(img.subsurface( pygame.Rect((size[0]*x,0),(size[0],size[1])) ).copy() )
	return frames

def kreis(monde,O,r):
	felder = []
	for l in monde:
		for e in l:
			pos = e.pos
			if (pos[0]-O[0])**2 + (pos[1]-O[1])**2 <= r**2+2:
				felder.append(pos)
	return felder

def convert_from_string(string):
	out = string
	if string != "":
		if (string[0] == "["):
			out = []
			string2 =  string.strip("[]").split(",")
			if string2[0].strip().isdigit() and string2[1].strip().isdigit(): 	## [0, 1]
				for n in string2:
					out.append(int(n))
			elif string.count("[") > 1:							## [[0, 1], [0, 2]]
				string =  string.strip("[]").split("], [")
				for l in string:
					l = l.split(",")
					out.append([int(l[0]) , int(l[1])])
		elif string[0] == "{":									## {0: 1} ou {0: 0.001}
			out = {}				
			out2 = {}			
			if string != "{}":
				string = string.strip("{}").split(",")
				for s in string:
					s =s.split(":")
					out[s[0]] = s[1]
				for o in out:
					if o.strip().isdigit():									## key --> int
						tmp = out[o].strip().split(".")
						if out[o].strip().isdigit():								## value --> int
							out2[int(o)] = int(out[o])
						elif tmp[0].strip().isalnum() and len(tmp) > 1:				## value --> float
							out2[int(o)] = float(out[o])	
				out = out2
		elif string.replace(".","").isdigit():									## float
			out = 0.0
			out = float(string)
			if out.is_integer():												## int
				out = int(out)
	return out
	
	
	
def anschluss_geb(monde,f):
	anschluss = []
	if monde[f[0]][f[1]].islink()  == False:
		
		if len(monde) > f[0]+monde[f[0]][f[1]].size[0]:
			for col in monde[f[0] : f[0]+monde[f[0]][f[1]].size[0]]:
	
				if len(col) > f[1]+monde[f[0]][f[1]].size[1]:
					for el in col[f[1] : f[1]+monde[f[0]][f[1]].size[1]]:
					
						for i in [1,-1]:
							for po in [[el.pos[0]+i , el.pos[1]]  ,  [el.pos[0] , el.pos[1]+i]]:
								if monde[po[0]][po[1]].islink() == False:
									if (monde[po[0]][po[1]].typ in [2]):
										anschluss.append(monde[po[0]][po[1]].pos[:])
	return anschluss



def create_file(filename):
	con = sqlite3.connect(filename)
	con.row_factory = sqlite3.Row

	c= con.cursor()
	c.execute('''create table monde(pos text, datei text, type text , bauen texte , size text,link text)''')
	c.execute('''create table players(num integer, name text,cash integer, pop integer , ausg integer , eing integer)''')
	# Gebäude: [10,11,15,20,21]:
	c.execute('''create table farmen(player integer, typ integer, pos text , prod integer , prodv integer, spe text, reqfeld integer, aura text)''')
	c.execute('''create table markthauser(player integer, typ integer, pos text , spe text, aura text)''')
	con.commit()
	c.close()
