forked from eeg1412/wikimoe_card_system_for_emlog_plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wm_card_bouerse.php
444 lines (437 loc) · 17.6 KB
/
wm_card_bouerse.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
require_once('../../../source/class/class_core.php');
require_once('../../../source/function/function_home.php');
require_once('module.php');
// error_reporting(0);
$discuz = C::app();
$cachelist = array('magic','usergroups', 'diytemplatenamehome');
$discuz->cachelist = $cachelist;
$discuz->init();
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
function wmBouerseNews($wmNewsInfo){
if(file_exists('bouerseNews.json')){//判断json文件是否存在
$wmBouerseNewsList = json_decode(file_get_contents('bouerseNews.json'),true);
$wmBouerseNewsList = array_merge($wmNewsInfo,$wmBouerseNewsList);
if(count($wmBouerseNewsList)>20){
$wmBouerseNewsList = array_slice($wmBouerseNewsList,0,20);
}
file_put_contents('bouerseNews.json', json_encode($wmBouerseNewsList),LOCK_EX);
}else{
file_put_contents('bouerseNews.json', json_encode($wmNewsInfo),LOCK_EX);
}
}
function wmBouerse($getMode,$updataMode){
if(file_exists('bouerse.json')){//判断json文件是否存在
$bourseListJson = json_decode(file_get_contents('bouerse.json'),true);
$bourseTime = $bourseListJson['updateTime'];
$bourseNowTime = time();
if($bourseNowTime - $bourseTime>1800){//如果有30分钟没更新了
$bourseRunTime = intval(($bourseNowTime - $bourseTime)/1800);//间隔几个30分钟
if($bourseRunTime>100){//最多刷新一天份
$bourseRunTime = 100;
}
$bourseList = $bourseListJson['data'];
for($j=0;$j<$bourseRunTime;$j++){
for($i=0; $i<count($bourseList); $i++){
if($bourseList[$i]['bankrupted']===0){//判断是否破产
$gailvYinzi = 0;
if($bourseList[$i]['preTrans']!=0){
$gailvYinzi = $bourseList[$i]['trans'] - $bourseList[$i]['preTrans'];
if($gailvYinzi>150){
$gailvYinzi = 150;
}else if($gailvYinzi<-150){
$gailvYinzi = -150;
}
}
$upOrDown = BouerseGailv($gailvYinzi);
$upDownLiang = mt_rand(0, 5);
if($upOrDown===0){
$bourseList[$i]['prePrice'] = $bourseList[$i]['price'];
$bourseList[$i]['price'] = $bourseList[$i]['price'] + $upDownLiang>233?233:$bourseList[$i]['price'] + $upDownLiang;
array_push($bourseList[$i]['history'],$bourseList[$i]['price']);
if(count($bourseList[$i]['history'])>100){
array_shift($bourseList[$i]['history']);
}
}else{
$bourseList[$i]['prePrice'] = $bourseList[$i]['price'];
$bourseList[$i]['price'] = $bourseList[$i]['price'] - $upDownLiang<1?1:$bourseList[$i]['price'] - $upDownLiang;
array_push($bourseList[$i]['history'],$bourseList[$i]['price']);
if(count($bourseList[$i]['history'])>100){
array_shift($bourseList[$i]['history']);
}
}
if($bourseList[$i]['price']<=1){//如果价格小于等于1则破产
//产生新的股票
$noBankrupted = wmKeyForobject($bourseList,'bankrupted',1);
$randomBouerseIndex = mt_rand(0, count($noBankrupted)-1);
$newBouerseId = $noBankrupted[$randomBouerseIndex]['id'];
$isBankrupted = false;//是否是破产重组
if($bourseList[$newBouerseId]['price']<=1){
$isBankrupted = true;
$bourseList[$newBouerseId]['prePrice'] = $bourseList[$newBouerseId]['price'];
$randomBouersePriceYinzi = mt_rand(1, 100);
$randomBouersePriceAdd = 0;
if($randomBouersePriceYinzi>98){
$randomBouersePriceAdd = 40;
}else if($randomBouersePriceYinzi>=91&&$randomBouersePriceYinzi>=98){
$randomBouersePriceAdd = 15;
}
//随机重组金额
$randomBouersePrice = mt_rand(10, 15);
$randomBouersePrice = $randomBouersePrice + $randomBouersePriceAdd;
$bourseList[$newBouerseId]['price'] = $randomBouersePrice;
array_push($bourseList[$newBouerseId]['history'],$bourseList[$newBouerseId]['price']);
if(count($bourseList[$newBouerseId]['history'])>100){
array_shift($bourseList[$newBouerseId]['history']);
}
}
$bourseList[$newBouerseId]['bankrupted'] = 0;
$bourseList[$i]['bankrupted'] = 1;
$bourseNewsText = '';
if($isBankrupted){//如果是破产重组
$bourseNewsText = $bourseList[$newBouerseId]['name'].'股已成功重组并上市,初始股价为'.$randomBouersePrice;
}else{
$bourseNewsText = $bourseList[$newBouerseId]['name'].'股已上市。';
}
$newsTime = intval($bourseNowTime - ($bourseNowTime%1800));
$newsBankruptedText = $bourseList[$i]['name'].'股已破产,进入重组阶段。';
$bourseNewsArr = array(array('time'=>$newsTime,'text'=>$bourseNewsText),array('time'=>$newsTime,'text'=>$newsBankruptedText));
wmBouerseNews($bourseNewsArr);
}
$bourseList[$i]['preTrans'] = $bourseList[$i]['trans'];
}
}
}
$bourseNowTime_ = intval($bourseNowTime - ($bourseNowTime%1800));//矫正到三十分
$wmBouerseData = array('data'=>$bourseList , 'updateTime'=>$bourseNowTime_);
file_put_contents('bouerse.json', json_encode($wmBouerseData),LOCK_EX);
}
}else{
initBouerse();
}
if(!$updataMode){
$bourseListOutData = json_decode(file_get_contents('bouerse.json'),true);
$initBouerseData_ = json_decode(file_get_contents('initBouerList.json'),true);
$bourseListOutDataCount = count($bourseListOutData['data']);
if(count($initBouerseData_['data'])>$bourseListOutDataCount){
$addBouerseLength = count($initBouerseData_['data']) - $bourseListOutDataCount;
for($k=0;$k<$addBouerseLength;$k++){
$addBouerseData = $initBouerseData_['data'][$bourseListOutDataCount+$k];
array_push($bourseListOutData['data'],$addBouerseData);
file_put_contents('bouerse.json', json_encode($bourseListOutData),LOCK_EX);
}
}
//过滤仅显示未破产的
$bourseListOutData['data'] = wmKeyForobject($bourseListOutData['data'],'bankrupted',0);
$bouerseOutData = array('listData'=>$bourseListOutData);
if($getMode){
echo json_encode($bouerseOutData);
}else{
return $bouerseOutData;
}
}
}
function wmSearchUserBouerse($uid){
$DB = DB::object();
// $password = strip_tags($_POST['password']);
if(isset($uid) && $uid!=""){
{//用正则表达式函数进行判断
$uidMd5 = "\"".md5($uid)."\"";
$mgid=$DB->query("SELECT * FROM pre_common_wm_card WHERE uid=".$uidMd5."");
$mgidinfo=$DB->fetch_array($mgid);
if ($mgidinfo) {
$bouerseOutData = wmBouerse(false,false);
$userBouerseInfo = $mgidinfo['bouerse'];
$userBouerseInfoObject = null;
$userBouerseInfoForId = '';
if($userBouerseInfo!=''){
$userBouerseInfoObject = json_decode($userBouerseInfo,true);
$userBouerseInfoForId = array();
//过滤仅输出上市的
for($i=0; $i<count($bouerseOutData['listData']['data']); $i++){
if(isset($userBouerseInfoObject[$bouerseOutData['listData']['data'][$i]['id']])){
$userObjectSingle = $userBouerseInfoObject[$bouerseOutData['listData']['data'][$i]['id']];
$userObjectSingle['id'] = $bouerseOutData['listData']['data'][$i]['id'];
array_push($userBouerseInfoForId,$userObjectSingle);
}else{
array_push($userBouerseInfoForId,null);
}
}
}
$bourseListNewsData = '';
if(file_exists('bouerseNews.json')){//判断json文件是否存在
$bourseListNewsData = json_decode(file_get_contents('bouerseNews.json'),true);
}
$userStar = $mgidinfo['starCount'];
$bouerseOutData['code'] = 202;
$bouerseOutData['news'] = $bourseListNewsData;
$bouerseOutData['starCount'] = $userStar;
$bouerseOutData['bouerse'] = $userBouerseInfoForId;
echo json_encode($bouerseOutData);
}else{
$bouerseOutData = array('code'=>3);//无该用户
echo json_encode($bouerseOutData);
}
}
}else{
$bouerseOutData = array('code'=>2);//uid有误
echo json_encode($bouerseOutData);
}
}
function initBouerse(){
$initBouerseData = json_decode(file_get_contents('initBouerList.json'),true);
for($i=0; $i<15; $i++){
$initBouerseData['data'][$i]['bankrupted'] = 0;
}
$initNowTime = time();
$initBouerseData['updateTime'] = $initNowTime - ($initNowTime % 1800);
file_put_contents('bouerse.json', json_encode($initBouerseData),LOCK_EX);
}
function BouerseGailv($gailvYinzi){
$randN = mt_rand(1, 1000);
if($randN>(500-$gailvYinzi)){
return 0;
}else{
return 1;
}
}
function wmBuyBouerse($uid){
$DB = DB::object();
// $password = strip_tags($_POST['password']);
if(isset($uid) && $uid!=""){
{//用正则表达式函数进行判断
$uidMd5 = "\"".md5($uid)."\"";
$mgid=$DB->query("SELECT * FROM pre_common_wm_card WHERE uid=".$uidMd5."");
$mgidinfo=$DB->fetch_array($mgid);
if ($mgidinfo) {
//$password = intval($_POST['password']);
$bdPassword = intval($mgidinfo['verifyCode']);
{
$timeStamp = time();
$passwordTime = intval($mgidinfo['verifyCodeStamp']);
$verifyCodeRemember = intval($mgidinfo['verifyCodeRemember']);
{
wmBouerse(false,true);
$buyId = intval($_POST['id']);
$buyPrice = intval($_POST['price']);
$buyValue = intval($_POST['value']);
$bourseListOrigin = json_decode(file_get_contents('bouerse.json'),true);
$bourseList = $bourseListOrigin['data'];
$myStar = intval($mgidinfo['starCount']);
if(!isset($bourseList[$buyId])){
$bouerseOutData = array('code'=>4);//无该股票
echo json_encode($bouerseOutData);
return false;
}
if($bourseList[$buyId]['bankrupted']==1){
$bouerseOutData = array('code'=>4);//破产股票
echo json_encode($bouerseOutData);
return false;
}
if($bourseList[$buyId]['price']!=$buyPrice){
$bouerseOutData = array('code'=>6);//价格对不上
echo json_encode($bouerseOutData);
return false;
}
if($bourseList[$buyId]['price']<=10){
$bouerseOutData = array('code'=>5);//价格过低无法购买
echo json_encode($bouerseOutData);
return false;
}
if($buyValue<=0){
$bouerseOutData = array('code'=>7);//购买数量不能为0
echo json_encode($bouerseOutData);
return false;
}
$shoudStar = $bourseList[$buyId]['price'] * $buyValue;
if($shoudStar>$myStar){
$bouerseOutData = array('code'=>8);//星星不足
echo json_encode($bouerseOutData);
return false;
}
$mybouerse = $mgidinfo['bouerse'];
if($mgidinfo['bouerse']==''){
$mybouerse = '{}';
}
$mybouerse = json_decode($mybouerse,true);
if(!isset($mybouerse[$buyId])){//如果用户没有该股票信息
if($buyValue>9){
$bouerseOutData = array('code'=>9);//已经超过购买上限
echo json_encode($bouerseOutData);
return false;
}
//购买信息:时间、价格、多少股、花费、类型(0买1卖)
$mybouerse[$buyId] = array('have'=>$buyValue , 'exData'=>array(array($timeStamp,$bourseList[$buyId]['price'],$buyValue,$shoudStar,0)));
}else{
$mybouerse[$buyId]['have'] = $mybouerse[$buyId]['have'] + $buyValue;
if($mybouerse[$buyId]['have']>9){
$bouerseOutData = array('code'=>9);//已经超过购买上限
echo json_encode($bouerseOutData);
return false;
}
array_push($mybouerse[$buyId]['exData'],array($timeStamp,$bourseList[$buyId]['price'],$buyValue,$shoudStar,0));
if(count($mybouerse[$buyId]['exData'])>10){
array_shift($mybouerse[$buyId]['exData']);
}
}
$starCountAfter = $myStar - $shoudStar;
$databaseBouerse = json_encode($mybouerse);
$verifyCodeRemember = intval($mgidinfo['verifyCodeRemember']);
$rememberPass = intval($_POST['rememberPass']);
//如果保存密码的话则verifyCodeRemember为1
$verifyCodeRemember = 0;
if($rememberPass==1){
$verifyCodeRemember = 1;
}
$wmBourseNowTrans = $bourseListOrigin['data'][$buyId]['trans'] + $buyValue;
$bourseListOrigin['data'][$buyId]['trans'] = $wmBourseNowTrans;
file_put_contents('bouerse.json', json_encode($bourseListOrigin),LOCK_EX);
$query = "Update pre_common_wm_card set verifyCodeRemember='".$verifyCodeRemember."' , starCount=".$starCountAfter." , bouerse='".$databaseBouerse."' where uid=".$uidMd5."";
$result=$DB->query($query);
$cardJsonData = array('mailMD5'=>md5($uid),'name'=>$bourseList[$buyId]['name'],'value'=>$buyValue,'useStar'=>$shoudStar,'massageType'=>'bouerseBuy');
wmWriteJson($cardJsonData);
$bouerseOutData = array('code'=>202);//成功
echo json_encode($bouerseOutData);
}
}
}else{
$bouerseOutData = array('code'=>3);//无该用户
echo json_encode($bouerseOutData);
}
}
}else{
$bouerseOutData = array('code'=>2);//uid有误
echo json_encode($bouerseOutData);
}
}
function wmSellBouerse($uid){
$DB = DB::object();
// $password = strip_tags($_POST['password']);
if(isset($uid) && $uid!=""){
{//用正则表达式函数进行判断
$uidMd5 = "\"".md5($uid)."\"";
$mgid=$DB->query("SELECT * FROM pre_common_wm_card WHERE uid=".$uidMd5."");
$mgidinfo=$DB->fetch_array($mgid);
if ($mgidinfo) {
//$password = intval($_POST['password']);
$bdPassword = intval($mgidinfo['verifyCode']);
{
$timeStamp = time();
$passwordTime = intval($mgidinfo['verifyCodeStamp']);
$verifyCodeRemember = intval($mgidinfo['verifyCodeRemember']);
{
wmBouerse(false,true);
$buyId = intval($_POST['id']);
$buyPrice = intval($_POST['price']);
$buyValue = intval($_POST['value']);
$bourseListOrigin = json_decode(file_get_contents('bouerse.json'),true);
$bourseList = $bourseListOrigin['data'];
$myStar = intval($mgidinfo['starCount']);
if(!isset($bourseList[$buyId])){
$bouerseOutData = array('code'=>4);//无该股票
echo json_encode($bouerseOutData);
return false;
}
if($bourseList[$buyId]['bankrupted']==1){
$bouerseOutData = array('code'=>4);//破产股票
echo json_encode($bouerseOutData);
return false;
}
if($bourseList[$buyId]['price']!=$buyPrice){
$bouerseOutData = array('code'=>6);//价格对不上
echo json_encode($bouerseOutData);
return false;
}
if($bourseList[$buyId]['price']<=10){
$bouerseOutData = array('code'=>5);//价格过低无法交易
echo json_encode($bouerseOutData);
return false;
}
if($buyValue<=0){
$bouerseOutData = array('code'=>7);//购买数量不能为0
echo json_encode($bouerseOutData);
return false;
}
$mybouerse = $mgidinfo['bouerse'];
if($mgidinfo['bouerse']==''){
$mybouerse = '{}';
}
$mybouerse = json_decode($mybouerse,true);
if(!isset($mybouerse[$buyId])){//如果用户没有该股票信息
$bouerseOutData = array('code'=>9);//没有那么多持有
echo json_encode($bouerseOutData);
return false;
}
$havBouerseAfter = $mybouerse[$buyId]['have'] - $buyValue;
if($havBouerseAfter<0){
$bouerseOutData = array('code'=>9);//没有那么多持有
echo json_encode($bouerseOutData);
return false;
}
$getStar = $bourseList[$buyId]['price'] * $buyValue;
$wmFee = round($getStar*0.05);
if($wmFee<20){
$wmFee =20;
}
$getStar = intval($getStar - $wmFee);//扣除税
if($getStar<=0){
$bouerseOutData = array('code'=>10);//卖出不够税
echo json_encode($bouerseOutData);
return false;
}
array_push($mybouerse[$buyId]['exData'],array($timeStamp,$bourseList[$buyId]['price'],$buyValue,$getStar,1));
if(count($mybouerse[$buyId]['exData'])>10){
array_shift($mybouerse[$buyId]['exData']);
}
$mybouerse[$buyId]['have'] = $havBouerseAfter;
$starCountAfter = $myStar + $getStar;
$databaseBouerse = json_encode($mybouerse);
$verifyCodeRemember = intval($mgidinfo['verifyCodeRemember']);
$rememberPass = intval($_POST['rememberPass']);
//如果保存密码的话则verifyCodeRemember为1
$verifyCodeRemember = 0;
if($rememberPass==1){
$verifyCodeRemember = 1;
}
$wmBourseNowTrans = $bourseListOrigin['data'][$buyId]['trans'] - $buyValue;
if($wmBourseNowTrans<0){
$wmBourseNowTrans = 0;
}
$bourseListOrigin['data'][$buyId]['trans'] = $wmBourseNowTrans;
file_put_contents('bouerse.json', json_encode($bourseListOrigin),LOCK_EX);
$query = "Update pre_common_wm_card set verifyCodeRemember='".$verifyCodeRemember."' , starCount=".$starCountAfter." , bouerse='".$databaseBouerse."' where uid=".$uidMd5."";
$result=$DB->query($query);
$cardJsonData = array('mailMD5'=>md5($uid),'name'=>$bourseList[$buyId]['name'],'value'=>$buyValue,'useStar'=>$getStar,'massageType'=>'bouerseSell');
wmWriteJson($cardJsonData);
$bouerseOutData = array('code'=>202);//成功
echo json_encode($bouerseOutData);
}
}
}else{
$bouerseOutData = array('code'=>3);//无该用户
echo json_encode($bouerseOutData);
}
}
}else{
$bouerseOutData = array('code'=>2);//uid有误
echo json_encode($bouerseOutData);
}
}
if(isset($_POST['type'])){
if($_POST['type']=='search'){
wmSearchUserBouerse($_G['uid']);
}else if($_POST['type']=='buy'){
if(isset($_POST['id'])&&isset($_POST['price'])&&isset($_POST['value'])){
wmBuyBouerse($_G['uid']);
}
}else if($_POST['type']=='sell'){
if(isset($_POST['id'])&&isset($_POST['price'])&&isset($_POST['value'])){
wmSellBouerse($_G['uid']);
}
}
}else{
wmBouerse(true,false);
}
?>