Skip to content

Commit

Permalink
ClashKing v4
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicTheDev committed Jan 29, 2024
1 parent 861f806 commit c2ee6bf
Show file tree
Hide file tree
Showing 7 changed files with 541 additions and 546 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ emoji==1.6.3
expiring-dict==1.1.0
ipython==8.13.0
kafka-python==2.0.2
loguru==0.7.2
matplotlib==3.7.1
motor==3.3.2
msgspec==0.15.1
Expand Down
20 changes: 20 additions & 0 deletions tracking/player/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from os import getenv
from dotenv import load_dotenv
from dataclasses import dataclass
load_dotenv()


@dataclass(frozen=True, slots=True)
class Config:
min_coc_email = 1
max_coc_email = 12
coc_password = getenv("COC_PASSWORD")

secondary_loop_change = 15
tertiary_loop_change = 150
max_tag_split = 50_000

static_mongodb = getenv("STATIC_MONGODB")
stats_mongodb = getenv("STATS_MONGODB")
redis_ip = getenv("REDIS_IP")
redis_pw = getenv("REDIS_PW")
3 changes: 0 additions & 3 deletions tracking/player/constants.py

This file was deleted.

22 changes: 22 additions & 0 deletions tracking/player/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import motor.motor_asyncio
import asyncio

from collections import deque
from redis import asyncio as redis
from .config import Config
from .tracking import main
from ..utils import create_keys


if __name__ == '__main__':
config = Config()
loop = asyncio.get_event_loop()

stats_mongo_client = motor.motor_asyncio.AsyncIOMotorClient(config.stats_mongodb)
static_mongo_client = motor.motor_asyncio.AsyncIOMotorClient(config.static_mongodb)

redis_host = redis.Redis(host=config.redis_ip, port=6379, db=0, password=config.redis_pw, decode_responses=False, max_connections=2500)
keys = create_keys([f"apiclashofclans+test{x}@gmail.com" for x in range(config.min_coc_email, config.max_coc_email + 1)], [config.coc_password] * config.max_coc_email)
keys = deque(keys)
loop.create_task(main(keys=keys, cache=redis_host, stats_mongo_client=stats_mongo_client, static_mongo_client=static_mongo_client))
loop.run_forever()
954 changes: 412 additions & 542 deletions tracking/player/tracking.py

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions tracking/player/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from msgspec import Struct
import pendulum as pend
from datetime import timedelta
import coc

def gen_raid_date():
now = pend.now(tz=pend.UTC)
current_dayofweek = now.weekday()
if (current_dayofweek == 4 and now.hour >= 7) or (current_dayofweek == 5) or (current_dayofweek == 6) or (
current_dayofweek == 0 and now.hour < 7):
if current_dayofweek == 0:
current_dayofweek = 7
fallback = current_dayofweek - 4
raidDate = (now - timedelta(fallback)).date()
return str(raidDate)
else:
forward = 4 - current_dayofweek
raidDate = (now + timedelta(forward)).date()
return str(raidDate)

def gen_season_date():
end = coc.utils.get_season_end().replace(tzinfo=pend.UTC).date()
month = end.month
if month <= 9:
month = f"0{month}"
return f"{end.year}-{month}"

def gen_legend_date():
now = pend.now(tz=pend.UTC)
hour = now.hour
if hour < 5:
date = (now - timedelta(1)).date()
else:
date = now.date()
return str(date)

def gen_games_season():
now = pend.now(tz=pend.UTC)
month = now.month
if month <= 9:
month = f"0{month}"
return f"{now.year}-{month}"

def get_player_changes(previous_response: dict, response: dict):
new_json = {}
fields_to_update = []
ok_achievements = {"Gold Grab", "Elixir Escapade", "Heroic Heist", "Games Champion", "Aggressive Capitalism",
"Well Seasoned", "Nice and Tidy", "War League Legend", "Wall Buster"}
for key, item in response.items():
old_item = previous_response.get(key)
if old_item != item:
fields_to_update.append(key)
not_ok_fields = {"labels", "legendStatistics", "playerHouse", "versusBattleWinCount"}
if key in not_ok_fields:
continue
if old_item != item:
if isinstance(item, list):
for count, spot in enumerate(item):
spot_name = spot["name"]
if key == "achievements" and spot_name not in ok_achievements:
continue
old_ = next((item for item in old_item if item["name"] == spot_name), None)
if old_ != spot:
if key == "achievements":
if old_ is not None:
new_json[(key, spot_name.replace(".", ""))] = (old_["value"], spot["value"])
else:
new_json[(key, spot_name.replace(".", ""))] = (None, spot["value"])
else:
if old_ is not None:
new_json[(key, spot_name.replace(".", ""))] = (old_["level"], spot["level"])
else:
new_json[(key, spot_name.replace(".", ""))] = (None, spot["level"])
else:
if key == "clan":
new_json[(key, key)] = (None, {"tag": item["tag"], "name": item["name"]})
elif key == "league":
new_json[(key, key)] = (None, {"tag": item["id"], "name": item["name"]})
else:
new_json[(key, key)] = (old_item, item)

return (new_json, fields_to_update)


class Player(Struct):
tag: str
1 change: 0 additions & 1 deletion utility/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from classes.player import MyCustomPlayer
from typing import List
import coc
from utility.clash.other import gen_season_date
from utility.constants import BOARD_TYPES
from typing import Union

Expand Down

0 comments on commit c2ee6bf

Please sign in to comment.