Beispiel #1
0
        public void killPeer(STServerPeer peer)
        {
            Log.Debug("Kill Peer: " + peer.client_id);
            peer.Disconnect();

            setupCleaner();
        }
Beispiel #2
0
        private void sendScoreToPeer(Dictionary <byte, object> led_score_params)
        {
            int[] score = new int[2];
            int[] icar  = new int[2];

            score[0] = (int)led_score_params[(byte)1];
            score[1] = (int)led_score_params[(byte)2];

            icar[0] = (int)led_score_params[(byte)3];
            icar[1] = (int)led_score_params[(byte)4];

            //int iwinner = (score1<score2) ? 1 : 0;

            for (int i = 0; i < mcur_player; ++i)
            {
                int index = icur_player + i;
                if (index < 0 || index >= waiting_list.Count)
                {
                    continue;
                }
                STServerPeer peer = waiting_list[icur_player + i];
                peer.sendEventToPeer(STServerCode.CSend_GG,
                                     new Dictionary <byte, object>()
                {
                    { (byte)1, (score[i] >= score[(i + 1) % 2])?1:0 }, { (byte)2, score[i] }, { (byte)3, icar[i] }
                });

                InsertToSql(new String[] { peer.client_id, "score: " + score[i] });
            }
        }
Beispiel #3
0
        public void setupLedPeer(STServerPeer peer)
        {
            //if(led_peer!=null) return;


            aclient_peer.Remove(peer);

            // disconnect all other clients
            foreach (STServerPeer rest_peer in aclient_peer)
            {
                rest_peer.delayDisconnect();
            }
            //aclient_peer.Clear();


            this.led_peer = peer;
            LED_Ready     = true;

            if (debug_mode)
            {
                initGame(debug_game);
            }
            else
            {
                initGame(getGameSchedule() - 1);
            }
        }
Beispiel #4
0
 private int getNewWaitingIndex(STServerPeer peer)
 {
     lock (waiting_list)
     {
         // put in wait list
         waiting_list.Add(peer);
         return(waiting_list.Count - 1);
     }
 }
Beispiel #5
0
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            STServerPeer new_connected = new STServerPeer(initRequest.Protocol, initRequest.PhotonPeer, game_app);

            //lconnected_peer.Add(new_connected);
            //Log.Debug("mpeer="+lconnected_peer.Count);
            game_app.addNewClientPeer(new_connected);


            return(new_connected);
        }
Beispiel #6
0
        /* when client disconnect */
        override public void removeClient(STServerPeer peer_to_remove)
        {
            string sid  = peer_to_remove.client_id;
            int    side = peer_to_remove.client_side;

            Log.Debug("Client Disconnect!! " + sid);

            Dictionary <byte, Object> param = new Dictionary <byte, object>();

            param.Add((byte)100, sid);
            param.Add((byte)101, side);
            game_app.SendNotifyLED(STServerCode.LSet_User_Leave, param);

            lock (online_client)
            {
                bool rm = online_client.Remove(peer_to_remove);
                if (rm)
                {
                    Log.Debug("     Remove Disconnect Client Success!!");
                }
                else
                {
                    Log.Debug("     Remove Disconnect Client Fail!!");
                }
            }

            if (sid != null)
            {
                lock (left_online_client)
                {
                    left_online_client.Remove(sid);
                }
                lock (right_online_client)
                {
                    right_online_client.Remove(sid);
                }

                removeIdInGame(sid);
                InsertToSql(new String[] { sid, "Disconnect" });
            }
        }
Beispiel #7
0
        /* when client disconnect */
        override public void removeClient(STServerPeer peer_to_remove)
        {
            string sid = peer_to_remove.client_id;

            lock (online_client)
            {
                bool rm = online_client.Remove(peer_to_remove);
                if (rm)
                {
                    Log.Debug("     Remove Disconnect Client Success!!");
                }
                else
                {
                    Log.Debug("     Remove Disconnect Client Fail!!");
                }
            }

            if (sid != null)
            {
                removeIdInGame(sid);
                InsertToSql(new String[] { sid, "Disconnect" });
            }
        }
