Class | GameController |
In: |
app/controllers/game_controller.rb
|
Parent: | ApplicationController |
Ajax wrapper function for the attack view
# File app/controllers/game_controller.rb, line 194 194: def ajax_attack 195: attack 196: render :layout => false 197: end
Ajax wrapper funtion for the galaxy view
# File app/controllers/game_controller.rb, line 188 188: def ajax_galaxy 189: galaxy 190: render :layout => false 191: end
Ajax function that returns the minutes till the next round
# File app/controllers/game_controller.rb, line 158 158: def ajax_get_remaining_minutes 159: @player = @session[:user].player 160: seconds_to_next_round = (@player.last_update_on + Game.round_length) - Time.now 161: @minutes_remaining = (seconds_to_next_round/60).to_i.to_s.rjust(2,"0") 162: # I hate RadRails syntax highlighting 163: @seconds_remaining = (seconds_to_next_round%60).to_i.to_s.rjust(2,"0") 164: render_text "#{@minutes_remaining}:#{@seconds_remaining} minutes" 165: end
Ajax wrapper funtion for the log
# File app/controllers/game_controller.rb, line 200 200: def ajax_log 201: log 202: render :layout => false 203: end
Ajax wrapper funtion for the moon overview
# File app/controllers/game_controller.rb, line 182 182: def ajax_moon 183: moon 184: render :layout => false 185: end
Ajax funtion to select a moon
# File app/controllers/game_controller.rb, line 149 149: def ajax_select_moon 150: @player = @session[:user].player 151: @old_moon_id = @session[:moon].id 152: @new_moon_id = params[:id] 153: @session[:moon] = Moon.find(params[:id]) if @player.owns(Moon.find(params[:id])) 154: render :layout => false 155: end
Ajax function to update the interface periodically
# File app/controllers/game_controller.rb, line 168 168: def ajax_update 169: @player = @session[:user].player 170: seconds_to_next_round = (@player.last_update_on + Game.round_length) - Time.now 171: minutes_remaining = (seconds_to_next_round/60).to_i.to_s.rjust(2,"0") 172: # I hate RadRails syntax highlighting 173: seconds_remaining = (seconds_to_next_round%60).to_i.to_s.rjust(2,"0") 174: @countdown = "#{minutes_remaining}:#{seconds_remaining} minutes" 175: @money = @player.money 176: @level = @player.level 177: galaxy 178: render :layout => false 179: end
Inteferface to attack another moon
# File app/controllers/game_controller.rb, line 104 104: def attack 105: redirect_to_index("You didn't specify a moon!") and return if params[:id].nil? 106: @player = @session[:user].player 107: @moon = Moon.find(params[:id]) 108: @origin = @session[:moon] 109: @origin.create_fleet # Create new fleet on the moon if there's none yet 110: @fleet = @origin.fleet 111: 112: redirect_to_index("You can't attack yourself!") and return if @player.owns(@moon) 113: end
Cancel the production on a moon
# File app/controllers/game_controller.rb, line 76 76: def cancel_production 77: moon = Moon.find(params[:id]) 78: redirect_to_index("You don't own this moon!") and return unless @session[:user].player.owns(moon) 79: moon.cancel_production 80: redirect_to_index("Canceled production on #{moon.name}") and return 81: end
Changes the production on a moon to the posted product
# File app/controllers/game_controller.rb, line 47 47: def change_product 48: if @request.post? 49: @moon = Moon.find(params[:id]) 50: @moon.cancel_production 51: redirect_to_index("You don't own this moon!") and return unless @session[:user].player.owns(@moon) 52: redirect_to_index("You don't have enough money!") and return unless @session[:user].player.money >= Spaceship.get_price(params[:product])*params[:amount].to_i.abs 53: @moon.product = params[:product] 54: @moon.amount = params[:amount] 55: player = @session[:user].player 56: player.money -= Spaceship.get_price(@moon.product) * @moon.amount 57: player.save 58: case @moon.product 59: when 'satellite' 60: @moon.rounds_left = 2 61: when 'money' 62: @moon.rounds_left = 1 63: else 64: @moon.rounds_left = @moon.rounds_needed_to_produce_ship 65: end 66: if @moon.save 67: flash[:notice] = "Changed production on #{@moon.name}" 68: else 69: flash[:notice] = "Could not change production on #{@moon.name}" 70: end 71: end 72: redirect_to_index and return 73: end
Displays the moowaon galaxy
# File app/controllers/game_controller.rb, line 13 13: def galaxy 14: @player = @session[:user].player 15: @all_moons = Moon.find(:all) 16: @selected_moon = @session[:moon] || @player.home_moon 17: seconds_to_next_round = (@player.last_update_on + Game.round_length) - Time.now 18: @minutes_remaining = (seconds_to_next_round/60).to_i.to_s.rjust(2,"0") 19: # I hate RadRails syntax highlighting 20: @seconds_remaining = (seconds_to_next_round%60).to_i.to_s.rjust(2,"0") 21: end
Alias for galaxy
# File app/controllers/game_controller.rb, line 7 7: def index 8: galaxy 9: render_action 'galaxy' 10: end
Display a log of what happened
# File app/controllers/game_controller.rb, line 206 206: def log 207: @player = @session[:user].player 208: @events = @player.log_events.sort { |b,a| a.created_on <=> b.created_on } [0..40] # Sort events 209: (@player.log_events - @events).each { |e| e.destroy } # Delete old events 210: end
Moon overview
# File app/controllers/game_controller.rb, line 31 31: def moon 32: @player = @session[:user].player 33: params[:id] ||= @player.home_moon.id 34: @moon = Moon.find(params[:id]) 35: redirect_to_index("You don't own this moon!") and return unless @player.owns(@moon) 36: @origin = @session[:moon] 37: @fleet = @origin.fleet 38: end
Interface for the player view
# File app/controllers/game_controller.rb, line 41 41: def player 42: redirect_to_index("You didn't specify a player") and return if params[:id].nil? 43: @player = Player.find(params[:id]) 44: end
Selects a moon (this is now done with ajax calling: ajax_select_moon)
# File app/controllers/game_controller.rb, line 24 24: def select_moon 25: @player = @session[:user].player 26: @session[:moon] = Moon.find(params[:id]) if @player.owns(Moon.find(params[:id])) 27: redirect_to_index and return 28: end
Sends the posted fleet to another moon
# File app/controllers/game_controller.rb, line 116 116: def send_fleet 117: redirect_to_index and return unless @request.post? 118: 119: @player = @session[:user].player 120: @fleet = Fleet.new(params[:fleet]) 121: @fleet.departure_on = @player.last_update_on 122: 123: @origin = Moon.find(@fleet.starting_moon) 124: @destination = Moon.find(@fleet.destination_moon) 125: 126: @fleet.arrival_on = @player.last_update_on + @destination.time_to_reach_from(@origin)*Game.round_length 127: 128: if @fleet.total_strength > @origin.fleet.total_strength || 129: @fleet.weak_ships > @origin.fleet.weak_ships || 130: @fleet.medium_ships > @origin.fleet.medium_ships || 131: @fleet.strong_ships > @origin.fleet.strong_ships 132: flash[:notice] = "You can't send more ships than you have!" 133: elsif @fleet.total_strength.zero? 134: flash[:notice] = "Trying to send no ships, hu? Well... no ships have been sent!" 135: else 136: @origin.fleet.substract(@fleet) 137: @origin.fleet.save 138: @player.fleets << @fleet 139: if @fleet.save 140: flash[:notice] = "You sent your fleet to #{@destination.name}" 141: else 142: flash[:notice] = "There was an error while sending the fleet." 143: end 144: end 145: redirect_to_index and return 146: end
Sends the posted money to another player
# File app/controllers/game_controller.rb, line 84 84: def send_money 85: if @request.post? 86: reciever = Player.find(params[:id]) 87: sender = @session[:user].player 88: money = params[:amount].to_i.abs 89: redirect_to_index("You don't have that much money!") and return if money > sender.money 90: redirect_to_index("Trying to send no money? No money has been sent!") and return if money.zero? 91: reciever.money += money 92: sender.money -= money 93: reciever.add_to_log "You got #{money} money from #{sender.user.login}" 94: sender.add_to_log "You send #{money} money to #{reciever.user.login}" 95: reciever.save 96: sender.save 97: redirect_to_index("You send #{money} money to #{reciever.user.login}") and return 98: else 99: retdirect_to_index and return 100: end 101: end
This is were all events are handled, this function gets called everytime you load the site
# File app/controllers/game_controller.rb, line 213 213: def update 214: return true if @session[:user].nil? # If there's no user, then dont run update and just say the filter passed 215: player = @session[:user].player 216: 217: # Create a new player for the user if there's none 218: if player.nil? 219: @session[:user].player = Player.new 220: player = @session[:user].player 221: m = Moon.make 222: m.home_moon = 'true' 223: m.size = '2' 224: m.fleet.weak_ships,m.fleet.medium_ships,m.fleet.strong_ships = 5,5,5 225: player.moons << m 226: player.save 227: m.fleet.save 228: m.save 229: Galaxy.add_moons(3) # Add new moons 230: end 231: 232: @session[:moon] ||= player.home_moon # Initialize session moon 233: 234: if player.rounds_passed > 0 235: player.collect_taxes 236: player.run_production 237: player.run_fights 238: 239: player.last_update_on += player.rounds_passed*Game.round_length 240: player.save 241: 242: # Run fights for attacking players 243: player.moons.each do |m| 244: Fleet.find(:all, :conditions => ["moving = 'true' AND destination_moon = ? AND arrival_on < NOW()",m.id]).map { |f| f.player} .uniq.each { |p| p.run_fights } 245: end 246: end 247: true 248: end