Beispiel #1
0
    def isEnd(self, total_time):
        now = int(time.time())

        if now < self.start_time:
            Log.error('tournament start time error(<now)')
            return True

        offset = 5

        if now - self.start_time >= total_time - offset:
            return True
        else:
            return False
Beispiel #2
0
def checkPara(para, in_list):
    for i in in_list:
        if isinstance(i, tuple):
            (k, func) = i
            if not (k in para and func(para[k])):
                Log.error("param error:" + k)
                return False

        elif i not in para:
            Log.error("param not exist:" + i)
            return False

    return True
Beispiel #3
0
    def getLevelRange(self, range_id):
        if range_id > len(self.LV_RANGE):
            Log.error('range_id exceeded')
            return False

        r = range_id - 1

        start = 1
        end = self.LV_RANGE[r]
        if r > 0:
            start = self.LV_RANGE[r-1] + 1

        return {'start':start, 'end':end}
Beispiel #4
0
    def consume(self):
        now = self.left - 1
        if now < 0:
            Log.error("not enough spin")
            return False

        ret = (self.lines, self.bet)

        if now == 0:
            self.remove()
        else:
            self.left = now

        return ret
Beispiel #5
0
    def send(cls, para):
        from_uid = para['from_uid']
        to_uid = para['to_uid']

        #TODO
        # if cls.isSent(from_uid, to_uid):
        #     return Error.GIFT_SENT

        now = int(time.time())

        key = '%s:%s:%s' % (from_uid, to_uid, now)

        gift_type = random.randint(1, 2)

        data = {
                'from_uid': from_uid,
                'time': now,
                'gid': key,
                'type': gift_type,
                }

        item_type = cls.getIndexItemType()
        item_type(key).setAll(data)

        user_gift = cls(to_uid)
        if user_gift.size() < cls.MAX_SIZE:
            user_gift.addKey(key)

            cli = Redis()
            key = cls.genGiftCacheKey(from_uid, to_uid)
            cli.set(key, 1)
            cli.expire(key, cls.DAY_SEC)
        else:
            Log.error('user_gift full, unable to receive')

        return {}
Beispiel #6
0
def api(request):
    try:
        para = json.loads(request.body)

        Log.info('Request: %s' % (log_kv(para)))

        cmd = para['cmd']

        if cmd in API_ROUTER:
            processPara(para)

            if cmd == 'login':
              if request.META.has_key('HTTP_X_REAL_IP'):
                  para['ip'] = request.META['HTTP_X_REAL_IP']
              else:
                  para['ip'] = request.META['REMOTE_ADDR']

            if cmd != 'verify_purchase' and para['version'] < Config.MIN_VERSION:
                Log.info('unsupported version: %d' % para['version'])
                return HttpResponse(genResponse(Error.VERSION_TOO_LOW))

            if cmd != 'login' and cmd != 'verify_purchase':
                if not Session.is_valid(para['uid'], para['sid']):
                    Log.error('session invalid')
                    return HttpResponse(genResponse(Error.INVALID_SESSION))

            ret = API_ROUTER[cmd](para)

            response = genResponse(ret, para['uid'])

            Log.info('Request: %s, Response: %s' % (para, response))

            return HttpResponse(response, content_type="application/json")
        else:
            Log.error('unknown cmd')
            return HttpResponse(genResponse(Error.UNKNOWN_CMD))

    except Exception, e:
        traceback.print_exc()
        Log.error(traceback.format_exc())
        return HttpResponseServerError("Unknown error: %s" % str(e))
Beispiel #7
0
 def getSymbolWeightById(cls, game_id):
     if game_id not in cls.SYMBOL_WEIGHT:
         Log.error('getSymbolWeightById:game_id=%s invalid, using 1 instead'%game_id)
         game_id = 1
     return cls.SYMBOL_WEIGHT[game_id]
Beispiel #8
0
 def getPayTable(cls, game_id):
     if game_id not in cls.PAYTABLE:
         Log.error('getPayTable:game_id=%s invalid, using 1 instead'%game_id)
         game_id = 1
     return cls.PAYTABLE[game_id]