Beispiel #8
0
 virtual public void removeClient(STServerPeer peer_to_remove)
 {
 }
Beispiel #9
0
 virtual public void handleMessage(STServerPeer sender, STClientCode code, Dictionary <byte, object> ev_params)
 {
 }
Beispiel #10
0
        override public void handleMessage(STServerPeer sender, STClientCode code, Dictionary <byte, object> event_params)
        {
            String sid = (event_params.ContainsKey((byte)100)) ? (String)event_params[(byte)100] : "";

            Dictionary <byte, object> response_params = new Dictionary <byte, object>();

            switch (code)
            {
            case STClientCode.APP_Join:
                Log.Warn("Join Game B!!");


                int join_success = checkJoinSuccess(event_params);
                response_params.Add((byte)1, join_success);


                if (join_success == 1)
                {
                    int iwait = getNewWaitingIndex(sender);
                    Log.Debug("New in Waiting List: " + iwait);
                    response_params.Add((byte)101, iwait);
                    addIdInGame(sid);

                    InsertToSql(new String[] { sid, "Join Game" });
                    lock (online_client)
                    {
                        online_client.Add(sender);
                    }
                    checkWaitingStatus();
                }
                else
                {
                    //sender.delayDisconnect(5);
                }

                sender.sendOpResponseToPeer(STServerCode.CJoin_Success, response_params);


                break;

            case STClientCode.APP_Rotate:
                if (isIdInGame(sid))
                {
                    game_app.SendNotifyLED(STServerCode.LSet_Rotate, event_params);
                }
                else
                {
                    Log.Error("!! Not in-game ID: " + sid + " ! Kill it!!");
                    //sender.delayDisconnect(3);
                }
                break;

            case STClientCode.LED_StartRun:
                if (waiting_list.Count >= mcur_player)
                {
                    sendStartRun();
                }
                break;

            case STClientCode.LED_EatIcon:

                int          ipeer = (int)event_params[(byte)101];
                STServerPeer peer  = null;
                if (ipeer == 1 && waiting_list.Count > 0)
                {
                    peer = waiting_list[icur_player];
                }
                if (ipeer == 0 && waiting_list.Count > 1)
                {
                    peer = waiting_list[icur_player + 1];
                }

                if (peer != null)
                {
                    peer.sendEventToPeer(STServerCode.CGameB_Eat, event_params);
                }

                break;

            case STClientCode.LED_Score:
                sender.sendOpResponseToPeer(STServerCode.LSend_Score_Success, response_params);
                //game_app.sendGameOverToAll(event_params);
                sendScoreToPeer(event_params);
                EndRound();

                InsertToSql(new String[] { "game", "End Round" });

                break;
            }
        }
