merge_games.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. """
  3. In the event the scoring program is killed or crashes mid-game there is
  4. currently no way to resume a game, so a new game must be started.
  5. This script takes a list of games (provided in the `gameIDs` list) and
  6. combines them into the first game in the list.
  7. It should work fine, but I'd recommend backing up your CarcassonneScore sqlite
  8. database, just in case.
  9. """
  10. import sqlite3
  11. # edit this to say which gameIDs will need to combined into a single game
  12. # You'll need to look at the actual sqlite database.
  13. gameIDs = (8, 9, 10)
  14. conn = sqlite3.connect('CarcassonneScore.db')
  15. cur = conn.cursor()
  16. # update turns and scores
  17. lastturn = []
  18. lastscore = []
  19. for game in gameIDs:
  20. lastturn.append(cur.execute('SELECT turnNum FROM turns WHERE gameID={0:1.0f} ORDER BY turnNum DESC LIMIT 1;'.format(game)).fetchall()[0][0])
  21. lastscore.append(cur.execute('SELECT scoreID FROM scores WHERE gameID={0:1.0f} ORDER BY scoreID DESC LIMIT 1;'.format(game)).fetchall()[0][0])
  22. curturn = lastturn[0]
  23. curscore = lastscore[0]
  24. for i, game in enumerate(gameIDs[1:]):
  25. print("Fixing game {0:1.0f}".format(game))
  26. # get a list of all the turns
  27. turns = cur.execute('SELECT gameID,turnNum FROM turns WHERE gameID={0:1.0f}'.format(game)).fetchall()
  28. for turn in turns:
  29. curturn += 1
  30. # update turns database
  31. command = 'UPDATE turns SET (gameID,turnNum) = ({0:1.0f},{1:1.0f}) WHERE gameID={2:1.0f} AND turnNum={3:1.0f};'.format(gameIDs[0],curturn,game,turn[1])
  32. print(command)
  33. cur.execute(command)
  34. # update all scores
  35. command = 'SELECT gameID,turnNum,scoreID from scores where gameID={0:1.0f} and turnNum={1:1.0f}'.format(game,
  36. turn[1])
  37. turnscores = cur.execute(command).fetchall()
  38. for score in turnscores:
  39. curscore += 1
  40. command = 'UPDATE scores SET (gameID,turnNum,scoreID) = ({0:1.0f},{1:1.0f},{2:1.0f}) WHERE gameID={3:1.0f} AND turnNum={4:1.0f};'.format(gameIDs[0], curturn, curscore, game, turn[1])
  41. cur.execute(command)
  42. # fix the end of game scores
  43. curturn += 1
  44. command = 'SELECT gameID,turnNum,scoreID from scores where gameID={0:1.0f} and turnNum={1:1.0f}'.format(game,
  45. 14)
  46. turnscores = cur.execute(command).fetchall()
  47. for score in turnscores:
  48. curscore += 1
  49. command = 'UPDATE scores SET (gameID,turnNum,scoreID) = ({0:1.0f},{1:1.0f},{2:1.0f}) WHERE gameID={3:1.0f} AND turnNum={4:1.0f};'.format(gameIDs[0], curturn, curscore, game, 14)
  50. cur.execute(command)
  51. conn.commit()
  52. conn.close()