Skip to content

Instantly share code, notes, and snippets.

@PilotBob
Created February 10, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PilotBob/8c7cacf63042babff640 to your computer and use it in GitHub Desktop.
Save PilotBob/8c7cacf63042babff640 to your computer and use it in GitHub Desktop.
vote_check:begin
declare tally_id int(11);
declare new_count int(11);
declare new_tally int(1);
declare nightfall int(1);
if new.valid != 1 then
leave vote_check;
end if;
set new_tally := 0;
set new_count := (select max(vote_count) from Tally where game_id = new.game_id and day = new.day);
if(new_count is NULL) then
set new_count := 1;
else
set new_count := new_count + 1;
end if;
if(new.type = 'vote') then
select id, nightfall from Tally where Tally.game_id=new.game_id and Tally.day=new.day and Tally.voter=new.voter and Tally.unvote=0 into tally_id, nightfall;
if(tally_id is null) then
insert into Tally(game_id, day, votee, voter, vote_article, vote_count,misc) Values(new.game_id, new.day, new.votee, new.voter,new.article_id, new_count,new.misc);
set new_tally := 1;
elseif(new.misc = 'nightfall') then
update Tally set nightfall=1, nightfall_article=new.article_id where id=tally_id;
set new_tally = 1;
elseif(nightfall = 1) then
set new.valid := 0;
set new.misc := 'nightfallen';
else
update Tally set unvote=1 where id=tally_id;
insert into Tally(game_id, day, votee, voter, vote_article, vote_count,misc) Values(new.game_id, new.day, new.votee, new.voter,new.article_id, new_count,new.misc);
set new_tally := 1;
end if;
elseif(new.type = 'unvote') then
set tally_id := (select id from Tally where Tally.game_id=new.game_id and Tally.day=new.day and Tally.voter=new.voter and Tally.votee = new.votee and Tally.unvote=0 and Tally.nightfall=0);
if(tally_id is not null) then
update Tally set Tally.unvote=1, Tally.unvote_article=new.article_id where Tally.id=tally_id;
set new_tally = 1;
elseif(new.misc = 'all') then
update Tally set Tally.unvote=1, Tally.unvote_article=new.article_id where Tally.game_id=new.game_id and Tally.day=new.day and Tally.voter=new.voter and Tally.unvote=0 and Tally.nightfall=0;
set new_tally = 1;
else
set new.valid := 0;
set new.misc := 'vote not found';
end if;
end if;
if(new_tally = 1) then
update Games set updated_tally = 1 where Games.id = new.game_id;
end if;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment