def _inferExtraPlays(games, plays): import library result = [] messages = [] playsByDate = library.DictOfLists() playCountByMonth = library.Counts() for play in plays: playsByDate.add(play.date, play) playCountByMonth.add(play.date[:7]) for date in playsByDate.keys(): datePs = playsByDate[date] count = 0 mms = [] while True: mms = [] (rs, ms, changed) = _inferExtraPlaysForADate(games, datePs) mms = mms + ms if not changed: break count += 1 if count == 200: mms += ["Got really confused"] break datePs = rs result = result + rs messages = messages + mms return (result, messages)
def _inferExtraPlays(geek, db, plays): import library result = [] playsByDate = library.DictOfLists() playCountByMonth = library.Counts() for play in plays: playsByDate.add(play.date, play) playCountByMonth.add(play.date[:7]) for date in playsByDate.keys(): debug = False datePs = playsByDate[date] if debug: print "before", datePs count = 0 while True: (rs, changed) = _inferExtraPlaysForADate(debug, db, datePs) if not changed: break count = count + 1 if count == 200: print "Got really confused", geek, date break datePs = rs if debug: print "after", rs result = result + rs return result
def _writeOpponentsToDB(db, geek, playerRecs, month, year): import library sql = "insert into opponents %s values %s" counts = library.Counts() for (username, name, colour) in playerRecs: if username == geek: continue counts.add(PlayerRecKey(username, name, colour)) for (prk, count) in counts.items(): if count < 2: continue lhs = [] rhs = [] binds = [] if prk.name is not None and len(prk.name) <= 45: lhs.append("name") rhs.append("%s") binds.append(prk.name) if prk.username is not None: lhs.append("username") rhs.append("%s") binds.append(prk.username) if prk.colour is not None: lhs.append("colour") rhs.append("%s") binds.append(prk.colour) lhs = lhs + ["geek", "month", "year", "count"] rhs = rhs + ["%s", "%s", "%s", "%s"] binds = binds + [geek, month, year, count] l = "(" + ", ".join(lhs) + ")" r = "(" + ", ".join(rhs) + ")" s = sql % (l, r) db.execute(s, binds)
def addPlaysDataToGeekGames(self, geekgames): import library, datetime plays = self.getPlaysForDescribedRange([])[0] byGame = {} for p in plays: p.game.plays = [] byGame[p.game.bggid] = p.game for e in p.expansions: e.plays = [] byGame[e.bggid] = e for p in plays: p.game.plays.append(p) for e in p.expansions: e.plays.append(p) today = datetime.date.today() if today.month == 2 and today.day == 29: oneYearAgo = datetime.date(today.year - 1, 2, 28) else: oneYearAgo = datetime.date(today.year - 1, today.month, today.day) oneYearAgo = oneYearAgo.strftime("%Y-%m-%d") for gg in geekgames: g = byGame.get(gg.bggid) if g is None: (gg.plays, gg.firstPlay, gg.lastPlay, gg.monthsPlayed, gg.playsInLastYear) = (0, None, None, 0, 0) else: data = g.plays playCount = 0 playsInLastYear = 0 first = None last = None playsByMonth = library.Counts() for play in data: if play.date > oneYearAgo: playsInLastYear += play.count playCount += play.count if ((first is None) or (play.date < first)) and not play.date.endswith("00"): first = play.date if ((last is None) or (play.date > last)) and not play.date.endswith("00"): last = play.date playDateMonth = play.date[:7] playsByMonth.add(playDateMonth, play.count) monthCount = len(playsByMonth) try: firstPlay = library.parseYYYYMMDD(first) except ValueError: firstPlay = None try: lastPlay = library.parseYYYYMMDD(last) except ValueError: lastPlay = None (gg.plays, gg.firstPlay, gg.lastPlay, gg.monthsPlayed, gg.pbm, gg.playsInLastYear) = (playCount, firstPlay, lastPlay, monthCount, playsByMonth, playsInLastYear)
def getFrontPageHindexData(db, fpg): import plays, library data = plays.getAllPlays(db, fpg.geek) byGame = library.Counts() for play in data: byGame.add(play.game, play.count) counts = byGame.values()[:] counts.sort(lambda a,b: -cmp(a,b)) hindex = 0 while len(counts) > hindex and counts[hindex] > hindex: hindex = hindex + 1 fpg.hindex = hindex
def createNewPlaysGraph(context, data): """graph of new plays accumulating over time, and over years""" # data is a list of dates import library, datetime, math imgspec = context.imageSpec (img, draw, xlo, xhi, ylo, yhi) = newImage(imgspec.width, imgspec.height, 0, 10) countPlaysByYear = library.Counts() for date in data: countPlaysByYear.add(date.year) data.sort() minYear = None maxYear = None if len(countPlaysByYear) > 0: maxYear = datetime.date.today().year while countPlaysByYear[maxYear] == 0: maxYear -= 1 minYear = maxYear while countPlaysByYear[minYear - 1] > 0: minYear -= 1 if minYear is not None and maxYear is not None: oneDay = datetime.timedelta(1) startDate = datetime.date(minYear, 1, 1) endDate = datetime.date(datetime.date.today().year, 12, 31) data = [d for d in data if startDate <= d <= endDate] widthInDays = (endDate - startDate).days daysX = 0 curDate = startDate newGamesPerYear = {} index = 0 yearTrails = [] allTimeTrail = [] allTime = 0 while curDate <= endDate: if len(data) == index: break if newGamesPerYear.get(curDate.year) is None: newGamesPerYear[curDate.year] = 0 found = False while index < len(data) and curDate.toordinal( ) == data[index].toordinal(): newGamesPerYear[curDate.year] += 1 allTime += 1 index += 1 found = True if found: yearTrails.append((daysX, newGamesPerYear[curDate.year])) allTimeTrail.append((daysX, allTime)) curDate = curDate + oneDay daysX += 1 maxInOneYear = max(newGamesPerYear.values()) maxInOneYear = math.ceil(maxInOneYear / 10.0) * 10 allTime = math.ceil(allTime / 100.0) * 100 def convY(dy, my): return int(yhi - (yhi - ylo) * dy / my) def convX(dx): return int(xlo + (xhi - xlo) * dx / widthInDays) curYear = startDate.year while curYear <= endDate.year: jan1 = datetime.date(curYear, 1, 1) jan1NextYear = datetime.date(curYear + 1, 1, 1) x1 = int(xlo + (xhi - xlo) * (jan1 - startDate).days / widthInDays) if x1 == xlo: x1 += 1 x2 = int(xlo + (xhi - xlo) * (jan1NextYear - startDate).days / widthInDays) centreText(draw, str(curYear), x1, x2, yhi + 10) if curYear % 2 == 1: draw.rectangle([(x1, ylo), (x2, yhi - 1)], fill=YELLOWGREEN) curYear += 1 ly = 0 while ly <= maxInOneYear: y = convY(ly, maxInOneYear) draw.line([xlo - 5, y, xlo, y], BLACK) if 0 < ly < maxInOneYear: draw.text((xlo - 30, y - 10), str(ly), fill=BLACK) draw.line([xlo, y, xhi, y], DARKGRAY) ly += 10 ry = 0 while ry <= allTime: y = convY(ry, allTime) draw.line([xhi + 5, y, xhi, y], BLUE) if 0 < ry < allTime: draw.text((xhi + 10, y - 10), str(ry), fill=BLUE) ry += 50 for (dx, dy) in yearTrails: x = convX(dx) y = convY(dy, maxInOneYear) draw.ellipse((x - 2, y - 2, x + 2, y + 2), fill=BLACK) for (dx, dy) in allTimeTrail: x = convX(dx) y = convY(dy, allTime) draw.polygon([(x - 2, y + 1), (x, y - 1), (x + 2, y + 1)], outline=BLUE) del draw return img
def getCounts(self, context, opts, startDate, endDate): import library return library.Counts()