Пример #1
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;
            }
        }
Пример #2
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;
            }
        }
Пример #3
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;
            }
        }