Skip to content

Commit

Permalink
Merge branch 'master' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
George C. Privon committed Dec 7, 2020
2 parents 9681138 + d516e3a commit 7edbd6a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## 0.4 Series

### 0.4.4 (07 December 2020)

#### Enhancements

* Note when a score has not been recorded.

### 0.4.3 (22 May 2019)

#### Enhancements
Expand Down
9 changes: 6 additions & 3 deletions CarcassonneScore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Carcassonne score keeping system.
Copyright 2018 George C. Privon
Copyright 2018-2019 George C. Privon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -22,7 +22,7 @@
import sys
import cgame

__version__ = "0.4.2"
__version__ = "0.4.4"

def getargs():
"""
Expand All @@ -33,6 +33,9 @@ def getargs():
system.")
parser.add_argument('-c', '--config', default='CarcassonneScore.conf',
help='Location of the configuration file.')
parser.add_argument('--random-exp', default=False,
help="Select a random set of active expansions and \
mini-expansions.")

return parser.parse_args()

Expand All @@ -44,7 +47,7 @@ def main():

args = getargs()

mygame = cgame.cgame(config=args.config)
mygame = cgame.cgame(args=args)

mygame.runGame()

Expand Down
53 changes: 47 additions & 6 deletions cgame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class cgame:
Carcassonne game object
"""

def __init__(self, config='CarcassonneScore.db'):
def __init__(self, args=None):
"""
Initialize some variables and set up a game
"""
Expand All @@ -44,7 +44,13 @@ def __init__(self, config='CarcassonneScore.db'):
('q', 'quit (will be removed for real gameplay'),
('?', 'print help')]

self.loadConfig(config)
if args is None:
_sys.stderr.write("Error: must provide a set of arguments when using the cgame class.\n")
_sys.stderr.write("Exiting.\n\n")
_sys.exit()
self.args = args

self.loadConfig()

self.conn = _sqlite3.connect(self.config.get('CarcassonneScore', 'DBNAME'))
self.cur = self.conn.cursor()
Expand All @@ -53,17 +59,17 @@ def __init__(self, config='CarcassonneScore.db'):
self.setupGame()


def loadConfig(self, cfile):
def loadConfig(self):
"""
Load configuration file
"""

if not _os.path.isfile(cfile):
_sys.stderr.write("Error: could not find configuration file '" + cfile + "'\n")
if not _os.path.isfile(self.args.config):
_sys.stderr.write("Error: could not find configuration file '" + self.args.config + "'\n")
_sys.exit()

self.config = _configparser.RawConfigParser()
self.config.read(cfile)
self.config.read(self.args.config)

# set up a preferences dictionary
self.preferences = {}
Expand Down Expand Up @@ -258,6 +264,40 @@ def getExpansions(self):
return 0


def getRandomExpansions(self):
"""
Select a set of random expansions for play.
"""
res = cur.execute('''SELECT DISTINCT expansionID FROM expansions WHERE active=1;''')

explist = res.fetchall()
explist = np.array([x[0] for x in explist])


exps = explist[explist < 100]
miniexps = explist[explist >= 100]

nexp = random.randint(0, len(exps))
nminiexp = random.randint(0, len(miniexps))

print("Selecting {0:d} full expansions and {1:d} mini expansions.".format(nexp,
nminiexp))

if nexp:
selexp = sorted(random.sample(list(exps), nexp))
print("Full expansions: ", selexp)
else:
selexp = []

if nminiexp:
selminiexp = sorted(random.sample(list(miniexps), nminiexp))
print("Mini expansions: ", selminiexp)
else:
selminiexp = []

return selexp + selminiexp


def getPlayerName(self, playerID):
"""
Given a playerID, return a player's name from the database.
Expand Down Expand Up @@ -363,6 +403,7 @@ def recordScore(self):
_sys.stdout.write(' with ' + score['tokens'] + '.\n')
answer = input("Is this correct? (y/n) ")
if not _re.match('y', answer, _re.IGNORECASE):
_sys.stdout.write("Note: score not recorded.\n")
return 1

# now construct a SQL query
Expand Down

0 comments on commit 7edbd6a

Please sign in to comment.