-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap-update.py
124 lines (93 loc) · 3.13 KB
/
map-update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
import random
import sys
import time
import sqlalchemy
import sqlalchemy.exc
import db
import levelData
import cellData
import worldData
#we'll load these values from config files later
worldId = 1
#currently we'll only be running this without threads so we can store the database
# connectin in a global variable
conn = None
def update():
i = 1
while(i==1):
i +=1
trans = conn.begin()
try:
#query database for a dirty level
c = db.levelData.columns
statement = db.select([c.worldId, c.cellIdX, c.cellIdY, c.levelIdZ],(c.dirtyImage == True)).limit(1)
result = conn.execute(statement)
row = result.fetchone()
result.close()
if row == None:
#we didn't find anything that needed updating
trans.rollback()
#sleep
time.sleep(30)
#loop
else:
#get all layers for that cell
c = db.levelData.columns
statement = db.select([c.worldId, c.cellIdX, c.cellIdY, c.levelIdZ,
c.water, c.air, c.rock,
c.artic, c.swamp, c.hills, c.mountains, c.jungle, c.plains, c.grassland, c.forest,
c.baseImage, c.dirtyImage, c.cityId],(c.worldId == row['worldId']) &
(c.cellIdX == row['cellIdX']) & (c.cellIdY == row['cellIdY']))
result = conn.execute(statement)
rows = result.fetchall()
result.close()
#update the image for each of the layers that needs it
levels = {}
for row in rows:
print dict(row)
level = levelData.LevelData(rowData = dict(row))
if row['dirtyImage'] == True:
level.updateImage(conn)
levels[row['levelIdZ']] = level
##we can commit the changes to the database here
trans.commit()
cell = cellData.CellData(row['worldId'], row['cellIdX'], row['cellIdY'], levels)
##check if the surface image has changed
changed = cell.compressLevels(conn)
print 'changed:', changed
worldObj = worldData.WorldData(worldId)
worldObj.loadFromDb(conn)
#if so then update all the zoom images until we reach a level that isn't affected
zoom = 2
nCells = 2 ** (zoom -1)
while nCells < worldObj.lengthX and nCells < worldObj.lengthY and changed:
print "Stage:", zoom, "Number of base images per new image:", nCells
print 'x,y:', row['cellIdX'], row['cellIdY']
x = row['cellIdX'] - (row['cellIdX'] % nCells)
y = row['cellIdY'] - (row['cellIdY'] % nCells)
print 'x,y:', x, y
cell = cellData.CellData(row['worldId'], x, y)
changed = cell.zoomLevel(zoom)
zoom = zoom + 1
nCells = 2 ** (zoom -1)
trans.rollback()
except sqlalchemy.exc.SQLAlchemyError, exc:
trans.rollback()
print 'Could not commit changes to database'
print 'Error:', exc
conn.close()
if __name__=='__main__':
conn = db.connect()
random.seed()
if (len(sys.argv) == 2 and sys.argv[1] == "update") or len(sys.argv) == 2:
##update the tiles to reflect changes in the database
#worldObj = worldData(worldId)
#cProfile.run('worldObj = worldData(worldId)')
#worldObj.drawTiles()
update()
else:
print "usage:"
print "map-update.py update"
conn.close()