1 | // Copyright (c) Athena Dev Teams - Licensed under GNU GPL |
---|
2 | // For more information, see LICENCE in the main folder |
---|
3 | |
---|
4 | #include "../common/mmo.h" |
---|
5 | #include "../common/malloc.h" |
---|
6 | #include "../common/showmsg.h" |
---|
7 | #include "../common/socket.h" |
---|
8 | #include "../common/strlib.h" |
---|
9 | #include "../common/sql.h" |
---|
10 | #include "../common/timer.h" |
---|
11 | |
---|
12 | #include "char.h" |
---|
13 | #include "inter.h" |
---|
14 | #include "int_quest.h" |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <stdlib.h> |
---|
19 | |
---|
20 | //Load entire questlog for a character |
---|
21 | int mapif_quests_fromsql(int char_id, struct quest questlog[]) |
---|
22 | { |
---|
23 | |
---|
24 | int count, i, j, num; |
---|
25 | struct quest tmp_quest; |
---|
26 | struct quest_objective tmp_quest_objective; |
---|
27 | SqlStmt * stmt; |
---|
28 | |
---|
29 | stmt = SqlStmt_Malloc(sql_handle); |
---|
30 | if( stmt == NULL ) |
---|
31 | { |
---|
32 | SqlStmt_ShowDebug(stmt); |
---|
33 | return 0; |
---|
34 | } |
---|
35 | |
---|
36 | memset(&tmp_quest, 0, sizeof(struct quest)); |
---|
37 | memset(&tmp_quest_objective, 0, sizeof(struct quest_objective)); |
---|
38 | |
---|
39 | if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `quest_id`, `state` FROM `%s` WHERE `char_id`=? LIMIT %d", quest_db, MAX_QUEST) |
---|
40 | || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) |
---|
41 | || SQL_ERROR == SqlStmt_Execute(stmt) |
---|
42 | || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_quest.quest_id, 0, NULL, NULL) |
---|
43 | || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_quest.state, 0, NULL, NULL) ) |
---|
44 | //|| SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_INT, &tmp_quest.time, 0, NULL, NULL) |
---|
45 | SqlStmt_ShowDebug(stmt); |
---|
46 | |
---|
47 | for( i = 0; i < MAX_QUEST && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i ) |
---|
48 | { |
---|
49 | memcpy(&questlog[i], &tmp_quest, sizeof(tmp_quest)); |
---|
50 | } |
---|
51 | count = i; |
---|
52 | |
---|
53 | for( i = 0; i < count; ++i ) |
---|
54 | { |
---|
55 | |
---|
56 | if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `num`, `name`, `count` FROM `%s` WHERE `char_id`=? AND `quest_id`=? LIMIT %d", quest_obj_db, MAX_QUEST_OBJECTIVES) |
---|
57 | || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) |
---|
58 | || SQL_ERROR == SqlStmt_BindParam(stmt, 1, SQLDT_INT, &questlog[i].quest_id, 0) |
---|
59 | || SQL_ERROR == SqlStmt_Execute(stmt) |
---|
60 | || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &num, 0, NULL, NULL) |
---|
61 | || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_STRING, &tmp_quest_objective.name, NAME_LENGTH, NULL, NULL) |
---|
62 | || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_INT, &tmp_quest_objective.count, 0, NULL, NULL) ) |
---|
63 | SqlStmt_ShowDebug(stmt); |
---|
64 | |
---|
65 | for( j = 0; j < MAX_QUEST && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++j ) |
---|
66 | { |
---|
67 | memcpy(&questlog[i].objectives[num], &tmp_quest_objective, sizeof(struct quest_objective)); |
---|
68 | } |
---|
69 | questlog[i].num_objectives = j; |
---|
70 | |
---|
71 | } |
---|
72 | SqlStmt_Free(stmt); |
---|
73 | return count; |
---|
74 | } |
---|
75 | |
---|
76 | //Delete a quest |
---|
77 | int mapif_parse_quest_delete(int fd) |
---|
78 | { |
---|
79 | |
---|
80 | bool success = true; |
---|
81 | int char_id = RFIFOL(fd,2); |
---|
82 | int quest_id = RFIFOL(fd,6); |
---|
83 | |
---|
84 | if ( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `quest_id` = '%d' AND `char_id` = '%d'", quest_db, quest_id, char_id) ) |
---|
85 | { |
---|
86 | Sql_ShowDebug(sql_handle); |
---|
87 | success = false; |
---|
88 | } |
---|
89 | |
---|
90 | if ( success && SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `quest_id` = '%d' AND `char_id` = '%d'", quest_obj_db, quest_id, char_id) ) |
---|
91 | { |
---|
92 | Sql_ShowDebug(sql_handle); |
---|
93 | success = false; |
---|
94 | } |
---|
95 | |
---|
96 | WFIFOHEAD(fd,11); |
---|
97 | WFIFOW(fd,0) = 0x3862; |
---|
98 | WFIFOL(fd,2) = char_id; |
---|
99 | WFIFOL(fd,6) = quest_id; |
---|
100 | WFIFOB(fd,10) = success?1:0; |
---|
101 | WFIFOSET(fd,11); |
---|
102 | |
---|
103 | return 0; |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | //Add a quest to a questlog |
---|
108 | int mapif_parse_quest_add(int fd) |
---|
109 | { |
---|
110 | |
---|
111 | StringBuf buf; |
---|
112 | bool success = true; |
---|
113 | int char_id = RFIFOL(fd,4); |
---|
114 | struct quest qd; |
---|
115 | int i; |
---|
116 | |
---|
117 | memcpy(&qd, RFIFOP(fd,8), RFIFOW(fd,2)-8); |
---|
118 | |
---|
119 | StringBuf_Init(&buf); |
---|
120 | StringBuf_Printf(&buf, "INSERT INTO `%s`(`quest_id`, `char_id`, `state`) VALUES ('%d', '%d', '%d')", quest_db, qd.quest_id, char_id, qd.state); |
---|
121 | |
---|
122 | |
---|
123 | if ( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) |
---|
124 | { |
---|
125 | Sql_ShowDebug(sql_handle); |
---|
126 | success = false; |
---|
127 | } |
---|
128 | |
---|
129 | for(i=0; i<qd.num_objectives && success; i++) |
---|
130 | { |
---|
131 | |
---|
132 | StringBuf_Clear(&buf); |
---|
133 | StringBuf_Printf(&buf, "INSERT INTO `%s`(`quest_id`, `char_id`, `num`, `name`, `count`) VALUES ('%d', '%d', '%d', '%s', '%d')", |
---|
134 | quest_obj_db, qd.quest_id, char_id, i, qd.objectives[i].name, qd.objectives[i].count); |
---|
135 | |
---|
136 | if ( success && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) |
---|
137 | { |
---|
138 | Sql_ShowDebug(sql_handle); |
---|
139 | success = false; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | WFIFOHEAD(fd,11); |
---|
144 | WFIFOW(fd,0) = 0x3861; |
---|
145 | WFIFOL(fd,2) = char_id; |
---|
146 | WFIFOL(fd,6) = qd.quest_id; |
---|
147 | WFIFOB(fd,10) = success?1:0; |
---|
148 | WFIFOSET(fd,11); |
---|
149 | |
---|
150 | StringBuf_Destroy(&buf); |
---|
151 | |
---|
152 | return 0; |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | //Send questlog to map server |
---|
157 | int mapif_send_quests(int fd, int char_id) |
---|
158 | { |
---|
159 | |
---|
160 | struct quest tmp_questlog[MAX_QUEST]; |
---|
161 | int num_quests, i; |
---|
162 | |
---|
163 | for(i=0; i<MAX_QUEST; i++) |
---|
164 | { |
---|
165 | memset(&tmp_questlog[i], 0, sizeof(struct quest)); |
---|
166 | } |
---|
167 | |
---|
168 | num_quests = mapif_quests_fromsql(char_id, tmp_questlog); |
---|
169 | |
---|
170 | WFIFOHEAD(fd,num_quests*sizeof(struct quest)+8); |
---|
171 | WFIFOW(fd,0) = 0x3860; |
---|
172 | WFIFOW(fd,2) = num_quests*sizeof(struct quest)+8; |
---|
173 | WFIFOL(fd,4) = char_id; |
---|
174 | |
---|
175 | for(i=0; i<num_quests; i++) |
---|
176 | { |
---|
177 | memcpy(WFIFOP(fd,i*sizeof(struct quest)+8), &tmp_questlog[i], sizeof(struct quest)); |
---|
178 | } |
---|
179 | |
---|
180 | WFIFOSET(fd,num_quests*sizeof(struct quest)+8); |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | //Map server requesting a character's quest log |
---|
186 | int mapif_parse_loadquestrequest(int fd) |
---|
187 | { |
---|
188 | mapif_send_quests(fd, RFIFOL(fd,2)); |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | int inter_quest_parse_frommap(int fd) |
---|
193 | { |
---|
194 | |
---|
195 | switch(RFIFOW(fd,0)) |
---|
196 | { |
---|
197 | case 0x3060: mapif_parse_loadquestrequest(fd); break; |
---|
198 | case 0x3061: mapif_parse_quest_add(fd); break; |
---|
199 | case 0x3062: mapif_parse_quest_delete(fd); break; |
---|
200 | default: |
---|
201 | return 0; |
---|
202 | } |
---|
203 | return 1; |
---|
204 | |
---|
205 | } |
---|