Imported Upstream version 2008.1+svn1648
[opeanno-debian-packaging.git] / development / recalculate_unit_speed.py
blobcbe033ced5d5c7ce3aa547c50985a9ecaf345683
1 #!/usr/bin/env python
2 # ###################################################
3 # Copyright (C) 2008 The OpenAnno Team
4 # team@openanno.orgk
5 # This file is part of OpenAnno.
7 # OpenAnno is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the
19 # Free Software Foundation, Inc.,
20 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 # ###################################################
22 import sys
23 import os.path
24 import sqlite3
26 if __name__ == "__main__":
28 usage = '''Parameter:
29 -u num: recalculate velocity for unit num
30 -g num: recalculate velocity for ground num
32 If you e.g. change the velocity modifier for ground 2, run this script with '-g 2'.
33 '''
36 dbfile = 'content/openanno.sqlite'
38 if not os.path.exists(dbfile):
39 print 'Please execute from openanno root directory.'
40 sys.exit(1)
42 # copied from dbreader.py:
43 con = sqlite3.connect(dbfile)
44 con.isolation_level = None
45 con.text_factory = str
47 cur = con.cursor()
49 def db(cmd, *args):
50 cur.execute(cmd, args)
51 return cur.fetchall()
53 def check_rounding_error(val, unit, groundid, diagonal):
54 if abs(val - round(val)) >= 0.4:
55 if diagonal:
56 print 'warning: big rounding error for DIAGONAL move'
57 else:
58 print 'warning: big rounding error'
60 print 'unit:',unit,' ground:',groundid
61 print 'raw value %.4f rounded: %d'%(val, int(val))
62 print
64 def update(unit, groundid, base_vel, vel_mod):
65 if len(db("select rowid from unit_velocity where unit = %d and ground = %d;"%(unit, groundid))) > 0:
66 db("delete from unit_velocity where unit = %d and ground = %d"%(unit, groundid))
68 straight = base_vel * vel_mod
69 check_rounding_error(straight, unit, groundid, False)
71 diagonal = straight * 2**0.5
72 check_rounding_error(diagonal, unit, groundid, True)
74 db("insert into unit_velocity(unit, ground, time_move_straight, time_move_diagonal) values(%d, %d, %d, %d)"%(unit, groundid, int(round(straight)), int(round(diagonal))))
77 argv = sys.argv
79 if len(argv) != 3:
80 print usage
81 sys.exit(0)
83 print '-'*8,'USE AT OWN RISK','-'*8
84 print 'NOTE: previous values will be overwritten, manually entered changes will be lost'
85 print 'Are you sure you want to run this script with these params:',sys.argv[1:],'? (y/n)'
86 if raw_input() != 'y':
87 print 'Aborting'
88 sys.exit(0)
91 print 'If your specified number does not exist in the db, nothing will happen'
94 if sys.argv[1] == '-u':
95 unit = int(sys.argv[2])
96 print 'Recalculating for unit', unit
97 for base_vel, in db("select base_velocity from unit where rowid = %d;"%unit):
98 for groundid, vel_mod in db("select rowid, velocity_modifier from ground"):
99 update(unit, groundid, base_vel, vel_mod)
100 print 'Done. If no warnings occured, then you chose good values'
102 elif sys.argv[1] == '-g':
103 groundid = int(sys.argv[2])
104 print 'Recalculating for ground', groundid
105 for vel_mod, in db("select velocity_modifier from ground where rowid = %d"%groundid):
106 for unit, base_vel in db("select rowid, base_velocity from unit"):
107 update(unit, groundid, base_vel, vel_mod)
108 print 'Done. If no warnings occured, then you chose good values'
110 else:
111 print usage