PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src-extra/chaos/sql/chaos/server/actions/NextPhase.sql

http://github.com/JakeWheat/hssqlppp
SQL | 179 lines | 97 code | 15 blank | 67 comment | 9 complexity | b56cdaa9139b9d0b48438d8883db6eca MD5 | raw file
Possible License(s): BSD-3-Clause
  1. select module('Chaos.Server.Actions.NextPhase');
  2. /*
  3. == next phase
  4. next phase is the function to end a wizard's turn and move to the next
  5. one. It can be called implicitly by other actions as well as directly,
  6. e.g. once you've cast your spell, there's nothing you can do except
  7. end your turn so next_phase gets called automatically
  8. */
  9. select create_var('dont_nest_ai_next_phase', 'bool');
  10. select set_relvar_type('dont_nest_ai_next_phase_table', 'hack');
  11. create function action_next_phase() returns void as $$
  12. declare
  13. --c int;
  14. next_phase_again boolean := false;
  15. begin
  16. /*
  17. === check for game completion
  18. */
  19. if (exists (select 1 from game_completed_table)) then
  20. return;
  21. end if;
  22. --check for win or draw
  23. case (select count(1) from wizards where not expired)
  24. when 1 then --someone has won
  25. perform game_completed();
  26. update current_wizard_table set current_wizard =
  27. (select wizard_name from wizards where not expired);
  28. perform add_history_game_won();
  29. return;
  30. when 0 then --game is drawn
  31. perform game_completed();
  32. perform add_history_game_drawn();
  33. delete from current_wizard_table;
  34. return;
  35. else
  36. null;
  37. end case;
  38. /*
  39. === current wizard clean up phase
  40. If the user selects next phase when they have a spell to cast, then we
  41. want to call the usual skip spell action so as not to duplicate the
  42. work. But skip spell will call next_phase itself automatically and we
  43. don't want to do two next phases, so if there is a spell to be
  44. skipped, run that and don't run the rest of the next_phase function
  45. since it will be called via skip spell.
  46. this might be better if skip spell wasn't used for both explicitly and
  47. implicitly skipping
  48. */
  49. -- if the current spell isn't completed, then skip it
  50. if get_turn_phase() = 'cast'
  51. and exists(select 1 from current_wizard_table
  52. inner join wizard_spell_choices
  53. on (current_wizard = wizard_name)) then
  54. perform skip_spell();
  55. return;
  56. end if;
  57. --multiple update hack to get round constraints
  58. update in_next_phase_hack_table
  59. set in_next_phase_hack = true;
  60. --complete current phase:
  61. if (select turn_phase = 'move' from turn_phase_table) then
  62. delete from pieces_moved;
  63. end if;
  64. /*
  65. === all wizards clean up phase
  66. clean up if this is the last wizard for this phase, then move to next
  67. phase, if this is autonomous, then do it and move to move phase this
  68. works because all the end phase stuff happens before the autonomous
  69. phase is run in this function, and all the setup runs after it is run.
  70. */
  71. if is_last_wizard() then
  72. case get_turn_phase()
  73. when 'cast' then
  74. --clear the cast alignment which is used to adjust the world
  75. --alignment when a spell is cast
  76. delete from cast_alignment_table;
  77. when 'move' then
  78. --if this is the end of the move phase then we're on the next turn
  79. update turn_number_table
  80. set turn_number = turn_number + 1;
  81. perform add_history_new_turn();
  82. else
  83. null;
  84. end case;
  85. --move to the next turn phase
  86. update turn_phase_table
  87. set turn_phase = next_turn_phase(turn_phase);
  88. if get_turn_phase() = 'autonomous' then
  89. perform do_autonomous_phase();
  90. update turn_phase_table
  91. set turn_phase = next_turn_phase(turn_phase);
  92. end if;
  93. end if;
  94. /*
  95. === init new current phase
  96. */
  97. -- move to the next wizard, this is the meat of this function
  98. update current_wizard_table
  99. set current_wizard = next_wizard(current_wizard);
  100. --initialise the spell for this phase
  101. if get_turn_phase() = 'cast' then
  102. --setup the cast alignment table if this is the start of the cast
  103. --phases
  104. if is_first_wizard() then
  105. insert into cast_alignment_table values(0);
  106. end if;
  107. if exists(select 1 from current_wizard_spell) then
  108. insert into spell_parts_to_cast_table
  109. select coalesce(numb, 0) from target_spells
  110. natural inner join current_wizard_spell;
  111. insert into cast_success_checked_table values (false);
  112. else
  113. --skip to the next phase automatically
  114. next_phase_again := true;
  115. end if;
  116. /*elseif (select turn_phase = 'move' from turn_phase_table) then
  117. insert into pieces_to_move
  118. select ptype, allegiance, tag
  119. from moving_pieces
  120. inner join current_wizard_table
  121. on allegiance = current_wizard;*/
  122. end if;
  123. --finished our updates for this next phase
  124. update in_next_phase_hack_table
  125. set in_next_phase_hack = false;
  126. perform add_history_wizard_up();
  127. /*
  128. === continue
  129. */
  130. --if there is nothing to do in the new current phase - continue to
  131. --next phase automatically
  132. if next_phase_again then
  133. perform action_next_phase();
  134. end if;
  135. end;
  136. $$ language plpgsql volatile;
  137. /*
  138. === internals
  139. */
  140. create function is_last_wizard() returns boolean as $$
  141. with
  142. uw as (select wizard_name,original_place
  143. from wizards
  144. where not expired)
  145. select original_place = (select max(original_place) from uw)
  146. from current_wizard
  147. natural inner join uw
  148. $$ language sql stable;
  149. create function is_first_wizard() returns boolean as $$
  150. with
  151. uw as (select wizard_name,original_place
  152. from wizards
  153. where not expired)
  154. select original_place = (select min(original_place) from uw)
  155. from current_wizard
  156. natural inner join uw
  157. $$ language sql stable;