def index(): if request.method == 'GET': #- load the page -# resp = make_response(flask.render_template('index.html')) if (request.cookies.get('usrID') is None) or \ (mg.getUsr( request.cookies.get('usrID') ) == -1 ): #- either client or server is new, create new usr -# usr = mg.createUsr() resp.set_cookie('usrID', usr.getUsrID() ) return resp elif request.method == 'POST': usr = mg.getUsr( request.cookies.get('usrID') ) if usr == -1: return jsonify( usrID=-1 ) elif ('init' in request.form) or ('polling' in request.form) : space = mg.getSpaceN() return jsonify( usrID=usr.getUsrID(), space0=space[0], space1=space[1] ) elif 'choose' in request.form: #- for sendWait() -# IPs[usr.getUsrID()] = request.remote_addr side = request.form.get('side') space = mg.takeSpace( usr.getUsrID(), int(side) ) return jsonify( side=side, space0=space[0], space1=space[1] )
def shuffle(): usr = mg.getUsr( request.cookies.get('usrID') ) game = mg.getGame( usr.getGameID() ) if request.method == 'GET': #- for recvOrder() -# if game != -1 and game.isReady() : ctrler = mg.getUsr( game.getCtrl()[0] ) return flask.jsonify( order=game.getOrder(), gameID=game.getGameID() ) else: return flask.jsonify( gameID=-1 ) elif request.method == 'POST': #- for sendOrder(), the usr is ctrler of 1st turn -# game.setOrder( request.form.getlist('order') ) return flask.jsonify( gameID=game.getGameID() )
def wait(): #- used by recvAns() from teammate and enemies -# usr = mg.getUsr( request.cookies.get('usrID') ) game = mg.getGame( usr.getGameID() ) (ctrlUID, ctrlGID) = game.getCtrl() if ctrlUID == -1 or ctrlGID == -1: return flask.make_response() ctrler = mg.getUsr(ctrlUID) ctrlGrp = mg.getGrp(ctrlGID) (imgs, boxes) = ctrlGrp.getAns() isEnemy = usr.isEnemyWith(ctrler) if ctrlGrp.isGiveUp(): print 'Know Give Up' usr.listenAns() if game.allKnowAns(): #- reset for next turn -# game.reset() if ctrlGrp.isHalfAns(): #- ctrler runs out of time with 1 card open -# return flask.jsonify( giveup=1, img=imgs[0], box=boxes[0], isEnemy=isEnemy ) else: #- ctrler runs out of time with 0 card open -# return flask.jsonify( giveup=1, img='', box='', isEnemy=isEnemy ) elif ctrlGrp.isHalfAns(): #- just for display imgs -# print 'Know Half Ans' print game.getCountdown() return flask.jsonify( half=1, img=imgs[0], box=boxes[0], countdown=game.getCountdown() ) elif ctrlGrp.isFullAns(): #- ctrler finished ans -# print 'Know Full Ans' if game.allKnowAns(): #- all player know answer, so reset for next turn -# return flask.jsonify( full=1, allknow=1, isEnemy=isEnemy, end=game.isOver() ) elif usr.knowAns(): return flask.jsonify( full=1, know=1 ) else: usr.listenAns() return flask.jsonify( full=1, know=0, img0=imgs[0], box0=boxes[0], img1=imgs[1], box1=boxes[1] ) else: print 'No Move' print game.getCountdown() return flask.jsonify( countdown=game.getCountdown() )
def countdown(): usr = mg.getUsr( request.cookies.get('usrID') ) game = mg.getGame( usr.getGameID() ) game.setCountDown( request.form.get('countdown') ) print 'Set Countdown' print request.form.get('countdown') return flask.make_response()
def wait(): #- used by recvAns() from teammate and enemies -# usr = mg.getUsr( request.cookies.get('usrID') ) game = mg.getGame( usr.getGameID() ) UID = int(usr.getUsrID()) #print 'GET wait %d' %(usr.getUsrID()) (ctrlUID, ctrlGID) = game.getCtrl() if not(game.allChanged) or ctrlUID == -1 or ctrlGID == -1: return make_response() ctrlGrp = mg.getGrp(ctrlGID) (imgs, boxes) = ctrlGrp.getAns() isEnemy = int( usr.isEnemyWith( mg.getUsr(ctrlUID) ) ) img = '' box = '' half = 0 if len(boxes) > 0: img = imgs[0] box = boxes[0] half = 1 moveN = ctrlGrp.getMoveN() correctN = ctrlGrp.getCorrectN() if ctrlGrp.isGiveUp(): if not isEnemy: img = '' box = '' return jsonify( giveup=1, half=half, img=img, box=box, moveN=moveN, isEnemy=isEnemy, side=ctrlGrp.getSide() ) elif ctrlGrp.isFullAns(): #- ctrler finished ans -# return jsonify( full=1, img0=imgs[0], box0=boxes[0], img1=imgs[1], box1=boxes[1], moveN=moveN, correctN=correctN, isEnemy=isEnemy, end=game.isOver(), side=ctrlGrp.getSide() ) elif mg.isSupervisor(usr) and ctrlGrp.isHalfAns(): #- just for display imgs -# return jsonify( giveup=0, half=half, img=img, box=box, countdown=game.getCountdown(), side=ctrlGrp.getSide() ) else: #- no move -# return jsonify( countdown=game.getCountdown(), UID=int(usr.getUsrID()), side=ctrlGrp.getSide())
def shuffle(): global GAME_ID, READY usr = mg.getUsr( request.cookies.get('usrID') ) game = mg.getGame( usr.getGameID() ) GAME_ID = game.getGameID() if request.method == 'GET': #- for recvOrder() -# if ( game == -1 ) or ( not game.isReady() ): #print 'GET shuffle %d' %(usr.getUsrID()) return jsonify( gameID=-1 ) else: ctrler = mg.getUsr( game.getCtrl()[0] ) return jsonify( order=game.getOrder(), gameID=game.getGameID(), isEnemy=int(usr.isEnemyWith(ctrler)), side=usr.getSide() ) elif request.method == 'POST': #- for sendOrder(), the usr is ctrler of 1st turn -# #print 'POST shuffle %d' %(usr.getUsrID()) READY = True game.setOrder( request.form.getlist('order') ) return jsonify( gameID=game.getGameID(), side=usr.getSide() )
def game(): if request.method == 'GET': #- load the page -# #print 'GET game' return make_response( flask.render_template('game.html') ) elif request.method == 'POST': usr = mg.getUsr( request.cookies.get('usrID') ) if 'init' in request.form: #- for init(). if gameID == -1: waiting; else: game starting; -# #print 'POST game init %d' %(usr.getUsrID()) return jsonify( gameID = mg.joinUsr( usr.getUsrID() ) ) elif 'waiting' in request.form: #- for sendWait() -# #print 'POST game waiting %d' %(usr.getUsrID()) return jsonify( gameID = usr.getGameID() )
def index(): if request.method == 'GET': #- load the page -# usr = mg.createUsr() resp = flask.make_response(flask.render_template('index.html')) resp.set_cookie('usrID', usr.getUsrID() ) return resp elif request.method == 'POST': usr = mg.getUsr( request.cookies.get('usrID') ) if 'init' in request.form: #- for init(), return -1: waiting, else: game starting -# return flask.jsonify( gameID = mg.addUsr(usr) ) elif 'waiting' in request.form: #- for sendWait() -# return flask.jsonify( gameID = usr.getGameID() )
def restart(): usr = mg.getUsr( request.cookies.get('usrID') ) if mg.isSupervisor( usr ): return jsonify(success=1) usrID = usr.getUsrID() mg.killGame( usrID ) #print 'POST RESTART %d' %(int(usrID)) side_char = request.form.get('side') side = 0 #- if side_char == 'A': side = 0 -# if side_char == 'B': side = 1 mg.takeSpace( usrID, side ) return jsonify(success=1)