﻿/*--
标题：斗地主控件
设计：王集鹄
博客：http://blog.csdn.net/zswang
日期：2009年4月19日
--*/

if (!Common.scripts["Landlord"]) {
	loadCss("/Scripts/Landlord.css");
	Common.scripts["Landlord"] = true;
}

Landlord.sortChars = "pPcCqQdDrReEsSfFtTgGuUhHvViIwWjJxXkKyYlLzZmMnNaAoObB01";

function Landlord(parent, button_parent) {
	if (!parent) parent = document.body;
	if (!button_parent) button_parent = parent;
	var self = this;
	this.documentTitle = document.title;
	this.parent = parent;
	this.button_parent = button_parent;
	this.channel = {};
	this.width = 555;
	this.height = 515;
	this.currPlayer = -1; // 当前玩家
	this.echoPlace = 0; // 操作方位
	this.landlordPlace = -1; // 地主
	this.currPlace = 0; // 当前方位
	this.state = "waiting"; // 游戏状态
	this.sortStyle = 0;
	this.point = 0;
	this.bomb = 0; // 炸弹数
	this.hint = [];

	this.div_desktop = document.createElement("div");
	this.div_desktop.className = "div_desktop";
	this.div_desktop.style.width = this.width + "px";
	this.div_desktop.style.height = this.height + "px";
	this.div_desktop.oncontextmenu = function() {
		if (self.currPlayer != self.echoPlace) return;
		if (!self.players[self.currPlayer].hand.getSelection()) return false;
		if (typeof self.onaction == "function")
			self.onaction("fetch", self.players[self.currPlayer].hand.getSelection());
		return false;
	}
	this.parent.appendChild(this.div_desktop);
	
	this.button_start = document.createElement("input");
	this.button_start.type = "button";
	this.button_start.value = "我要玩斗地主";
	this.button_start.onclick = function() {
		publics.counter('click_start', location);
		this.disabled = true;
		if (typeof self.onaction == "function") self.onaction("apply", "");
	}
	this.button_parent.appendChild(this.button_start);
	
	this.button_exit = document.createElement("input");
	this.button_exit.type = "button";
	this.button_exit.value = "我闪了，再见";
	this.button_exit.style.color = "Red";
	this.button_exit.onclick = function() {
		publics.counter('click_exit', location);
		if (self.state == "playing" && self.currPlayer != -1 && 
			!confirm("中途退出将导致游戏结束，是否确定？")) return;
		this.disabled = true;
		if (typeof self.onaction == "function") self.onaction("exit", "");
	}
	this.button_parent.appendChild(this.button_exit);

	this.div_point = document.createElement("div");
	this.div_point.className = "div_point";
	this.div_desktop.appendChild(this.div_point);
	this.input_point = document.createElement("input");
	this.input_point.className = "input_point";
	this.input_point.readOnly = true;
	this.input_point.style.top = "16px";
	this.div_point.appendChild(this.input_point);

	this.div_clock = document.createElement("div");
	this.div_clock.className = "div_clock";
	this.div_desktop.appendChild(this.div_clock);
	
	this.a_clock = document.createElement("a");
	this.a_clock.className = "a_clock";
	this.div_clock.appendChild(this.a_clock);
	
	this.div_buttons = document.createElement("div");
	this.div_buttons.className = "div_buttons";
	this.div_desktop.appendChild(this.div_buttons);

	var buttonLeft = 0;
	this.button_point1 = document.createElement("input");
	this.button_point1.type = "button";
	this.button_point1.value = "1分";
	this.button_point1.style.left = buttonLeft + "px";
	this.button_point1.onclick = function() {
		if (self.currPlayer != self.echoPlace) return;
		if (typeof self.onaction == "function") self.onaction("point", "1");
	}
	this.div_buttons.appendChild(this.button_point1);

	buttonLeft += 70;
	this.button_fetch = document.createElement("input");
	this.button_fetch.type = "button";
	this.button_fetch.value = "出牌";
	this.button_fetch.style.left = buttonLeft + "px";
	this.button_fetch.onclick = function() {
		publics.counter('click_fetch', location);
		if (self.currPlayer != self.echoPlace) return;
		if (!self.players[self.currPlayer].hand.getSelection()) return;
		if (typeof self.onaction == "function")
			self.onaction("fetch", self.players[self.currPlayer].hand.getSelection());
	}
	this.div_buttons.appendChild(this.button_fetch);

	this.button_point2 = document.createElement("input");
	this.button_point2.type = "button";
	this.button_point2.value = "2分";
	this.button_point2.style.left = buttonLeft + "px";
	this.button_point2.onclick = function() {
		publics.counter('click_point2', location);
		if (self.currPlayer != self.echoPlace) return;
		if (typeof self.onaction == "function") self.onaction("point", "2");
	}
	this.div_buttons.appendChild(this.button_point2);

	buttonLeft += 70;
	this.button_pass = document.createElement("input");
	this.button_pass.type = "button";
	this.button_pass.value = "Pass";
	this.button_pass.style.color = "Red";
	this.button_pass.style.left = buttonLeft + "px";
	this.button_pass.onclick = function() {
		if (self.currPlayer != self.echoPlace) return;
		if (typeof self.onaction == "function") self.onaction("pass", "");
	}
	this.div_buttons.appendChild(this.button_pass);

	this.button_point3 = document.createElement("input");
	this.button_point3.type = "button";
	this.button_point3.value = "3分";
	this.button_point3.style.left = buttonLeft + "px";
	this.button_point3.onclick = function() {
		publics.counter('click_point3', location);
		if (self.currPlayer != self.echoPlace) return;
		if (typeof self.onaction == "function") self.onaction("point", "3");
	}
	this.div_buttons.appendChild(this.button_point3);
	
	buttonLeft += 70;
	this.button_hint = document.createElement("input");
	this.button_hint.type = "button";
	this.button_hint.value = "提示";
	this.button_hint.style.left = buttonLeft + "px";
	this.button_hint.style.width = "65px";
	this.button_hint.onclick = function() {
		publics.counter('click_hint', location);
		if (self.currPlayer != self.echoPlace) return;
		if (self.hint.length > 0)
			self.doHint();
		else if (typeof self.onaction == "function") self.onaction("hint", "");
	}
	this.div_buttons.appendChild(this.button_hint);

	this.button_no = document.createElement("input");
	this.button_no.type = "button";
	this.button_no.value = "不叫";
	this.button_no.style.color = "Red";
	this.button_no.style.left = buttonLeft + "px";
	this.button_no.onclick = function() {
		publics.counter('click_no', location);
		if (self.currPlayer != self.echoPlace) return;
		if (typeof self.onaction == "function") self.onaction("point", "0");
	}
	this.div_buttons.appendChild(this.button_no);
	
	buttonLeft += 70;
	this.button_sort = document.createElement("input");
	this.button_sort.type = "button";
	this.button_sort.value = "排序";
	this.button_sort.style.left = buttonLeft + "px";
	this.button_sort.style.width = "65px";
	this.button_sort.onclick = function() {
		publics.counter('click_sort', location);
		if (self.currPlayer < 0) return;
		self.sortStyle = (self.sortStyle + 1) % 2;
		self.sortHand();
	}
	this.div_buttons.appendChild(this.button_sort);

	this.backCards = new Cards(this.div_desktop, (this.width - (15 * 2 + 71)) / 2, 3, "***", true);
	this.players = [
		{
			desktop: new Cards(this.div_desktop, 0, 0, "", true)
			,hand: new Cards(this.div_desktop, 0, 0, "", true)
		}
		,{
			desktop: new Cards(this.div_desktop, 0, 0, "", true)
			,hand: new Cards(this.div_desktop, 0, 0, "", true)
		}
		,{
			desktop: new Cards(this.div_desktop, 0, 0, "", true)
			,hand: new Cards(this.div_desktop, 0, 0, "", true)
		}
	];
	for (var i = 0; i < 3; i++) {
		this.players[i].div_player = document.createElement("div");
		this.players[i].input_rank = document.createElement("input");
		this.players[i].input_rank.className = "input_rank";
		this.players[i].input_rank.readOnly = true;

		this.players[i].input_username = document.createElement("input");
		this.players[i].input_username.className = "input_username";
		this.players[i].input_username.readOnly = true;
		
		this.players[i].div_player.className = "div_player";
		this.div_desktop.appendChild(this.players[i].div_player);
		this.players[i].div_player.appendChild(this.players[i].input_rank);
		this.players[i].div_player.appendChild(this.players[i].input_username);
	}
	this.doChange();
}

