Coverage for src/season_balances_info.py: 0%
105 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-03 19:06 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-03 19:06 +0000
1import numpy as np
2import pandas as pd
3from dateutil import parser
5from src.configuration import store
6from src.utils import store_util, progress_util, spl_util
9def update_balances_store(account_name, current_season_id):
10 if not (store.season_sps.empty or store.season_sps.loc[store.season_sps.player == account_name].empty):
11 start_from_season = store.season_sps.loc[store.season_sps.player == account_name].season_id.max() + 1
13 if start_from_season == current_season_id:
14 progress_util.update_season_msg("No new season balances to process for: " + str(account_name))
15 return
17 season_array = np.arange(start_from_season, current_season_id)
18 start_date, end_date = get_start_end_time_season(start_from_season)
19 start_date = parser.parse(start_date)
20 dec_df = pd.DataFrame(
21 spl_util.get_balance_history_for_token(
22 account_name,
23 start_date=start_date,
24 token="DEC",
25 )
26 )
27 unclaimed_sps_df = pd.DataFrame(
28 spl_util.get_unclaimed_sps_balance_history_for_token(
29 account_name,
30 start_date=start_date,
31 )
32 )
33 sps_df = pd.DataFrame(
34 spl_util.get_balance_history_for_token(
35 account_name,
36 start_date=start_date,
37 token="SPS",
38 )
39 )
40 merits_df = pd.DataFrame(
41 spl_util.get_balance_history_for_token(
42 account_name,
43 start_date=start_date,
44 token="MERITS",
45 )
46 )
47 credits_df = pd.DataFrame(
48 spl_util.get_balance_history_for_token(
49 account_name,
50 start_date=start_date,
51 token="CREDITS",
52 )
53 )
54 vouchers_df = pd.DataFrame(
55 spl_util.get_balance_history_for_token(
56 account_name,
57 start_date=start_date,
58 token="VOUCHER",
59 )
60 )
61 glint_df = pd.DataFrame(
62 spl_util.get_balance_history_for_token(
63 account_name,
64 start_date=start_date,
65 token="GLINT",
66 )
67 )
69 else:
70 dec_df = pd.DataFrame(
71 spl_util.get_balance_history_for_token(
72 account_name,
73 token="DEC",
74 )
75 )
77 unclaimed_sps_df = pd.DataFrame(
78 spl_util.get_unclaimed_sps_balance_history_for_token(
79 account_name,
80 )
81 )
82 sps_df = pd.DataFrame(
83 spl_util.get_balance_history_for_token(
84 account_name,
85 token="SPS",
86 )
87 )
88 merits_df = pd.DataFrame(
89 spl_util.get_balance_history_for_token(
90 account_name,
91 token="MERITS",
92 )
93 )
94 credits_df = pd.DataFrame(
95 spl_util.get_balance_history_for_token(
96 account_name,
97 token="CREDITS",
98 )
99 )
100 vouchers_df = pd.DataFrame(
101 spl_util.get_balance_history_for_token(
102 account_name,
103 token="VOUCHER",
104 )
105 )
106 glint_df = pd.DataFrame(
107 spl_util.get_balance_history_for_token(
108 account_name,
109 token="GLINT",
110 )
111 )
113 # Concatenate the dataframes
114 combined_df = pd.concat([dec_df, unclaimed_sps_df, sps_df, merits_df, credits_df, vouchers_df, glint_df],
115 ignore_index=True)
117 first_season = determine_first_season_id_played(combined_df)
118 season_array = np.arange(first_season, current_season_id)
120 if len(season_array) > 0:
121 progress_util.update_season_msg("Start processing DEC")
122 store.season_dec = process_season_balances(dec_df, store.season_dec.copy(), account_name, season_array)
123 progress_util.update_season_msg("Start processing UNCLAIMED SPS")
124 store.season_unclaimed_sps = process_season_balances(unclaimed_sps_df, store.season_unclaimed_sps.copy(),
125 account_name, season_array, unclaimed_sps=True)
126 progress_util.update_season_msg("Start processing SPS")
127 store.season_sps = process_season_balances(sps_df, store.season_sps.copy(), account_name, season_array)
128 progress_util.update_season_msg("Start processing MERITS")
129 store.season_merits = process_season_balances(merits_df, store.season_merits.copy(), account_name,
130 season_array)
131 progress_util.update_season_msg("Start processing VOUCHERS")
132 store.season_vouchers = process_season_balances(vouchers_df, store.season_vouchers.copy(), account_name,
133 season_array)
134 progress_util.update_season_msg("Start processing GLINT")
135 store.season_glint = process_season_balances(glint_df, store.season_glint.copy(), account_name,
136 season_array)
137 progress_util.update_season_msg("Start processing CREDITS")
138 store.season_credits = process_season_balances(credits_df, store.season_credits.copy(), account_name,
139 season_array)
140 progress_util.update_season_msg("Get balances for account (" + str(account_name) + ") Done")
141 store_util.save_stores()
144def process_season_rewards(balance_df, store_copy, account_name, season_id, search_type,
145 unclaimed_sps=False, unclaimed_sps_rewards_share=False):
146 # when season rewards are found these always belong to a previous season
147 # timeframe of the claim rewards is always in the new season
149 # if the balance of the current season is greater than 0 update the previous season
150 column_name = search_type
151 if unclaimed_sps_rewards_share:
152 column_name = search_type + "_fee"
153 start_date, end_date = get_start_end_time_season(season_id)
154 balance_mask = get_balance_mask(account_name,
155 balance_df,
156 start_date,
157 end_date,
158 search_type,
159 unclaimed_sps,
160 unclaimed_sps_rewards_share)
161 amount = balance_df.loc[balance_mask].amount.sum()
162 if amount > 0:
163 store_copy.loc[(store_copy.season_id == season_id - 1)
164 & (store_copy.player == account_name), column_name] = amount
166 # if for the new season already a claim is done also capture this season rewards to the current
167 start_date, end_date = get_start_end_time_season(season_id + 1)
168 balance_mask = get_balance_mask(account_name,
169 balance_df,
170 start_date,
171 end_date,
172 search_type,
173 unclaimed_sps,
174 unclaimed_sps_rewards_share)
175 store_copy.loc[(store_copy.season_id == season_id)
176 & (store_copy.player == account_name), column_name] = balance_df.loc[balance_mask].amount.sum()
178 return store_copy
181def get_balance_mask(account_name, balance_df, start_date, end_date, search_type,
182 unclaimed_sps=False, unclaimed_sps_reward_share=False):
183 if unclaimed_sps_reward_share:
184 return (balance_df.created_date > start_date) \
185 & (balance_df.created_date <= end_date) \
186 & (balance_df.type == search_type) \
187 & (balance_df.amount < 0.0) \
188 & (balance_df.to_player != account_name) \
189 & (balance_df.to_player.notnull())
190 elif unclaimed_sps:
191 return (balance_df.created_date > start_date) \
192 & (balance_df.created_date <= end_date) \
193 & (balance_df.type == search_type) \
194 & (balance_df.amount > 0.0)
195 else:
196 return (balance_df.created_date > start_date) & \
197 (balance_df.created_date <= end_date) & \
198 (balance_df.type == search_type)
201def process_season_balances(balance_df, store_copy, account_name, season_array, unclaimed_sps=False):
202 if not balance_df.empty:
203 for season_id in season_array:
204 store_copy = add_season_id(account_name, season_id, store_copy)
206 start_date, end_date = get_start_end_time_season(season_id)
208 balance_df.created_date = pd.to_datetime(balance_df.created_date)
209 balance_df.amount = pd.to_numeric(balance_df.amount)
211 for search_type in balance_df['type'].unique().tolist():
212 progress_util.update_season_msg("Processing season '" + str(season_id) +
213 "' for '" + str(account_name) +
214 "' type: " + str(search_type))
216 # special treatment for season_rewards they are in different timeframe
217 # for unclaimed sps season_rewards are season called
218 if search_type == 'season_rewards' or (unclaimed_sps and search_type == 'season'):
219 store_copy = process_season_rewards(balance_df, store_copy, account_name, season_id, search_type,
220 unclaimed_sps)
221 # For unclaimed SPS season process it twice one for the rewards and one for shared reward/fees
222 if search_type == 'season':
223 store_copy = process_season_rewards(balance_df, store_copy, account_name, season_id,
224 search_type,
225 unclaimed_sps, True)
226 else:
227 balance_mask = get_balance_mask(account_name,
228 balance_df,
229 start_date,
230 end_date,
231 search_type,
232 unclaimed_sps)
234 if search_type == 'market_purchase':
235 store_copy.loc[(store_copy.season_id == season_id)
236 & (store_copy.player == account_name),
237 'sell_' + search_type] = balance_df.loc[
238 balance_mask & (pd.to_numeric(balance_df.amount) > 0)].amount.sum()
239 store_copy.loc[(store_copy.season_id == season_id)
240 & (store_copy.player == account_name),
241 'buy_' + search_type] = balance_df.loc[
242 balance_mask & (pd.to_numeric(balance_df.amount) < 0)].amount.sum()
243 elif search_type == 'rental_payment':
244 store_copy.loc[(store_copy.season_id == season_id)
245 & (store_copy.player == account_name),
246 'earn_' + search_type] = balance_df.loc[
247 balance_mask & (pd.to_numeric(balance_df.amount) > 0)].amount.sum()
248 store_copy.loc[(store_copy.season_id == season_id)
249 & (store_copy.player == account_name),
250 'cost_' + search_type] = balance_df.loc[
251 balance_mask & (pd.to_numeric(balance_df.amount) < 0)].amount.sum()
253 else:
254 store_copy.loc[(store_copy.season_id == season_id)
255 & (store_copy.player == account_name), search_type] = balance_df.loc[
256 balance_mask].amount.sum()
258 # For unclaimed SPS process it twice one for the rewards and one for shared reward/fees
259 if unclaimed_sps:
260 balance_mask = get_balance_mask(account_name,
261 balance_df,
262 start_date,
263 end_date,
264 search_type,
265 unclaimed_sps, True)
266 store_copy.loc[(store_copy.season_id == season_id)
267 & (store_copy.player == account_name), search_type + "_fee"] = balance_df.loc[
268 balance_mask].amount.sum()
270 return store_copy
273def add_season_id(account_name, season_id, store_copy):
274 # if season id is not in the table yet first add it
275 if 'season' not in store_copy.columns.tolist() or \
276 store_copy.loc[(store_copy.season == season_id) & (store_copy.player == account_name)].empty:
277 store_copy = pd.concat([store_copy,
278 pd.DataFrame({'season_id': season_id,
279 'player': account_name}, index=[0])], ignore_index=True)
280 return store_copy
283def determine_first_season_id_played(df):
284 first_earned_date_str = df.created_date.sort_values().dropna().values[0]
285 first_earned_date = parser.parse(first_earned_date_str)
287 season_end_times = store.season_end_dates
288 # determine which was the first season earning start
289 for index, season_end_time in season_end_times.iterrows():
290 # for season_end_time in season_end_times:
291 if first_earned_date <= parser.parse(season_end_time['end_date']):
292 return season_end_time['id']
295def get_start_end_time_season(season_id):
296 return store.season_end_dates.loc[(store.season_end_dates.id == season_id - 1)]['end_date'].values[0], \
297 store.season_end_dates.loc[(store.season_end_dates.id == season_id)]['end_date'].values[0]