Browse Source

new utilities directory. script to merge multiple gameIDs into a single gameID

George C. Privon 7 năm trước cách đây
mục cha
commit
b1642ffdf5
4 tập tin đã thay đổi với 75 bổ sung0 xóa
  1. 1 0
      CHANGELOG.md
  2. 4 0
      README.md
  3. 6 0
      utilities/README.md
  4. 64 0
      utilities/merge_games.py

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 #### Enhancements
 
 * add a configuration file specifying the database location
+* add a utilities directory for scripts which should not be needed for normal operation but may be useful for working around issues.
 
 ## 0.3 Series
 

+ 4 - 0
README.md

@@ -40,3 +40,7 @@ Use `python manage_database.py -h` to see the full list of options.
 ### Analysis
 
 A sample analysis jupyter notebook and a sample sqlite database containing one game is available in the `analysis/` directory.
+
+### Miscellaneous Utilities
+
+The `utilities/` directory contains scripts which may be useful for correcting issues, but these should not be needed for normal operations.

+ 6 - 0
utilities/README.md

@@ -0,0 +1,6 @@
+# Miscellaneous Utilities
+
+Scripts in this directory should not be needed during normal use, but may be helpful if problems are encountered.
+Backing up the `CarcassonneScore.db` file is highly recommended, prior to running any of these.
+
+* `merge_games.py` - A script to merge turns and scores from multiple gameIDs into a single gameID. This is useful if the scoring program crashes during play (as there is currently no way to resume games).

+ 64 - 0
utilities/merge_games.py

@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+"""
+In the event the scoring program is killed or crashes mid-game there is 
+currently no way to resume a game, so a new game must be started.
+This script takes a list of games (provided in the `gameIDs` list) and
+combines them into the first game in the list.
+
+It should work fine, but I'd recommend backing up your CarcassonneScore sqlite
+database, just in case.
+"""
+
+import sqlite3
+
+
+# edit this to say which gameIDs will need to combined into a single game
+# You'll need to look at the actual sqlite database.
+gameIDs = (8, 9, 10)
+
+conn = sqlite3.connect('CarcassonneScore.db')
+cur = conn.cursor()
+
+# update turns and scores
+lastturn = []
+lastscore = []
+for game in gameIDs:
+    lastturn.append(cur.execute('SELECT turnNum FROM turns WHERE gameID={0:1.0f} ORDER BY turnNum DESC LIMIT 1;'.format(game)).fetchall()[0][0])
+    lastscore.append(cur.execute('SELECT scoreID FROM scores WHERE gameID={0:1.0f} ORDER BY scoreID DESC LIMIT 1;'.format(game)).fetchall()[0][0])
+
+curturn = lastturn[0]
+curscore = lastscore[0]
+
+for i, game in enumerate(gameIDs[1:]):
+    print("Fixing game {0:1.0f}".format(game))
+    # get a list of all the turns
+    turns = cur.execute('SELECT gameID,turnNum FROM turns WHERE gameID={0:1.0f}'.format(game)).fetchall()
+    for turn in turns:
+        curturn += 1
+        # update turns database
+        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])
+        print(command)
+        cur.execute(command)
+        # update all scores
+        command = 'SELECT gameID,turnNum,scoreID from scores where gameID={0:1.0f} and turnNum={1:1.0f}'.format(game,
+                                                                                                                turn[1])
+
+        turnscores = cur.execute(command).fetchall()
+        for score in turnscores:
+            curscore += 1
+            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])
+            cur.execute(command)
+
+# fix the end of game scores
+curturn += 1
+command = 'SELECT gameID,turnNum,scoreID from scores where gameID={0:1.0f} and turnNum={1:1.0f}'.format(game,
+                                                                                                        14)
+
+turnscores = cur.execute(command).fetchall()
+for score in turnscores:
+    curscore += 1
+    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)
+    cur.execute(command)
+
+conn.commit()
+conn.close()