summaryrefslogtreecommitdiff
path: root/battle/summons.py
blob: f3009dffcaf1d15ff27878e702cbc56bd47bd761 (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
#################################################################################
#     This file is part of Spheres.
#     Copyright (C) 2019-2021  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/>.
#################################################################################
# Battle Module - Summons
import json, traceback
from utils import (compress, allsummons, stdout, dl_search,
    Player, Battle)
from consts import (ERR_ERR, ERR_SUMMON, SRV_SUMMON)
from battle.skills import skill_core
from battle.common import conditions
#from battle.main import advance_wave, battle_endturn, get_result

def handle_summon(token, summon, summon_id):
    global Battle, Player

    try:
        # Log that summon was used (single action log)
        Battle[token]["log"]=[["", 0, SRV_SUMMON, summon_id, "", 0]]

        # Summon strength is based on player rank
        force=summon["strength"]*Player[token]["level"]

        # Cast the skill, return error 500 on exception
        skill_core(token, summon["type"], force, summon["attribute"])
    except:
        traceback.print_exc()
        return ERR_ERR

    stdout("Summon: Victory/Defeat Check", 2)

    # HOLD THAT! Handle victory/defeat conditions
    check = conditions(token, Battle[token]["spheres"])
    if check is not None:
        return check

    # The check is: if not ERR_ERR or ERR_BAD, then reload battle/result
    sjson=compress(Battle[token])
    # We should also clear the log - again
    Battle[token]["log"] = []
    return sjson


def summon(args, token):
    # Data validation
    try:
        stdout("Summon: %s" % args)
        ss=json.loads(args)
        # [summon_id]
        # Validation
        summon_id=int(ss[0])

        # Create summon object
        summon=allsummons[summon_id]

        # Verify the cost
        if Battle[token]["bp"] < summon["cost"]:
            stdout("Cannot summon \"%s\": Insufficient BP (%d/%d)" % (summon["name"], Battle[token]["bp"], summon["cost"]))
            raise Exception("Insufficient BP")

        stdout("All fine thus far")
    except:
        # Invalid data
        return compress(ERR_SUMMON)

    # Already summoned (reverse logic)
    try:
        Battle["s"]+=1
        Battle["s"]-=1
        return compress(ERR_SUMMON)
    except:
        pass

    Battle[token]["bp"]-=summon["cost"]
    Battle["s"]=1
    return handle_summon(token, summon, summon_id)


#################################################
# Handles CI false positives in a lame way
def pyflakes():
    return Player