Landlord.prototype.sortHand = function() {
	if (this.currPlayer < 0) return;
	var self = this;
	var sortChars = Landlord.sortChars;
	if (this.sortStyle == 0) {
		var charCounts = {};
		var chars = this.players[this.currPlayer].hand.chars;
		for (var i = 0; i < chars.length; i++) {
			var j = Math.floor(sortChars.indexOf(chars.charAt(i)) / 4);
			if (!charCounts[j])
				charCounts[j] = 1;
			else {
				charCounts[j]++;
				if (j == 13) charCounts[j] += 4;
			}
		}
	}
	this.players[this.currPlayer].hand.onsort = function(card1, card2) {
		if (self.sortStyle == 0) {
			var j1 = Math.floor(Landlord.sortChars.indexOf(card1.cchar) / 4);
			var j2 = Math.floor(Landlord.sortChars.indexOf(card2.cchar) / 4);
			var i = charCounts[j2] - charCounts[j1];
			if (i) return i;
		}
		return sortChars.indexOf(card2.cchar, 0) - sortChars.indexOf(card1.cchar, 0)
	}
	this.players[this.currPlayer].hand.alphaSort();
}

Landlord.prototype.setCurrPlace = function(place) {
	if (this.currPlace == place) return;
	this.currPlace = place;
	this.doChange();
}

