Skip to content

Instantly share code, notes, and snippets.

View vgheri's full-sized avatar

Valerio Gheri vgheri

View GitHub Profile
@vgheri
vgheri / gist:3163644
Created July 23, 2012 13:40
$.ready()
$(document).ready(function () {
var chat = new chatR.chatViewModel();
var users = new chatR.connectedUsersViewModel();
var currentUser = new chatR.user(@Html.Raw(Json.Encode(Model))); // The username chose by the user is stored in the model
// Proxy creation
var chatHub = $.connection.chatHub; // chatHub is the name of the Hub as declared in server side code
chatHub.username = currentUser.username; // This is the round-trip state
// Client-side event handlers, as declared inside the Hub
@vgheri
vgheri / gist:4105971
Created November 18, 2012 15:57
Estabilishing a connection to the server
$(document).ready(function () {
var username = @Html.Raw(Json.Encode(Model));
PongR.createInstance(1200,600,username);
PongR.connect();
});
pongR.PublicPrototype.connect = function () {
$.connection.hub.start()
.done(function () {
@vgheri
vgheri / gist:4105999
Created November 18, 2012 16:03
Joined() method
/// <summary>
/// Invoked when a new client joins the system
/// </summary>
public void Joined()
{
// 1: Add user to list of connected users
// 2: If waiting list is empty add user to waiting list
// 3: Else find an opponent (first in the waiting list) and remove him from the waiting list
// 4: Create room and assign both users
// 5: Create a group for this room
@vgheri
vgheri / gist:4106045
Created November 18, 2012 16:13
Scheduling the server update loops
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
InMemoryUserRepository.GetInstance().ConnectedUsers.ToList().Clear();
InMemoryUserRepository.GetInstance().WaitingList.ToList().Clear();
InMemoryRoomRepository.GetInstance().Rooms.ToList().Clear();
@vgheri
vgheri / gist:4106053
Created November 18, 2012 16:15
The server physics update loop
/// <summary>
/// Compute the next state for this game, based on player inputs and ball position
/// </summary>
/// <param name="game"></param>
private static void ProcessTick(Game game)
{
// 1: Apply inputs received from players (and progressively remove them from the buffer)
// 2: Update ball position
// 3: Check for collisions and if collision, update ball status
// 4: If no collision, check for a goal condition and update status if goal
@vgheri
vgheri / gist:4106062
Created November 18, 2012 16:17
Outer physics update loop
/// <summary>
/// Run the physics update loop for each game registered in _games
/// </summary>
private static void ProcessGamesTick()
{
DateTime timestamp;
foreach(var game in _games.Values)
{
// If in one of the previous rounds we had a goal condition
if (_goalTimestamps.TryGetValue(game.GameId, out timestamp))
@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
@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 / 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 / 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);