aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/scriptables/SXStore.h
blob: f6a13e9eebe9a6d32f26fd72bef228d646d07dff (plain)
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
166
167
168
169
170
171
172
/*
This file is part of WME Lite.
http://dead-code.org/redir.php?target=wmelite

Copyright (c) 2011 Jan Nedoma

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef __WmeSXStore_H__
#define __WmeSXStore_H__
#include "engines/wintermute/BPersistMgr.h"
#include "engines/wintermute/utils.h"
#include "engines/wintermute/BObject.h"

namespace WinterMute {

class CSXStore : public CBObject {
public:

	//////////////////////////////////////////////////////////////////////////
	class CBStoreProduct {
	public:
		CBStoreProduct() {
			m_Id = m_Name = m_Desc = m_Price = NULL;
		}

		CBStoreProduct(const char *id, const char *name, const char *desc, const char *price) {
			m_Id = m_Name = m_Desc = m_Price = NULL;

			CBUtils::SetString(&m_Id, id);
			CBUtils::SetString(&m_Name, name);
			CBUtils::SetString(&m_Desc, desc);
			CBUtils::SetString(&m_Price, price);
		}

		~CBStoreProduct() {
			delete [] m_Id;
			delete [] m_Name;
			delete [] m_Desc;
			delete [] m_Price;
		}

		HRESULT Persist(CBPersistMgr *PersistMgr) {
			PersistMgr->Transfer(TMEMBER(m_Id));
			PersistMgr->Transfer(TMEMBER(m_Name));
			PersistMgr->Transfer(TMEMBER(m_Desc));
			PersistMgr->Transfer(TMEMBER(m_Price));
			return S_OK;
		}

		const char *GetId() {
			return m_Id;
		}
		const char *GetName() {
			return m_Name;
		}
		const char *GetDesc() {
			return m_Desc;
		}
		const char *GetPrice() {
			return m_Price;
		}

	private:
		char *m_Id;
		char *m_Name;
		char *m_Desc;
		char *m_Price;
	};

	//////////////////////////////////////////////////////////////////////////
	class CBStoreTransaction {
	public:
		CBStoreTransaction() {
			m_Id = m_ProductId = m_State = NULL;
		}

		CBStoreTransaction(const char *id, const char *productId, const char *state) {
			m_Id = m_ProductId = m_State = NULL;

			CBUtils::SetString(&m_Id, id);
			CBUtils::SetString(&m_ProductId, productId);
			CBUtils::SetString(&m_State, state);
		}

		~CBStoreTransaction() {
			delete [] m_Id;
			delete [] m_ProductId;
			delete [] m_State;
		}

		const char *GetId() {
			return m_Id;
		}
		const char *GetProductId() {
			return m_ProductId;
		}
		const char *GetState() {
			return m_State;
		}

	private:
		char *m_Id;
		char *m_ProductId;
		char *m_State;
	};


	DECLARE_PERSISTENT(CSXStore, CBObject)
	CSXStore(CBGame *inGame);
	virtual ~CSXStore();
	virtual CScValue *ScGetProperty(char *name);
	virtual HRESULT ScCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, char *name);

	void AfterLoad();
	void OnObjectDestroyed(CBScriptHolder *obj);

	bool IsAvailable();
	void SetEventsEnabled(CScScript *script, bool val);
	bool GetEventsEnabled() const {
		return m_EventsEnabled;
	}
	void ValidateProducts(const char *prodIdList);

	void ReceiveTransactionsStart();
	void ReceiveTransactionsEnd();
	void AddTransaction(const char *id, const char *productId, const char *state);

	void ReceiveProductsStart();
	void ReceiveProductsEnd();
	void AddValidProduct(const char *id, const char *name, const char *desc, const char *price);
	void AddInvalidProduct(const char *id);

	void OnRestoreFinished(bool error);

private:
	void Cleanup();
	bool Purchase(CScScript *script, const char *productId);
	bool FinishTransaction(CScScript *script, const char *transId);
	void RestoreTransactions(CScScript *script);

	bool m_EventsEnabled;
	CBArray<CBStoreProduct *, CBStoreProduct *> m_ValidProducts;
	AnsiStringArray m_InvalidProducts;
	CBScriptHolder *m_LastProductRequestOwner;
	CBScriptHolder *m_LastPurchaseOwner;
	CBScriptHolder *m_LastRestoreOwner;

	CBArray<CBStoreTransaction *, CBStoreTransaction *> m_Transactions;

};

} // end of namespace WinterMute

#endif