2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/play/level/control_bar'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-11-29 15:46:04 -05:00
|
|
|
GameMenuModal = require 'views/play/menu/GameMenuModal'
|
2014-11-18 12:01:04 -05:00
|
|
|
RealTimeModel = require 'models/RealTimeModel'
|
Real-time multiplayer initial commit
Simple matchmaking, synchronous multiplayer PVP, flags!
Rough matchmaking is under the game menu multiplayer tab, for ladder
games only. After creating a 2-person game there, you can exit that
modal and real-time cast to play against each other.
If you’re the first person to cast, you’ll sit at the real-time level
playback view waiting until the other player casts. When they do, you
both should start the real-time playback (and start placing flags like
crazy people).
If in a multiplayer session, the real-time simulation runs the players’
code against each other. Your multiplayer opponent’s name should be up
near the level name.
Multiplayer sessions are stored completely in Firebase for now, and
removed if both players leave the game. There’s plenty of bugs,
synchronization issues, and minimal polish to add before we push it to
master.
2014-08-29 02:34:07 -04:00
|
|
|
RealTimeCollection = require 'collections/RealTimeCollection'
|
2014-11-07 11:54:22 -05:00
|
|
|
LevelSetupManager = require 'lib/LevelSetupManager'
|
2014-11-29 15:46:04 -05:00
|
|
|
GameMenuModal = require 'views/play/menu/GameMenuModal'
|
2014-11-20 15:41:53 -05:00
|
|
|
CampaignOptions = require 'lib/CampaignOptions'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-17 20:20:11 -04:00
|
|
|
module.exports = class ControlBarView extends CocoView
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'control-bar-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'bus:player-states-changed': 'onPlayerStatesChanged'
|
2014-11-08 11:58:36 -05:00
|
|
|
'level:disable-controls': 'onDisableControls'
|
|
|
|
'level:enable-controls': 'onEnableControls'
|
2014-11-18 14:21:29 -05:00
|
|
|
'ipad:memory-warning': 'onIPadMemoryWarning'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
events:
|
2014-08-27 15:24:03 -04:00
|
|
|
'click #next-game-button': -> Backbone.Mediator.publish 'level:next-game-pressed', {}
|
2014-08-26 01:05:24 -04:00
|
|
|
'click #game-menu-button': 'showGameMenuModal'
|
|
|
|
'click': -> Backbone.Mediator.publish 'tome:focus-editor', {}
|
2014-11-20 14:11:27 -05:00
|
|
|
'click .levels-link-area': 'onClickHome'
|
2014-09-25 03:02:53 -04:00
|
|
|
'click .home a': 'onClickHome'
|
2014-11-18 12:01:04 -05:00
|
|
|
'click .multiplayer-area': 'onClickMultiplayer'
|
2014-09-25 03:02:53 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
constructor: (options) ->
|
|
|
|
@worldName = options.worldName
|
|
|
|
@session = options.session
|
|
|
|
@level = options.level
|
2014-11-18 12:01:04 -05:00
|
|
|
@levelID = @level.get('slug')
|
2014-03-12 20:51:09 -04:00
|
|
|
@spectateGame = options.spectateGame ? false
|
2014-01-03 13:32:13 -05:00
|
|
|
super options
|
2014-11-18 12:01:04 -05:00
|
|
|
if @isMultiplayerLevel = @level.get('type') in ['hero-ladder']
|
|
|
|
@multiplayerStatusManager = new MultiplayerStatusManager @levelID, @onMultiplayerStateChanged
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
setBus: (@bus) ->
|
|
|
|
|
|
|
|
onPlayerStatesChanged: (e) ->
|
2014-02-12 15:41:41 -05:00
|
|
|
# TODO: this doesn't fire any more. Replacement?
|
2014-01-03 13:32:13 -05:00
|
|
|
return unless @bus is e.bus
|
|
|
|
numPlayers = _.keys(e.players).length
|
|
|
|
return if numPlayers is @numPlayers
|
|
|
|
@numPlayers = numPlayers
|
|
|
|
text = 'Multiplayer'
|
|
|
|
text += " (#{numPlayers})" if numPlayers > 1
|
|
|
|
$('#multiplayer-button', @$el).text(text)
|
|
|
|
|
2014-11-18 12:01:04 -05:00
|
|
|
onMultiplayerStateChanged: => @render?()
|
|
|
|
|
2014-02-26 21:58:25 -05:00
|
|
|
getRenderData: (c={}) ->
|
|
|
|
super c
|
|
|
|
c.worldName = @worldName
|
|
|
|
c.multiplayerEnabled = @session.get('multiplayer')
|
2014-10-18 17:51:43 -04:00
|
|
|
c.ladderGame = @level.get('type') in ['ladder', 'hero-ladder']
|
2014-11-18 12:01:04 -05:00
|
|
|
if c.isMultiplayerLevel = @isMultiplayerLevel
|
|
|
|
c.multiplayerStatus = @multiplayerStatusManager?.status
|
2014-03-12 20:51:09 -04:00
|
|
|
c.spectateGame = @spectateGame
|
2014-11-18 14:21:29 -05:00
|
|
|
@homeViewArgs = [{supermodel: if @hasReceivedMemoryWarning then null else @supermodel}]
|
2014-10-18 17:51:43 -04:00
|
|
|
if @level.get('type', true) in ['ladder', 'ladder-tutorial', 'hero-ladder']
|
2014-09-25 03:02:53 -04:00
|
|
|
levelID = @level.get('slug').replace /\-tutorial$/, ''
|
|
|
|
@homeLink = c.homeLink = '/play/ladder/' + levelID
|
|
|
|
@homeViewClass = require 'views/play/ladder/LadderView'
|
|
|
|
@homeViewArgs.push levelID
|
2014-10-18 17:51:43 -04:00
|
|
|
else if @level.get('type', true) in ['hero', 'hero-coop']
|
2014-09-25 18:32:33 -04:00
|
|
|
@homeLink = c.homeLink = '/play'
|
2014-09-25 03:02:53 -04:00
|
|
|
@homeViewClass = require 'views/play/WorldMapView'
|
2014-11-20 15:41:53 -05:00
|
|
|
campaign = CampaignOptions.getCampaignForSlug @level.get 'slug'
|
2014-11-10 15:47:24 -05:00
|
|
|
if campaign isnt 'dungeon'
|
|
|
|
@homeLink += '/' + campaign
|
|
|
|
@homeViewArgs.push campaign
|
2014-05-05 18:33:08 -04:00
|
|
|
else
|
2014-09-25 03:02:53 -04:00
|
|
|
@homeLink = c.homeLink = '/'
|
|
|
|
@homeViewClass = require 'views/HomeView'
|
2014-08-19 00:49:58 -04:00
|
|
|
c.editorLink = "/editor/level/#{@level.get('slug')}"
|
2014-11-11 19:36:44 -05:00
|
|
|
c.homeLink = @homeLink
|
2014-02-26 21:58:25 -05:00
|
|
|
c
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-08-08 14:36:41 -04:00
|
|
|
showGameMenuModal: ->
|
2014-11-07 11:54:22 -05:00
|
|
|
gameMenuModal = new GameMenuModal level: @level, session: @session, supermodel: @supermodel
|
|
|
|
@openModalView gameMenuModal
|
|
|
|
@listenToOnce gameMenuModal, 'change-hero', ->
|
2014-11-09 19:19:18 -05:00
|
|
|
@setupManager?.destroy()
|
2014-11-18 12:01:04 -05:00
|
|
|
@setupManager = new LevelSetupManager({supermodel: @supermodel, levelID: @levelID, parent: @, session: @session})
|
2014-11-09 19:19:18 -05:00
|
|
|
@setupManager.open()
|
Real-time multiplayer initial commit
Simple matchmaking, synchronous multiplayer PVP, flags!
Rough matchmaking is under the game menu multiplayer tab, for ladder
games only. After creating a 2-person game there, you can exit that
modal and real-time cast to play against each other.
If you’re the first person to cast, you’ll sit at the real-time level
playback view waiting until the other player casts. When they do, you
both should start the real-time playback (and start placing flags like
crazy people).
If in a multiplayer session, the real-time simulation runs the players’
code against each other. Your multiplayer opponent’s name should be up
near the level name.
Multiplayer sessions are stored completely in Firebase for now, and
removed if both players leave the game. There’s plenty of bugs,
synchronization issues, and minimal polish to add before we push it to
master.
2014-08-29 02:34:07 -04:00
|
|
|
|
2014-09-25 03:02:53 -04:00
|
|
|
onClickHome: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
Backbone.Mediator.publish 'router:navigate', route: @homeLink, viewClass: @homeViewClass, viewArgs: @homeViewArgs
|
2014-11-08 11:58:36 -05:00
|
|
|
|
2014-11-18 12:01:04 -05:00
|
|
|
onClickMultiplayer: (e) ->
|
|
|
|
@openModalView new GameMenuModal showTab: 'multiplayer', level: @level, session: @session, supermodel: @supermodel
|
|
|
|
|
2014-11-08 11:58:36 -05:00
|
|
|
onDisableControls: (e) -> @toggleControls e, false
|
|
|
|
onEnableControls: (e) -> @toggleControls e, true
|
|
|
|
toggleControls: (e, enabled) ->
|
|
|
|
return if e.controls and not ('level' in e.controls)
|
|
|
|
return if enabled is @controlsEnabled
|
|
|
|
@controlsEnabled = enabled
|
|
|
|
@$el.toggleClass 'controls-disabled', not enabled
|
2014-11-09 19:19:18 -05:00
|
|
|
|
2014-11-18 14:21:29 -05:00
|
|
|
onIPadMemoryWarning: (e) ->
|
|
|
|
@hasReceivedMemoryWarning = true
|
|
|
|
|
2014-11-09 19:19:18 -05:00
|
|
|
destroy: ->
|
|
|
|
@setupManager?.destroy()
|
2014-11-18 12:01:04 -05:00
|
|
|
@multiplayerStatusManager?.destroy()
|
2014-11-09 19:19:18 -05:00
|
|
|
super()
|
2014-11-18 12:01:04 -05:00
|
|
|
|
|
|
|
# MultiplayerStatusManager ######################################################
|
|
|
|
#
|
|
|
|
# Manages the multiplayer status, and calls @statusChangedCallback when it changes.
|
|
|
|
#
|
|
|
|
# It monitors these:
|
|
|
|
# Real-time multiplayer players
|
|
|
|
# Internal multiplayer status
|
|
|
|
#
|
|
|
|
# Real-time state variables:
|
|
|
|
# @playersCollection - Real-time multiplayer players
|
|
|
|
#
|
|
|
|
# TODO: Not currently using player counts. Should remove if we keep simple design.
|
|
|
|
#
|
|
|
|
class MultiplayerStatusManager
|
|
|
|
|
|
|
|
constructor: (@levelID, @statusChangedCallback) ->
|
|
|
|
@status = ''
|
|
|
|
# @players = {}
|
|
|
|
# @playersCollection = new RealTimeCollection('multiplayer_players/' + @levelID)
|
|
|
|
# @playersCollection.on 'add', @onPlayerAdded
|
|
|
|
# @playersCollection.each (player) => @onPlayerAdded player
|
|
|
|
Backbone.Mediator.subscribe 'real-time-multiplayer:player-status', @onMultiplayerPlayerStatus
|
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
Backbone.Mediator.unsubscribe 'real-time-multiplayer:player-status', @onMultiplayerPlayerStatus
|
|
|
|
# @playersCollection?.off 'add', @onPlayerAdded
|
|
|
|
# player.off 'change', @onPlayerChanged for id, player of @players
|
|
|
|
|
2014-11-20 14:11:27 -05:00
|
|
|
onMultiplayerPlayerStatus: (e) =>
|
2014-11-18 12:01:04 -05:00
|
|
|
@status = e.status
|
|
|
|
@statusChangedCallback()
|
|
|
|
|
|
|
|
# onPlayerAdded: (player) =>
|
|
|
|
# unless player.id is me.id
|
|
|
|
# @players[player.id] = new RealTimeModel('multiplayer_players/' + @levelID + '/' + player.id)
|
|
|
|
# @players[player.id].on 'change', @onPlayerChanged
|
|
|
|
# @countPlayers player
|
2014-11-20 14:11:27 -05:00
|
|
|
#
|
2014-11-18 12:01:04 -05:00
|
|
|
# onPlayerChanged: (player) =>
|
|
|
|
# @countPlayers player
|
2014-11-20 14:11:27 -05:00
|
|
|
#
|
2014-11-18 12:01:04 -05:00
|
|
|
# countPlayers: (changedPlayer) =>
|
|
|
|
# # TODO: save this stale hearbeat threshold setting somewhere
|
|
|
|
# staleHeartbeat = new Date()
|
|
|
|
# staleHeartbeat.setMinutes staleHeartbeat.getMinutes() - 3
|
|
|
|
# @playerCount = 0
|
|
|
|
# @playersCollectionAvailable = 0
|
|
|
|
# @playersCollectionUnavailable = 0
|
|
|
|
# @playersCollection.each (player) =>
|
|
|
|
# # Assume changedPlayer is fresher than entry in @playersCollection collection
|
|
|
|
# player = changedPlayer if changedPlayer? and player.id is changedPlayer.id
|
|
|
|
# unless staleHeartbeat >= new Date(player.get('heartbeat'))
|
|
|
|
# @playerCount++
|
|
|
|
# @playersCollectionAvailable++ if player.get('state') is 'available'
|
|
|
|
# @playersCollectionUnavailable++ if player.get('state') is 'unavailable'
|
|
|
|
# @statusChangedCallback()
|