def saveBot(request): rs = {} POST = request.POST botId = POST['id'] if(botId != ''): Bot.objects.filter(id=botId).update(f_nick = POST['nick'].strip(), f_password = POST['password'], f_money_type = POST['moneyType'] , f_enabled = POST['enabledStatus'], f_strategy = POST['strategy'], f_min_bb = Tools.toInt(POST['minBB']) , f_max_bb = Tools.toInt(POST['maxBB']), f_bb_exit = Tools.toInt(POST['bbExit'])) else: nicks = POST['nick'].split(",") tableNames = POST['tables'].strip().split(",") for nick in nicks: nick = nick.strip() bot = Bot.objects.create(f_nick = nick, f_password = POST['password'], f_money_type = POST['moneyType'] , f_enabled = POST['enabledStatus'], f_strategy = POST['strategy'], f_min_bb = Tools.toInt(POST['minBB']) , f_max_bb = Tools.toInt(POST['maxBB']), f_bb_exit = Tools.toInt(POST['bbExit'])) for table in tableNames: tableName = table.strip() if(tableName != ''): BotTable.objects.create(bot_id=bot.id, f_table_name = tableName.strip(), f_enabled = 1, f_strategy = '', f_min_bb = 0, f_max_bb = 0, f_bb_exit = 0) rs['success'] = True return HttpResponse(json.dumps(rs), mimetype="application/json")
def botsGridData(request): ctx = Tools.defaultContextData(request, {'POST':request.POST}); #TODO implement the filter rs = Bot.objects.filter() Tools.populateContextForGrid(ctx, rs, request.GET['rows'], request.GET['page'], request.GET['sidx'], request.GET['sord']) return render_to_response("templates/home/bots_grid_data.xml", ctx);
def sendChat(request): ctx = {} bot = request.GET['bot'] table = request.GET['table'] msg = request.GET['msg'] if(bot.strip() != "" and table.strip()!="" and msg.strip() != "" and table != "------Tournaments-------"): BotTableChat.objects.create(f_bot_nick=bot, f_table_name = table, f_message = msg) ctx['success'] = True ctx['chats'] = BotTableChat.objects.all() Tools.defaultContextData(request, ctx) return render_to_response('templates/chat/pending_msgs.html', ctx);
def saveBotTable(request): rs = {} POST = request.POST botTableId = POST['id'] if(botTableId != ''): BotTable.objects.filter(id=botTableId).update(f_table_name = POST['tableName'], f_enabled = POST['enabledStatus'], f_strategy = POST['strategy'], f_min_bb = Tools.toInt(POST['minBB']) , f_max_bb = Tools.toInt(POST['maxBB']), f_bb_exit = Tools.toInt(POST['bbExit'])) else: tableNames = POST['tableName'].split(",") nTime = datetime.datetime.now() - datetime.timedelta(minutes = 30) for tableName in tableNames: BotTable.objects.create(bot_id=POST['botId'], f_table_name = tableName.strip(), f_enabled = POST['enabledStatus'], f_strategy = POST['strategy'], f_min_bb = Tools.toInt(POST['minBB']) , f_max_bb = Tools.toInt(POST['maxBB']), f_bb_exit = Tools.toInt(POST['bbExit']), f_last_exit_time = nTime, f_last_enter_time = nTime) rs['success'] = True return HttpResponse(json.dumps(rs), mimetype="application/json")
def botsChat(request): ctx = {} tmpTables = BotTable.objects.values('f_table_name').distinct() lstTables = [x['f_table_name'] for x in tmpTables] tmpTables = BotTournament.objects.values("f_table_name").distinct() if len(tmpTables): lstTables.append("------Tournaments-------") lstTables = lstTables + [x['f_table_name'] for x in tmpTables] ctx['tables'] = lstTables ctx['bots'] = Bot.objects.values("f_nick").distinct(); Tools.defaultContextData(request, ctx) return render_to_response('templates/chat/chat_board.html', ctx);
def botsGrid(request): ctx = Tools.defaultContextData(request, {'POST':request.POST}); return render_to_response("templates/home/bots_grid.html", ctx);
def bots(request): ctx = Tools.defaultContextData(request, {}); return render_to_response("templates/home/bots_tab.html", ctx);
def home(request): ctx = {} Tools.defaultContextData(request, ctx) return render_to_response('templates/home/home.html', ctx);
def runScheduler(request): """ Gets all configured/enabled scheduling intervals that falls within the current time. 1- For each table configured in each interval, check if the number of bots who can play is below the configured number and if yes pick up a bot that is not enabled from the list of allowed bots to play and enable it 2- if number of configured bots is bigger then disable a bot that is playing 3- Checks if there is a bot enabled that is not in the current interval and turn it off 4- disable the bots that finished within 30 min """ lstMsgs = [] def printMsg(msg): print msg lstMsgs.append(msg) print("Scheduler started at :%s" % (datetime.datetime.now())) lstSchedules = BotSchedule.objects.filter(f_enabled=1) now = datetime.datetime.now().time() expTime = datetime.datetime.now() - datetime.timedelta(minutes = 30) processedTable = {} lstTablesToStop = set() #list of tables that are in an enabled schedule but not in any interval lstTablesInSchedule = set() #list of tables that are in an enabled schedule and within interval for schedule in lstSchedules: printMsg("Checking schedule: %s from: %s to: %s" % (schedule.f_name, schedule.f_from_time, schedule.f_to_time) ) try: tFrom = datetime.time(int(schedule.f_from_time.split(":")[0]), int(schedule.f_from_time.split(":")[1])) tTo = datetime.time(int(schedule.f_to_time.split(":")[0]), int(schedule.f_to_time.split(":")[1])) tMidnight = tTo if tFrom > tTo: tMidnight = datetime.time(23, 59, 59) if (now >= tFrom and now <= tMidnight) or (now >= tMidnight and now <= tTo) : printMsg("\tProcessing matching schedule: %s from: %s to: %s" %(schedule.f_name, tFrom, tTo) ) lstTables = schedule.f_tables.split(",") for table in lstTables: table = table.strip().lower() if table in processedTable: printMsg("\t\tWARNING! table %s is configured in more than one schedule. Already Processed with %s, skipped schedule: %s " % (table, processedTable[table], schedule.f_name)) continue lstTablesInSchedule.add(table) #disable BotTable where the entry is enabled and bot is disabled in order not to interfere with the results BotTable.objects.filter(bot__f_enabled = 0, f_enabled = 1).update(f_enabled=0) lstActive = BotTable.objects.filter(f_table_name=table, f_enabled = 1, f_last_exit_time__lt = expTime) lstCanPlay = BotTable.objects.filter(f_table_name=table, f_enabled = 0, f_last_exit_time__lt = expTime) printMsg("\tNum of bots who are active on table: %s is %s vs. %s configured" % (table, len(lstActive), schedule.f_num_bots)) scheduledBots = [b.strip().lower() for b in schedule.f_nicks.split(",")] lstCanPlayNicks = [t.bot.f_nick.strip().lower() for t in lstCanPlay] lstBots = [b for b in scheduledBots if b.strip() in lstCanPlayNicks] #Enable a bot if len(lstActive) < schedule.f_num_bots: printMsg("\t\tNumber of bots who can plan on table: %s is less then configured: %s" % (table, schedule.f_num_bots)) if len(lstBots): #Retry few times in order to account for errors when someone enters a bot that is not assigned to the given table nbRetries = 5 choosenBot = None while choosenBot is None and nbRetries != 0 : choosenBot = lstBots[random.randint(0, len(lstBots) - 1)] tmp = BotTable.objects.filter(f_table_name=table, bot__f_nick = choosenBot) nbRetries -= 1 if len(tmp): choosenBot = tmp[0] choosenBot.f_enabled = 1 choosenBot.bot.f_enabled = 1 choosenBot.bot.save() choosenBot.save() printMsg("\t\tEnabled bot: %s to play on table: %s" % (choosenBot.bot.f_nick, table)) else: printMsg("\t\tWARNING! bot: %s is scheduled to play on %s but not configured in the bot tab" % (choosenBot, table) ) choosenBot = None else: printMsg("\t\tWARNING! not enough configured bots to play on table: %s" % (table)) elif len(lstActive) > schedule.f_num_bots: printMsg("\t\tNumber of bots who can plan on table: %s is more then configured: %s. Going to stop one" % (table, schedule.f_num_bots)) botToStop = lstActive[random.randint(0, len(lstActive)-1 )] botToStop.f_enabled = 0 botToStop.save() printMsg("\t\tStopped bot: %s " % (botToStop.bot.f_nick)) #Disable the bots that stopped playing within the last 30 min BotTable.objects.filter(f_table_name=table, f_enabled = 1, f_last_exit_time__gt = expTime).update(f_enabled=0) #Disable the fishes that aren't in this rule lstNotInThisRule = [b for b in lstActive if b.bot.f_nick.strip().lower() not in scheduledBots] if len(lstNotInThisRule): bot = lstNotInThisRule[random.randint(0, len(lstNotInThisRule)-1)] bot.f_enabled= 0 bot.save() printMsg("\t\tDisabled bot: %s from table: %s because not in current schedule: %s" % (bot.bot.f_nick, table, schedule.f_name)) processedTable[table] = "%s--> %s-%s" % (schedule.f_name, schedule.f_from_time, schedule.f_to_time) else: lstTablesToStop.update([t.strip() for t in schedule.f_tables.split(",") if t.strip() != ''] ) print "tbls: %s" % (schedule.f_tables,) except: printMsg("ERROR in rule: %s, wrong time [from:%s, to:%s]" % (schedule.f_name, schedule.f_from_time, schedule.f_to_time)) traceback.print_exc() lstTablesToStop = lstTablesToStop - lstTablesInSchedule printMsg("\nOut of schedule tables: %s" % (lstTablesToStop)) for table in lstTablesToStop: lstActive = BotTable.objects.filter(f_table_name=table, f_enabled = 1, bot__f_enabled = 1) if len(lstActive): tbl = lstActive[random.randint(0, len(lstActive)-1 )] tbl.f_enabled = 0 tbl.save() printMsg("\tStopping bot: %s from table: %s" % (tbl.bot.f_nick, tbl.f_table_name)) printMsg("\nSuccessfuly completed") ctx = Tools.defaultContextData(request, {'content': "\n".join(lstMsgs)}) return render_to_response("templates/scheduler/run.html", ctx);
def saveSchedule(request): rs = {} POST = request.POST rId = POST['id'] if(rId != ''): BotSchedule.objects.filter(id=rId).update(f_name = POST['name'], f_from_time = POST['from_time'], f_to_time = POST['to_time'] , f_enabled = POST['enabledStatus'], f_num_bots = Tools.toInt(POST['num_bots']), f_tables = POST['tables'] , f_nicks = POST['bots']) else: BotSchedule.objects.create(f_name = POST['name'], f_from_time = POST['from_time'], f_to_time = POST['to_time'] , f_enabled = POST['enabledStatus'], f_num_bots = Tools.toInt(POST['num_bots']), f_tables = POST['tables'] , f_nicks = POST['bots']) rs['success'] = True return HttpResponse(json.dumps(rs), mimetype="application/json")
def schedulerBoard(request): ctx = {} Tools.defaultContextData(request, ctx) return render_to_response('templates/scheduler/schedule_grid.html', ctx);