com.happyuno.controller.JoinGameTable.java Source code

Java tutorial

Introduction

Here is the source code for com.happyuno.controller.JoinGameTable.java

Source

/*
 * Copyright 2013 Ruoke Sun.
 *
 * This file is part of WebBaseCardGame.
 *
 * WebBaseCardGame is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * WebBaseCardGame is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with WebBaseCardGame.
 * 
 * If not, see <http://www.gnu.org/licenses/>.
 */

package com.happyuno.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;

import org.json.JSONArray;
import org.json.JSONObject;

import com.happyuno.entity.User;
import com.happyuno.entity.maindeal;
import com.happyuno.model.GameTable;

@WebServlet("/JoinGameTable")
public class JoinGameTable extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
      * @see HttpServlet#HttpServlet()
      */
    public JoinGameTable() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        //?
        String tableId = (String) request.getParameter("tableId");
        //      int tableId =Integer.parseInt(request.getSession().getAttribute("tableId").toString());

        //      String setNum = request.getParameter("setNum").trim();

        //      
        ServletContext application = request.getSession().getServletContext();
        HashMap<Integer, GameTable> gameTableMap = (HashMap<Integer, GameTable>) application
                .getAttribute("gameTableMap");

        //?ID user
        String userId = (String) request.getSession().getAttribute("userId");
        maindeal md = new maindeal();
        List dbUserList = md.query("from User as u where u.id='" + userId + "'");
        User curUser = (User) dbUserList.get(0);

        //
        String resultMessage = "?";
        int index = Integer.parseInt(tableId);
        System.out.println(index);
        //
        GameTable gametable = gameTableMap.get(index);
        if (gametable == null) {
            System.out.println("true");
        }

        int defiNum = gametable.getDefiNum();

        //
        if (gametable.isStateStart() == false) { //?
            if (gametable.getUserList().size() < defiNum) { //??
                if (!gametable.getUserList().contains(curUser)) {
                    gametable.getUserList().add(curUser);
                }
                resultMessage = "??";
            } else {
                resultMessage = "???";
            }
        } else {
            resultMessage = "????";
        }

        //
        application.setAttribute("gameTableMap", gameTableMap);

        //??
        HttpSession session = request.getSession();
        session.setAttribute("tableId", index);
        session.setAttribute("userList", gameTableMap.get(index).getUserList());

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/waiting.jsp");
        dispatcher.forward(request, response);

        //      PrintWriter out = response.getWriter();
        //      out.print("ID");
        //      out.println(userId);
        //      out.print("?:");
        //      out.println(resultMessage);
        //      out.print("?");
        //      out.println(gametable.getUserList().size());

    }
}