Landlord.prototype.doChange = function() {
	if (typeof this.onchange == "function") this.onchange();
	if (this.currPlayer != -1) {
		if (this.state == "playing")
			this.channel.unloadmessage = "中途退出将导致游戏结束。";
		else this.channel.unloadmessage = "退出将导致举手失效。";
	} else this.channel.unloadmessage = null;
	
	this.players[this.currPlace].hand.setLocking(this.state == "waiting" || this.currPlayer < 0);
	var displays = {"true":"", "false":"none"};
	this.div_clock.style.display = displays[this.state != "waiting"];
	this.button_fetch.style.display = 
		this.button_pass.style.display = 
		this.button_hint.style.display = displays[this.currPlayer == this.echoPlace && this.state == "playing"];
	this.button_sort.style.display = displays[this.currPlayer >= 0 && !!this.players[this.currPlayer].hand.chars];
	var canPoint = this.currPlayer == this.echoPlace && this.state == "pausing";
	this.button_point1.style.display = displays[canPoint && this.point <= 0];
	this.button_point2.style.display = displays[canPoint && this.point <= 1];
	this.button_point3.style.display = displays[canPoint && this.point <= 2];
	this.button_no.style.display = displays[canPoint];
	
	var place = this.currPlace;
	var player = this.players[place];
	this.button_start.disabled = this.state != "waiting" ||
		!this.channel.entering || this.currPlayer >= 0;
	for (var i = 0; i < 3; i++) {
		this.players[i].input_username.style.color = i == this.landlordPlace ? "#CC33CC" : "";
		this.players[i].input_username.value = this.players[i].username ? this.players[i].username : "";
		
		this.players[i].input_rank.style.color = i == this.landlordPlace ? "#CC33CC" : "";
		this.players[i].input_rank.value = i == this.landlordPlace ? "【地主】" : 
			(this.landlordPlace >= 0 ? "农民兄弟" : "");
	}
	this.input_point.value = "底分：" + this.point + "　倍数：" + this.bomb * 2;
	
	player.hand.move(
		(this.width - player.hand.getWidth()) / 2, 
		this.height - CardConsts.cardHeight - 50 - CardConsts.selectSpace
	);
	player.desktop.move(
		(this.width - player.desktop.getWidth()) / 2, 
		this.height - (CardConsts.cardHeight + CardConsts.selectSpace) * 2 - 50 - 35
	);
	player.div_player.style.left = (this.width - player.hand.getWidth()) / 2 + 100 + "px";
	player.div_player.style.top = this.height - 40 + "px";
	
	if (place == this.echoPlace) {
		this.div_clock.style.left = (this.width - player.hand.getWidth()) / 2 + 100 - 50 + "px";
		this.div_clock.style.top = this.height - 22 - 25 + "px";
	}

	place = (place + 1) % 3;
	player = this.players[place];
	player.hand.move(this.width - player.hand.getWidth() - 3, 76);
	player.desktop.move(
		this.width / 3 + (this.width * 2 / 3 - player.desktop.getWidth()) / 2, 
		103
	);
	player.div_player.style.left = this.width - 100 - 3 + "px";
	player.div_player.style.top = 210 + "px";
	if (place == this.echoPlace) {
		this.div_clock.style.left = this.width - 100 - 3 + 50 + "px";
		this.div_clock.style.top = 230 - 43 + "px";
	}
	
	place = (place + 1) % 3;
	player = this.players[place];
	player.hand.move(3, 75);
	player.desktop.move(
		(this.width * 2 / 3 - player.desktop.getWidth()) / 2,
		103
	);
	player.div_player.style.left = 3 + "px";
	player.div_player.style.top = 210 + "px";
	if (place == this.echoPlace) {
		this.div_clock.style.left = 3 + 50 + "px";
		this.div_clock.style.top = 230 - 43 + "px";
	}

	if (this.state != "waiting" && this.echoPlace == this.currPlayer) {
		if (this.flash)
			document.title = "【请出牌】" + this.documentTitle;
		else document.title = "【　　　】" + this.documentTitle;
	} else {
		document.title = this.documentTitle.replace(/^【.*?】/, "");
	}
}