Beispiel #11
0
        override public void handleMessage(STServerPeer sender, STClientCode code, Dictionary <byte, object> event_params)
        {
            String sid = (event_params.ContainsKey((byte)100))?(String)event_params[(byte)100]:"";

            Dictionary <byte, object> response_params = new Dictionary <byte, object>();

            switch (code)
            {
            case STClientCode.APP_Join:
                Log.Warn("Join Game A!!");
                //bool enough_time = checkEnoughTimeForPlayer();
                //if (!enough_time)
                //{
                //    Log.Debug("Not Enough Time!!");
                //    response_params.Add(1, 0);
                //}else{

                int join_success = checkJoinSuccess(event_params);
                response_params.Add(1, join_success);

                if (join_success == 1)
                {
                    lock (online_client)
                    {
                        online_client.Add(sender);
                    }

                    addIdInGame(sid);

                    InsertToSql(new String[] { sid, "Join Game" });
                }
                else
                {
                    // sender.delayDisconnect(5);
                }
                // if is first one joining
                //if(isWaiting()) StartRound();
                //}
                sender.sendOpResponseToPeer(STServerCode.CJoin_Success, response_params);
                return;

            case STClientCode.LED_StartRun:
                bool enough_time_round = checkEnoughTimeForRound(INTERACTIVE_SPAN);
                if (isWaiting() && enough_time_round)
                {
                    response_params.Add(1, 1);

                    response_params.Add(2, getGameRemainTime());

                    //sender.sendOpResponseToPeer(STServerCode.LGameB_Start, response_params);
                    game_app.SendNotifyLED(STServerCode.LGameB_Start, response_params);
                    StartRound();
                    return;
                }

                Log.Debug("Not Enough Time!!");
                response_params.Add(1, 0);

                game_app.SendNotifyLED(STServerCode.LGameB_Start, response_params);


                return;

            case STClientCode.LED_Score:
                sender.sendOpResponseToPeer(STServerCode.LSend_Score_Success, response_params);
                game_app.sendGameOverToAll(event_params);
                EndRound();

                return;
            }

            if (!isIdInGame(sid))
            {
                Log.Error("!! Not in-game ID: " + sid + " ! Kill it!!");
                //sender.delayDisconnect(3);
                return;
            }

            switch (code)
            {
            case STClientCode.APP_Set_Side:

                bool has_vacancy = false;
                int  side_index  = -1;
                if ((int)event_params[(byte)101] == 1)
                {
                    lock (left_online_client)
                    {
                        has_vacancy = (left_online_client.Count < Client_Limit / 2);
                        if (has_vacancy)
                        {
                            if (!left_online_client.ContainsKey((String)event_params[(byte)100]))
                            {
                                left_online_client.Add((String)event_params[(byte)100], sender);
                            }
                            side_index         = 1;
                            sender.client_side = side_index;
                        }
                        else
                        {
                            Log.Debug("No vacancy at left " + left_online_client.Count);
                        }
                    }
                }
                else if ((int)event_params[(byte)101] == 0)
                {
                    lock (right_online_client)
                    {
                        has_vacancy = (right_online_client.Count < Client_Limit / 2);
                        if (has_vacancy)
                        {
                            if (!right_online_client.ContainsKey((String)event_params[(byte)100]))
                            {
                                right_online_client.Add((String)event_params[(byte)100], sender);
                            }
                            side_index         = 0;
                            sender.client_side = side_index;
                        }
                        else
                        {
                            Log.Debug("No vacancy at right " + right_online_client.Count);
                        }
                    }
                }
                response_params.Add(1, has_vacancy?1:0);
                response_params.Add(101, side_index);

                sender.sendOpResponseToPeer(STServerCode.CSet_Side_Success, response_params);

                game_app.SendNotifyLED(STServerCode.LAdd_House, event_params);

                break;


            case STClientCode.APP_Set_Name:

                //TODO:check id & side??

                String _name = (String)event_params[(byte)1];
                //check word
                bool isgood = game_app.checkGoodName(_name);

                response_params.Add((byte)1, isgood?1:2);

                if (isgood)
                {
                    byte[] _bname = System.Text.Encoding.UTF8.GetBytes(_name);
                    event_params[(byte)1] = _bname;

                    game_app.SendNotifyLED(STServerCode.LSet_Name, event_params);

                    InsertToSql(new String[] { sid, "Name: " + _name });
                }


                sender.sendOpResponseToPeer(STServerCode.CSet_Name_Success, response_params);

                break;

            case STClientCode.APP_Set_House:

                //TODO: check id & side

                response_params.Add((byte)1, 1);
                game_app.SendNotifyLED(STServerCode.LSet_House, event_params);

                sender.sendOpResponseToPeer(STServerCode.CSet_House_Success, response_params);

                break;

            case STClientCode.APP_Blow:
                game_app.SendNotifyLED(STServerCode.LSet_Blow, event_params);
                break;

            case STClientCode.APP_Light:
                game_app.SendNotifyLED(STServerCode.LSet_Light, event_params);
                break;

            case STClientCode.APP_Shake:
                game_app.SendNotifyLED(STServerCode.LSet_Shake, event_params);

                break;

            case STClientCode.APP_Leave:

                game_app.SendNotifyLED(STServerCode.LSet_User_Leave, event_params);

                response_params.Add((byte)1, 1);
                sender.sendOpResponseToPeer(STServerCode.CSet_Leave_Success, response_params);

                /* disconnect finished player */
                //sender.delayDisconnect();


                lock (online_client)
                {
                    bool rm = online_client.Remove(sender);
                    if (rm)
                    {
                        Log.Debug("     Remove Client Success!!");
                    }
                    else
                    {
                        Log.Debug("     Remove Client Fail!!");
                    }
                }

                //int side = (int)event_params[(byte)101];
                lock (left_online_client)
                {
                    left_online_client.Remove(sid);
                }
                lock (right_online_client)
                {
                    right_online_client.Remove(sid);
                }

                removeIdInGame(sid);
                InsertToSql(new String[] { sid, "Leave" });

                break;
            }
        }
