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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
*
* 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 2 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/>.
*/
#include "InstanceScenario.h"
#include "DatabaseEnv.h"
#include "DB2Stores.h"
#include "InstanceSaveMgr.h"
#include "Log.h"
#include "Map.h"
#include "ObjectMgr.h"
#include "Player.h"
InstanceScenario::InstanceScenario(Map const* map, ScenarioData const* scenarioData) : Scenario(scenarioData), _map(map)
{
ASSERT(_map);
LoadInstanceData(_map->GetInstanceId());
Map::PlayerList const& players = map->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if (Player* player = itr->GetSource()->ToPlayer())
SendScenarioState(player);
}
void InstanceScenario::SaveToDB()
{
if (_criteriaProgress.empty())
return;
DifficultyEntry const* difficultyEntry = sDifficultyStore.LookupEntry(_map->GetDifficultyID());
if (!difficultyEntry || difficultyEntry->Flags & DIFFICULTY_FLAG_CHALLENGE_MODE) // Map should have some sort of "CanSave" boolean that returns whether or not the map is savable. (Challenge modes cannot be saved for example)
return;
uint32 id = _map->GetInstanceId();
if (!id)
{
TC_LOG_DEBUG("scenario", "Scenario::SaveToDB: Can not save scenario progress without an instance save. Map::GetInstanceId() did not return an instance save.");
return;
}
SQLTransaction trans = CharacterDatabase.BeginTransaction();
for (auto iter = _criteriaProgress.begin(); iter != _criteriaProgress.end(); ++iter)
{
if (!iter->second.Changed)
continue;
Criteria const* criteria = sCriteriaMgr->GetCriteria(iter->first);
switch (CriteriaTypes(criteria->Entry->Type))
{
// Blizzard only appears to store creature kills
case CRITERIA_TYPE_KILL_CREATURE:
break;
default:
continue;
}
if (iter->second.Counter)
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_SCENARIO_INSTANCE_CRITERIA);
stmt->setUInt32(0, id);
stmt->setUInt32(1, iter->first);
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_SCENARIO_INSTANCE_CRITERIA);
stmt->setUInt32(0, id);
stmt->setUInt32(1, iter->first);
stmt->setUInt64(2, iter->second.Counter);
stmt->setUInt32(3, uint32(iter->second.Date));
trans->Append(stmt);
}
iter->second.Changed = false;
}
CharacterDatabase.CommitTransaction(trans);
}
void InstanceScenario::LoadInstanceData(uint32 instanceId)
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_SCENARIO_INSTANCE_CRITERIA_FOR_INSTANCE);
stmt->setUInt32(0, instanceId);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
SQLTransaction trans = CharacterDatabase.BeginTransaction();
time_t now = time(nullptr);
std::vector<CriteriaTree const*> criteriaTrees;
do
{
Field* fields = result->Fetch();
uint32 id = fields[0].GetUInt32();
uint64 counter = fields[1].GetUInt64();
time_t date = time_t(fields[2].GetUInt32());
Criteria const* criteria = sCriteriaMgr->GetCriteria(id);
if (!criteria)
{
// Removing non-existing criteria data for all instances
TC_LOG_ERROR("criteria.instancescenarios", "Removing scenario criteria %u data from the table `instance_scenario_progress`.", id);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_SCENARIO_INSTANCE_CRITERIA);
stmt->setUInt32(0, instanceId);
stmt->setUInt32(1, uint32(id));
trans->Append(stmt);
continue;
}
if (criteria->Entry->StartTimer && time_t(date + criteria->Entry->StartTimer) < now)
continue;
switch (CriteriaTypes(criteria->Entry->Type))
{
// Blizzard appears to only stores creatures killed progress for unknown reasons. Either technical shortcoming or intentional
case CRITERIA_TYPE_KILL_CREATURE:
break;
default:
continue;
}
SetCriteriaProgress(criteria, counter, nullptr, PROGRESS_SET);
if (CriteriaTreeList const* trees = sCriteriaMgr->GetCriteriaTreesByCriteria(criteria->ID))
for (CriteriaTree const* tree : *trees)
criteriaTrees.push_back(tree);
}
while (result->NextRow());
CharacterDatabase.CommitTransaction(trans);
for (CriteriaTree const* tree : criteriaTrees)
{
ScenarioStepEntry const* step = tree->ScenarioStep;
if (!step)
continue;
if (IsCompletedCriteriaTree(tree))
SetStepState(step, SCENARIO_STEP_DONE);
}
}
}
std::string InstanceScenario::GetOwnerInfo() const
{
return Trinity::StringFormat("Instance ID %u", _map->GetInstanceId());
}
void InstanceScenario::SendPacket(WorldPacket const* data) const
{
_map->SendToPlayers(data);
}
|