def send_notification(game:Game): if len(game.subscribed())==0: return for user in game.subscribed(True): try: utils.spool_func(notificating.mail.tpl.oncoming_game, game, user) except Exception as e: logging.message('Error on sending email of upcoming game <{}> notification for <{}>'.format(game.game_id(), user.user_id()), e)
def main(): game = Game() print(game.code) turn = [0, 0, 0, 0] game.code = ['red', 'red', 'red', 'red'] while not game.is_game_over: turn = game.play_turn_computer(turn)
def _main(): game = Game() print(game.code) while not game.is_game_over: turn = game.play_turn() print(turn)
def send_notification(game: Game): if len(game.subscribed()) == 0: return for user in game.subscribed(True): try: utils.spool_func(notificating.mail.tpl.oncoming_game, game, user) except Exception as e: logging.message( 'Error on sending email of upcoming game <{}> notification for <{}>' .format(game.game_id(), user.user_id()), e)
def get_by_id(game_id, dbconnection: dbutils.DBConnection = None) -> Game: if isinstance(game_id, str) and len(game_id.split(',')) > 0: game_id = splitstrlist(game_id) if len(game_id) == 1: game_id = game_id[0] if isinstance(game_id, list) and len(game_id) == 0: return list() if isinstance(game_id, int): dbconnection.execute( "SELECT * FROM games WHERE game_id={}".format(game_id), dbutils.dbfields['games']) elif isinstance(game_id, list): dbconnection.execute( "SELECT * FROM games WHERE game_id IN (" + ','.join(map(str, game_id)) + ")", dbutils.dbfields['games']) if len(dbconnection.last()) == 0: return list() games = dbconnection.last() games = list(map(lambda x: Game(x, dbconnection=dbconnection), games)) if isinstance(game_id, int): return games[0] elif isinstance(game_id, list): return games
def game_notification(): with dbutils.dbopen() as db: db.execute( "SELECT * FROM games WHERE deleted=0 AND notificated=0 AND NOW()<datetime", dbutils.dbfields['games']) if len(db.last()) == 0: return games = list(map(lambda x: Game(x, dbconnection=db), db.last())) weekdays = list( filter(lambda x: x.datetime.date().weekday() < 5, games)) weekends = list( filter(lambda x: x.datetime.date().weekday() > 4, games)) notify = list() if len(weekdays) > 0: morning_day = list( filter(lambda x: x.datetime.time().hour < 18, weekdays)) if len(morning_day) > 0: now = datetime.datetime.now() for game in morning_day: if game.datetime.tommorow and 18 < now.hour < 19: notify.append(game) evening = list( filter(lambda x: 18 <= x.datetime.time().hour, weekdays)) if len(evening) > 0: now = datetime.datetime.now() for game in evening: if game.datetime.today and 9 < now.hour < 10: notify.append(game) if len(weekends) > 0: morning_day = list( filter(lambda x: x.datetime.time().hour < 18, weekends)) if len(morning_day) > 0: now = datetime.datetime.now() for game in morning_day: if game.datetime.tommorow and 9 < now.hour < 10: notify.append(game) evening = list( filter(lambda x: 18 <= x.datetime.time().hour, weekends)) if len(evening) > 0: now = datetime.datetime.now() for game in evening: if game.datetime.tommorow and 18 < now.hour < 19: notify.append(game) for game in notify: try: send_notification(game) except Exception as e: logging.message( 'Error on sending upcoming game notifications for <{}>'. format(game.game_id()), e) else: db.execute( "UPDATE games SET notificated=1 WHERE game_id={}".format( game.game_id()))
def randdd(): all_turns = [] for i in range(1000): num_turns = 0 correct = '' while correct != 4: game = Game() correct = game.provide_feedback(['red', 'white', 'blue', 'yellow'])[0] num_turns += 1 all_turns.append(num_turns) print(min(all_turns))
def user_game_intersection(user_id: int, game: Game, dbconnection: dbutils.DBConnection = None) -> Game: sql = ( " SELECT * FROM games WHERE game_id!='{game_id}'" " AND game_id IN (SELECT game_id FROM usergames WHERE user_id='{user_id}' AND status=2)" " AND deleted=0 AND (" " (datetime BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE)" " OR " " (datetime + INTERVAL duration MINUTE BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE))" " AND " " datetime!='{datetime}' AND datetime!=('{datetime}' + INTERVAL '{duration}' MINUTE)" " AND " " (datetime + INTERVAL duration MINUTE)!='{datetime}' AND (datetime + INTERVAL duration MINUTE)!=('{datetime}' + INTERVAL '{duration}' MINUTE)" ) sql = sql.format(datetime=game.datetime, duration=game.duration(), user_id=user_id, game_id=game.game_id()) dbconnection.execute(sql, dbutils.dbfields['games']) if len(dbconnection.last()) > 0: return Game(dbconnection.last()[0], dbconnection=dbconnection) return None
def calc_game(game_id:int, dbconnection:dbutils.DBConnection=None) -> dict: game = dbconnection.execute("SELECT * FROM games WHERE game_id={}".format(game_id), dbutils.dbfields['games'])[0] game = Game(game, dbconnection=dbconnection) reports = dbconnection.execute("SELECT * FROM reports WHERE game_id={}".format(game_id), dbutils.dbfields['reports']) additional_charges = dbconnection.execute("SELECT * FROM additional_charges WHERE game_id={}".format(game_id), dbutils.dbfields['additional_charges']) finances = dict() finances['game_id'] = game_id finances['datetime'] = game.datetime() finances['capacity'] = game.capacity() finances['cost'] = game.cost() finances['sport_id'] = game.sport_type() finances['responsible_user_id'] = game.responsible_user_id() finances['created_by'] = game.created_by() finances['visited'] = len(list(filter(lambda x: x['status']!=0, reports))) finances['empty'] = finances['capacity']-finances['visited'] if finances['empty']<0: finances['empty']=0 finances['lost_empty'] = finances['empty']*finances['cost'] finances['notvisited'] = len(list(filter(lambda x: x['status']==0, reports))) finances['lost_notvisited'] = finances['notvisited']*finances['cost'] finances['notpayed'] = len(list(filter(lambda x: x['status']==1, reports))) finances['lost_notpayed'] = finances['notpayed']*finances['cost'] finances['playedpayed'] = len(list(filter(lambda x: x['status']==2, reports))) finances['real_income'] = finances['playedpayed']*finances['cost'] finances['ideal_income'] = finances['cost']*finances['capacity'] if finances['capacity']>0 else finances['real_income'] finances['rent_charges'] = game.court_id(True).cost()*(game.duration()/60) finances['additional_charges'] = sum([i['cost'] for i in additional_charges]) finances['profit'] = finances['real_income']-finances['additional_charges']-finances['rent_charges'] dbconnection.execute("SELECT percents FROM responsible_salary WHERE user_id={}".format(game.responsible_user_id())) finances['responsible_salary'] = 0 if len(dbconnection.last())>0: finances['responsible_salary'] = round((finances['profit'])*(dbconnection.last()[0][0]/100)) finances['profit'] -= finances['responsible_salary'] finances['real_profit'] = finances['profit'] + finances['rent_charges'] return finances
def court_game_intersection(court_id: int, datetime: str, duration: int, dbconnection: dbutils.DBConnection = None) -> Game: sql = ( " SELECT * FROM games WHERE court_id='{court_id}'" " AND deleted=0 AND (" " (datetime BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE)" " OR " " (datetime + INTERVAL duration MINUTE BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE))" " AND " " datetime!='{datetime}' AND datetime!=('{datetime}' + INTERVAL '{duration}' MINUTE)" " AND " " (datetime + INTERVAL duration MINUTE)!='{datetime}' AND (datetime + INTERVAL duration MINUTE)!=('{datetime}' + INTERVAL '{duration}' MINUTE)" ) sql = sql.format(court_id=court_id, datetime=datetime, duration=duration) dbconnection.execute(sql, dbutils.dbfields['games']) if len(dbconnection.last()) > 0: return Game(dbconnection.last()[0], dbconnection=dbconnection) return None
async def start(ctx): '''@user1 @user2 @user3 [@user4] [@user5] [$user6]''' # check if game was started in lobby mom_lobby = bot.get_channel(mom_lobby_id) if ctx.channel == mom_lobby: # check if there is already a game in progress global game if game: await ctx.send("There is currently a game in progress!") return # check that we start the game with min-max players users = ctx.message.mentions if not (min_players <= len(users) <= max_players): await ctx.send("Mayor of Markham is a " + str(min_players) + "-" + str(max_players) + " player game!") return # create a text channel for each player mom_category = bot.get_channel(mom_category_id) random.shuffle(users) i = 1 players = [] for user in users: overwrites = { mom_category.guild.default_role: discord.PermissionOverwrite(read_messages=False), user: discord.PermissionOverwrite(read_messages=True) } channel = await mom_category.create_text_channel( "player-" + str(i), overwrites=overwrites) players.append({"user": user, "channel": channel}) i += 1 # initialize game game = Game(players) for player in players: game.do_draw(player["user"], 6, 0, 0) await player["channel"].send( game.display_hand(player["user"]) + message_append) await mom_lobby.send(game.display_status() + message_append) await mom_lobby.send("{}, it is your turn to {}!".format( game.turn.user.name, game.subphase) + message_append) await game.turn.channel.send("{}, it is your turn to {}!".format( game.turn.user.name, game.subphase) + message_append)
def get_recent(court_id: int = 0, city_id: int = 1, sport_type: int = 0, count: slice = slice(0, 20), old: bool = False, dbconnection: dbutils.DBConnection = None) -> list: sql = "SELECT * FROM games WHERE {} ORDER BY DATETIME {} LIMIT {}, {}" where = list() where.append("city_id={}".format(city_id)) where.append("deleted=0") where.append("datetime+INTERVAL duration MINUTE {} NOW()".format( '>' if not old else '<')) if court_id: where.append("court_id={}".format(court_id)) if sport_type: where.append("sport_type={}".format(sport_type)) sql = sql.format(' AND '.join(where), 'ASC' if not old else 'DESC', count.start if count.start else 0, count.stop) games = dbconnection.execute(sql, dbutils.dbfields['games']) games = list(map(lambda x: Game(x, dbconnection=dbconnection), games)) return games
server = "192.168.0.64" port = 5555 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.bind((server, port)) except socket.error as e: str(e) s.listen(2) print('Waiting for connection, Server started') game = Game([ Player(45, 285, 10, 150, (255, 255, 255)), Player(945, 285, 10, 150, (255, 255, 255)) ], Ball(465, 330, 25, (255, 255, 255)), -1, 0, 0) def threaded_client(conn, player): global playersCount conn.send(str.encode(str(player))) while True: try: data = pickle.loads(conn.recv(2048)) if not data: print('Disconnected') break else: if data == 'ready':
def play_game(): game = Game() while not game.isGameOver: take_turn(game) print() print("Game over!")
from objects import Game import pygame game = Game() pygame.init() game.start() pygame.quit()
def get_all(dbconnection: dbutils.DBConnection = None) -> Game: dbconnection.execute("SELECT * FROM games WHERE deleted=0", dbutils.dbfields['games']) if len(dbconnection.last()) == 0: return list() return list(map(lambda x: Game(x, dbconnection), dbconnection.last()))