summaryrefslogtreecommitdiff
path: root/tavern.py
blob: b7660ecdc81eb1a83975376e5f0ab9c3f6870d53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
########################################################################################
#     This file is part of Spheres.
#     Copyright (C) 2019  Jesusalva

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.

#     This program 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 General Public License for more details.

#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.
########################################################################################
# Tavern Recruitment Module

from utils import (stdout, compress, allbars, Player)
from consts import (ERR_OK, ERR_BAD, ERR_INS, ERR_FULL, ST_TOWN)
from player import inventoryplace
import random, json

def recruit(args, token):
    # we have: args["tavern"] and args["amount"], besides Player[token]
    try:
        stdout("Recruit: %s" % args)
        tv=json.loads(args)
        stdout("JSON loaded")
        # Validation
        tmp=int(tv["amount"])
        tmp=int(tv["tavern"])
        # Load requested tavern
        tavern=allbars[tmp]
        if tavern == "ERROR":
            stdout("Request for inexistent tavern id %d" % tmp)
            raise Exception("Tavern not found")
        # Tavern must be active
        if tavern["min_quest"] < 0:
            stdout("Request for disabled tavern id %d" % tmp)
            raise Exception("Tavern is not active")
        # Tavern must be unlocked
        if tavern["min_quest"] > Player[token]["quest"]:
            stdout("Request for locked tavern id %d" % tmp)
            raise Exception("Tavern is not active")
        # Currency must exist and be valid (actually our fault...)
        tmp=int(Player[token][tavern["currency"]])
    except:
        # Invalid data
        return ERR_BAD

    # Configure tavern settings
    price=tavern["price"]
    poll=tavern["tavern_list"]

    # Check if you can recruit right now
    if (Player[token]["status"] != ST_TOWN):
        return ERR_BAD

    # Where we'll be saving your acquisitions
    Result={"units": []}
    Rarity=[] # <-- Internal control only

    # Main Loop
    i=0
    while i < tv["amount"]:
        i+=1
        # Not enough crystals/currency
        if (Player[token][tavern["currency"]] < price):
            Result["code"]=ERR_INS
            break

        # Inventory is full (also, get IDX)
        idx=inventoryplace(token)
        if (idx == -1):
            Result["code"]=ERR_FULL
            break

        # Payment in specified currency... (Defaults to crystals)
        Player[token][tavern["currency"]]-=price

        # Grab a random entry per groups
        r=random.randint(0, 10000)
        o=0
        for group in poll:
            if (r < group["chance"]):
                o=random.choice(group["units"])
                break
            else:
                # Adjust random seed so it is consistent
                r-=group["chance"]

        # Maybe, just maybe, you have a warranted max-rarity card
        if i % tavern["min_draws"] == 0:
            ssr=0
            for ur in Rarity:
                if ur in poll[0]["units"]:
                    ssr+=1
            if ssr < i / tavern["min_draws"]:
                o=random.choice(poll[0]["units"])

        # We don't want base id, we need unit id
        o*=100

        # Now we need to add it in a structured form
        unit={
            "unit_id": o,
            "level": 1,
            "exp": 0
        }

        # Record the new unit
        Result["units"].append(unit)
        try:
            Player[token]["inv"][idx]=unit
        except:
            Player[token]["inv"].append(unit)
            print("\033[31mAn error happened in recruit (token: %s idx: %d unit: %s InvLen: %d)\033[0m" % (token, idx, str(unit), len(Player[token]["inv"])))

        # We want to update Rarity Control
        Rarity.append(o)

    # Feed back the currency data
    Result["currency"]=(tavern["currency"], Player[token][tavern["currency"]])

    # Shuffle Results to look more random
    random.shuffle(Result["units"])

    try:
        stdout("Response: %d " % Result["code"])
    except:
        Result["code"]=ERR_OK

    sjson=compress(Result)
    #stdout("Reply: %s" % sjson)
    return sjson