示例#1
0
        public async Task <ShapeModel> OtherPlayer(ShapeModel clientModel)
        {
            clientModel.ShapeOwner = Context.ConnectionId;
            ShapeModel temp = new ShapeModel();

            _connections.TryGetValue(clientModel.ShapeOwner, out temp);
            return(await Clients.Others.otherPlayer(temp));
        }
示例#2
0
 public void UserChoose(ShapeModel clientModel)
 {
     if (clientModel.ShapeOwner == null ||
         clientModel.ShapeOwner.Equals("none"))
     {
         clientModel.LastUpdatedBy = Context.ConnectionId;
         clientModel.ShapeOwner    = Context.ConnectionId;
         _broadcaster.UserChoose(clientModel);
     }
 }
示例#3
0
        public void SetUserName(ShapeModel sm)
        {
            ShapeModel model = new ShapeModel();

            _connections.TryGetValue(sm.ShapeOwner, out model);
            model.ShapeId        = sm.ShapeId;
            model.ActivePlayerId = sm.ActivePlayerId;
            ShapeModel temp = new ShapeModel();

            _connections.TryRemove(sm.ShapeOwner, out temp);
            _connections.TryAdd(model.ShapeOwner, model);
        }
示例#4
0
        public override Task OnDisconnected(bool stopcalled)
        {
            ShapeModel sm = new ShapeModel();

            sm.ShapeOwner = Context.ConnectionId;
            sm.PlayerId   = "player" + _connections.Count.ToString();
            sm.CoinScore  = 0;
            ShapeModel value;

            _connections.TryRemove(Context.ConnectionId, out value);
            sm.PlayerId = value.PlayerId;
            return(Clients.AllExcept(Context.ConnectionId).clientDisconnected(sm));
        }
示例#5
0
        public override Task OnConnected()
        {
            int        free = 0;
            ShapeModel sm   = new ShapeModel();

            sm.ShapeOwner = Context.ConnectionId;
            //sm.PlayerId = "player" + (_connections.Count + 1).ToString();
            _connections.ReturnFreeId(out free);
            sm.PlayerId = "player" + free.ToString();
            sm.ShapeId  = free.ToString();
            _connections.TryAdd(Context.ConnectionId, sm);
            return(Clients.Caller.clientConnected(sm));
        }
示例#6
0
        public void MoveCoin(ShapeModel coinModel)
        {
            //Move coin to random position within game area
            Random rnd      = new Random();
            int    randLeft = rnd.Next(1, 800);
            int    randTop  = rnd.Next(1, 600);

            coinModel.Left = randLeft;
            coinModel.Top  = randTop;

            // Update the shape model within our broadcaster
            _broadcaster.UpdateCoinShape(coinModel);
        }
示例#7
0
 public Broadcaster()
 {
     // Save our hub context so we can easily use it
     // Faskdklas
     // to send to its connected clients
     _hubContext   = GlobalHost.ConnectionManager.GetHubContext <MoveShapeHub>();
     _model        = new ShapeModel();
     _modelUpdated = false;
     // Start the broadcast loop
     _broadcastLoop = new Timer(
         BroadcastShape,
         null,
         BroadcastInterval,
         BroadcastInterval);
 }
示例#8
0
        public async Task <int> SaveScore(ShapeModel winnerShape)
        {
            using (var db = new PlayerDb())
            {
                var players = from p in db.player select p;

                foreach (var p in players)
                {
                    p.GamesPlayed += 1;
                    if (p.PlayerID == winnerShape.ActivePlayerId)
                    {
                        p.GamesWon += 1;
                    }
                    db.Entry(p).State = System.Data.Entity.EntityState.Modified;
                }
                var i = await db.SaveChangesAsync();

                return(i);
            }
        }
示例#9
0
        public async Task <int> updateDb(List <ShapeModel> shapeList, ShapeModel winner)
        {
            using (var db = new PlayerDb())
            {
                var players = from p in db.player select p;

                foreach (var p in players)
                {
                    if (shapeList.Exists(s => s.ActivePlayerId == p.PlayerID))
                    {
                        p.GamesPlayed += 1;
                    }
                    if (p.PlayerID == winner.ActivePlayerId)
                    {
                        p.GamesWon += 1;
                    }
                    db.Entry(p).State = System.Data.Entity.EntityState.Modified;
                }
                var i = await db.SaveChangesAsync();

                return(i);
            }
        }
示例#10
0
        public Task UpdateScore(ShapeModel clientModel)
        {
            if (System.Web.HttpContext.Current.Application["score"] == null)
            {
                System.Web.HttpContext.Current.Application["score"] = 0;
            }
            else
            {
                System.Web.HttpContext.Current.Application["score"] = Convert.ToInt32(System.Web.HttpContext.Current.Application["score"]) + 1;
            }

            int testScore = Convert.ToInt32(System.Web.HttpContext.Current.Application["score"]);

            ShapeModel sm = new ShapeModel();

            _connections.TryGetValue(Context.ConnectionId, out sm);

            sm.CoinScore += 5;

            foreach (var item in _connections.Values)
            {
                if (item.CoinScore > 100)
                {
                    var list = _connections.Values.ToList();
                    var t    = new Task(async() =>
                    {
                        await updateDb(list, item);
                    });
                    t.Start();
                    return(Clients.All.winner(item));
                }
            }

            return(Clients.All.updateScore(sm));
            // Update the shape model within our broadcaster
        }
示例#11
0
 public void UpdateModel(ShapeModel clientModel)
 {
     clientModel.LastUpdatedBy = Context.ConnectionId;
     // Update the shape model within our broadcaster
     _broadcaster.UpdateShape(clientModel);
 }
示例#12
0
 public void UpdateCoinShape(ShapeModel coinModel)
 {
     _coinModel   = coinModel;
     _coinUpdated = true;
 }
示例#13
0
 public void UpdateShape(ShapeModel clientModel)
 {
     _model        = clientModel;
     _modelUpdated = true;
 }