-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dff459
commit 9d7ca0c
Showing
24 changed files
with
3,819 additions
and
2,816 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
using Newtonsoft.Json; | ||
|
||
namespace Nim.Adaptors | ||
{ | ||
public class JsonHelper | ||
{ | ||
public static object SerializeObject<T>(T game) | ||
{ | ||
return JsonConvert.DeserializeObject( | ||
JsonConvert.SerializeObject(game, Formatting.Indented, new JsonSerializerSettings() | ||
{ | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore | ||
}) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
Nim/Content/Scripts/Nim/Controllers/GameController.v2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/// <reference path="../docs.js" /> | ||
|
||
define(["$", "Underscore", "Backbone", "Marionette", "Nim/App", "Nim/Views/GameLayout"], function ($, _, Backbone, Marionette, app, GameLayout) { | ||
var CanvasViewModel = Backbone.Model.extend({ | ||
}); | ||
|
||
var gameController, GameController = Backbone.Marionette.Controller.extend({ | ||
//Properties | ||
game: null, | ||
layout: null, | ||
|
||
//Constructor | ||
initialize: function () { | ||
this.layout = new GameLayout(); | ||
}, | ||
|
||
//Methods | ||
start: function (game) { | ||
var gameController = this; | ||
|
||
//Main starting point for a game | ||
this.sync(game); | ||
|
||
//Display the layout | ||
app.content.show(this.layout); | ||
|
||
//Display the canvas | ||
require(["Nim/Views/CanvasView"], function (CanvasView) { | ||
gameController.layout.canvas.show(new CanvasView({ | ||
numberOfLines: gameController.game.ActiveGame.NumberOfLines, | ||
controller: gameController, | ||
model: new CanvasViewModel({ | ||
lines: createLinesArray(gameController.game.ActiveGame.NumberOfLines) | ||
}) | ||
})); | ||
}); | ||
|
||
this.switchTurn(); | ||
}, | ||
sync: function (game) { | ||
//Should be call every time a callback from the server comes | ||
|
||
this.game = game; | ||
}, | ||
crossOut: function (sum) { | ||
// Send a message to the server about the sum of lines to cross out | ||
app.gameHub.server.requestCrossOut(this.game.GameId, sum); | ||
}, | ||
playAgain: function () { //When a user want to play again with the opponets | ||
// Tell the server that this player wants to play again | ||
|
||
//TODO: Use the factory | ||
app.gameHub.server.requestSpecificGame(this.game.GameId, app.user.get("playerId")); | ||
}, | ||
finish: function (winner) { | ||
var gameController = this; | ||
|
||
require(["Nim/Models/FinishModel", "Nim/Views/FinishView"], function (FinishModel, FinishView) { | ||
//Create a finish view | ||
var finishView = new FinishView({ | ||
model: new FinishModel({ | ||
you: (winner === app.user.get("playerId")) //TODO: BETTER | ||
}), | ||
controller: gameController | ||
}); | ||
|
||
//Display the finish view as a modal | ||
gameController.layout.modal.show(finishView); | ||
|
||
}); | ||
}, | ||
playerDisconnected: function () { | ||
var gameController = this; | ||
require(["Nim/Models/FinishModel", "Nim/Views/FinishView"], function (FinishModel, FinishView) { | ||
|
||
//Create a finish view | ||
var finishView = new FinishView({ | ||
model: new FinishModel({ | ||
you: true //TODO: BETTER | ||
}), | ||
controller: gameController | ||
}); | ||
|
||
//Display the finish view as a modal | ||
gameController.layout.modal.show(finishView); | ||
|
||
}); | ||
}, | ||
switchTurn: function () { | ||
var gameController = this; | ||
|
||
if (this.game.CurrentTurn.PlayerId === app.user.get("playerId")) { | ||
require(["Nim/Views/CommandView"], function (CommandView) { | ||
var commandView = new CommandView({ | ||
controller: gameController | ||
}); | ||
|
||
gameController.layout.command.show(commandView); | ||
}); | ||
} else { | ||
require(["Nim/Views/IdleView"], function (IdleView) { | ||
gameController.layout.command.show(new IdleView()); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
gameController = new GameController(); | ||
|
||
gameController.listenTo(gameController, "server:crossOut", function (sum, game) { | ||
this.sync(game); | ||
|
||
//Trigger switch turn | ||
this.switchTurn(); | ||
}); | ||
|
||
gameController.listenTo(gameController, "server:finish", function (winner, game) { | ||
this.sync(game); | ||
|
||
//Trigger finish | ||
this.finish(winner); | ||
}); | ||
|
||
gameController.listenTo(gameController, "server:player:disconnect", function (player, game) { | ||
this.sync(game); | ||
|
||
//Listen to server finish | ||
this.playerDisconnected(); | ||
}); | ||
|
||
function createLinesArray(linesCount) { | ||
var lines = []; | ||
|
||
for (var i = 0; i < linesCount; i += 1) { | ||
lines.push({}); | ||
} | ||
|
||
return lines; | ||
} | ||
|
||
return gameController; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.