折角作ったので2 | Polyhedron

Polyhedron

速水が思った事を書いていきます。twitterを始めたのでこちらはほぼ廃墟かも。

書き換えたのをあげてみる。
いくつか機能削除&追加。
相変わらずgetCookieはパクリ(引用元を忘れてしまった…ごめんなさい;)です。
まだ改善の余地はあるんだろうか。今の知識レベルではよくわからない。

if(window.location.href.indexOf("http://ほげほげ")==0){
document.addEventListener("load",function(){

/* Villagers Database */
const names = ["グレ", "占1", "占2","占3", "霊能", "共有", "狩人"];
const ncls = ["norm", "ura1", "ura2","ura3" "rei", "kyo", "hunt"];
const col= ["", "red", "blue","yellow", "#33ccff", "#33dd33", "#aaccff"];
const pcls = ["gray", "white", "black"];
const pics = ["--", "○", "●"];
const v_init = ["norm", "gray", "gray", "gray"];

var vill = document.getElementsByTagName('table')[1].getElementsByTagName('img');
var vill_data = splitData(getCookie("village_data"));

/* initialize */
for (i=0;i vill[i].setAttribute('id','id'+i+'0');
if (vill[i].getAttribute('src') =="./img/grave.gif") {
vill[i].setAttribute('class','v_dead');
continue;
} else {
var newNode = document.createElement('div');
for (j=1;j<=3;j++) {
var span = document.createElement('span');
span.setAttribute('style', "color:"+col[j]); span.setAttribute('id','id'+i+j);
setOX(span, vill_data[i][j]);
newNode.appendChild(span);
newNode.appendChild(document.createTextNode("|"));
}
newNode.removeChild(newNode.lastChild);
newNode.appendChild(document.createElement('br'));
var span2 = document.createElement('span');
span2.setAttribute('id','id'+i+'0sub');
newNode.appendChild(span2);
with(vill[i].parentNode.nextSibling) replaceChild(newNode, lastChild);
setProp(vill[i], vill_data[i][0]);
}
}

/* apply save utility */
var subm=document.body.getElementsByTagName('input');
for (i = 0; i < subm.length; i++) {
if (subm[i].getAttribute('type') != "submit") continue;
subm[i].setAttribute('onclick', 'jinro_repCookie('+vill.length+')');
subm[i].scrollIntoView(true);
break;
}

/*jusatu taiou*/
var imgs=document.body.getElementsByTagName('IMG');
for (i = 0; i < imgs.length; i++) {
if (imgs[i].getAttribute('src') != "./img/dead2.gif") continue;
imgs[i].nextSibling.nextSibling.setAttribute('onclick','jinro_jusatu(this)');
}

/* func */
function splitData(st) {
var arr1 = st.split("|");
var arr2 = [[] , []];
if (arr1.length != vill.length) {
for (i = 0; i < vill.length; i++) arr2[i]=v_init;
} else {
for (i = 0; i < vill.length; i++) arr2[i]=arr1[i].split(",");
}
return arr2;
}

function getCookie(name) {
if (!name || !document.cookie) return "";
var cookies = document.cookie.split("; ");
for (var i = 0; i < cookies.length; i++) {
var str = cookies[i].split("=");
if (str[0] != name) continue;
return unescape(str[1]);
}

return "";
}

function repCookie(len) {
var arr1 = [];
for (i=0;i < len;i++) {
var arr2 = [];
for (j=0;j < 4;j++) {
if(!document.getElementById('id'+i+j)) continue;
arr2[j] = document.getElementById('id'+i+j).getAttribute('class');
}
arr1[i]=arr2.join(",");
}
document.cookie="; village_data="+escape(arr1.join("|"));
}

function jusatu(ele) {
var str = "占いの結果、"+ele.innerHTML+"さんは村人でございました。";
window.document.form[3].value=str;
window.document.form[2].click();
}

function kangureget() {
var str = "完グレ(敬称略)\n";
for (i = 0; i < vill.length; i++) {
if (document.getElementById('id'+i+'0').getAttribute('class')=='v_dead') continue;
for (j = 0; j < 4; j++) if (document.getElementById('id'+i+j).getAttribute('class')!=v_init[j]) continue;
str+=vill[i].parentNode.nextSibling.firstChild.nodeValue+"、";
}
alert(str);
}

function katari() {
var str = "";
for (i = 0; i < vill.length; i++) {
str+="占いの結果、"+vill[i].parentNode.nextSibling.firstChild.nodeValue+"さんは村人でございました。\n";
}
alert(str);
}

function setOX(ele,cla) {
var i = pcls.indexOf(cla);
ele.innerHTML=pics[i];
ele.setAttribute('class',cla);
ele.setAttribute('onclick','jinro_setOX(this, "'+pcls[(i+1)%pcls.length]+'")');
}

function setProp(ele,cla) {
var i = ncls.indexOf(cla);
var targetId = ele.getAttribute('id') + 'sub';
document.getElementById(targetId).innerHTML=names[i];
document.getElementById(targetId).setAttribute('style', "color:"+col[i]);
ele.setAttribute('class',cla);
ele.setAttribute('onclick','jinro_setProp(this, "'+ncls[(i+1)%ncls.length]+'")');
}

/* local func to global */
jinro_repCookie = repCookie;
jinro_jusatu = jusatu;
jinro_kangureget= kangureget;
jinro_katari = katari;
jinro_setOX = setOX;
jinro_setProp = setProp;

},null);}