Landlord.prototype.action = function(action, param, result) {
	if (!result) return;
	var self = this;
	for (var i = 0; i < 3; i++) {
		if (typeof result["p" + i] != "undefined") {
			this.players[i].id = result["p" + i].id;
			if (typeof this.players[i].id == "undefined" || parseInt(this.players[i].id) == -1) {
				if (this.currPlayer == i) this.currPlayer = -1;
				this.players[i].username = "";
			} else {
				this.players[i].username = result["p" + i].username;
				if (this.players[i].id == publics.passinfo.ident) {
					this.currPlayer = i;
					this.currPlace = i;
					this.sortHand();
				}
			}
		}
		if (typeof result["h" + i] != "undefined") {
			this.players[i].hand.setChars(result["h" + i]);
			if (this.currPlayer == i) this.sortHand();
		}
		if (typeof result["d" + i] != "undefined")
			this.players[i].desktop.setChars(result["d" + i]);
	}
	if (typeof result.back != "undefined")
		this.backCards.setChars(result.back);
	if (typeof result.state != "undefined")	{
		this.state = result.state;
		if (this.state != "waiting") {
			clearInterval(this.timer);
			this.timer = setInterval(
				function() {
					self.flash = !self.flash;
					if (self.state != "waiting" && self.echoPlace == self.currPlayer) {
						if (self.flash)
							document.title = "【请出牌】" + self.documentTitle;
						else document.title = "【　　　】" + self.documentTitle;
					}
				}, 1000);
		}
	}
	if (typeof result.echo != "undefined")
		this.echoPlace = result.echo;
	if (typeof result.landlord != "undefined")
		this.landlordPlace = result.landlord;
	if (typeof result.point != "undefined")
		this.point = result.point;
	if (typeof result.bomb != "undefined")
		this.bomb = result.bomb;
	if (typeof result.hint != "undefined") {
		if (result.hint[0] == "#") {
			if (typeof self.onaction == "function") self.onaction("pass", "");
			return;
		}
		this.hint = result.hint;
		this.hintOffset = 0;
		this.doHint();
	}
	this.doChange();
}

Landlord.prototype.doHint = function() {
	if (this.currPlayer < 0) return;
	if (this.hint.length <= 0) return;
	this.players[this.currPlayer].hand.setSelection(this.hint[this.hintOffset]);
	this.hintOffset = (this.hintOffset + 1) % this.hint.length;
}