package SWWrite;

#----------------------------------------
# 発言書き込み処理
#----------------------------------------
sub ExecuteCmdWrite {
	my ($sow, $vil, $writepl, $memoid) = @_;
	my $query  = $sow->{'query'};
	require "$sow->{'cfg'}->{'DIR_LIB'}/string.pl";

	my ($mestype, $saytype) = &GetMesType($sow, $vil, $writepl);

	my $act = 0;
	$act = 1 if (index($saytype, '_act') >= 0);
	$sow->{'saytype'} = $saytype;

	my $mes = &SWString::GetTrimString($sow, $vil, $query->{'mes'});

	my $saypoint;
	if (($saytype ne 'maker') && ($saytype ne 'admin')) {
		# 発言数消費量
		if ($act > 0) {
			$saypoint = 1;
		} else {
			$saypoint = &SWBase::GetSayPoint($sow, $vil, $mes);
		}

		# 発言数がない（仮）
		$sow->{'debug'}->raise($sow->{'APLOG_NOTICE'}, "発言数が足りません。","not enough saypoint.[$saytype: $writepl->{$saytype} / $saypoint]") if ((!defined($writepl->{$saytype})) || (($writepl->{$saytype} - $saypoint) < 0));
	}

	# 発言
	require "$sow->{'cfg'}->{'DIR_LIB'}/file_log.pl";
	require "$sow->{'cfg'}->{'DIR_LIB'}/log.pl";
	my $logfile = SWBoa->new($sow, $vil, $vil->{'turn'}, 0);

	if (($saytype ne 'maker') && ($saytype ne 'admin')) {
		if (($act == 0) && ($writepl->{'lastwritepos'} >= 0)) {
			# 二重発言防止
			my $log = $logfile->read($writepl->{'lastwritepos'});
			my $logmes = $log->{'log'};
			$logmes = &SWLog::ReplaceAnchorHTMLRSS($sow, $vil, $logmes);
			my $checkmestype = $mestype;
			$checkmestype = $sow->{'MESTYPE_SAY'} if ($mestype == $sow->{'MESTYPE_QUE'});
			my $checklogmestype = $log->{'mestype'};
			$checklogmestype = $sow->{'MESTYPE_SAY'} if ($log->{'mestype'} == $sow->{'MESTYPE_QUE'});
			$sow->{'debug'}->raise($sow->{'APLOG_NOTICE'}, "直前の発言と同じ内容の発言をしようとしています。","same last mes.") if (($logmes eq $mes) && ($checklogmestype == $checkmestype));
		}
	}

	my $logsubid = $sow->{'LOGSUBID_SAY'};
	$logsubid = $sow->{'LOGSUBID_ACTION'} if ($act > 0);
	$logsubid = $sow->{'LOGSUBID_BOOKMARK'} if ((defined($query->{'actionno'})) && ($query->{'actionno'} == -2));

	my $monospace = 0;
	$monospace = 1 if ($query->{'monospace'} ne ''); # アクションの等幅は隠し機能という事で（ぉ
	my $expression = 0;
	$expression = $query->{'expression'} if (defined($query->{'expression'}));

	my %say = (
		mestype    => $mestype,
		logsubid   => $logsubid,
		uid        => $writepl->{'uid'},
		csid       => $writepl->{'csid'},
		cid        => $writepl->{'cid'},
		chrname    => $writepl->getchrname(),
		expression => $expression,
		mes        => &CvtRandomText($sow, $vil, $mes),
		memoid     => $memoid,
		undef      => 0,
		monospace  => $monospace,
	);
	my $lastwritepos = $logfile->executesay(\%say);
	$logfile->close();

	if (($saytype ne 'maker') && ($saytype ne 'admin')) {
		# 発言数消費
		$writepl->{$saytype} -= $saypoint;
		if (($act == 0) && (($mestype == $sow->{'MESTYPE_QUE'}) || ($mestype == $sow->{'MESTYPE_GSAY'}))) {
			# 発言済み回数／pt
			$writepl->{'saidcount'}++ if ($mestype == $sow->{'MESTYPE_GSAY'});
			$writepl->{'saidpoint'} += $saypoint;
		}
		$writepl->{'limitentrydt'} = $sow->{'time'} + $sow->{'cfg'}->{'TIMEOUT_ENTRY'} * 24 * 60 * 60 if ($writepl->{'limitentrydt'} > 0);
		$writepl->{'lastwritepos'} = $lastwritepos;

		$vil->writevil();
	}
}

