Skip to content

Commit

Permalink
Timer and buildings cost
Browse files Browse the repository at this point in the history
  • Loading branch information
azram19 committed Jun 19, 2012
1 parent 385a95c commit 5bdcd11
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
# Order is EXTREMELY important here
ENGINE = Namespace.js HeightMap.js GameObject.js ObjectState.js Field.js Signal.js Types.js Player.js Properties.js SignalFactory.js HQBehaviour.js ChannelBehaviour.js PlatformBehaviour.js ResourceBehaviour.js ObjectFactory.js Map.js GameManager.js
RENDERER = Loader.js Human.js People.js Board.js MenuDisplayHelper.js MapHelper.js RadialMenu.js UI.js Terrain.js
RENDERER = Timer.js Loader.js Human.js People.js Board.js MenuDisplayHelper.js MapHelper.js RadialMenu.js UI.js Terrain.js

all: dir | js css
cp dev/build/*.js dev/webroot/js
Expand Down
37 changes: 25 additions & 12 deletions dev/client/Negotiator.coffee
@@ -1,13 +1,19 @@
class Negotiator

constructor: ( @communicator ) ->
self= @

_.extend @, Backbone.Events
@myPlayer = {}
@game = {}
@renderer = {}

window.loader = @loader = new S.Loader()
@loader.start()
@timer = new S.Timer @
@timer.setTime 1200

$.when( @loader.start() ).then ->
self.timer.start()

@loading = new $.Deferred()

Expand Down Expand Up @@ -42,7 +48,6 @@ class Negotiator
@on 'resource:produce', (xy, amount, type) ->
p = @ui.getPoint xy[0], xy[1]
@ui.showTextBubble "-#{amount}", p.x+40, p.y+20
#console.debug xy, amount, type

@on 'resource:receive', (xy, amount, type) ->
p = @ui.getPoint xy[0], xy[1]
Expand All @@ -64,13 +69,17 @@ class Negotiator
userHas[k] > v

if canAfford
_.each cost, ( v, k ) ->
userHas[k] -= v
_.each cost, ( v, k ) =>
@myPlayer.resources[k] -= v
@ui.showTextBubble "-#{v} #{ k }", p.x+40, p.y+20

@buildPlatform x, y, type, owner
@communicator.trigger 'send:build:platform', x, y, type, owner
@buildPlatform x, y, type, @myPlayer
@communicator.trigger 'send:build:platform', x, y, type, @myPlayer

@ui.showResources 0, 6
@ui.showResources 0, 7
else
@ui.showTextBubble "Not enough resources", p.x+40, p.y+20, color: color: [159, 17, 27, 1]
@ui.showTextBubble "Not enough resources", p.x+40, p.y+20, color: [159, 17, 27, 1]

@on 'build:channel', (x, y, k, owner) =>
p = @ui.getPoint x, y
Expand All @@ -82,13 +91,17 @@ class Negotiator
userHas[k] > v

if canAfford
_.each cost, ( v, k ) ->
userHas[k] -= v
_.each cost, ( v, k ) =>
@myPlayer.resources[k] -= v
@ui.showTextBubble "-#{v} #{ k }", p.x+40, p.y+20

@buildChannel x, y, k, owner
@communicator.trigger 'send:build:channel', x, y, k, owner
@buildChannel x, y, k, @myPlayer
@communicator.trigger 'send:build:channel', x, y, k, @myPlayer

@ui.showResources 0, 6
@ui.showResources 0, 7
else
@ui.showTextBubble "Not enough resources", p.x+40, p.y+20, color: color: [159, 17, 27, 1]
@ui.showTextBubble "Not enough resources", p.x+40, p.y+20, color: [159, 17, 27, 1]

@on 'routing', (obj, routing) =>
_.extend obj.platform.state.routing, routing
Expand Down
3 changes: 0 additions & 3 deletions dev/client/renderer/RadialMenu.coffee
Expand Up @@ -163,9 +163,6 @@ class RadialMenu

$.when( helperPromise ).done () ->
@executeAction.apply @, arguments

$.when( helperPromise ).fail () ->
null
else
@executeAction.call @, @event

Expand Down
61 changes: 61 additions & 0 deletions dev/client/renderer/Timer.coffee
@@ -0,0 +1,61 @@
class Timer
constructor: ( @events ) ->
@started = false

setInterval @tick, 1000

@counter = new Text()
@counter.textBaseline = "middle"
@counter.textAlign = "center"
@counter.color = "#fff"
@counter.font = "bold 32px 'Cabin', Helvetica,Arial,sans-serif"
@counter.x = 200
@counter.y = 50

canvas = document.getElementById 'timer'
@stage = new Stage canvas
@stage.addChild @counter

setTime: ( time ) ->
@time = time

start:() ->
@started = true

stop:() ->
@started = false

endOfTime: () ->
@events.trigger "time:out"

draw: () ->
minutes = Math.floor(@time / 60)
seconds = @time % 60

if minutes < 10
minutes = "0#{minutes}"

if seconds < 10
seconds = "0#{seconds}"

if minutes > 0
text = "#{ minutes }:#{ seconds }"
else
text = seconds

@counter.text = text

tick:() =>
if @started
if @time <= 0
@endOfTime()
else if @time <= 60
@time--
@draw()
else
@time--
@draw()

@stage.update()

window.S.Timer = Timer
16 changes: 16 additions & 0 deletions dev/client/templates/costinfo.handlebars
@@ -0,0 +1,16 @@
<div class='build-costinfo'>
<ul>
{{#each resources}}
{{#with this}}
<li>
<div class='img'>
<img src='/img/{{name}}.png'>
</div>
<div class='desc res-{{name}}'>
{{value}}
</div>
</li>
{{/with}}
{{/each}}
</ul>
</div>
79 changes: 71 additions & 8 deletions dev/config.coffee
Expand Up @@ -42,16 +42,22 @@ module.exports = ( app, express ) ->

app.Mongoose.model 'User', app.userSchema

app.historySchema = new Schema
players: [
app.historySchema = new Schema(
{
players: [
type: Schema.ObjectId
ref: 'User'
]
winners: [
type: Schema.ObjectId
ref: 'User'
]
channel: String
]
winners: [
type: Schema.ObjectId
ref: 'User'
]
channel: String
},
{
collection: 'history'
}
)

app.Mongoose.model 'History', app.historySchema

Expand All @@ -65,6 +71,63 @@ module.exports = ( app, express ) ->

app.Mongoose.model 'Chat', app.chatSchema

app.getHistory = ( user ) ->
defer = new Promise.Deferred()

console.log user

handleHistory = ( err, history ) =>
if err?
console.error "[Mongoose] Cannot fetch history"
console.log err
defer.reject err
else if history.length > 0
console.log history
history = _.map history, ( v, k ) ->
userInWinners = _.any history.winners, ( v ) ->
v.id == user.id

if userInWinners
v.win = true
else
v.win = false

console.log history
defer.resolve history
else
defer.resolve []

historyModel = app.Mongoose.model 'History'
historyModel
.where( user._id ).in( 'players' )
.populate( 'players winners' )
.run handleHistory

defer.promise

app.getHighscores = ->
defer = new Promise.Deferred()

handleHighscores = ( err, highscores ) =>
if err?
console.error "[Mongoose] Cannot fetch highscores"
console.log err
defer.reject err
else if highscores.length > 0
defer.resolve highscores
else
defer.resolve []

userModel = app.Mongoose.model 'User'
userModel
.find()
.desc( 'highscore' )
.limit( 10 )
.select( 'name highscore' )
.run handleHighscores

defer.promise

app.getUserImgSrc = ( user ) ->
defer = new Promise.Deferred()

Expand Down
56 changes: 56 additions & 0 deletions dev/game.coffee
Expand Up @@ -29,6 +29,9 @@ app.get '/lobby2', ( req, res ) ->
if app.requireAuth and not req.loggedIn
res.redirect '/'
else
#history = app.getHistory req.user
#highscores = app.getHighscores()

games = JSON.parse app.gameServer.getGames()
games = _.map games, ( o ) ->
o.playersConnected = ( _.flatten o.players ).length
Expand All @@ -42,6 +45,59 @@ app.get '/lobby2', ( req, res ) ->
games: games
title: 'Signals - lobby'
bodyClass: 'lobby'
history:[
{name: "Piotr Bar"
result: "Won"},
{name: "Robert Kruszewski"
result: "Defeated"},
{name: "Łukasz Koprowski"
result: "Won"}
]
highscores:[
{
position: 1
name: "Łukasz Koprowski"
points: 1234
},
{
position: 2
name: "Piotr Bar"
points: 1034
},
{
position:3
name: "Robert Kruszewski"
points: 1002
},
{
name: "Piotr Bar"
points: 1034
},
{
name: "Robert Kruszewski"
points: 1002
},
{
name: "Piotr Bar"
points: 1034
},
{
name: "Robert Kruszewski"
points: 1002
},
{
name: "Piotr Bar"
points: 1034
},
{
name: "Robert Kruszewski"
points: 1002
},
{
name: "Robert Kruszewski"
points: 1002
}
]

app.get '/game/join', ( req, res ) ->
if app.requireAuth and req.loggedIn
Expand Down
4 changes: 3 additions & 1 deletion dev/server/views/board.hbs
Expand Up @@ -13,7 +13,9 @@
<canvas width="{{x}}" height="{{y}}" id="helpers" keepalive="true"></canvas>
<canvas width="20" height="20" id="off" keepalive="true"></canvas>
<canvas width="100" height="100" id="bitmaps" keepalive="true"></canvas>
<div class='scrollIt' style='width: {{x}}; height: {{y}}; display:block;position:absolute;'></div>
<div class='scrollIt' style='width: {{x}}; height: {{y}}; display:block;position:absolute;'>
</div>
<canvas width="400" height="100" id="timer" keepalive="true" class='noScroll'></canvas>
<div class='loader'>
<canvas id='loader' width='600' height='600' class='noScroll'></canvas>
</div>
Expand Down
25 changes: 22 additions & 3 deletions dev/server/views/lobby2.hbs
Expand Up @@ -40,18 +40,37 @@
<a class="withTooltip" title="History" href="#history" data-toggle="tab"><i class="icon-book"></i></a>
</li>
<li>
<a class="withTooltip" title="Highgscores" href="#highscrores" data-toggle="tab"><i class="icon-bar-chart"></i></a>
<a class="withTooltip" title="Highgscores" href="#highscores" data-toggle="tab"><i class="icon-bar-chart"></i></a>
</li>
</ul>
</div>
</div>
<div class='row-fluid'>
<div class='span12 tab-content'>
<div class='tab-pane' id='history'>

{{#each history}}
{{#with this}}
<div class='row-fluid'>
<div class='span8 name'><span>vs</span> {{name}}</div>
<div class='span4 status status-{{result}}'>{{result}}</div>
</div>
{{/with}}
{{/each}}
</div>
<div class='tab-pane' id='highscores'>

{{#each highscores}}
{{#with this}}
<div class='row-fluid'>
<div class='span1 position'>
{{#if position}}
{{position}}.
{{/if}}
</div>
<div class='span7 name'>{{name}}</div>
<div class='span4 status'>{{points}}</div>
</div>
{{/with}}
{{/each}}
</div>
<div class='tab-pane active games' id='gameMenu'>
{{#each games}}
Expand Down

0 comments on commit 5bdcd11

Please sign in to comment.