$LOAD_PATH << '.'
require 'Connector'
require 'Dictionary'
require 'Guesser'
require 'json'

#程序入口
class MainGame

	def initialize
		@guesser
		@connector
		@score = 0
		@SubmitThreshHold	 = 1200
		@gameEndFlag = false
	end

	def startGame
		startingGameMessage = Connector.startGame
		@guesser = Guesser.new(startingGameMessage)
		@connector = Connector.new(@guesser.getSessionId)
		puts "游戏开始：".concat(startingGameMessage.to_json)
	end

	def nextWord
		newWordMessage = @connector.nextWord
		@guesser.updateNewWordMessage(newWordMessage)	
		puts "获取单词：".concat(newWordMessage.to_json)
	end

	def guess
		guessCharacter = @guesser.guess
		guessFeedbackMessage = @connector.guessWord(guessCharacter)
		@guesser.updateGuessFeedbackMessage(guessFeedbackMessage)
		puts "猜测字母：".concat(guessCharacter)
		puts "猜测反馈：".concat(guessFeedbackMessage.to_json)
	end

	def getScore
		scoreMessage = @connector.getResult
		@score = scoreMessage["data"]["score"]
		puts "得分信息：".concat(scoreMessage.to_json)
	end

	def submitAtHighScore
		if @score>@SubmitThreshHold
			submitMessage = @connector.submitResult
			puts "信息已提交，提交信息：".concat(submitMessage.to_json)
			@gameEndFlag = true
		else
			puts "分数低于阈值，未提交"
		end
	end

	def game
		while !@gameEndFlag
			startGame
			begin
				nextWord
				begin
					guess
				end while !@guesser.isOneWordGuessOver
			end while !@guesser.isGuessingLastWord&&@guesser.isOneWordGuessOver 
			getScore
			submitAtHighScore	
		end
	end
end

mainGame = MainGame.new
mainGame.game