Beispiel #9
0
    def spin(cls, para):
        if not checkPara(para, ['uid', 'machine_id', 'lines', 'bet', 'is_free']):
            return Error.INVALID_PARAM

        uid = para['uid']
        game_id = para['machine_id']
        lines = para['lines']
        bet = para['bet']
        is_free = para['is_free']

        fake_flag = 0
        if 'fakeflag' in para:
            fake_flag = int(para['fakeflag'])

        lv = User(uid).level

        #check
        if not ConfMgr.isMachineLocked(game_id, lv):
            return Error.MACHINE_LOCKED

        if not ConfMgr.isLineValid(game_id, lines):
            return Error.LINE_EXCEEDED

        if not ConfMgr.isBetValid(lv, bet):
            return Error.BET_EXCEEDED

        pay_in = 0
        user = User(uid)
        cur_coin = user.coin

        # may change wild_tune for this user
        user.addSpin()

        if not is_free:
            user.clearWinRec()

        if not is_free:
            pay_in = lines * bet
            if cur_coin < pay_in:
                Log.error('no enough coin to spin')
                return Error.NO_COIN
        else:
            # pick saved bet and lines for free spin
            free_spin = FreeSpin(uid, game_id)
            ret = free_spin.consume()

            if ret is False:
                return Error.NO_FREE_SPIN

            # override
            (lines, bet) = ret

        machine = cls(game_id)

        # ignore bonus if skip playing it
        game_type = BonusGameType.getBonusGameType(game_id)
        bonus_game = game_type(uid, game_id)
        bonus_game.remove()

        (response, win, bonus_num, spin_num) = machine.doSpin(game_id, lv, lines, bet, is_free, fake_flag)

        if bonus_num > 0:
            game_type.create(uid, game_id, bonus_num, bet)
            Log.debug('created bonus games: bonus_num=%s'%bonus_num)

        if spin_num > 0:
            free_spin = FreeSpin(uid, game_id)
            free_spin.add(spin_num, lines, bet)
            response['is_new_freespin'] = True
            Log.debug('added free spin=%s'%spin_num)

        # save for big/mega win
        user.addWinRec(win / bet)

        # Tournament update
        Tournament.compete(uid, game_id, win)

        # compose reponse
        now_coin = cur_coin - pay_in + win
        user.coin = now_coin
        response['coin'] = now_coin

        (now_exp, lv_up) = user.addExp(lines*bet)
        response['exp'] = now_exp

        response['level'] = user.level

        free_spin = FreeSpin(uid, game_id).getAll()
        if free_spin:
            response['free_spin'] = free_spin

        win_info = user.checkWinRec()
        if win_info:
            response.update(win_info)

        rank_info = Tournament.getRank({'uid': uid, 'machine_id': game_id})
        if rank_info != Error.TOUR_NOT_OPEN:
            response['rank_info'] = rank_info

        # share gift flag
        share = UserShareGift(uid)
        if '5combo' in response:
            share.setFlag(UserShareGift.TYPE_COMBO)

        # bonus may also trigger it
        if 'is_mega' in response:
            share.setFlag(UserShareGift.TYPE_MEGA)

        if lv_up:
            share.setFlag(UserShareGift.TYPE_LEVELUP)

        #only for stat
        data = {'bet': pay_in,
                'win': win,
                'spin': 1,
                'trigger_freespin': 1 if spin_num > 0 else 0,
                'trigger_bonus': 1 if bonus_num > 0 else 0,
                'free_spin': 1 if is_free else 0,
                }
        Stat().incr(data)

        return response
Beispiel #10
0
 def getNum(cls, machine_id, n):
     if machine_id not in cls.SCATTER_RATE:
         Log.error("FreeSpin::getNum: machine_id=%s invalid" % machine_id)
         machine_id = 1
     return cls.SCATTER_RATE[machine_id][n]