#!/usr/local/bin/perl # ゲームブック・HTML出力用CGI # 製作:Ntaki(ntaki@indigo.plala.or.jp) # 最終更新日 2000年9月16日 # このプログラムは、Ntakiが著作権を有しています。一応。 # 無償での複製・再配布に関しては自由に行って構いません。事前連絡も不要です。 # たとえこのプログラムを改変して「僕が一から作ったんです」と名乗った人がいても #Ntakiは一向に構いません。 # ただし、このプログラム、またこのプログラムに手を加えたものを、(いないとは #思うが)有償で配布してはいけません。たとえ有償配布の許可申請を行っても、 #Ntakiは一切許可しません。このプログラムは、いかなる人も無料で入手可能な状態で #なければなりません。 # また改変しない場合、最初からこの行までのコメントは削除しないでください。 # 出来れば。 # このプログラム内で用いている関数「init_form」については、 #『Perlで作るCGI入門 基礎編』 結城浩著 ソフトバンク(株)刊 ISBN 4-7973-0507-X #の例題プログラムにある関数を借用させていただきました。 require "jcode.pl"; &init_form('euc'); # データファイル群 # 各章のデータファイル群を格納しているフォルダ(ディレクトリ) $dir{sentence} = 'sentence'; # フォームから章とフラグの情報を取得 $data{chapter_name} = $form{chapter}; $data{scene} = $form{scene}; $data{item} = $form{item}; if ($data{chapter_name} eq ''){ # デフォルトのシーン $data{chapter_name} = $form{chap_name}; $data{scene} = 0; $data{item} = ''; &init_manage; } else{ &init_manage; # 選択されたボタンを調べる $data{selected} = &check_choice; # 次に表示するシーンidを調べる $data{scene} = &pick_scene($data{scene}, $data{selected}); if ($data{scene} eq ''){ &html_begin('エラー発生'); print '

エラー発生

'; print '何らかの原因で、次のシーンが検出できません。管理者に連絡してください。

'; print "パスワード:「$data{chapter_name}」, 選択肢:「$data{selected}」, 次のシーン:「$data{scene}」

"; &html_end; exit(1); } } # 出力する場面を検索する &pick_scene($data{scene}); &html_begin('ゲームブックCGI'); &out_scene; &html_end; ################# # 初期処理(章ID取得、章ファイルオープン) sub init_manage { # 章のデータファイルを開く if (!open(DATA, "$dir{sentence}/$data{chapter_name}.file")){ &html_begin('エラー発生'); print 'エラー発生

',"\n"; print "cannot open $dir{sentence}/$data{chapter_name}.file ファイルが存在しないか、開けません。\n"; &html_end; exit(0); } } # 選択された番号を調べる sub check_choice { local($i, $tmp); for($i=1;;$i++){ $tmp = join('', 'choice_', $i); if ($form{$tmp} ne ''){ last; } } return($i); } ################# # 出力する場面を検索する sub pick_scene { local($tmp, $check, $count, @items); $check = join('', '*', $_[0], '*'); # 欲しいシーンidの行を探す seek(DATA, 0, 0); while($tmp = ){ chop($tmp); if ($tmp eq ''){ break; } if ($tmp eq $check){ # 画像ファイル名を取り出す $tmp = ;chop($tmp); if ($tmp eq ''){ break; } undef($image_filename); $image_filename = $tmp; ; # 本文を取り出す undef($text_body); while(($tmp = ) ne "\n"){ $text_body = join('', $text_body, $tmp); } # 選択肢を取り出す $count = 1; undef(%text_choice); while(($tmp = ) ne "\n"){ chop($tmp); if ($tmp eq ''){ break; } ($text_choice{$count, text}, $text_choice{$count, get}, $text_choice{$count, jump}) = split(/@/, $tmp, 3); # 探している選択肢があったら、その番号を返す # フラグの変化があれば、リストに追加する #(ジャンプ先の確認) if ($count == $_[1]){ if ($text_choice{$count, get} eq 'clear'){ undef($data{item}); } elsif ($text_choice{$count, get} ne ''){ $data{item} = join('@', $data{item}, $text_choice{$count, get}); } # フラグとジャンプ条件を照らし合わせる @items = split(/\@/, $data{item}); # 現在立っているフラグを配列に格納 @selections = split(/\@/, $text_choice{$count, jump}); # 分岐候補を配列に格納 foreach $i (@selections){ ($condition, $jump) = split(/:/, $i); foreach $j (@items){ if (($j eq $condition) && ($jump ne '')){ return($jump); } } } # どの条件にも当てはまらない場合、最後のジャンプ先を選択 if ($jump eq ''){ $jump = $condition; # 条件フラグがない場合の処理 } return($jump); } $count++; } return; } } } # シーン出力文 sub out_scene { local($count); # 表示形式(中央そろえ="center") print '

',"\n"; # イメージの出力 print '',"\n"; print '

',"\n"; # 本文 print ''; print "$text_body\n"; print ''; # 選択肢 print '

',"\n"; for($count = 1;$text_choice{$count, text} ne '';$count++){ print '
',"\n"; } # その他の情報フォーム print '',"\n"; print '',"\n"; print '',"\n"; print '
',"\n"; print '
',"\n"; print "

$error_string"; } # HTML記述部分(ヘッダ、フッタ) sub html_begin{ print "Content-type: text/html\n\n"; print '',"$_[0]",'',"\n"; print '',"\n"; # print '',"\n"; print '',"\n"; print ''; } sub html_end{ print "\n"; } # 漢字変換 sub init_form { local(@assocarray, $assoc, $property, $value, $charcode, $method); $charcode = $_[0]; $method = $ENV{'REQUEST_METHOD'}; $method =~ tr/A-Z/a-z/; if ($method eq 'post') { read(STDIN, $query, $ENV{'CONTENT_LENGTH'}); } else { $query = $ENV{'QUERY_STRING'}; } @assocarray = split(/&/, $query); foreach $assoc (@assocarray) { ($property, $value) = split(/=/, $assoc); $value =~ tr/+/ /; $value =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg; &jcode'convert(*value, $charcode); $form{$property} = $value; } }