1
0

update_database.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. """
  3. Database management for Carcassonne score keeping system.
  4. Copyright 2018 George C. Privon
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. """
  16. import sys
  17. import argparse
  18. import re
  19. import sqlite3
  20. def parseArgs():
  21. """
  22. Command line arguments
  23. """
  24. parser = argparse.ArgumentParser(description="Update the Carcassonne \
  25. scoring database.")
  26. cmds = parser.add_mutually_exclusive_group(required=True)
  27. cmds.add_argument('-n', '--newplayer', type=str, default=None,
  28. nargs='+',
  29. help='Add a new player.')
  30. cmds.add_argument('-e', '--enableexpansion', action='store_true',
  31. default=False,
  32. help='Enable an expansion.')
  33. cmds.add_argument('-d', '--disableexpansion', action='store_true',
  34. default=False,
  35. help='Disable an expansion.')
  36. return parser.parse_args()
  37. def getExpans(cur, active=True):
  38. """
  39. Get a list of (in)active expansions
  40. """
  41. command = 'SELECT expansionID,name FROM expansions WHERE active='
  42. if active:
  43. command = command + '1'
  44. else:
  45. command = command + '0'
  46. return cur.execute(command).fetchall()
  47. def toggleExpan(cur, ID, activate=True):
  48. """
  49. Flip the
  50. """
  51. command = 'UPDATE expansions SET active='
  52. if activate:
  53. command = command + '1'
  54. elif not activate:
  55. command = command + '0'
  56. else:
  57. sys.stderr.write("What have you done?\n")
  58. sys.exit(-1)
  59. command = command + ' where expansionID={0:1.0f}'.format(ID)
  60. cur.execute(command)
  61. def main():
  62. """
  63. main routine
  64. """
  65. args = parseArgs()
  66. conn = sqlite3.connect('CarcassonneScore.db')
  67. cur = conn.cursor()
  68. VALID = False
  69. if args.newplayer:
  70. pname = ' '.join(args.newplayer)
  71. sys.stdout.write("Adding new player: " + pname)
  72. sys.stdout.write(" to the database.\n")
  73. while not VALID:
  74. ans = input("Is this correct (y/n)? ")
  75. if re.match('y', ans, re.IGNORECASE):
  76. cur.execute('INSERT INTO players (name) VALUES ("' + \
  77. pname + '")')
  78. sys.stdout.write(pname + ' added to the database.\n')
  79. VALID = True
  80. elif re.match('n', ans, re.IGNORECASE):
  81. sys.stdout.write('Canceling.\n')
  82. VALID = True
  83. if args.enableexpansion:
  84. expans = getExpans(cur, active=False)
  85. kwds = ("enable", "inactive")
  86. activate = True
  87. elif args.disableexpansion:
  88. expans = getExpans(cur, active=True)
  89. kwds = ("disable", "active")
  90. activate = False
  91. if args.enableexpansion or args.disableexpansion:
  92. expnumlist = []
  93. for expan in expans:
  94. expnumlist.append(int(expan[0]))
  95. sys.stdout.write("{0:1.0f}) ".format(expan[0]) + expan[1] + "\n")
  96. try:
  97. toggleexp = input("Please enter the " + kwds[1] + ' expansion to ' + kwds[0] + ': ')
  98. if not list:
  99. sys.stderr.write("No expansions specified. Exiting.\n")
  100. except (EOFError, KeyboardInterrupt):
  101. conn.close()
  102. sys.exit()
  103. #TODO actually update the expansions
  104. try:
  105. toggleexp = int(toggleexp)
  106. except:
  107. sys.stderr.write("Error: invalid input. Please enter a number from the list next time.\n")
  108. sys.exit()
  109. if toggleexp not in expnumlist:
  110. sys.stderr.write("Error: invalid input. Please enter a number from the list next time.\n")
  111. sys.exit()
  112. toggleExpan(cur, toggleexp, activate=activate)
  113. conn.commit()
  114. conn.close()
  115. if __name__ == "__main__":
  116. main()