forked from tobymao/18xx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_auto_actions.rb
324 lines (280 loc) · 10.2 KB
/
migrate_auto_actions.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# frozen_string_literal: true
# rubocop:disable all
require_relative 'models'
Dir['./models/**/*.rb'].sort.each { |file| require file }
Sequel.extension :pg_json_ops
require_relative 'lib/engine'
$broken = {}
def switch_actions(actions, first, second)
first_idx = actions.index(first)
second_idx = actions.index(second)
id = second['id']
second['id'] = first['id']
first['id'] = id
actions[first_idx] = second
actions[second_idx] = first
return [first, second]
end
# Switches the position of two blocks of actions.
# If an array of actions `[abcdeFGHIjklMNopq]` is passed with ranges `6..9` and
# `13..14` then this will modify the array order to be `[abcdeMNjklFGHIopq]`.
# @param actions [Array] Array of actions.
# @param first_block [Range] ID range of the first block of actions.
# @param second_block [Range] ID range of the second block of actions.
def switch_action_blocks(actions, first_block, second_block)
raise RangeError if second_block.first <= first_block.last
raise RangeError if first_block.size.zero? || second_block.size.zero?
middle_block = (first_block.last + 1)..(second_block.first - 1)
start_idx = actions[first_block.first]['id']
reordered = actions[second_block] + actions[middle_block] + actions[first_block]
reordered.each_with_index { |action, i | action['id'] = start_idx + i }
actions[first_block.first..second_block.last] = reordered
end
# Moves an auto_actions array from one action to another. If both actions have
# auto_actions arrays then they will be switched. If neither have auto_actions
# then nothing is changed.
# @param actions [Array] Array of actions.
# @param from [Integer] Index of the action with the auto_actions.
# @param to [Integer] Index of the action to move the auto_actions to.
def move_auto_actions(actions, from, to)
from, to = [from, to].minmax # In case from > to
first = actions[from]
second = actions[to]
auto_actions1 = first['auto_actions']
auto_actions2 = second['auto_actions']
return if auto_actions1.nil? && auto_actions2.nil?
if auto_actions1.nil?
second.delete('auto_actions')
else
second['auto_actions'] = auto_actions1
end
if auto_actions2.nil?
first.delete('auto_actions')
else
first['auto_actions'] = auto_actions2
end
end
# If inserting/deleting actions, modify the given `actions` and return `nil`
#
# If editing existing actions, modify them on `actions` in place, and return an
# array containing the just the modified actions (so the modified actions will
# be in both the originally given `actions` and in the returned array)
def repair(game, original_actions, actions, broken_action, data, pry_db: false)
optionalish_actions = %w[message buy_company]
broken_action_idx = actions.index(broken_action)
action = broken_action['original_id'] || broken_action['id']
step = game.active_step
prev_actions = actions[0..broken_action_idx - 1]
prev_action = prev_actions[prev_actions.rindex { |a| !optionalish_actions.include?(a['type']) }]
next_actions = actions[broken_action_idx + 1..]
next_action = next_actions.find { |a| !optionalish_actions.include?(a['type']) }
current_entity = step.current_entity
entity_id = broken_action['entity']
entity = game.corporation_by_id(entity_id) ||
game.company_by_id(entity_id) ||
game.player_by_id(entity_id)
step_actions = step.actions(current_entity)
################
# BEGIN REPAIR #
################
# When a new migration is needed for something more than adding/removing pass
# actions, delete blocks here for completed migrations and add a new commented
# block; see the history of this file for examples of previous migrations.
if pry_db
require 'pry-byebug'
binding.pry
end
if step.class.to_s.include?("Destination")
broken_action['auto_actions'] = broken_action['auto_actions'] || []
broken_action['auto_actions'] << Engine::Action::Pass.new(current_entity).to_h
puts ' patched: Inserted auto_action pass'
return
end
pass = Engine::Action::Pass.new(current_entity)
pass.user = pass.entity.player.id
actions.insert(broken_action_idx, pass.to_h)
puts ' patched: Add pass'
return
################
# END REPAIR #
################
end
def attempt_repair(actions, debug, data, pry_db: false)
puts "attempt repair"
repairs = []
rewritten = false
ever_repaired = false
iteration = 0
loop do
game = yield
game.instance_variable_set(:@loading, true)
# Locate the break
repaired = false
filtered_actions, _active_undos = game.class.filtered_actions(actions)
filtered_actions.compact!
filtered_actions.each.with_index do |action, _index|
action = action.copy(game) if action.is_a?(Engine::Action::Base)
begin
game.process_action(action).maybe_raise!
raise Exception if game.active_step.class.to_s.include?("Destination")
rescue Exception => e
puts e.backtrace if debug
iteration += 1
puts " iteration #{iteration}; action #{action['id']}; #{game.active_step.type} step; #{action['entity']}, #{action['type']}"
raise Exception, "Stuck in infinite loop?" if iteration > 1000
ever_repaired = true
inplace_actions = repair(game, actions, filtered_actions, action, data, pry_db: pry_db)
repaired = true
if inplace_actions
repairs += inplace_actions
else
rewritten = true
# Added or moved actions... destroy undo states and renumber.
filtered_actions.each_with_index do |a, idx|
a['original_id'] = a['id'] unless a.include?('original_id')
a['id'] = idx + 1
end
actions = filtered_actions
end
break
end
end
break unless repaired
end
repairs = nil if rewritten
return [actions, repairs] if ever_repaired
end
def migrate_data(data, debug=true)
begin
data['actions'], repairs = attempt_repair(data['actions'], debug, data) do
Engine::Game.load(data, actions: []).maybe_raise!
end
rescue Exception => e
puts 'Failed to fix :(', e
return data
end
# running a migration on a game without issues returns nil actions
return unless data['actions']
data
end
# This doesn't write to the database
def migrate_db_actions_in_mem(data, debug=false)
original_actions = data.actions.map(&:to_h)
begin
actions, repairs = attempt_repair(original_actions, debug, data) do
Engine::Game.load(data, actions: []).maybe_raise!
end
puts repairs
return actions || original_actions
rescue Exception => e
puts 'Something went wrong', e
#raise e
end
return original_actions
end
def migrate_db_actions(data, pin=nil, dry_run=false, debug=false, pry_db: false, require_pin: false)
raise Exception, "pin is not valid" if !pin && require_pin
puts "\nGame #{data.id}"
original_actions = data.actions.map(&:to_h)
begin
actions, repairs = attempt_repair(original_actions, debug, data, pry_db: pry_db) do
Engine::Game.load(data, actions: []).maybe_raise!
end
if actions && !dry_run
if repairs
puts ' saving changed actions'
repairs.each do |action|
# Find the action index
idx = actions.index(action)
data.actions[idx].action = action
data.actions[idx].save
end
else # Full rewrite.
puts ' game fixed, rewriting all actions'
DB.transaction do
Action.where(game: data).delete
game = Engine::Game.load(data, actions: []).maybe_raise!
# Set back to loading
game.instance_variable_set(:@loading, true)
actions.each do |action|
game.process_action(action)
game.maybe_raise!
Action.create(
game: data,
user: action.key?('user') ? User[action['user']] : data.user,
action_id: game.actions.last.id,
action: action,
)
end
end
end
end
return actions || original_actions
rescue Exception => e
$broken[data.id]=e
puts e.backtrace if debug
puts " #{e}"
if !dry_run
if pin == :delete || pin == :archive
puts " Archiving #{data.id}"
data.archive!
else
if pin
puts " Pinning #{data.id} to #{pin}"
data.settings['pin'] = pin
data.save
else
puts " Skipping pin (none given)"
end
end
else
puts " Needs pinning #{data.id} to #{pin}"
end
end
return original_actions
end
def migrate_json(filename, debug=true)
puts "Loading #{filename} for migration"
data = migrate_data(JSON.parse(File.read(filename)), debug)
if data
File.write(filename, JSON.pretty_generate(data))
else
puts 'Nothing to do, game works'
end
end
def db_to_json(id, filename)
game = Game[id]
json = game.to_h(include_actions: true)
File.write(filename, JSON.pretty_generate(json))
end
def migrate_db_to_json(id, filename)
game = Game[id]
json = game.to_h(include_actions: true)
json['actions'] = migrate_db_actions(game)
File.write(filename, JSON.pretty_generate(json))
end
# Pass pin=:archive to archive failed games
def migrate_title(title, pin, dry_run=false, debug = false, require_pin: false)
DB[:games].order(:id).where(Sequel.pg_jsonb_op(:settings).has_key?('pin') => false, status: %w[active finished], title: title).select(:id).paged_each(rows_per_fetch: 1) do |game|
games = Game.eager(:user, :players, :actions).where(id: [game[:id]]).all
games.each {|data|
migrate_db_actions(data, pin, dry_run, debug, require_pin: require_pin)
}
end
end
def migrate_all(pin=nil, dry_run=false, debug = false, pry_db: false, game_ids: nil, require_pin: false, status: %w[active finished])
# can uncomment this for less noise in dev; don't commit it uncommented as
# that breaks the script in prod
# DB.loggers.first.level = Logger::FATAL
where_args = {
Sequel.pg_jsonb_op(:settings).has_key?('pin') => false,
status: status,
}
where_args[:id] = game_ids if game_ids
DB[:games].order(:id).where(**where_args).select(:id).paged_each(rows_per_fetch: 1) do |game|
games = Game.eager(:user, :players, :actions).where(id: [game[:id]]).all
games.each {|data|
migrate_db_actions(data, pin, dry_run, debug, pry_db: pry_db, require_pin: require_pin)
}
end
end