summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-09-03 17:24:21 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-09-03 17:24:21 +0200
commita503d93dbc45062abaa27f8f5d394001dc2e25e2 (patch)
treea51c067f1a68bb5d1c71f5b1847c810ffb64af00
parent75b0668650af1111a7de4b7420d2ae928595a23b (diff)
downloadcolonial-twilight-a503d93dbc45062abaa27f8f5d394001dc2e25e2.zip
colonial-twilight-a503d93dbc45062abaa27f8f5d394001dc2e25e2.tar.gz
Game : quick and dirty turn and actions save system
-rw-r--r--lib/colonial_twilight/board.rb19
-rw-r--r--lib/colonial_twilight/fln_bot.rb2
-rw-r--r--lib/colonial_twilight/game.rb41
-rw-r--r--lib/colonial_twilight/player.rb1
4 files changed, 41 insertions, 22 deletions
diff --git a/lib/colonial_twilight/board.rb b/lib/colonial_twilight/board.rb
index c539dd2..f877f84 100644
--- a/lib/colonial_twilight/board.rb
+++ b/lib/colonial_twilight/board.rb
@@ -1,8 +1,6 @@
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
-require 'json'
-
module ColonialTwilight
class Forces
@@ -43,7 +41,8 @@ module ColonialTwilight
end unless rm.nil?
end
- def to_s
+ def to_s; inspect end
+ def inspect
"
#{gov_bases} GOV bases
#{french_troops} french troops
@@ -187,7 +186,8 @@ module ColonialTwilight
@resettled = false
end
- def to_s
+ def to_s; @name end
+ def inspect
"#@descr #@terrain
control : #{@forces.control}
alignment : #@alignment
@@ -385,17 +385,6 @@ module ColonialTwilight
h
end
- def to_json
- # JSON.pretty_generate(data)
- JSON.generate(data)
- end
-
- def save
- File.open('save.json','w') do |f|
- f.write(JSON.generate(data))
- end
- end
-
def load scenario
case scenario
when :short; short
diff --git a/lib/colonial_twilight/fln_bot.rb b/lib/colonial_twilight/fln_bot.rb
index 3c00b26..4b14400 100644
--- a/lib/colonial_twilight/fln_bot.rb
+++ b/lib/colonial_twilight/fln_bot.rb
@@ -613,7 +613,7 @@ module ColonialTwilight
@expended_resources += cost unless h.has_key? :already_expended # _reserve_agitate
h[:resources] = {:cost=>cost, :value=>@board.fln_resources}
h[:controls] = h[:controls].inject({}){|ch,(k,v)| ch[k] = [v, k.control] if v != k.control; ch}
- @ui.show_player_action self, h
+ @game.action_done self, h
true
end
diff --git a/lib/colonial_twilight/game.rb b/lib/colonial_twilight/game.rb
index 6578eb4..75c4534 100644
--- a/lib/colonial_twilight/game.rb
+++ b/lib/colonial_twilight/game.rb
@@ -1,6 +1,8 @@
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
+require 'json'
+
require 'colonial_twilight/board'
require 'colonial_twilight/cards'
require 'colonial_twilight/player'
@@ -64,7 +66,7 @@ module ColonialTwilight
@scenario = s
@board.load [:short, :medium, :long][s]
@max_card = 71
- @turn = 1
+ @turn = 0
@cards = []
@actions = []
@players = [FLNBot.new(self, :FLN), GOVPlayer.new(self, :GOV)]
@@ -74,28 +76,30 @@ module ColonialTwilight
def current_card; @cards[-1] end
def play
+ save_board 0
while true
+ @turn += 1
ui.turn_start @turn, *@players
c = ui.pull_card @max_card
c = 1 # FIXME
@cards << @deck.pull(c)
ui.show_card @cards[-1]
+ File.open("actions-#{@turn}.json",'w') { |f| f << JSON.generate([]) }
ui.continue? @players[0].instance_of? FLNBot
ui.player @players[0], true
@actions[0] = @players[0].play possible_actions
@ui.adjust_track @board.compute_victory_points
+ save_board 0
ui.continue? @players[1].instance_of? FLNBot
ui.player @players[1], false
@actions[1] = @players[1].play possible_actions @actions[0]
@ui.adjust_track @board.compute_victory_points
+ save_board 1
@cards.shift if @cards.length > 2
- @turn += 1
- if eligibility_swap?
- @players[0], @players[1] = @players[1], @players[0]
- end
+ @players[0], @players[1] = @players[1], @players[0] if eligibility_swap?
@actions.clear
end
end
@@ -104,6 +108,33 @@ module ColonialTwilight
Game.swap_actions.include? @actions[0]
end
+ def save_board phase
+ json = JSON.generate @board.data
+ File.open("turn-#{@turn}-#{phase}.json",'w') { |f| f << json }
+ end
+
+ def action_done player, action
+ data = nil
+ File.open("actions-#{@turn}.json",'r') { |f| data = JSON.load f }
+ data << action
+
+ json = JSON.generate data
+ File.open("actions-#{@turn}.json",'w') { |f| f << json }
+ @ui.show_player_action player, action
+ end
+
+ end
+
+ class Sector
+ def to_json *args
+ name.to_json args
+ end
+ end
+
+ class Box
+ def to_json *args
+ name.to_json args
+ end
end
end
diff --git a/lib/colonial_twilight/player.rb b/lib/colonial_twilight/player.rb
index f8ed62c..123475b 100644
--- a/lib/colonial_twilight/player.rb
+++ b/lib/colonial_twilight/player.rb
@@ -10,7 +10,6 @@ module ColonialTwilight
def initialize game, faction
@game = game
@board = game.board
- @ui = game.ui
@faction = faction
@debug = game.options.debug_bot
@possible_actions = nil