forked from alexraison/Magic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automatedPairings.py
97 lines (71 loc) · 2.47 KB
/
automatedPairings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from sqlalchemy import func, and_, extract, case
from sqlalchemy.sql import text
from datetime import date, datetime
from app.models import Player, Tournament, Match, Set, Statistics, TournamentType, Entity, EntityParticipant, MatchParticipant
import smtplib
import json
import collections
from app.post import slack_bot
from app.slackApiUser import User
from app.slackApiChannel import Channel
from app.pairings import postPairings
from app.api import getPlayerNamesFromSlackUsers
import schedule
import time
import os
from app import app
def scheduledPairings():
with open('app/pairings.settings') as config:
settings = json.loads(config.read())
schedule.every().day.at(str(os.environ['PAIRINGS_TIME'])).do(automatePairings)
while True:
schedule.run_pending()
time.sleep(60) # wait one minute
def automatePairings():
with open('app/pairings.settings') as config:
settings = json.loads(config.read())
token = str(os.environ['SLACK_TOKEN'])
pairingsChannel = Channel(token, os.environ['PAIRINGS_CHANNEL_ID'])
pairingsMessage = pairingsChannel.getPairingsMessage()
pairingsMessageReactions = []
if pairingsMessage:
pairingsMessageReactions = pairingsMessage.get('reactions')
playList = []
draftList = []
for reaction in pairingsMessageReactions:
if reaction['name'] == 'hand':
for userId in reaction['users']:
user = User(token, userId)
playList.append(user.getUserName())
if reaction['name'] =='metal':
for userId in reaction['users']:
user = User(token, userId)
draftList.append(user.getUserName())
if len(draftList) > 5:
for player in draftList:
if player in playList:
playList.remove(player)
drafters = getPlayerNamesFromSlackUsers(draftList)
postDraftingMessage(drafters)
if len(playList) > 1:
players = getPlayerNamesFromSlackUsers(playList)
postPairings(players)
def postDraftingMessage(playerList):
with open('app/pairings.settings') as config:
settings = json.loads(config.read())
if app.config['TESTING'] == True:
channel = os.environ['TESTING_CHANNEL_ID']
else:
channel = os.environ['CHANNEL_ID']
pairings_bot = slack_bot(channel, settings['bot_name'], settings['bot_icon'])
if playerList:
message = ''
for player in playerList:
message += player + '\n'
attachment = {
'title': "Enough people have raised their horned hands to the God-Pairaoh, and he has answered! Praise Him!",
'text': message,
'color': "#7CD197",
'mrkdwn_in': ["text"]
}
pairings_bot.post_attachment(attachment)