#----------------------------------------
# 発言種別の取得
#----------------------------------------
sub GetMesType {
	my($sow, $vil, $writepl) = @_;
	my ($mestype, $saytype);
	my $query = $sow->{'query'};
	my $cfg   = $sow->{'cfg'};

	if ($vil->isepilogue() > 0) {
		# エピローグ発言
		$mestype = $sow->{'MESTYPE_QUE'};
		$saytype = 'esay';
	} elsif (($vil->{'turn'} == 0) && (($cfg->{'ENABLED_TSAY_PRO'} == 0) || ($query->{'think'} ne 'on'))) {
		# プロローグ発言
		$mestype = $sow->{'MESTYPE_QUE'};
		$saytype = 'psay';
	} elsif (($query->{'think'} eq 'on') && (($writepl->{'live'} eq 'live') || ($cfg->{'ENABLED_TSAY_GRAVE'} > 0))) {
		# 独り言
		$mestype = $sow->{'MESTYPE_TSAY'};
		$saytype = 'tsay';
	} elsif ($writepl->{'live'} ne 'live') {
		# 死者のうめき
		$mestype = $sow->{'MESTYPE_GSAY'};
		$saytype = 'gsay';
	} elsif ((($writepl->iswolf() > 0) || ($writepl->{'role'} == $sow->{'ROLEID_CPOSSESS'})) && ($query->{'wolf'} eq 'on')) {
		# ささやき
		$mestype = $sow->{'MESTYPE_WSAY'};
		$saytype = 'wsay';
	} elsif (($writepl->{'role'} == $sow->{'ROLEID_SYMPATHY'}) && ($query->{'sympathy'} eq 'on')) {
		# 共鳴
		$mestype = $sow->{'MESTYPE_SPSAY'};
		$saytype = 'spsay';
	} elsif (($writepl->{'role'} == $sow->{'ROLEID_WEREBAT'}) && ($query->{'werebat'} eq 'on')) {
		# 念話
		$mestype = $sow->{'MESTYPE_BSAY'};
		$saytype = 'bsay';
	} else {
		# 通常発言
		$mestype = $sow->{'MESTYPE_QUE'};
		$saytype = 'say';
	}

	if (($query->{'cmd'} eq 'action') || ($query->{'cmd'} eq 'wrmemo')) {
		# アクション
		$saytype = 'say' if (($saytype eq 'psay') || ($saytype eq 'esay'));
		$saytype .= '_act';
		$mestype = $sow->{'MESTYPE_SAY'} if ($mestype == $sow->{'MESTYPE_QUE'});
	}

	if (($query->{'admin'} ne '') && ($sow->{'uid'} eq $cfg->{'USERID_ADMIN'})) {
		# 管理人発言
		$mestype = $sow->{'MESTYPE_ADMIN'};
		$saytype = 'admin';
	} elsif (($query->{'maker'} ne '') && ($sow->{'uid'} eq $vil->{'makeruid'})) {
		# 村建て人発言
		$mestype = $sow->{'MESTYPE_MAKER'};
		$saytype = 'maker';
	}

	return ($mestype, $saytype);
}

#----------------------------------------
# ランダム表示機能
#----------------------------------------
sub CvtRandomText {
	my ($sow, $vil, $mes) = @_;
	my $cfg = $sow->{'cfg'};

	return $mes if ($cfg->{'ENABLED_RANDOMTEXT'} == 0);

	# 1d6
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_1D6'}\]\]/{my $a = int(rand(6))+1;"<strong>{$a}<\/strong>"}/eg;

	# 1d10
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_1D10'}\]\]/{my $a = int(rand(10))+1; sprintf("<strong>(%02d)<\/strong>", $a)}/eg;

	# 1d20
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_1D20'}\]\]/{my $a = int(rand(20))+1; sprintf("<strong>[%02d]<\/strong>", $a)}/eg;

	# fortune
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_FORTUNE'}\]\]/{my $a = int(rand(101)); sprintf("<strong>%s<\/strong>", $a)}/eg;

	# who
	my $livepllist = $vil->getlivepllist();
	$livepllist = $vil->getpllist() if ($vil->{'turn'} >= $vil->{'epilogue'});
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_LIVES'}\]\]/{my $a = int(rand(scalar(@$livepllist))); sprintf("<strong>&lt;&lt;%s&gt;&gt;<\/strong>", $livepllist->[$a]->getchrname())}/eg;

	# omikuji
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_MIKUJI'}\]\]/{my $a = int(rand(6))+1; my $b = int(rand(6))+1; my $c = int(rand(6))+1; sprintf("<strong>*%s*<\/strong>", $cfg->{'MIKUJI'}->[$a+$b+$c - 3])}/eg;

	# role
	my $rolename = $sow->{'textrs'}->{'ROLENAME'};
	$mes =~ s/\[\[$cfg->{'RANDOMTEXT_ROLE'}\]\]/{my $a = int(rand(scalar(@$rolename)-1))+1; sprintf("<strong>((%s))<\/strong>", $rolename->[$a])}/eg;

	return $mes;
}

1;