Skip to content

Instantly share code, notes, and snippets.

View vgheri's full-sized avatar

Valerio Gheri vgheri

View GitHub Profile
function setup(app, handlers) {
app.post('/api/profiles', handlers.account.createAccount);
app.get('/api/profiles/:username', handlers.account.getAccount);
app.put('/api/profiles/:username', handlers.account.updateAccount);
app.del('/api/profiles/:username', handlers.account.deleteAccount);
app.post('/api/lists', handlers.list.createShoppingList);
app.post('/api/lists/:id', handlers.list.createShoppingList);
app.put('/api/lists/:id', handlers.list.updateShoppingList);
app.get('/api/lists/:userId', handlers.list.getShoppingLists);
app.del('/api/lists/:id', handlers.list.deleteShoppingList);
@vgheri
vgheri / updateGame.js
Created December 24, 2012 14:09
Handling server updates
function updateGame(updatePacket) {
var goal = false;
if (pongR.game.player1.score() < updatePacket.Game.Player1.Score) {
goal = true;
}
else if (pongR.game.player2.score() < updatePacket.Game.Player2.Score) {
goal = true;
}
var remoteMe = pongR.me.playerNumber === 1 ? updatePacket.Game.Player1 : updatePacket.Game.Player2;
@vgheri
vgheri / updateLoop2.js
Created December 24, 2012 14:02
Second part of the client update loop
// A single step of the client update loop (a frame)
function updateLoopStep() {
// Step 1: Clear canvas
pongR.canvasContext.clearRect(0, 0, pongR.settings.viewport.width, pongR.settings.viewport.height);
// Step 2: Handle user inputs (update internal model)
var playerInput = handleClientInputs(pongR.me);
if (playerInput !== null) {
// Step 3: Send the just processed input batch to the server.
sendInput(pongR.game.gameId, pongR.me.user.connectionId, playerInput);
}
@vgheri
vgheri / updateLoop1.js
Created December 24, 2012 14:02
First part of the client update loop
// Starts the client update loop
function startUpdateLoop() {
if (pongR.goalTimestamp) {
var now = new Date().getTime();
var timelapse = (now - pongR.goalTimestamp) / 1000; // in seconds
if (timelapse > pongR.settings.PAUSE_AFTER_GOAL) { // TODO Change here to adjust with network latency
pongR.goalTimestamp = undefined;
updateLoopStep();
}
}
@vgheri
vgheri / physicsLoop.js
Created December 24, 2012 14:01
client physics loop
function updatePhysics() {
// 1: updates self position and direction
if (!pongR.updateTimestamp) { // The first time this loop runs, pongR.updateTimestamp will be undefined
pongR.updateTimestamp = new Date().getTime();
}
else {
var now = new Date().getTime();
pongR.deltaTime = (now - pongR.updateTimestamp) / 1000;
//console.log("Delta time: " + pongR.deltaTime + ". Now: " + now + ", previous update: " + pongR.updateTimestamp);
pongR.updateTimestamp = now;
@vgheri
vgheri / setupMatch.js
Created December 24, 2012 14:00
Match setup
function setupMatch(opts) {
$("#waiter").remove();
initMatch(opts);
performCountdown(3, startGame);
};
// Initial setup of the match state
function initMatch(opts) {
pongR.game = new Game(opts.PlayRoomId, opts.Player1, opts.Player2, opts.BallDirection);
@vgheri
vgheri / connection.js
Created December 24, 2012 13:45
SignalR connection flow
$(document).ready(function () {
var username = @Html.Raw(Json.Encode(Model));
PongR.createInstance(1200,600,username);
PongR.connect();
});
// Public methods
pongR.PublicPrototype.createInstance = function (width, height, username) {
pongR.settings = new Settings(width, height);
@vgheri
vgheri / qUnit_example.js
Created December 24, 2012 13:39
qUnit unit test example
//process_input(player) : number // returns the increment on the y axis
test("Test process_input", function () {
// Test 1
var commands1 = ["up", "up"];
var inputs = [{ commands: commands1, sequenceNumber: 0}];
var player = { inputs: inputs, lastProcessedInputId: -1 };
var expectedIncrement = -20;
var observedIncrement = PongR.UnitTestPrototype.process_input(player, 10, 1);
deepEqual(observedIncrement, expectedIncrement);
deepEqual(player.lastProcessedInputId, 0);
@vgheri
vgheri / gist:4106163
Created November 18, 2012 16:45
Elapsed event handler
// Specify what you want to happen when the Elapsed event is
// raised.
public static void OnPhysicsTimedEvent(object source, ElapsedEventArgs e)
{
Engine.ProcessGamesTick();
}
// Specify what you want to happen when the Elapsed event is
// raised.
public static void OnUpdateClientsTimedEvent(object source, ElapsedEventArgs e)
@vgheri
vgheri / gist:4106081
Created November 18, 2012 16:24
Notifier
public class Notifier
{
// Synchronize clients with the new authoritative game status
public static void UpdateClients(Game game)
{
var packet = new UpdatePacket()
{
Game = game,
//Timestamp = DateTime.UtcNow.Subtract(new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc)).TotalMilliseconds
Timestamp = DateTime.UtcNow