-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_server.rb
executable file
·118 lines (97 loc) · 3.23 KB
/
game_server.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
# typed: false
# Try running with:
# $ RAILS_ENV=development ruby game_server.rb
require 'date'
require "./config/environment"
require "./app/helpers/populate_db_helper"
require "./app/helpers/spring_clean_db_helper"
require "./app/helpers/push_notification_helper"
daily_tasks_last = Date.today
DAYS_REST_BETWEEN_SEASONS = 1
five_minutely_tasks_last = nil
PushNotificationHelper.test_config()
def daily_task
Rails.logger.info "executing daily tasks..."
if SeasonHelper.is_end_of_season? then
if Date.today - SeasonHelper.last_game_date >= DAYS_REST_BETWEEN_SEASONS then
SeasonHelper.handle_end_of_season
SpringCleanDbHelper.go
end
end
League.is_cup.each{|cup|
CupHelper.update_cup(cup, SeasonHelper.current_season)
}
PlayerHelper.daily_develop_youth_players
PlayerHelper.daily_cure_injury
PlayerHelper.daily_reduce_suspension
PlayerHelper.daily_maybe_change_player_form
# AI update
Team.all.each do |t|
AiManagerHelper.daily_task(t)
end
end
def five_minutely_task
Rails.logger.info "executing five-minutely tasks..."
TransferMarketHelper.update_transfer_market
PlayerHelper.spawn_injuries
# AI update
Team.all.each do |t|
AiManagerHelper.five_minutely_task(t)
end
end
def notify_game_starting(team_id, is_home, opponent_name)
puts "Fcm notification to manager of team id #{team_id}"
PushNotificationHelper.send_to_manager_of_team_id(
team_id,
"Your #{is_home ? 'home' : 'away'} game against #{opponent_name} starts in 5 minutes!",
'',
'https://myfitba.club/'
)
end
def notify_games_starting(now)
Game.where.not(status: 'Played')
.where.not(notified: true)
.where('start < ?', now + (60 * 5))
.each do |g|
notify_game_starting(g.home_team_id, true, g.away_team.name)
notify_game_starting(g.away_team_id, false, g.home_team.name)
g.update(notified: true)
end
end
def per_second_task(now)
# XXX turned off game start notifications, because push notifications are
# so crap and seem to limit to 1 per day, and the result notifications are more useful
# given push notifications almost never arrive on time.
#notify_games_starting(now)
games = Game.where.not(status: 'Played').where('start < ?', now).all
if games.size > 0
Rails.logger.info "#{games.size} games to simulate."
end
games.each do |game|
game.simulate(now)
pens = if game.home_penalties > 0 or game.away_penalties > 0 then " Penalties: #{game.home_penalties}:#{game.away_penalties}" else "" end
Rails.logger.info "#{game.league.kind} game between #{game.home_team.name} and #{game.away_team.name}: #{game.status} (#{game.home_goals}:#{game.away_goals}) #{pens}"
end
end
if __FILE__ == $0 then
Rails.logger.info "Fitba server up!"
if ARGV[0] == "season" then
puts "Simulating whole season........................"
daily_task
per_second_task(Time.now + 40*24*3600)
end
while sleep 1 do
now = Time.now
today = Date.today
if daily_tasks_last != today then
daily_task()
daily_tasks_last = today
end
if five_minutely_tasks_last == nil or now - five_minutely_tasks_last > 5*60 then
five_minutely_task()
five_minutely_tasks_last = now
end
per_second_task(Time.now)
end
end