//@unloadnpc GOTM
//@loadnpc npc/woe-fe/flags.txt

-	script	GOTM	HIDDEN_NPC,{
	end;

OnCheck:
	query_sql "SELECT `guild_id`,`name`, `score`  FROM `gotm` ORDER BY `score` DESC, `name` ASC",.@guild_id,.@gname$,.@score;
	.@size = getarraysize( .@guild_id );
	if( !.@size ){
		mes "[ Guild Of The Month ]";
		mes "No record found.";
	} else {
		mes "[ Guild Of The Month ]";
		for( .@i = 0; .@i < .@size; .@i++ )
			mes ( .@i+1 )+". "+.@gname$[.@i]+" ^FF0000["+.@score[.@i]+"]^000000";
	}
	close;

OnGOTM:
	query_sql "SELECT `guild_id`,`name`, `score`  FROM `gotm` ORDER BY `score` DESC, `name` ASC",.@guild_id,.@gname$,.@score;
	.@size = getarraysize( .@guild_id );
	if( !.@size ){
		mes "[ Guild Of The Month ]";
		mes "No record found.";
	} else {
		mes "[ Guild Of The Month ]";
		for( .@i = 0; .@i < .@size; .@i++ )
			mes ( .@i+1 )+". "+.@gname$[.@i]+" ^FF0000["+.@score[.@i]+"]^000000";
	}
	next;
	while(1) {
		switch(select(
		 ((getgmlevel())?"[ ^28bf00GM Setup^000000 ]:":":")
		)) {
		case 1:
			switch(select(
			"[ ^28bf00Add/Edit Entry^000000 ]:"+
			"Cancel"
			)) {
			case 1:
				input .@guild$;
				query_sql "SELECT `guild_id`,`name` FROM `guild` WHERE `name` = '"+escape_sql(.@guild$)+"'",.@guild_id,.@name$;
				if(.@guild_id){
					dispbottom .@guild_id+" "+.@name$;
					query_sql "SELECT `guild_id` FROM `gotm` WHERE `guild_id` = "+.@guild_id,.@dummy;
					if(.@dummy){	
						switch(select(
							"[ ^28bf00Score^000000 ]:"+
							"[ ^28bf00Delete^000000 ]:"+
							"Cancel"
						)) {	
						case 1:
							query_sql "UPDATE `gotm` SET `score` = `score` + 1 WHERE `guild_id` = "+.@guild_id;
							dispbottom "entry updated";
							break;	
						case 2:
							query_sql "DELETE FROM `gotm` WHERE `guild_id` = "+.@guild_id;
							dispbottom "entry deleted";
							break;
						default: close;
						}
						close;
					} else {
						if(select("Cancel","Add") == 2){
							query_sql( "INSERT INTO `gotm` ( `guild_id`,`name`,`score` ) VALUES ( "+.@guild_id+",'"+escape_sql( .@name$ )+"',0 )" );
							dispbottom "entry added";
							close;
						}
					}
					close;
				} else {
					dispbottom "guild not found";
					close;	
				}
			default: close;
			}
			end;
		default: close; 
		}
	}	
	end;

OnUpdate:
	query_sql "SELECT `guild_id`,`name`, `score`  FROM `gotm` ORDER BY `score` DESC, `name` ASC",.@guild_id,.@gname$,.@score;
	.@size = getarraysize( .@guild_id );
	if(!.@size) end;
	for( .@i = 0; .@i < .@size; .@i++ ) {	
		setd("$GOTM" +.@i,.@guild_id[.@i]);
		donpcevent "GOTM"+.@i+"::OnRecvCastle";
	}	
	end;

OnInit:
	bindatcmd("gotm","GOTM::OnGOTM",99,99);
	bindatcmd("gotmupdate","GOTM::OnUpdate",99,99);
	bindatcmd("gotmcheck","GOTM::OnCheck",99,99);
	set .GMAccess,99;
	end;
}


-	script	GOTM0	GUILD_FLAG,{
	.@GID = $GOTM0;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM0);
	end;
}

-	script	GOTM1	GUILD_FLAG,{
	.@GID = $GOTM1;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM1);
	end;
}

-	script	GOTM2	GUILD_FLAG,{
	.@GID = $GOTM2;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM2);
	end;
}

-	script	GOTM3	GUILD_FLAG,{
	.@GID = $GOTM3;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM3);
	end;
}

-	script	GOTM4	GUILD_FLAG,{
	.@GID = $GOTM4;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM4);
	end;
}

-	script	GOTM5	GUILD_FLAG,{
	.@GID = $GOTM5;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM5);
	end;
}

-	script	GOTM6	GUILD_FLAG,{
	.@GID = $GOTM6;
	mes "[ Edict of the Divine Rune-Midgarts Kingdom ]";
	mes " ";
	mes "1. Follow the ordinance of The Divine Rune-Midgarts Kingdom, ";
	mes "we approve that this place is in";
	mes "the private prossession of ^ff0000"+getguildname(.@GID)+"^000000 Guild.";
	mes " ";
	mes "2. The guild Master of ^ff0000"+getguildname(.@GID)+"^000000 Guild is";
	mes "^ff0000"+getguildmaster(.@GID)+"^000000";
	mes "If there is anyone who objects to this,";
	mes "prove your strength and honor with a steel blade in your hand.";
	close;

OnInit:
OnRecvCastle:
	flagemblem($GOTM6);
	end;
}

morocc,155,117,4	duplicate(GOTM0)	Flag#moc1	GUILD_FLAG
morocc,164,117,0	duplicate(GOTM0)	Flag#moc2	GUILD_FLAG

morocc,155,134,4	duplicate(GOTM1)	Flag#moc5	GUILD_FLAG
morocc,155,128,4	duplicate(GOTM2)	Flag#moc6	GUILD_FLAG
morocc,155,122,4	duplicate(GOTM3)	Flag#moc7	GUILD_FLAG
morocc,164,134,0	duplicate(GOTM4)	Flag#moc8	GUILD_FLAG
morocc,164,128,0	duplicate(GOTM5)	Flag#moc9	GUILD_FLAG
morocc,164,122,0	duplicate(GOTM6)	Flag#moc10	GUILD_FLAG