Beispiel #12
0
 public void handleMessage(STServerPeer sender, STClientCode code, Dictionary <byte, object> ev_params)
 {
     agame_scene[cur_game].handleMessage(sender, code, ev_params);
 }
Beispiel #13
0
        override public void handleMessage(STServerPeer sender, STClientCode code, Dictionary <byte, object> event_params)
        {
            String sid = (event_params.ContainsKey((byte)100)) ? (String)event_params[(byte)100] : "";

            Dictionary <byte, object> response_params = new Dictionary <byte, object>();

            switch (code)
            {
            case STClientCode.APP_Join:
                Log.Warn("Join Game C!!");

                int success = checkJoinSuccess(event_params);
                response_params.Add(1, success);
                //response_params.Add(2,game_app.getClientIndex(sender));
                if (success == 1)
                {
                    lock (online_client)
                    {
                        if (!online_client.Contains(sender))
                        {
                            online_client.Add(sender);
                        }
                    }
                    addIdInGame(sid);
                }
                else
                {
                    // sender.delayDisconnect(5);
                }
                sender.sendOpResponseToPeer(STServerCode.CJoin_Success, response_params);


                break;


            case STClientCode.APP_Face:

                if (!clock_mode && isIdInGame(sid))
                {
                    // TODO: save to server
                    String uid        = (String)event_params[(byte)100];
                    byte[] image_byte = Convert.FromBase64String((String)event_params[(byte)2]);

                    bool img_good = checkImage(image_byte);
                    if (img_good)
                    {
                        String file_path = saveImage(uid, image_byte);
                        InsertToSql(new String[] { uid, file_path });
                        //event_params[(byte)2] = file_path;
                        game_app.SendNotifyLED(STServerCode.LSet_Face, event_params);
                        response_params.Add((byte)1, 1);
                    }
                    else
                    {
                        response_params.Add((byte)1, 0);
                    }

                    sender.sendOpResponseToPeer(STServerCode.CSet_Face_Success, response_params);
                }
                else
                {
                    Log.Error("!! Not in-game ID: " + sid + " ! Kill it!!");
                    response_params.Add((byte)1, 0);
                    sender.sendOpResponseToPeer(STServerCode.CSet_Face_Success, response_params);
                }
                /* disconnect finished player */
                //sender.delayDisconnect();
                removeIdInGame(sid);
                lock (online_client)
                {
                    online_client.Remove(sender);
                }
                break;

                //case STClientCode.LED_Score:
                //    sender.sendOpResponseToPeer(STServerCode.LSend_Score_Success,response_params);
                //    game_app.sendGameOverToAll(event_params);
                //    EndRound();

                //    break;
            }
        }